<?php

ini_set('display_errors', 0);


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
define("TITLE_ELEMENT", 'doc_vente');
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 Vente 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>* Client : </td><td><b><? 
				if($_GET["id_client"] <> "" ) {
					echo client($_GET["fournisseur"]);
				}else{
					echo "Tous";
				}
				?></b></td></tr><tr>
				<td>* Référence facture Vente : </td><td><b><? 
				if($_GET["ref_fac_vente"] <> "" ) {
					echo $_GET["ref_fac_vente"];
				}else{
					echo "Tous";
				}
				?></b></td></tr><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>
				<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>
			<tr><td>* Type : </td><td><b><? 
				if($_GET["type"] <> "" ) {
					echo $_GET["type"];
				}else{
					echo "Tous";
				}?>
				</b></td>
				</tr>				
</table>				
				
				<div class="table-responsive">
				
				
				<h1 style="text-align: center;">A - Liste des factures de vente</h1><br><br>
                                <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>Vente</b></th>
										<th><b>Client</b></th>										
										<th><b>Remarque</b></th>
										
										<th><b>Total HT Brute</b></th>
										<th><b>Montant<br>remise</b></th>
										<th><b>Total HT Net</b></th>
										<?
										$r = 0;
										$total_tva = array();
										$total_tva_month = array();
										$total_tva_month_all = 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> 
									
									<?
									$qte_all 	 = 0;
									$prix_ht_all = 0;
									
									$pu_all  = 0;
									$total_ht_all = 0;
									$remise_all = 0;
									$remise_ht_all = 0;
									$total_ht_net_all = 0;
									$total_ttc_all = 0;
									
									$array_tva = array();
									$array_tva_qte = array();
									$array_remise = array();
									$array_remise_all = array();
									$i = 0;
									
									
									$count = 0;
									$cnt_moi = 0;
									$cnt_mounth = 1;
									$total = 0;
									$total_ttc_all = 0;
									$total_ttc_all_month = 0;
									$total_ttc_all_month_all = 0;
									$total_total_ht = 0;
									$total_total_ht_all = 0;
									$total_total_ht_all_a = 0;
									$total_total_ht_all_month = 0;
									$remise_all_month = 0;
									$remise_all_month_all = 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 where date_facture >= ".$date_trans." AND date_facture < ".$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["id_client"] <> ""){
										$query .= " AND id_client = ".$_GET["id_client"];
									}
									
									if($_GET["ref_fac_vente"] <> ""){
										$query .= " AND id = '".$_GET["ref_fac_vente"]."'";
									}
									if($_GET["ref_fac_avoir"] <> ""){
										$query .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
									}
									
									if($_GET["type"] <> ""){
										$query .= " AND type = '".$_GET["type"]."'";
									}
									
																			
									$query .= " order by date_facture 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 ++;
											
										// ***************************************************	
										// ***************************************************
											
											$ref_bl = $row["ref_bl"];
											$array_bl = explode(",", $ref_bl);
											$count_bl = count($array_bl);
								

											//========= Recuperé les produits =====
											for ($x = 0; $x < $count_bl; $x++)
											{	
												// echo "====>".$count_bl[$x];
												// $query_prod = "SELECT * FROM `pm_produits`as a , pm_bl_product as b WHERE a.id = b.id_produits AND b.id_bl = ".$array_bl[$x] ;
												// $query_prod = "SELECT * FROM `pm_facture_gen` WHERE id_facture = ".$id_facture ;
												$query_prod = "SELECT * FROM `pm_produits`as a , pm_bl_product as b WHERE a.id = b.id_produits AND b.id_bl = ".$array_bl[$x] ;
												// echo "====>".$query_prod."<br>";
												$result_prod = @$db->query($query_prod);
												if($result_prod !== false){
													foreach($result_prod as $row_2){
													//======== get coulissage ======	
													$queryP = "SELECT * FROM `pm_produits` WHERE `code_produit` = '".$row_2["code_produit"]."'";	
													// echo $queryP."<br>" ;
													$result_p = @$db->query($queryP);
													if($result_p !== false){
														$row_p = $result_p->fetch();
														$array_tva[$i] = $row_p["tva"];
													}
													//======== get uniter ======	
													$queryU = "SELECT * FROM `pm_unite` WHERE `id` = ".$row_p["unite"];	
													$result_u = @$db->query($queryU);
													if($result_u !== false){
														$row_u = $result_u->fetch();
													}						
													$qte     = 0;	
													$prix_ht = 0;	
													$remise  = 0;	
													$qte = $row_2["qte"];	
													$prix_ht = $row_2["prix_ht"];	
													$remise = $row_2["remise"];
													//=====================
													$prix_ht_bl = $row_2["prix_ht1"];	
													$remise_bl = $row_2["remise1"];	
													if($prix_ht_bl){
														$prix_ht_new = $prix_ht_bl;
													}else{
														$prix_ht_new = $prix_ht;
													}
													if($remise_bl){
														$remise_new = floatval($remise_bl);
													}else{
														$remise_new = floatval($remise);
													}	
													
													// echo $remise_new;
													// $array_remise[$row["id"]] += $remise_new ;
													$array_remise_all[$row["id"]] += $remise_new ;
													// echo "//".$array_remise[$row["id"]]."\\";
													
													$total_ht = $qte  * $prix_ht_new ;
													$remise_ht = ($remise_new * $total_ht)/100;
													$array_remise[$row["id"]] += $remise_ht ;
													$remise_all_month += $remise_ht ;
													$remise_all_month_all += $remise_ht ;
													$total_ht_net = $total_ht- $remise_ht;
													
													$array_tva_qte[$row_p["tva"]] += $total_ht_net;	
													
													//---------------------------
													$qte_all 	 = $qte_all + $qte;
													$total_ht = intval($qte) * $prix_ht_new ;
													// TOTAUX ==============
												  $pu_all += floatval($prix_ht_new);
												  $total_ht_all += $total_ht;
												  $remise_all += floatval($remise_new);
												  // $remise_all_month += floatval($remise_new);
												  // echo $remise_all;
												  $remise_ht_all += $remise_ht;
												  $total_ht_net_all += $total_ht_net;
												  $total_ttc = $total_ht_net + (($total_ht_net*$row_p["tva"])/100);
												  // echo "momo >>>".$total_ttc."<br>";
												  $total_ttc_all += $total_ttc;
													$i++;
														}
													}
												}													
										// ***************************************************	
										// ***************************************************	
										
										
										$date_facture = date('d-m-Y', $row["date_facture"]);
										$count++;
										// $nbr_resultat = $row["nb"];
										$total_ttc_all += $row["total_ttc"];
										$total_ttc_all_month += $row["total_ttc"];
										$total_ttc_all_month_all += $row["total_ttc"];
										$total_total_ht = number_format($total_ht_all,3, '.',',');										
										$total_total_ht_all += floatval($total_ht_all);
										$total_total_ht_all_month += floatval($total_ht_all);
										
										// echo $total_ht_all."<br>";
										
										
									// ================ GROUP BY MONTH ===============	
										$cnt_mounth++;
										$tva_total_tot = "";
										$date = $date_facture;
										$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 where date_facture >= ".$date_int." AND date_facture <= ".$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["id_client"] <> ""){
											$nbr_mounth .= " AND id_client = ".$_GET["id_client"];
										}
										
										if($_GET["ref_fac_vente"] <> ""){
											$nbr_mounth .= " AND id = '".$_GET["ref_fac_vente"]."'";
										}
										if($_GET["ref_fac_avoir"] <> ""){
											$nbr_mounth .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
										}
										
										if($_GET["type"] <> ""){
											$nbr_mounth .= " AND type = '".$_GET["type"]."'";
										}
										
										$result_mounth = @$db->query($nbr_mounth);
										if($result_mounth !== false){
											foreach($result_mounth as $row_mounth){
												
												$nbr_return = $row_mounth["cnt"];
											
											}
										}
										// echo $nbr_mounth."<br>";
										//******************************************
										$jour  = date("w",strtotime($date));
											
										if($mois[$month] == 0){											
											echo "<tr><td colspan='".($r+9)."' 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;
											$mois[$month-1] = 0;
											$cnt_moi++;
											$cnt_mounth = 1;
											if($cnt_moi == 12){
												$cnt_moi = 1;
											}
										}	

										
									?>
									<tr>	
										<td><a href="/admin/facture/print.php?id=<? echo $row["id"]; ?>" target="_blank"><? echo $row["id"]; ?></a></td>		
										<td><? echo $date_facture; ?></td>		
										<td><? echo $row["idd"]; ?></td>		
										<td><? echo client($row["id_client"]); ?></td>		
										<td><? echo substr($row["remarque"], 0, 50); ?></td>		
										<td align='right'><? echo number_format($total_ht_all,3, '.',','); ?></td>
										
										<td align='right'><? echo number_format($array_remise[$row["id"]],3, '.',','); ?></td>
										<td align='right'><? echo number_format($total_ht_all-$array_remise[$row["id"]],3, '.',','); ?></td>										
									<?
									
										$total_total_ht = 0;
										$total_ht_all = 0;

										$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){
												
												$tvaaa = ($array_tva_qte[$row_tva["value"]] * $row_tva["value"])/100 ;
												
												$total_tva_month[$row_tva["value"]] += $tvaaa;
												$total_tva_month_all[$row_tva["value"]] += $tvaaa;
												
												$val_tva = "<td align='right'>0</td>";
										
												$val_tva = "<td  align='right'>".number_format($tvaaa,3, '.',',')."</td>";
											
												
												echo $val_tva;
												
												$array_tva_qte[$row_tva["value"]] = 0 ;
											}	
										}
									
									?>
										
										
										<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,3, '.',',')."</b></td><td align='right'><b>".number_format($remise_all_month,3, '.',',')."</b></td><td align='right'><b>".number_format($total_total_ht_all-$remise_all,3, '.',',')."</b></td>".$tva_total_tot."<td align='right'><b>".number_format($total_ttc_all_month,3, '.',',')."</b></td></tr>";
												
																							
												$remise_all_month = 0;
												$total_total_ht_all = 0;
												$remise_all = 0;
												
												// ========= 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_month_all[$row_tva["value"]],3, '.',',')."</b></td>";
											}
										}
										
										
										$total_1 = "<tr><td colspan='8' align='center' style='background-color: #00a1af;'><b style='text-transform: capitalize;color:#FFF;'> TOTAL </b></td></tr>";
										$total_1 .= "<tr><td  align='right' ></td><td align='center'><b>Total HT Brute</b></td><td align='center'><b>Total Montant remise</b></td><td align='center'><b>Total HT Net</b></td>".$tva_text."<td align='right'><b>TOTAL TTC</b></td></tr>";
										$total_1 .= "<tr><td  align='right' ><b>Total A (liste des factures de vente )</b></td><td align='right'><b>".number_format($total_total_ht_all_month,3, '.',',')."</b></td><td align='right'><b>".number_format($remise_all_month_all,3, '.',',')."</b></td><td align='right'><b>".number_format($total_total_ht_all_month-$remise_all_month,3, '.',',')."</b></td>".$tva_total."<td align='right'><b>".number_format($total_ttc_all_month_all,3, '.',',')."</b></td></tr>";
										
										
									}	
									?>

										
                                    </tbody>
                                </table>
																
								
                </div>
				
				<br><br>
				<?
				
				$remise_ht_all = 0;
				$remise_ht_month2 = 0;
				$total_total_ht_month2 = 0;
				?>
				<div class="table-responsive">
				
				
				<h1 style="text-align: center;">B - Liste des factures d'avoir/retour</h1><br><br>
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>
										<th><b>ID</b></th>
										<th><b>Type</b></th>
										<th><b>Date<br>Facture</b></th>
										<th><b>Ref Fac<br>d'avoir</b></th>
										<th><b>Client</b></th>										
										<th><b>Remarque</b></th>
										
										<th><b>Total HT Brute</b></th>
										<th><b>Montant remise</b></th>
										<th><b>Total HT Net</b></th>
										
										<?
										$r = 0;
										$total_tva2 = array();
										$total_tva_month2 = array();
										$tva_total2 = "";
										
										$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_tva2[$row_tva["value"]] = 0;
												$total_tva_month2[$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_all2 = 0;
									$total_ttc_all_month2 = 0;
									$total_total_ht = 0;
									$total_total_ht_all2 = 0;
									$total_total_ht_all_month2 = 0;
									$total_ht_all_alllll = 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_vente where date_facture >= ".$date_trans." AND date_facture < ".$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["id_client"] <> ""){
										$query .= " AND client = ".$_GET["id_client"];
									}
									
							
									if($_GET["ref_fac_avoir"] <> ""){
										$query .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
									}
									
									if($_GET["typeb"] <> ""){
										$query .= " AND type = '".$_GET["typeb"]."'";
									}
																			
									$query .= " order by date_facture 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_facture = date('d-m-Y', $row["date_facture"]);
										$count++;
										// $nbr_resultat = $row["nb"];
										$id_facture = $row["id"];
										$type = $row["type"];
										
										
										
										// ===========================================
										// ===========================================
					$query_prod = "SELECT * FROM `pm_facture_avoir_vente_gen` WHERE id_facture = ".$id_facture ;
					// $query_prod = "SELECT * FROM `pm_produits`as a , pm_brv_product as b WHERE a.id = b.id_produits AND b.id_brv = ".$array_bl[$x] ;
					// echo "====>".$query_prod."<br>";
					$total_ht_all = 0 ;
					$total_ht_net_all = 0 ;
					$result_prod = @$db->query($query_prod);
					if($result_prod !== false){
						foreach($result_prod as $row_2){
						//======== get coulissage ======	
						$queryP = "SELECT *,a.coulissage as coul FROM `pm_produits` as a , `pm_produitsachat` as b WHERE a.code_produit = '".$row_2["code"]."' OR b.code_produit = '".$row_2["code"]."'";	
						// echo $queryP."<br>" ;
						$result_p = @$db->query($queryP);
						if($result_p !== false){
							$row_p = $result_p->fetch();
							$array_tva[$i] = $row_p["tva"];
							$coulissage = $row_p["coul"];
						}
						//======== get uniter ======	
						$queryU = "SELECT * FROM `pm_unite` WHERE `id` = ".$row_p["unite"];	
						$result_u = @$db->query($queryU);
						if($result_u !== false){
							$row_u = $result_u->fetch();
						}						
						$qte     = 0;	
						$prix_ht = 0;	
						$remise  = 0;	
						$qte = $row_2["qte"];	
						$prix_ht = $row_2["pu_ht"];	
						$remise = $row_2["remise"];
						
						//=====================
						$prix_ht_bl = $row_2["prix_ht1"];	
						$remise_bl = $row_2["remise1"];	
						if($prix_ht_bl){
							$prix_ht_new = $prix_ht_bl;
						}else{
							$prix_ht_new = $prix_ht;
						}
						if($remise_bl){
							$remise_new = $remise_bl;
						}else{
							$remise_new = $remise;
						}	
						$total_ht = $qte  * $prix_ht_new ;
						$remise_ht = ($remise_new * $total_ht)/100;
						$total_ht_net = $total_ht- $remise_ht;
						
						// echo $row_2["code"]." ==> ".$row_p["tva"]."<br>";
						$array_tva_qte[$row_p["tva"]] += $total_ht_net;
			
							$qte_all 	 = $qte_all + $qte;
							$total_ht = intval($qte) * $prix_ht_new ;
							// TOTAUX ==============
						  $pu_all += floatval($prix_ht_new);
						  $total_ht_all += $total_ht;
						  $total_ht_all_alllll += $total_ht;
						  $total_net_all_alllll += $total_ht_net;
						  $remise_all += floatval($remise_new);
						  $remise_ht_all += $remise_ht;
						  $remise_ht_month2 += $remise_ht;
						  $total_ht_net_all += $total_ht_net;
						  $total_ttc = $total_ht_net + (($total_ht_net*$row_p["tva"])/100);
						  // echo "momo >>>".$total_ttc."<br>";
						  $total_ttc_all += $total_ttc;
							$i++;
								}
							}
										// ===========================================
										// ===========================================
										$total_ttc_all2 += $row["total_ttc"];
										$total_ttc_all_month2 += $row["total_ttc"];
										$total_total_ht = $total_ht;
										$total_total_ht_all2 += $total_ht_net;
										$total_total_ht_month2 += $total_ht_net;
										$total_total_ht_all_month2 += $total_total_ht;
										$total_total_ht_all_month2_all += $total_total_ht;
										
										
										
									// ================ GROUP BY MONTH ===============	
										$cnt_mounth++;
										$tva_total_tot2 = "";
										$date = $date_facture;
										$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_vente where date_facture >= ".$date_int." AND date_facture <= ".$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["id_client"] <> ""){
										$nbr_mounth .= " AND client = ".$_GET["id_client"];
										}
										
								
										if($_GET["ref_fac_avoir"] <> ""){
											$nbr_mounth .= " AND ref_facture_vente = '".$_GET["ref_fac_vente"]."'";
										}
										if($_GET["typeb"] <> ""){
											$nbr_mounth .= " AND type = '".$_GET["typeb"]."'";
										}
										// echo "<br>".$nbr_mounth;
										$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+10)."' 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;
											$mois[$month-1] = 0;
											$cnt_moi++;
											$cnt_mounth = 1;
											if($cnt_moi == 12){
												$cnt_moi = 1;
											}
											
										}	

										
									?>
									<tr>	
										<td><a href="/admin/modules/facture_avoir_vente/print.php?id=<? echo $row["id"]; ?>" target="_blank"><? echo $row["id"]; ?></a></td>		
										<td style="text-transform: uppercase;"><? echo $type; ?></td>		
										<td><? echo $date_facture; ?></td>		
										<td><? echo $row["idd"]; ?></td>		
										<td><? echo client($row["client"]); ?></td>		
										<td><? echo substr($row["remarque"], 0, 50); ?></td>		
										<td align='right'><? echo number_format($total_ht_all,3, '.',','); ?></td>	
										<td align='right'><? echo number_format($remise_ht,3, '.',','); ?></td>	
										<td align='right'><? echo number_format($total_ht_net_all,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){
												
														$value = $row_tva["value"] ;
												// $val_tva = "<td align='right'>0</td>";
														$tvaaa = ($array_tva_qte[$value] * $value)/100 ;
														// echo "==>".$total_tva[$row_tva["value"]]."<br>";
														// $total_tva2[$row_tva["value"]] += $array_tva[$value];
														$total_tva2[$row_tva["value"]] += $tvaaa;
														$total_tva_month2[$row_tva["value"]] += $tvaaa;
														$total_tva_month2_all[$row_tva["value"]] += $tvaaa;
														$val_tva = "<td  align='right'>".number_format($tvaaa,3, '.',',')."</td>";
													// }
												// }	
												$row_tva["value"] = 0;
												$array_tva_qte[$value] = 0;
												echo $val_tva;
											}	
										}
									
									?>
										
		
										<td align="right" ><? echo number_format($row["total_ttc"],3, '.',','); ?></td>		
									</tr>
								
									
									<?	
									//******************************************************************
																			
										// echo "<tr><td colspan='12' 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_tot2 .= "<td align='right'><b>".number_format($total_tva_month2[$row_tva["value"]],3, '.',',')."</b></td>";
													}
												}
												// ===============
												echo "<tr><td colspan='6' align='right'><b>TOTAL</b></td><td align='right'><b>".number_format($total_ht_all_alllll,3, '.',',')."</b></td>";
												echo "<td align='right'><b>".number_format($remise_ht_all,3, '.',',')."</b></td>";
												echo "<td align='right'><b>".number_format($total_net_all_alllll,3, '.',',')."</b></td>";
												echo $tva_total_tot2;
												echo "<td align='right'><b>".number_format($total_ttc_all_month2,3, '.',',')."</b></td></tr>";
											
												// ========= Mettre a zero TVA & Total HT ============
												$total_ht_all_alllll = 0;
												$total_net_all_alllll = 0;
												$total_total_ht_all_month2 = 0; 
												$total_ttc_all_month2 = 0;
												$remise_ht_all = 0;												
												$total_total_ht_all2 = 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_month2[$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_total2 .= "<td align='right'><b>".number_format($total_tva_month2_all[$row_tva["value"]],3, '.',',')."</b></td>";
											}
										}
										
										//******************************
										$tva_total_all = "";
										$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_all .= "<td align='right'><b>".number_format($total_tva_month_all[$row_tva["value"]]+$total_tva2[$row_tva["value"]],3, '.',',')."</b></td>";
											}
										}
										//******************************
										
										
										// $remise_ht_month2 ==> REMISE
										
											
										
										// $total_1 .= "<tr><tdht'><b>".number_format($total_total_ht_all_month,3, '.',',')."</b></td><td align='right'><b>".number_format($remise_all_month,3, '.',',')."</b></td><td align='right'><b>".number_format($total_total_ht_all_month-$remise_all_month,3, '.',',')."</b></td>".$tva_total."<td align='right'><b>".number_format($total_ttc_all_month_all,3, '.',',')."</b></td></tr>";
									
										
										$total_2 = "<tr><td  align='right' ><b>Total B (liste des factures d'avoir )</b></td>";
										$total_2 .="<td align='right'><b>".number_format($total_total_ht_all_month2_all,3, '.',',')."</b></td>";
										$total_2 .="<td align='right'><b>".number_format($remise_ht_month2,3, '.',',')."</b></td>";
										$total_2 .="<td align='right'><b>".number_format($total_total_ht_month2,3, '.',',')."</b></td>";
										$total_2 .= $tva_total2."<td align='right'><b>".number_format($total_ttc_all2,3, '.',',')."</b></td></tr>";
										$total_2 .= "<tr><td  align='right' ><b>Total général</b></td><td align='right'><b>".number_format($total_total_ht_all_month+$total_total_ht_all_month2_all,3, '.',',')."</b></td><td align='right'><b>".number_format($remise_all_month_all+$remise_ht_month2,3, '.',',')."</b></td><td align='right'><b>".number_format(($total_total_ht_all_month+$remise_all_month)+$total_total_ht_month2,3, '.',',')."</b></td>".$tva_total_all."<td align='right'><b>".number_format($total_ttc_all_month_all+$total_ttc_all2,3, '.',',')."</b></td></tr>";
									}	
									?>

										
                                    </tbody>
                                </table>
								
														
								
								
                </div>
				<br><br>
				<div class="table-responsive">
					<table class="table table-bordered table-hover table-striped" id="listing_base">
						<? echo $total_1; ?>
						<? echo $total_2; ?>
					</table>
				</div>				
					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<style>
@media print{

@page {size: landscape}
#wrapper {    margin-left: 0px;}
#page-wrapper{
	padding : 0px;
}
body {
	padding : 20px;
	
}
table{
	border-collapse:collapse !important;
	font-size: 7px;
	border-spacing: 2px !important;
}
h1{
	font-size: 14px;
	margin: 0;
}
table td, table th
{
    padding: 2px !important;
}
}
</style>
<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; ?>
