<?php

ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'rapport_achat');
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> Rapport Facture Achat non règle 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>Fournisseur</b></th>
										<th><b>Référence<br>Facture<br>Achat</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>Observations</b></th>
										<th></th>
                                    </thead>
                                    <tbody> 

							<?
								$total_ttc_all= 0;
								$somme_all = 0;
								$reste_all = 0;
								$reste_all_in = 0;
								$reste_all_out = 0;
								
								$observation = array();
								
								
								$query_jour = "select *, a.id as id_fact, a.observation as obs FROM  pm_facture_achat as a, pm_fournisseurs as b where a.fournisseur = b.id";
								$query_jour .= " AND a.date_entre > 1704067200"  ;
								$query_jour .= " order by reglement ASC, date_entre ASC"  ;
								// echo $query_jour.";<br>";
								$result_jour = @$db->query($query_jour);
								if($result_jour !== false){
									foreach($result_jour as $row){
										$total_moin = 0 ;
										$total_plus = 0;
										$total = 0;
										$somme = 0;
										
									$date_entre = $row["date_entre"];
									$ref_bl = $row["ref_bl"];
									$total_ttc = $row["total_ttc"];
									
									$observation[$row["id_fact"]] = $row["obs"];
									
									// echo "-".$observation;
									
									$date_entre = date('d-m-Y',$date_entre) ;
									// ============ Fournisseur ============
									$fournisseur = $row['name'];
									$date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row["reglement"].' days')); 
												// $query_fr = "SELECT * FROM pm_fournisseurs WHERE id = ".$row["fournisseur"];
															// $result_fr = $db->query($query_fr);
															// if($result_fr !== false && $db->last_row_count() > 0){
																// $row_fr = $result_fr->fetch();																
																// $fournisseur = $row_fr['name'];
																// $date_ech = date('d-m-Y', strtotime($date_entre. ' + '.$row_fr["reglement"].' days')); 
															// }	
									// ============ régelement ============
																					
												$query_in = "SELECT SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE type = 'in' AND ref_facture_achat = ".$row["id_fact"];
												$query_out = "SELECT SUM(montant) as somme, SUM(retenue_montant) as retenue FROM pm_transaction WHERE type = 'out' AND ref_facture_achat = ".$row["id_fact"];
												
												
												// echo $query_in.";<br>";
												$result_in = $db->query($query_in);
												if($result_in !== false && $db->last_row_count() > 0){
													$row_in = $result_in->fetch();																
													$somme_in = $row_in['somme'];
													$retenue_montant_in = $row_in['retenue'];
													
													$query_in2 = "SELECT SUM(montant) as somme, SUM(reste) as retenue FROM pm_transaction_product WHERE ref_facture_achat2 = ".$row["id_fact"];
													// echo $query_out2."<br>";
													$result_in2 = $db->query($query_in2);
													if($result_in2 !== false && $db->last_row_count() > 0){
														$row_in2 = $result_in2->fetch();
														// $somme_in += $row_in2['somme'];
														// $retenue_montant_out += $row_out2['retenue'];
													}													
													
													
												}
												$total_in = $somme_in + $retenue_montant_in ;
												//=== OUT ===
												$result_out = $db->query($query_out);
												if($result_out !== false && $db->last_row_count() > 0){
													$row_out = $result_out->fetch();																
													$somme_out = $row_out['somme'];
													$retenue_montant_out = $row_out['retenue'];
													
													$query_out2 = "SELECT SUM(montant) as somme, SUM(reste) as retenue FROM pm_transaction_product WHERE ref_facture_achat2 = ".$row["id_fact"];
													// 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_out += $row_out2['somme'];
														// $retenue_montant_out += $row_out2['retenue'];
													}
												}
												//============= Facture Avoir =================
												$total_avoir = 0;
												$query_avoir = "SELECT *, SUM(total_ttc) as total_avoir FROM `pm_facture_avoir` where ref_bl =".$row["id_fact"];
												// echo $query_avoir.";<br>";
												$result_avoir = @$db->query($query_avoir);
												if($result_avoir !== false){
													foreach($result_avoir as $row_avoir){
														$total_avoir = $row_avoir["total_avoir"];	 		
													}
												}
												//=============================================
												$total_out = $somme_out + $retenue_montant_out + $total_avoir ;
											$somme = $total_in - $total_out ;
											$reste = ($total_ttc+$total_in)-$total_out;	
											
											$reste = number_format($reste,3, '.',' ');
											
											if($reste <> 0){	
											
											$somme_all += $somme;							 
											$total_ttc_all += $total_ttc;
											
											if($reste > 0){
												$reste_all_in += $total_in;
											}else{
												$reste_all_out += $total_out;
											}
											$reste_all += $reste_all_in;
										?>
										<tr>
											<td align="left"><? echo $date_entre; ?></td>
											<td align="left"><? echo $fournisseur; ?></td>
											<td align="left"><a href='/admin/modules/facture_achat/index.php?view=form&id=<? echo $row["id"]; ?>' target="_black"><? echo $ref_bl; ?></a></td>
											<td align="right"><? echo number_format($total_ttc,3, '.',' '); ?></td>
											<td align="right"><? echo number_format($somme,3, '.',' '); ?></td>
											<td align="right"><? echo $reste; ?></td>
											<td align="left"><? echo $date_ech; ?></td>
											<td align="left"><? echo $observation[$row["id_fact"]]; ?></td>
											<td align="center"><a href="edit_observation.php?id=<? echo $row["id_fact"]; ?>&type=achat"><i class="fa fa-pencil"></i></a></td>
										</tr>
										<?	
											}

											}
										}
										
									?>	
										<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($total_ttc_all + $somme_all,3, '.',' '); ?></b></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'] = ""; ?>
