<?php

ini_set('display_errors', 1); 


define("ADMIN", true);
require_once("../../../common/lib.php");
require_once("../../../common/define.php");
define("TITLE_ELEMENT", 'doc_retenue');
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> Liste des retenues à la source (Achat) <? 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>* Fournisseur : </td><td><b><? 
				if($_GET["fournisseur"] <> "" ) {
					echo fournisseur($_GET["fournisseur"]);
				}else{
					echo "Tous";
				}
				?></b></td></tr>
				<td>* Référence facture Achat : </td><td><b><? 
				if($_GET["ref_fac_achat"] <> "" ) {
					echo ref_facture($_GET["ref_fac_achat"]);
				}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>				
</table>				
				
				<div class="table-responsive">
				
				
				<h1 style="text-align: center;">Liste des retenues à la source (Achat)</h1><br><br>
                                <table class="table table-bordered table-hover table-striped" id="listing_base">
                                    
									<thead>
										<th><b>ID</b></th>
										<th><b>Date de retenue</b></th>
										<th><b>Ref Fac Achat</b></th>
										<th><b>Fournisseur</b></th>										
										<th><b>Montant Facture TTC</b></th>										
										<th><b>Taux de retenue</b></th>
										<th><b>Montant retenue</b></th>
                                    </thead>
                                    <tbody> 
									
									<?
									
									$montant_all = 0;
									
									$count = 0;
									$cnt_moi = 0;
									$cnt_mounth = 1;
									$total = 0;
									
									$montant_all_month = 0;
									$total_total_ht = 0;
									$total_total_ht_all = 0;
									$total_total_ht_all_month = 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_transaction where add_date >= ".$date_trans." AND add_date < ".$date_transa ;
									$query .= " AND type = 'out' AND retenue_montant > 0";
									
									if($_GET["montant_from"] > 0){
										$query .= " AND retenue_montant >= ".$_GET["montant_from"]."";
									}
									if($_GET["montant_to"] > 0){
										$query .= " AND retenue_montant <= ".$_GET["montant_to"]."";
									}
									
									if($_GET["fournisseur"] <> ""){
										$query .= " AND fournisseur = ".$_GET["fournisseur"];
									}
									
									if($_GET["ref_fac_achat"] <> ""){
										$query .= " AND ref_facture_achat = ".$_GET["ref_fac_achat"]."";
									}
									
																			
									$query .= " order by add_date 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 ++;

										$add_date = date('d-m-Y', $row["add_date"]);
										$count++;
										// $nbr_resultat = $row["nb"];
										$montant_all += $row["retenue_montant"];
										$montant_all_month += $row["retenue_montant"];
										// $total_total_ht = number_format($row["total_ht"]+$row["fodec"],3, '.','');
										// $total_total_ht_all += $total_total_ht;
										// $total_total_ht_all_month += $total_total_ht;
										
									// ================ GROUP BY MONTH ===============	
										$cnt_mounth++;
										$tva_total_tot = "";
										$date = $add_date;
										$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_transaction where add_date >= ".$date_int." AND add_date <= ".$date_out ;
										$nbr_mounth .= " AND type = 'out' AND retenue_montant > 0";
										if($_GET["montant_from"] > 0){
										$nbr_mounth .= " AND retenue_montant >= ".$_GET["montant_from"]."";
										}
										if($_GET["montant_to"] > 0){
											$nbr_mounth .= " AND retenue_montant <= ".$_GET["montant_to"]."";
										}
										
										if($_GET["fournisseur"] <> ""){
											$nbr_mounth .= " AND fournisseur = ".$_GET["fournisseur"];
										}
										
										if($_GET["ref_fac_achat"] <> ""){
											$nbr_mounth .= " AND ref_facture_achat = '".$_GET["ref_fac_achat"]."'";
										}
										
										$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='7' 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;
											$cnt_moi++;
											$cnt_mounth = 1;
										}

											

										
									?>
									<tr>	
										<td><a href="/admin/modules/transaction/index.php?view=form&id=<? echo $row["ref_facture_achat"]; ?>&no_edit=1" target="_blank"><? echo $row["id"]; ?></a></td>		
										<td><? echo $add_date; ?></td>		
										<td><? echo ref_facture($row["ref_facture_achat"]); ?></td>		
										<td><? echo fournisseur($row["fournisseur"]); ?></td>		
										<td align="right"><? echo number_format($row["montant"]+$row["retenue_montant"],3, '.',' '); ?></td>		
										<td align="right"><? echo $row["retenue"]; ?></td>		
										<td align="right"><? echo number_format($row["retenue_montant"],3, '.',' '); ?></td>		
									</tr>
								
									
									<?	
									//******************************************************************
																			
										// echo "<tr><td colspan='10' align='center'><b>".$cnt_mounth." / ".$nbr_return."</b></td></tr>";
										
									//******************************************************************
									//**********************************************************
									
									if($cnt_mounth == $nbr_return){
												//============
												
												// ===============
												echo "<tr><td colspan='6' align='right'><b>TOTAL</b></td>";
												echo "<td align='right'><b>".number_format($montant_all,3, '.',' ')."</b></td></tr>";
											
												// ========= Mettre a zero TVA & Total HT ============
												$montant_all = 0; 
												// $montant_all_month = 0; 
											
												// ===================
											
									}
									//**********************************************************									
									}
									
									
										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($montant_all_month,3, '.','')."</b></td></tr>";
										}
									}
									if($count == 0){
										echo "<tr><td colspan='10' align='center'>No data !</td></tr>";
									}else{
										
										echo '<tr><td colspan="7" align="center" style="background-color: #00a1af;"><b style="text-transform: capitalize;color:#FFF;"> TOTAL </b></td></tr>';
										echo "<tr><td colspan='6' align='right'><b>TOTAL</b></td>";
										echo "<td align='right'><b>".number_format($montant_all_month,3, '.',' ')."</b></td></tr>";
											
									}	
									?>

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