<?php

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

include('ChiffresEnLettres.php'); 
?>

<?php
$id = $_GET["id"];

	if($db !== false){
		$query = "SELECT * FROM `pm_transaction` WHERE id = ".$id;
		$result = @$db->query($query);
        if($result !== false){
			$row = $result->fetch();  
			$date_echeance = date("d-m-Y", $row["date_echeance"]);
			$date_trans = date("d-m-Y", $row["add_date"]);
			$montant = $row["montant"]; 
			if($row["ref_facture_achat"]){
				$query_id = "select ref_bl from pm_facture_achat where id =".$row["ref_facture_achat"];
				$result_id = @$db->query($query_id);
				if($result_id !== false){
					$row_id = $result_id->fetch();
					$ref = $row_id["ref_bl"];
					
				}				
			}else{

				$query_fact = "select ref_facture_achat2 from pm_transaction_product where id_transaction =".$id;
				
				$result_fact= @$db->query($query_fact);
				if($result_fact !== false){
					foreach($result_fact as $row_fact){
							
							$query_fact = "select ref_bl from pm_facture_achat where id=".$row_fact["ref_facture_achat2"];
							// echo $query_fact;
							$result_mnt = @$db->query($query_fact);
							if($result_mnt !== false){
								$row_mnt = $result_mnt->fetch();
								$ref .= $row_mnt["ref_bl"]." - ";
							}

						// $ref_fact = $row_fact["ref_bl"];
					}
					
				}
				
				$ref = substr($ref, 0, -2);
			}	
			
//===================== New DEV
// $new_avoir = "";
			$queryt = "SELECT * FROM `pm_transaction_product` WHERE id_transaction = ".$row["id"];
			$resultt = @$db->query($queryt);
			if($resultt !== false){
				foreach($resultt as $row_avoiro){
											$query_avoir = "SELECT * FROM `pm_facture_avoir` where ref_bl =".$row_avoiro["ref_facture_achat2"];
											$result_avoir = @$db->query($query_avoir);
											if($result_avoir !== false){
												foreach($result_avoir as $row_avoir){
													$total += $row_avoir["total_ttc"];	
													if($row["type"] == "in"){
														$total_in += $row_avoir["total_ttc"];
													}else{
														$total_out += $row_avoir["total_ttc"];	
													}
													// $date_avoir = $avoir_array[$row["ref_facture_achat"]] ; 		
													$date_avoir = date( "d-m-Y", $row_avoir["date_avoir"]) ; 		
													// echo "<tr style='background-color: #b4b7ff;'><td style='background-color: #b4b7ff;' align='center'><b>".$date_avoir."</b></td><td colspan='6' style='background-color: #b4b7ff;' align='center'><b>Facture d'avoir (Achat)</b> - ".$row_avoir["remarque"]."</td><td style='background-color: #b4b7ff;' colspan='2'>".$row_avoir["ref_avoir"]."</td><td style='background-color: #b4b7ff;' align='right'></td><td style='background-color: #b4b7ff;' align='right'>- ".number_format($row_avoir["total_ttc"],3, '.',' ')."</td></tr>";
													$ref .= " - " .$row_avoir["ref_avoir"] ;
												}
											}
											
										}
			}	
//===============================				
			
			
			
			// $ref = $row["ref_facture_achat"]; 
			$mnt = number_format($montant, 3, '.', '');
			$amount = explode(".", $mnt);
			$lettre=new ChiffreEnLettre(); 
			$montant_lettre = $lettre->Conversion($amount[0])." dinars et ". $lettre->Conversion($amount[1])." millimes";
			if($row["fournisseur"]){
				$query_id = "select * from pm_fournisseurs where id =".$row["fournisseur"];
				$result_id = @$db->query($query_id);
				if($result_id !== false){
					$row_id = $result_id->fetch();
					$name = $row_id["name"];
					
				}				
			}else{
				$query_id = "select * from pm_client where id =".$row["client"];
			}
			//=== Journal INFO  =========
			if($row["journal"]){
				$query_fact = "select * from pm_journal where id=".$row["journal"];
				$result_fact= @$db->query($query_fact);
				if($result_fact !== false){
					$row_journal = $result_fact->fetch();
					$adress_journal = $row_journal["adresse"];
					$rib = $row_journal["rib"];
				}
			}else{
					$adress_journal = "";
					$rib = "";
			}	
	} 
}	
?>
<script language="javascript">

 window.print();

