<?php

ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
define("TITLE_ELEMENT", 'Lists des documents des Achat');
if(!isset($_SESSION['user'])){
    header("Location: ../../login.php");
    exit();
}
require_once("../../includes/fn_module.php"); 

header('Content-Type: text/html; charset=utf-8');
ini_set('default_charset', 'utf-8');
setlocale(LC_ALL, 'fr_FR');
setlocale(LC_TIME, "fr_FR");

$avoir_array = array();
$avoir_vente_array = array();
?>
<!DOCTYPE html>
<head>
    <?php include("../../includes/inc_header_common.php"); ?>
    <?php include("../../includes/functions.php"); ?>

</head>
<body>
    <div id="wrapper">
        <?php include("../../includes/inc_top.php"); ?>
        <div id="page-wrapper" >
            <div class="page-header">
                <div class="container-fluid">
                    <div class="row">
                        <div class="col-md-6">
                            <h1><i class="fa fa-registered"></i> Lists des documents des Achat du <? echo $_GET["date_from"]; ?> au <? echo $_GET["date_to"]; ?></h1>
                        </div>
						<div class="col-md-6 text-right" style="margin-top:15px;">
							<a href="#" target="_blank"><button class="btn btn-default" type="button" id="print_page"><i class="fa fa-print"></i> Imprimer</button></a>
						</div>
                    </div>
                </div>
            </div>
            <div class="container-fluid">

                <div class="row">	
<table cellspacing="10" cellpadding="10">				
				
				<td>* Fournisseur : </td><td><b><? 
				if($_GET["fournisseur"] <> "" ) {
					echo fournisseur($_GET["fournisseur"]);
				}else{
					echo "Tous";
				}
				?></b></td></tr>
				<td>* Référence facture Achat : </td><td><b><? 
				if($_GET["ref_fac_achat"] <> "" ) {
					echo $_GET["ref_fac_achat"];
				}else{
					echo "Tous";
				}
				?></b></td></tr>
				<tr><td>* Référence facture d'avoir : </td><td><b><? 
				if($_GET["ref_fac_avoir"] <> "" ) {
					echo ref_facture_vente($_GET["ref_fac_avoir"]);
				}else{
					echo "Tous";
				}
				?></b></td>
				
				<tr><td>* Montant de : </td><td><b><? 
					echo $_GET["montant_from"];
				?></b></td>
				<td>* Montant à : </td><td><b><? 
					echo $_GET["montant_to"];
				?></b></td>
				</tr>				
