<?php

ini_set('display_errors', 1);


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
define("TITLE_ELEMENT", 'balance_compte');
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();$total_1 = "";$total_2 = "";
?>
<!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-balance-scale"></i> Balance des comptes "<? echo $_GET["annee"]; ?>" 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">				
				
				<tr><td>* Année exercice : </td><td><b><? 
				if($_GET["annee"] <> "" ) {
					echo $_GET["annee"];
				}else{
					echo "Tous";
				}
				?></b></td></tr>
				<tr><td>* Class : </td><td><b><? 
				if($_GET["class"] <> "" ) {
					echo class_compte($_GET["class"]);
				}else{
					echo "Tous";
				}
				?></b></td></tr>
				<tr><td>* Etat du règlement : </td><td><b><? 
				if($_GET["etat"] == "non_paye" ) {
					echo "Non payé";
				}elseif($_GET["etat"] == "paye" ) {
					echo "Payé";	
				}else{
					echo "Tous";
				}
				?></b></td></tr>
							
</table>				
				
				<div class="table-responsive">
				<br><br>
				    <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    
												
							<tr> 
							<th rowspan="2">Code compte</th>
							<th rowspan="2">Intitulé compte</th>
							<? if($_GET["etat"] == "non_paye" ) { 
								$col = 5 ;
								$th = 1 ;
							?>
								<th colspan="2">Mouvement Non payé</th>
								<th colspan="<? echo $th; ?>" rowspan="2">Soldes Non payé</th>
							<? }elseif($_GET["etat"] == "paye" ) {
								$col = 5 ;
								$th = 1 ;
							?>
								<th colspan="2">Mouvement payé</th>
								<th colspan="<? echo $th; ?>" rowspan="2">Soldes payé</th>
							<? }else{
								$col = 7 ;
								$th = 1 ;
							?>
								<th colspan="2">Total encours Non payé</th>
								<th colspan="2">Total mouvement payé</th>
								<th colspan="<? echo $th; ?>" rowspan="2">Solde<br>Payé & Non payé</th>
							<? } ?>
							
							</tr> 
							
							<tr> 
							<? if($_GET["etat"] == "" ) { ?>
								<th>IN</th>
								<th>OUT</th>
								<th>IN</th>
								<th>OUT</th>
							<? }else{ ?>
								<th>IN</th>
								<th>OUT</th>
							<? } ?>
					
							</tr>
							
							<?
								$date_from = strtotime($_GET["date_from"]) ;
								$date_to = strtotime($_GET["date_to"]) ;
								
							if($_GET['etat'] == "paye"){
								$etat = " AND status_paiement = 1 ";
							}elseif($_GET['etat'] == "non_paye"){
								$etat = " AND ISNULL(status_paiement) ";
							}else{
								$etat = "";
							}	
							
							// $query = "SELECT * FROM `pm_transaction` WHERE compte =". $_GET['class'];
							$queryo = "SELECT * FROM `pm_classe_compte` ";
							if($_GET['class'] <> ""){
								$queryo .= " WHERE id =". $_GET['class'];
							}
							$cmpta = 0;
							$cmpt = 0;
							$ref = "";
							$total_1 = 0;
										$total_2 = 0;
										$total_3 = 0;
										$total_4 = 0;
										$total_5 = 0;
										
										$total_1_all = 0;
										$total_2_all = 0;
										$total_3_all = 0;
										$total_4_all = 0;
										$total_5_all = 0;
							// echo $query;
							$resulto = @$db->query($queryo);
							if($resulto !== false){
								foreach($resulto as $rowo){
							
							$query = "SELECT * FROM `pm_compte_financier` WHERE classe =". $rowo['id'];
							// echo $query."<br>";
							$result = @$db->query($query);
							 $nb_rows_default = $db->last_row_count();
							 $comptab  = 0 ;
							if($result !== false){
								foreach($result as $row){
									$comptab++;
									// echo class_compte($row["classe"])."<br>";
									if($ref <> $row["classe"]){
										$cmpt = 0;
									}
									$ref = $row["classe"];
									if($cmpt == 0){
										if($cmpta > 0){
											
										}
										if($row["classe"] == 8){
											echo "<tr><td align='center' colspan='2' style='background-color: #feffc3;'><b>TOTAL des classes 2 à 7</b></td>";
											if($_GET["etat"] == "paye" ) { 
												echo "<td align='right' style='background-color: #feffc3;'><b>".number_format($total_1_all,3, '.','')."</b></td>";
												echo "<td align='right' style='background-color: #feffc3;'><b>-".number_format($total_2_all,3, '.','')."</b></td>";
											}elseif($_GET["etat"] == "non_paye" ) {
												echo "<td align='right' style='background-color: #feffc3;'><b>".number_format($total_3_all,3, '.','')."</b></td>";
												echo "<td align='right' style='background-color: #feffc3;'><b>-".number_format($total_4_all,3, '.','')."</b></td>";
											}else{
											
												echo "<td align='right' style='background-color: #feffc3;'><b>".number_format($total_3_all,3, '.','')."</b></td>";
												echo "<td align='right' style='background-color: #feffc3;'><b>-".number_format($total_4_all,3, '.','')."</b></td>";
												echo "<td align='right' style='background-color: #feffc3;'><b>".number_format($total_1_all,3, '.','')."</b></td>";
												echo "<td align='right' style='background-color: #feffc3;'><b>-".number_format($total_2_all,3, '.','')."</b></td>";
											}	
											echo "<td align='right' style='background-color: #feffc3;'><b>".number_format($total_5_all,3, '.','')."</b></td>";
											echo "</tr>";
											
											$total_1_all = 0;
											$total_2_all = 0;
											$total_3_all = 0;
											$total_4_all = 0;
											$total_5_all = 0;
										}
										echo "<tr><td colspan='".$col."' align='center' style='background-color: #ffc000;color:#000'><b>".$row["classe"]." - ".class_compte($row["classe"])."</b></td></tr>";
										// echo "<tr><td colspan='7' align='center' style='background-color: #ffc000;color:#000'><b>".$ref." - ".$row["id"]."</b></td></tr>";
										$cmpt++;
										$cmpta++;
										$total_1 = 0;
										$total_2 = 0;
										$total_3 = 0;
										$total_4 = 0;
										$total_5 = 0;
									}
									
									
									
									$in_paye = 0;
									$out_paye = 0;
									$in_non_paye = 0;
									$out_non_paye = 0;
									$solde = 0;
									
									//============== in_paye
									$querys = "SELECT SUM(montant) as in_paye FROM `pm_transaction` WHERE type= 'in' AND status_paiement = 1 AND compte =". $row['id'];
									if($_GET["etat"] == 'non_paye'){
										$querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;	
									}elseif($_GET["etat"] == 'paye'){
										$querys .= " AND date_paiement >=".$date_from." AND date_paiement <= ".$date_to;	
									}else{
										$querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;	
										// $querys .= " AND ((date_echeance >=".$date_from." AND date_echeance <= ".$date_to.") ";
										// $querys .= " OR (date_paiement >=".$date_from." AND date_paiement <= ".$date_to." ))";
									}
									//== annee exercies
									if($_GET["annee"] <> ''){
										$querys .=" AND annee = ".$_GET["annee"];
									}
									
									// echo $querys."<br>" ;
									$results = @$db->query($querys);
									if($results !== false){
										foreach($results as $rows1){
											$in_paye = number_format($rows1["in_paye"],3, '.','');
										}
									}	
									//====== out_paye ======
									$querys = "SELECT SUM(montant) as out_paye FROM `pm_transaction` WHERE type= 'out' AND status_paiement = 1 AND compte =". $row['id'];
									if($_GET["etat"] == 'non_paye'){
										$querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;	
									}elseif($_GET["etat"] == 'paye'){
										$querys .= " AND date_paiement >=".$date_from." AND date_paiement <= ".$date_to;	
									}else{
										$querys .= " AND date_paiement >=".$date_from." AND date_paiement <= ".$date_to;	
										// $querys .= " AND ((date_echeance >=".$date_from." AND date_echeance <= ".$date_to.") ";
										// $querys .= " OR (date_paiement >=".$date_from." AND date_paiement <= ".$date_to." ))";
									}
									//== annee exercies
									if($_GET["annee"] <> ''){
										$querys .=" AND annee = ".$_GET["annee"];
									}
									$results = @$db->query($querys);
									if($results !== false){
										foreach($results as $rows2){
											$out_paye = number_format($rows2["out_paye"],3, '.','');
										}
									}	
									//============== in_non_paye
									$querys = "SELECT SUM(montant) as in_non_paye FROM `pm_transaction` WHERE type= 'in' AND ISNULL(status_paiement) AND compte =". $row['id'];
									if($_GET["etat"] == 'non_paye'){
										$querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;	
									}elseif($_GET["etat"] == 'paye'){
										$querys .= " AND date_paiement >=".$date_from." AND date_paiement <= ".$date_to;	
									}else{
										// $querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;
										// $querys .= " AND ((date_echeance >=".$date_from." AND date_echeance <= ".$date_to.") ";
										// $querys .= " OR (date_paiement >=".$date_from." AND date_paiement <= ".$date_to." ))";
									}
									//== annee exercies
									if($_GET["annee"] <> ''){
										$querys .=" AND annee = ".$_GET["annee"];
									}
									$results = @$db->query($querys);
									if($results !== false){
										foreach($results as $rows3){
											$in_non_paye = number_format($rows3["in_non_paye"],3, '.','');
										}
									}	
									// echo $querys."<br>"; 
									//====== out_non_paye ======
									$querys = "SELECT SUM(montant) as out_non_paye FROM `pm_transaction` WHERE type= 'out' AND ISNULL(status_paiement) AND compte =". $row['id'];
									if($_GET["etat"] == 'non_paye'){
										$querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;	
									}elseif($_GET["etat"] == 'paye'){
										$querys .= " AND date_paiement >=".$date_from." AND date_paiement <= ".$date_to;	
									}else{
										// $querys .= " AND date_echeance >=".$date_from." AND date_echeance <= ".$date_to;
										// $querys .= " AND ((date_echeance >=".$date_from." AND date_echeance <= ".$date_to.") ";
										// $querys .= " OR (date_paiement >=".$date_from." AND date_paiement <= ".$date_to." ))";
									}
									//== annee exercies
									if($_GET["annee"] <> ''){
										$querys .=" AND annee = ".$_GET["annee"];
									}
									$results = @$db->query($querys);
									if($results !== false){
										foreach($results as $rows4){
											$out_non_paye = number_format($rows4["out_non_paye"],3, '.','');
										}
									}
									
									
									if($_GET["etat"] == "paye" ) { 
									
										$solde =  $rows1["in_paye"]-$rows2["out_paye"];

									}elseif($_GET["etat"] == 'non_paye'){
										
										$solde =  $rows3["in_non_paye"]-$rows4["out_non_paye"];

									}else{
										$solde = 	($rows1["in_paye"]+$rows3["in_non_paye"])-($rows2["out_paye"]+$rows4["out_non_paye"]);	
									}		
									
									
									
									$total_1 += $rows1["in_paye"];
									$total_2 += $rows2["out_paye"];
									$total_3 += $rows3["in_non_paye"];
									$total_4 += $rows4["out_non_paye"];
									$total_5 += $solde;
									
									if($row["classe"] > 1){ // from 2
									
										$total_1_all += $rows1["in_paye"];
										$total_2_all += $rows2["out_paye"];
										$total_3_all += $rows3["in_non_paye"];
										$total_4_all += $rows4["out_non_paye"];
										$total_5_all += $solde;
									}
									
								?>
							<tr>
								<td align='center'><? echo $row["code"]; ?></td>
								<td align='center'><? echo $row["name"]; ?></td>
								
								<? if($_GET["etat"] == "non_paye" ) { ?>
									<td align='right'><? echo $in_non_paye; ?></td>
									<td align='right'>-<? echo $out_non_paye; ?></td>
								<? }elseif($_GET["etat"] == "paye" ) { ?>
									<td align='right'><? echo $in_paye; ?></td>
								<td align='right'>-<? echo $out_paye; ?></td>
								<? }else{ ?>
									<td align='right'><? echo $in_non_paye; ?></td>
									<td align='right'>-<? echo $out_non_paye; ?></td>
										<td align='right'><? echo $in_paye; ?></td>
								<td align='right'>-<? echo $out_paye; ?></td>
									
								<? } ?>
								
					
								
								<td align='right'><? echo number_format($solde,3, '.',''); ?></td>
							</tr>
<?									
								if($comptab == $nb_rows_default){
									echo "<tr><td align='center' colspan='2'><b>TOTAL</b></td>";
											if($_GET["etat"] == "paye" ) { 
												echo "<td align='right'><b>".number_format($total_1,3, '.','')."</b></td>";
												echo "<td align='right'><b>-".number_format($total_2,3, '.','')."</b></td>";
											}elseif($_GET["etat"] == "non_paye" ) {
												echo "<td align='right'><b>".number_format($total_3,3, '.','')."</b></td>";
												echo "<td align='right'><b>-".number_format($total_4,3, '.','')."</b></td>";
											}else{
											
												echo "<td align='right'><b>".number_format($total_3,3, '.','')."</b></td>";
												echo "<td align='right'><b>-".number_format($total_4,3, '.','')."</b></td>";
												echo "<td align='right'><b>".number_format($total_1,3, '.','')."</b></td>";
												echo "<td align='right'><b>-".number_format($total_2,3, '.','')."</b></td>";
											}	
											echo "<td align='right'><b>".number_format($total_5,3, '.','')."</b></td>";
											echo "</tr>";
									$comptab  = 0 ;
								}
								
							}

							}		
							
							}
							}
							
							?>
							
						
									
								
								
																	
								
								   </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'] = ""; ?>
