<?php

ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../common/lib.php");
require_once("../../common/define.php");
define("TITLE_ELEMENT", 'rapport');
if(!isset($_SESSION['user'])){
    header("Location: ../login.php");
    exit();
}
require_once("../includes/fn_module.php"); ?>
<!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> Journal quotidien du <? echo $_GET["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">				

<?


		

		
		
		
		$count_j = 0;

	
	$query_jour = "select * FROM  pm_journal"  ;
	if($_GET["journal"] <> ""){
		$query_jour .= " WHERE id = '".$_GET["journal"]."' ORDER BY id ASC";
	}
	$result_jour = @$db->query($query_jour);
	if($result_jour !== false){
		foreach($result_jour as $row_jour){
			$total_moin = 0 ;
			$total_plus = 0;
			$total = 0;
			
			$total_moin_all = 0 ;
			$total_plus_all = 0;
			$total_all = 0;
			
			$total_moin_alla = 0 ;
			$total_plus_alla = 0;
			$total_alla = 0;
			
			$total_moina = 0 ;
			$total_plusa = 0;
			$totala = 0;

		
			$get_journal = $row_jour["id"];
			$get_journal_name = $row_jour["name"];

?>
				
	<h3>Rapport <? echo $get_journal_name; ?></h3>
<br>	
<div class="table-responsive">
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    <thead>
										<th><b>Journal</b></th>
										<th><b>Année exercice</b></th>
										<th><b>ID</b></th>
										<th><b>__IN/OUT__</b></th>
										<th><b>Classe<br>Comptable</b></th>
										<th><b>Compte<br>Financier</b></th>
										<th><b>Fournisseur/client</b></th>
										<th><b>Ref Fac Achat/vente</b></th>
										<th><b>libellé</b></th>
										<th><b>Mode de paiement</b></th>
										<th><b>Référence</b></th>
                                    </thead>
                                    <tbody> 

										<?php
										
										if($db !== false){
											$date_paiement = strtotime($_GET["date"]) ;
											$date_paiementa_moin = date('Y-m-d', strtotime($_GET["date"]. ' - 1 days'));
											$date_paiementa_moin = strtotime($date_paiementa_moin);
											$date_paiementa_moin2 = date('Y-m-d', strtotime($_GET["date"]. ' - 2 days'));
											$date_paiementa_moin2 = strtotime($date_paiementa_moin2);
											$date_paiementa = date('Y-m-d', strtotime($_GET["date"]. ' + 1 days'));
											$date_paiementa = strtotime($date_paiementa);
											
											$query_all = "select * from pm_transaction where date_paiement < ".$date_paiement;
											$query_all .= " AND journal = '".$get_journal."' ";
											if($get_journal <> 1){	
												$query_all .= " AND status_paiement = 1";
											}
											$query_all .= " ORDER BY date_paiement ASC";
							
											$result_all = @$db->query($query_all);
											if($result_all !== false){
												foreach($result_all as $row_all){
													if($row_all["type"] == "out"){
														$total_moin_all += $row_all["montant"] ;
													}else{
														$total_plus_all += $row_all["montant"] ;
													}
													
													
												}												
											}	
											$total_all = $total_plus_all - $total_moin_all ;
											$query = "select * from pm_transaction where date_paiement >= ".$date_paiement." AND date_paiement < ".$date_paiementa  ;
											$query .= " AND journal = '".$get_journal."' ";
											if($get_journal <> 1){	
												$query .= " AND status_paiement = 1";
											}
											$query .= " ORDER BY date_paiement ASC";
											// echo $query."<br>";
											$count_ligne = 0;
											$result = @$db->query($query);
											if($result !== false){
												foreach($result as $row){
												$count_ligne++;	
												// echo $count_ligne."<br>";
												// =========== journal ===========
												$query_j = "select * from pm_journal where id =".$row["journal"];
												$result_j = @$db->query($query_j);
												if($result_j !== false){
													foreach($result_j as $row_j){
														$journal = $row_j["name"];
													}
												}	
												// =========== Compte ===========
												$query_c = "select * from pm_compte_financier where id =".$row["compte"];
												$result_c = @$db->query($query_c);
												if($result_c !== false){
													foreach($result_c as $row_c){
														$compte = $row_c["name"];
													}
												}	
												// =========== classe Compte ===========
												$query_ca = "select * from pm_classe_compte where id =".$row_c["classe"];
												$result_ca = @$db->query($query_ca);
												if($result_ca !== false){
													foreach($result_ca as $row_ca){
														$classe = $row_ca["name"];
													}
												}	
												// =========== mode_reglement ===========
												$query_cam = "select * from pm_mode_paiement where id =".$row["mode_reglement"];
												$result_cam = @$db->query($query_cam);
												if($result_cam !== false){
													foreach($result_cam as $row_cam){
														$mode_reglement = $row_cam["name"];
													}
												}	
												// ============ Fournisseur ============
												$name = "";
												$query_fr = "SELECT name 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();																
																$name = $row_fr['name'];
															}	
												// ============ Ref facture achat============
												//============= achat ================================
												$ref_facture_achat = "";
												if($row["ref_facture_achat"] <> ""){
													$ref_facture_achat = ref_facture($row["ref_facture_achat"]) ;
												}else{
													$query_fact = "select ref_facture_achat2 from pm_transaction_product where id_transaction =".$row["id"];
													// echo $query_fact;
													$result_mnt = @$db->query($query_fact);
													if($result_mnt !== false){
														// $row_mnt = $result_mnt->fetch();
														foreach($result_mnt as $row_mnt){
															$ref_facture_achat .=  ref_facture($row_mnt["ref_facture_achat2"])." - ";
														}
														$ref_facture_achat = substr($ref_facture_achat, 0, -3);	
													}
														
												
												}//============= Vente ======
												$ref_facture_vente = "";
												if($row["ref_facture_vente"] <> ""){
													$ref_facture_vente = ref_facture_vente($row["ref_facture_vente"]) ;
												}else{
													$query_fact = "select ref_facture_vente2 from pm_transaction_product where id_transaction =".$row["id"];
													// echo $query_fact;
													$result_mnt = @$db->query($query_fact);
													if($result_mnt !== false){
														// $row_mnt = $result_mnt->fetch();
														foreach($result_mnt as $row_mnt){
															$ref_facture_vente .=  ref_facture_vente($row_mnt["ref_facture_vente2"])." - ";
														}
														$ref_facture_vente = substr($ref_facture_vente, 0, -3);	
													}
														
												}
												
															
												// ================ Client ============
												$client = '';
												$query_cl = "SELECT name FROM pm_client WHERE id = ".$row["client"];
															// echo $query_fr;
															$result_cl = $db->query($query_cl);
															if($result_cl !== false && $db->last_row_count() > 0){
																$row_cl = $result_cl->fetch();																
																$client = ' / '.$row_cl['name'];
															}												
												// =========== Montant ===========
												if($row["type"] == "out"){
													$montant = "<b style='color:red;'>".number_format($row["montant"] *(-1),3, '.',' ')."</b>";
													$total_moin += $row["montant"] ;
												}else{
													$montant = "<b style='color:green;'>".number_format($row["montant"],3, '.',' ')."</b>";
													$total_plus += $row["montant"] ;
												}	
												$total = $total_plus - $total_moin;	
												
													
												// }
												// $count_j++;
												// =============================================
												// if($ref_facture_vente){
													// $ref_facture_vente = "- FAC1800".$row["ref_facture_vente"];
												// }else{
													// $ref_facture_vente = "";
												// }
										?>
										<tr>
											<td align="left"><? echo $journal; ?></td>
											<td align="left"><? echo $row["annee"]; ?></td>
											<td align="left"><a href="/admin/modules/transaction/index.php?view=form&id=<? echo $row["id"]; ?>" target="_blank"><? echo $row["id"]; ?></a></td>
											<td align="right"><? echo $montant; ?></td>
											<td align="left"><? echo $classe; ?></td>
											<td align="left"><? echo $compte; ?></td>
											<td align="left"><? echo $name."".$client; ?></td>
											<td align="left"><? echo $ref_facture_achat." / ".$ref_facture_vente; ?></td>
											<td align="left"><? echo $row["libelle"]; ?></td>
											<td align="left"><? echo $mode_reglement; ?></td>
											<td align="left"><? echo $row["ref"]; ?></td>
										</tr>
										<?	
												}
											}
										}
										if($count_ligne == 0){
										?>
										<tr>
											<td align="center" colspan="11">Pas d'opération !</td>											
										</tr>										
										<?
										}
										// =========== Solde du dernier jour ===========
												$total_moin_j = 0 ;
												$total_plus_j = 0;
												$total_j = 0;
												
													$query_allj = "select * from pm_transaction where date_paiement >= ".$date_paiementa_moin." AND date_paiement < ".$date_paiement  ;
													// if($_GET["journal"] <> ""){
														$query_allj .= " AND journal = '".$get_journal."' ";
														$query_allj .= " AND (status_paiement = 1 OR  mode_reglement = 1) ";
														// $query_allj .= " OR mode_reglement = 1";
													// }
													// echo $query_allj."<br>";
													$result_allj = @$db->query($query_allj);
													if($result_allj !== false){
														foreach($result_allj as $row_allj){
															
															if($row_allj["type"] == "out"){
																$montant_j = "<b style='color:red;'>".number_format($row_allj["montant"] *(-1),3, '.',' ')."</b>";
																$total_moin_j += $row_allj["montant"] ;
															}else{
																$montant_j = "<b style='color:green;'>".number_format($row_allj["montant"],3, '.',' ')."</b>";
																$total_plus_j += $row_allj["montant"] ;
															}	
															$total_j = $total_plus_j - $total_moin_j;
															
														}
													}
										// ==============================================================
											$query_alla = "select * from pm_transaction where date_paiement <= ".$date_paiementa_moin;
											$query_alla .= " AND journal = '".$get_journal."' ";
											$query_alla .= " AND (status_paiement = 1 OR  mode_reglement = 1) ";
											// if($get_journal <> 1){	
												// $query_alla .= " AND status_paiement = 1";
											// }
											$result_alla = @$db->query($query_alla);
											if($result_alla !== false){
												foreach($result_alla as $row_alla){
													if($row_alla["type"] == "out"){
														$total_moin_alla += $row_alla["montant"] ;
													}else{
														$total_plus_alla += $row_alla["montant"] ;
													}
													
													
												}												
											}	
											$total_alla = $total_plus_alla - $total_moin_alla ;	
																					
										// ==============================================================	
											// echo 	">>>===".$total_alla."<br>";	
										// ==============================================================						
										?>
										
                                    </tbody>
                                </table>
								<br><br><br>
								<table class="table table-bordered table-hover table-striped" style="width:30%;">
									<tr>
										<td><b>Solde du dernier jour</b></td>
										<td align="right"><b><? echo number_format(($total_all+$total)-($total_plus-$total_moin),3, '.',' '); ?></b></td>
									</tr>
									<tr>
										<td><b>Total IN</b></td>
										<td align="right"  style='color:green;'><b><? echo number_format($total_plus,3, '.',' '); ?></b></td>
									</tr>
									<tr>
										<td><b>Total OUT</b></td>
										<td align="right"  style='color:red;'><b><? echo number_format($total_moin,3, '.',' '); ?></b></td>
									</tr>
									<tr>
										<td><b>Solde le <? echo $_GET['date']; ?></b></td>
										<td align="right"><b><? echo number_format($total_all+$total,3, '.',' '); ?></b></td>
									</tr>
								</table>
							<?
							
									}
								}
								
							?>	
								
								
                            </div>
									

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