</table>				
				<br><br>
				<div class="table-responsive">
				
				
				
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>
										<th><b>ID</b></th>
										<th><b>Date<br>Facture</b></th>
										<th><b>Ref Fac<br>Achat</b></th>
										<th><b>Fournisseur</b></th>										
										<th><b>Remarque</b></th>
										
										<th><b>Total HT Net<br>+ Fodec</b></th>
										
										<?
										$r = 0;
										$total_tva = array();
										$total_tva_month = array();
										$tva_total = "";
										
										$tva_text = "";
										
										$tva = " select * from pm_tva where value <> 0 order by id ASC";
										$result_tva = @$db->query($tva);
										if($result_tva !== false){
											foreach($result_tva as $row_tva){
												$total_tva[$row_tva["value"]] = 0;
												$total_tva_month[$row_tva["value"]] = 0;
												echo "<th><b>Total<br>".$row_tva["name"]."</b></th>";
												$tva_text .= "<th><b>Total<br>".$row_tva["name"]."</b></th>";
												$r ++;
											}
										}
										?>
										
										
										<th><b>Total TTC</b></th>
                                    </thead>
                                    <tbody> 
									
									<?
									$count = 0;
									$cnt_moi = 0;
									$cnt_mounth = 1;
									$total = 0;
									$total_ttc_all = 0;
									$total_ttc_all_month = 0;
									$total_total_ht = 0;
									$total_total_ht_all = 0;
									$total_total_ht_all_month = 0;
									$weekand = "";
									$mois = array(1 => 0,2 => 0,3 => 0,4 => 0,5 => 0,6 => 0,7 => 0,8 => 0,9 => 0,10 => 0,11 => 0,12 => 0);
									$date_trans = strtotime($_GET["date_from"]) ;
									$date_transa = strtotime($_GET["date_to"]. '+1 days') ;
									
									$query = "select * from pm_facture_avoir where date_avoir >= ".$date_trans." AND date_avoir < ".$date_transa ;
									
									if($_GET["montant_from"] > 0){
										$query .= " AND total_ttc >= ".$_GET["montant_from"]."";
									}
									if($_GET["montant_to"] > 0){
										$query .= " AND total_ttc <= ".$_GET["montant_to"]."";
									}
									
									if($_GET["fournisseur"] <> ""){
										$query .= " AND fournisseur = ".$_GET["fournisseur"];
									}
									
									if($_GET["ref_fac_achat"] <> ""){
										$query .= " AND ref_bl = '".$_GET["ref_fac_achat"]."'";
									}
									if($_GET["ref_fac_avoir"] <> ""){
										$query .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
									}
									
																			
									$query .= " order by date_avoir ASC";
									
									// echo $query;
									$result = @$db->query($query);	
									$num_rows = $result->rowCount();
									$numcount = 0;

									// echo "$num_rows Rows\n";
									if($result !== false){
										
																				
										foreach($result as $row){
											$numcount ++;

										$date_avoir = date('d-m-Y', $row["date_avoir"]);
										$count++;
										// $nbr_resultat = $row["nb"];
										$total_ttc_all += $row["total_ttc"];
										$total_ttc_all_month += $row["total_ttc"];
										$total_total_ht = number_format($row["montant_ht"],3, '.','');
										$total_total_ht_all += $total_total_ht;
										$total_total_ht_all_month += $total_total_ht;
										
									// ================ GROUP BY MONTH ===============	
										$cnt_mounth++;
										$tva_total_tot = "";
										$date = $date_avoir;
										$month = date("n",strtotime($date));
										$month_m = date("m",strtotime($date));
										$year = date("Y",strtotime($date));
										$number_day_mount = cal_days_in_month(CAL_GREGORIAN, $month, $year);
										$date_int = strtotime($year."-".$month_m."-01");
										$date_out = strtotime($year."-".$month_m."-".$number_day_mount);
										//******************************************
										$nbr_mounth = "select COUNT(*) as cnt from pm_facture_avoir where date_avoir >= ".$date_int." AND date_avoir <= ".$date_out ;
										if($_GET["montant_from"] > 0){
											$nbr_mounth .= " AND total_ttc >= ".$_GET["montant_from"]."";
										}
										if($_GET["montant_to"] > 0){
											$nbr_mounth .= " AND total_ttc <= ".$_GET["montant_to"]."";
										}
										
										if($_GET["fournisseur"] <> ""){
											$nbr_mounth .= " AND fournisseur = ".$_GET["fournisseur"];
										}
										
										if($_GET["ref_fac_achat"] <> ""){
											$nbr_mounth .= " AND ref_bl = '".$_GET["ref_fac_achat"]."'";
										}
										if($_GET["ref_fac_avoir"] <> ""){
											$nbr_mounth .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
										}
										$result_mounth = @$db->query($nbr_mounth);
										if($result_mounth !== false){
											foreach($result_mounth as $row_mounth){
												
												$nbr_return = $row_mounth["cnt"];
											
											}
										}
										//******************************************
										$jour  = date("w",strtotime($date));
											
										if($mois[$month] == 0){											
											echo "<tr><td colspan='".($r+7)."' align='center' style='background-color: #ff9425;'><b style='text-transform: capitalize;color:#FFF;'>".utf8_encode(strftime("%B",strtotime($date)))." ".date("Y",strtotime($date))."</b></td></tr>";
											$mois[$month] = 1;
											$cnt_moi++;
											$cnt_mounth = 1;
										}	

										
									?>
									<tr>	
										<td><? echo $row["id"]; ?></td>		
										<td><? echo $date_avoir; ?></td>		
										<td><? echo $row["ref_bl"]; ?></td>		
										<td><? echo fournisseur($row["fournisseur"]); ?></td>		
										<td><? echo substr($row["remarque"], 0, 50); ?></td>		
										<td align='right'><? echo number_format($total_total_ht,3, '.',' '); ?></td>	
									<?
									
									

										$tva = " select * from pm_tva where value <> 0 order by value ASC";
										$result_tva = @$db->query($tva);
										
										if($result_tva !== false){
											
																						
											foreach($result_tva as $row_tva){
												
												$val_tva = "<td align='right'>0</td>";
												$get_tva = "SELECT * FROM `pm_facture_avoir` where tva = ".$row_tva["value"]." AND id =".$row["id"];
												// echo $get_tva."<br>";
												$result_get_tva = @$db->query($get_tva);
												if($result_get_tva !== false){
													foreach($result_get_tva as $row_get_tva){
														// echo "==>".$total_tva[$row_tva["value"]]."<br>";
														$total_tva[$row_tva["value"]] += $row_get_tva["montant_ht"];
														$total_tva_month[$row_tva["value"]] += $row_get_tva["montant_ht"];
														$val_tva = "<td  align='right'>".number_format($row_get_tva["montant_ht"],3, '.',' ')."</td>";
													}
												}	
												
												echo $val_tva;
											}	
										}
									
									?>
										
		
										<td align="right" ><? echo number_format($row["total_ttc"],3, '.',' '); ?></td>		
									</tr>
								
									
									<?	
									//******************************************************************
																			
										// echo "<tr><td colspan='10' align='center'><b>".$cnt_mounth." / ".$nbr_return."</b></td></tr>";
										
									//******************************************************************
									//**********************************************************
									
									if($cnt_mounth == $nbr_return){
												//============
												$tva = " select * from pm_tva where value <> 0 order by id ASC";
												$result_tva = @$db->query($tva);
												if($result_tva !== false){
													foreach($result_tva as $row_tva){
														$tva_total_tot .= "<td align='right'><b>".number_format($total_tva_month[$row_tva["value"]],3, '.','')."</b></td>";
													}
												}
												// ===============
												echo "<tr><td colspan='5' align='right'><b>TOTAL</b></td><td align='right'><b>".number_format($total_total_ht_all_month,3, '.','')."</b></td>".$tva_total_tot."<td align='right'><b>".number_format($total_ttc_all_month,3, '.','')."</b></td></tr>";
											
												// ========= Mettre a zero TVA & Total HT ============
												$total_total_ht_all_month = 0; 
												$total_ttc_all_month = 0; 
												$tva_month = " select * from pm_tva where value <> 0 order by id ASC";
												$result_tva_month = @$db->query($tva_month);
												if($result_tva_month !== false){
													foreach($result_tva_month as $row_tva_month){
														// $total_tva[$row_tva["value"]] = 0;
														$total_tva_month[$row_tva_month["value"]] = 0;
														// echo "<th><b>Total<br>".$row_tva["name"]."</b></th>";
														// $r ++;
													}
												}
												// ===================
											
									}
									//**********************************************************									
									}
									
									
										if($numcount == $num_rows){
											// echo "<tr><td colspan='5' align='right'><b>TOTAL</b></td><td align='right'><b>".number_format($total_total_ht_all_month,3, '.','')."</b></td>".$tva_total_tot."<td align='right'><b>".number_format($total_ttc_all_month,3, '.','')."</b></td></tr>";
										}
									}
									if($count == 0){
										echo "<tr><td colspan='10' align='center'>No data !</td></tr>";
									}else{
										
										$tva = " select * from pm_tva where value <> 0 order by id ASC";
										$result_tva = @$db->query($tva);
										if($result_tva !== false){
											foreach($result_tva as $row_tva){
												$tva_total .= "<td align='right'><b>".number_format($total_tva[$row_tva["value"]],3, '.','')."</b></td>";
											}
										}
										
										
										$total_1 = "<tr><td colspan='10' align='center' style='background-color: #00a1af;'><b style='text-transform: capitalize;color:#FFF;'> TOTAL </b></td></tr>";
										$total_1 .= "<tr><td colspan='5' align='right' width='50%'></td><td align='center'><b>Total HT Net<br>+ Fodec</b></td>".$tva_text."<td align='right'><b>TOTAL TTC</b></td></tr>";
										$total_1 .= "<tr><td colspan='5' align='right' width='50%'><b>Total A (liste des factures d'achat )</b></td><td align='right'><b>".number_format($total_total_ht_all,3, '.','')."</b></td>".$tva_total."<td align='right'><b>".number_format($total_ttc_all,3, '.','')."</b></td></tr>";
									}	
									?>

										
                                    </tbody>
                                </table>
								
								<table class="table table-bordered table-hover table-striped" id="listing_base">
									<? echo $total_1; ?>
								</table>
							
								
								
                </div>
					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; ?>
