<?php

ini_set('display_errors', 1);


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


$date = date("d-m-Y");

?>
<!DOCTYPE html>
<head>
    <?php include("../includes/inc_header_common.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-line-chart"></i> Relevé échéance clients du <? echo $date; ?></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="alert-container">
                    <div class="alert alert-success alert-dismissable"></div>
                    <div class="alert alert-warning alert-dismissable"></div>
                    <div class="alert alert-danger alert-dismissable"></div>
                </div>
                <div class="row">				

			
	<h3>Rapport <? echo $date; ?></h3>
<br>	
<div class="table-responsive">
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>
										<th><b>Date Facture</b></th>
										<th><b>Client</b></th>
										<th><b>Référence<br>Facture<br>Vente</b></th>
										<th><b>Montant<br>Facture<br>TTC</b></th>
										<th><b>Total<br>Réglement</b></th>
										<th><b>Reste à régler</b></th>
										<th><b>Date<br>échéance</b></th>
										<th><b>Reste<br>non payé</b></th>
										<th><b>Observations</b></th>
										<th></th>
                                    </thead>
                                    <tbody> 

							<?
								$total_ttc_all= 0;
								$somme_all = 0;
								$somme_nonp_all = 0;
								$reste_all = 0;
								$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_jour = time();
								
								
								// echo $date_jour;
								
								$query_jour = "select * FROM  pm_facture "  ;
								if($_GET["id_client"] <> ""){
									$query_jour .= " where id_client = ".$_GET["id_client"]  ;
								}
								$query_jour .= " order by date_facture ASC"  ;
								// $query_jour = "select *, a.id as id_fact FROM  pm_facture as a, pm_client as b where a.Id_client = b.id order by date_facture ASC"  ;
								// echo $query_jour.";<br>";
								$result_jour = @$db->query($query_jour);
								if($result_jour !== false){
									foreach($result_jour as $row){
										$is_day = "";
										$total_moin = 0 ;
										$total_plus = 0;
										$total = 0;
										$somme = 0;
										$somme_nonp = 0;
										$somme_avoir = 0;
										$type_facture = $row["type"];
									if($type_facture == 'B'){
										$is_day = " style='background-color:#c5c5ff;'"; 
									}else{
										$is_day = "";
									}	
										
										
																				
									$date_entre = $row["date_facture"];
									$ref_bl = $row["ref_bl"];
									$idd = $row["idd"];
									
									$id = $row["id"];
									
									$observations = $row['observation'];
									
																		
									
									
									$date_entre = date('d-m-Y',$date_entre) ;
									// ============ client ============
									// $client = $row['name'];
									
												$query_fr = "SELECT * FROM pm_client WHERE id = ".$row["id_client"];
															$result_fr = $db->query($query_fr);
															if($result_fr !== false && $db->last_row_count() > 0){
																$row_fr = $result_fr->fetch();																
																$client = $row_fr['name'];
																// $date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row_fr["echeance"].' days')); 
															}	
									$date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row_fr["echeance"].' days')); 						
									// ============ régelement ============
																					
												$query_opp = "SELECT * ,SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE ref_facture_vente = ".$row["id"];
												// echo $query_opp.";<br>";
												$result_opp = $db->query($query_opp);
															if($result_opp !== false && $db->last_row_count() > 0){
																$row_opp = $result_opp->fetch();																
																// $somme = $row_opp['somme'];
																$type = $row_opp['type'];
																$retenue_montant = $row_opp['retenue'];
																
																if($row_opp['status_paiement'] == 1){
																	$somme = $row_opp['somme'];
																}
																
																
																if($row_opp['status_paiement'] <> 1){
																	$somme_nonp = $row_opp['somme'];
																}
																$query_out2 = "SELECT id_transaction,SUM(montant) as somme, SUM(reste) as retenue FROM pm_transaction_product WHERE ref_facture_vente2 = ".$row["id"];
																// echo $query_out2."<br>";
																$result_out2 = $db->query($query_out2);
																if($result_out2 !== false && $db->last_row_count() > 0){
																	$row_out2 = $result_out2->fetch();
																	$somme += $row_out2['somme'];
																}	





																//id_transaction,SUM(montant) as somme, SUM(reste) as retenue
																$query_out2 = "SELECT * FROM pm_transaction_product WHERE ref_facture_vente2 = ".$row["id"];
																// echo $query_out2."<br>";
																$result_out2 = $db->query($query_out2);
																if($result_out2 !== false){
																	foreach($result_out2 as $row_out2){
																																			
																		$query_out20 = "SELECT status_paiement FROM pm_transaction WHERE id = ".$row_out2["id_transaction"];
																		// echo $query_out20." ".$row["id"]."<br>" ;
																		$result_out20 = $db->query($query_out20);
																		if($result_out20 !== false && $db->last_row_count() > 0){
																		$row_out20 = $result_out20->fetch();
																			$is_status_paiement = $row_out20['status_paiement'] ; 
																			if($is_status_paiement <> 1){
																				$somme_nonp += $row_out2['montant'];
																			}
																		}															
																	
																	}
																}


																
																
															}
									
											$total_ttc = $row["total_ttc"];
											
											
									if($date_jour >= strtotime($date_ech)){
										$is_day = " style='background-color:#ffb9b9;'";
									}

									
									
									// ============ facture_avoir_vente ============
									$query_avoir = "SELECT * ,SUM(total_ttc) as somme_avoir FROM pm_facture_avoir_vente WHERE ref_facture = ".$row["id"];
												// echo $query_opp.";<br>";
									$result_avoir = $db->query($query_avoir);
									if($result_avoir !== false && $db->last_row_count() > 0){
										$row_avoir = $result_avoir->fetch();																
										$somme_avoir = $row_avoir['somme_avoir'];
										$type = "in" ;
										// echo $somme_avoir." ==> ".$row["id"]."<br>";
									}
									// =============================================
									$somme_nonp = $somme_nonp  ;
									$somme = $somme + $somme_avoir ;
											$reste = $total_ttc-($somme+$retenue_montant);
											
											$reste = number_format($reste,3, '.','');
											// $reste2 = number_format($reste,3, '.',' ');
											
										if(($reste <> 0)||($somme_nonp <> 0)){
											// echo $row["total_ttc"]." ==>> '". $row_opp['status_paiement'] ."'<br>";
												
											
											if($type == "in"){	
												// $somme = $somme * (-1);
												// $somme_nonp = $somme_nonp * (-1);
											}
											
											$reste_all += $reste; 
											$somme_all += $somme;											
											$somme_nonp_all += $somme_nonp;											
											$total_ttc_all += $total_ttc;
											


									
											
											$month = date("n",strtotime($date_ech));
											$jour  = date("w",strtotime($date_ech));
											
											if($mois[$month] == 0){
													echo "<tr><td colspan='10' align='center' style='background-color: #FFF;'><b style='text-transform: capitalize;'>".utf8_encode(strftime("%B",strtotime($date_ech)))." ".date("Y",strtotime($date_ech))."</b></td></tr>";
											}
											
											
										?>
										<tr>
											<td align="left" <? echo $is_day; ?> ><? echo $date_entre; ?></td>
											<td align="left" <? echo $is_day; ?>><? echo $client; ?></td>
											<td align="left" <? echo $is_day; ?>><a href='/admin/modules/facture/index.php?view=form&id=<? echo $row["id"]; ?>' target="_black"><? echo $idd; ?></a></td>
											<td align="right" <? echo $is_day; ?>><? echo number_format($total_ttc,3, '.',' '); ?></td>
											<td align="right" <? echo $is_day; ?>><? echo number_format($somme,3, '.',' '); ?></td>
											<td align="right" <? echo $is_day; ?>><? echo number_format($reste,3, '.',' '); ?></td>
											<td align="left" <? echo $is_day; ?>><? echo $date_ech; ?></td>
											<td align="right" <? echo $is_day; ?>><span style=""><? echo number_format($somme_nonp,3, '.',' '); ?></span></td>
											<td align="left" <? echo $is_day; ?>><? echo $observations; ?></td>
											<td align="center" <? echo $is_day; ?>><a href="edit_observation.php?id=<? echo $id; ?>&type=vente"><i class="fa fa-pencil"></i></a></td>
										
										</tr>
										<?	
										$mois[$month] = 1;
											// }
										}
											}
										}
										
									?>	
										<tr>
											<td align="center" colspan="3"><b>Total</b></td>
											<td align="right"><b><? echo number_format($total_ttc_all,3, '.',' '); ?></b></td>
											<td align="right"><b><? echo number_format($somme_all,3, '.',' '); ?></b></td>
											<td align="right"><b><? echo number_format($reste_all,3, '.',' '); ?></b></td>
											<td align="left"></td>
											<td align="right"><b><? echo number_format($somme_nonp_all,3, '.',' '); ?></b></td>
											<td align="left"></td>
											<td align="left"></td>
										</tr>	
										<tr>
											<td align="center" colspan="3"><b>Total du crédit client</b></td>
											<td align="right"><b><? ?></b></td>
											<td align="right"><b><?  ?></b></td>
											<td align="center" colspan="3"><b><? echo number_format($reste_all+$somme_nonp_all,3, '.',' '); ?></b></td>
											<td align="left"></td>
											<td align="left"></td>
										</tr>	
										<tr>
											<td align="center" colspan="3"><b>Taux de crédit / Chiffres d'afffaire</b></td>
											<td align="right"><b><? ?></b></td>
											<td align="right"><b><?  ?></b></td>
											<td align="right"><b><? ?></b></td>
											<td align="left"></td>
											<td align="left"></td>
											<td align="left"></td>
											<td align="left"></td>
										</tr>										
                                    </tbody>
                                </table>
				

								
								
                            </div>
									

					
                </div>
            </div>
        </div>
    </div>
</body>
</html>
<?php
$_SESSION['msg_error'] = "";
$_SESSION['msg_success'] = "";
$_SESSION['msg_notice'] = ""; ?>