</script>
<html><head></head><body style="margin:0px;padding:0px;">

<div class="glob">

<div class="mnt_chiffre">#<? echo $mnt; ?>#</div>
<div class="mnt_chiffre_2">#<? echo $mnt; ?>#</div>
<div class="mnt_lettre"><? echo $montant_lettre; ?></div>
<div class="order"><? echo $name; ?></div>
<div class="lieux_1">Tunis</div>
<div class="lieux">Tunis</div>
<div class="order_2"><? echo $name; ?></div>
<div class="date_1"><? echo $date_trans; ?></div>
<div class="date"><? echo $date_trans; ?></div>
<div class="date_cheque"><? echo $date_echeance; ?></div>
<div class="date_cheque_1"><? echo $date_echeance; ?></div>
<div class="rip"><? echo $rib; ?></div>
<div class="rip2"><? echo $rib; ?></div>
<div class="tnd">TND</div>
<div class="ste">STE Oriental Snacks<br>LOT 16A, Z.I. Mghira 3<br>B.P. 57 - 2082 Fouchana</div>
<div class="banque"><? echo $adress_journal; ?></div>
<div class="ref"><? echo $ref; ?></div>
</div> <!-- Row end-->


<script language="javascript">

//window.print();

</script>
<style>
.glob{
	background-image:url('https://billing.unibro.net/ui/theme/softhash/img/traite_2.png');
	width:662px;
	height:431;
    margin-left: 65px;
	margin-top:5px;
    position: absolute;
}	
.mnt_chiffre{
    font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 144px;
    /* border: 1px red solid; */
    top: 96px;
    left: 498px;
    text-align: center;
}
.mnt_chiffre_2{
    font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 144px;
    /* border: 1px red solid; */
    top: 155px;
    left: 498px;
    text-align: center;
}
.order{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 662px;
    /* border: 1px red solid; */
    top: 159px;
    text-align: center;	
}
.mnt_lettre{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 662px;
    /* border: 1px red solid; */
    top: 182px;
    text-align: center;	
}
.date{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
    top: 223px;
    left: 110px;
    text-align: center;
}
.date_1{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
    top: 63px;
    left: 318px;
    text-align: center;
}
.date_cheque_1{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
    top: 63px;
    left: 192px;
    text-align: center;
}
.rip{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 234px;
    /* border: 1px red solid; */
	top: 100px;
    left: 235px;
    text-align: center;
}
.rip2{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 234px;
    /* border: 1px red solid; */
	top: 265px;
    left: 10px;
    text-align: center;
}
.ref{
	font-size: 12px;
    font-weight: bold;
    position: absolute;
    width: 175px;
    /* border: 1px red solid; */
	top: 365px;
    left: 116px;
    text-align: center;
}
.order_2{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 234px;
    /* border: 1px red solid; */
	top: 222px;
    left: 300px;
    text-align: center;
}
.date_cheque{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
    top: 223px;
    left: 213px;
    text-align: center;
}
.tnd{
    font-size: 13px;
    font-weight: bold;
    position: absolute;
    width: 50px;
    /* border: 1px red solid; */
    top: 245px;
    left: 337px;
    text-align: center;
}.ste{
    font-size: 13px;
    font-weight: bold;
    position: absolute;
    width: 148px;
    /* border: 1px red solid; */
    top: 294px;
    left: 286px;
    text-align: center
}
.banque{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 215px;
    /* border: 1px red solid; */
	top: 275px;
    left: 437px;
    text-align: center;
}
.lieux{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
	top: 222px;
    left: 10px;
    text-align: center;
}
.lieux_1{
	font-size: 14px;
    font-weight: bold;
    position: absolute;
    width: 106px;
    /* border: 1px red solid; */
    top: 44px;
    left: 318px;
    text-align: center;
}

@page {
    margin: 0;
}
</style>

</body></html>