<?php

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

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

	if($db !== false){
		$query = "SELECT * FROM `pm_transaction` WHERE id = ".$id;
		$result = @$db->query($query);
        if($result !== false){
			$row = $result->fetch();  
			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();
					$tva = $row_id["tva"];
					$name = $row_id["name"];
					$adresse = $row_id["adresse"];
					$tel = $row_id["tel"];
				}
				// === GET Montant facture ====
				if($row["ref_facture_achat"] <> ""){
					$query_mnt = "select * from pm_facture_achat where id =".$row["ref_facture_achat"];
				}else{
					$query_mnt = "select SUM(montant_facture) as total_ttc  from pm_transaction_product where id_transaction =".$id;
				}		
	
				$result_mnt = @$db->query($query_mnt);
				if($result_mnt !== false){
					$row_mnt = $result_mnt->fetch();
					$mnt = $row_mnt["total_ttc"];;
				}	
				


				
			}else{
				$query_id = "select * from pm_client where id =".$row["client"];
			}
			
			$ref_fact_avoir = "";
			$ttc_fact_avoir = 0;
			//==============================================
			$queryt = "SELECT * FROM `pm_transaction_product` WHERE id_transaction = ".$id;
			$resultt = @$db->query($queryt);
			if($resultt !== false){
				foreach($resultt as $row_avoir){
					

					$query_fact = "select ref_avoir,total_ttc from pm_facture_avoir where ref_bl=".$row_avoir["ref_facture_achat2"];
					// echo $query_fact;
					$result_fact= @$db->query($query_fact);
					if($result_fact !== false){
						foreach($result_fact as $row_fact){
							$ref_fact_avoir .= $row_fact["ref_avoir"] ." - ";
							$ttc_fact_avoir += $row_fact["total_ttc"] ;
						}					
					} 	
					

				}
			}
			// echo " ===>  ".$ttc_fact_avoir ;
			$mnt = $mnt - $ttc_fact_avoir;
			//==============================================
			
				
				
				
				$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_fact .= $row_mnt["ref_bl"]." - ";
							}

						// $ref_fact = $row_fact["ref_bl"];
					}
					
				}
				
				$ref_fact = substr($ref_fact, 0, -2);
				
		}	
	} 
?>

<script>
	window.print();
</script>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>CERTIFICAT DE RETENUE D'IMPÔT SUR LE REVENU</title>
</head>

<body>
	
<strong>République Tunisienne
<br>Ministère du plan et des finances
</strong>
<br><br>
<table width="100%" border="1"  cellspacing="0" cellpadding="10" align="center">
	<tr>
		<td align="center"><strong>CERTIFICAT DE RETENUE D'IMPÔT SUR LE REVENU OU D’IMPOT SUR LES SOCIETES</strong></td>
	</tr>	
</table>	
<br>	
<table border="1" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td width="696" colspan="5" valign="top">
		<p><strong>A- PERSONNE OU ORGANISME PAYEUR :</strong></p>
		<p>
		<table width="100%">
			<tr>
				<td>Matricule Fiscal:</td>
				<td>1087691 XAM 000</td>
			</tr>
			<tr>
				<td>Dénomination de la raison social :</td>
				<td>STE ORIENTAL SNACKS</td>
			</tr>
			<tr>
				<td>Adresse :</td>
				<td>LOT 16A  Z.I. MGHIRA 3  FOUCHANA 2082
					<br>TEL : 71 409 030    FAX : 71 409 020</td>
			</tr>
		</table>
		</p>
	</td>
  </tr>
</table>	
<br>	
<table border="1" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td width="696" colspan="5" valign="top"><p><strong>B- RETENUES EFFECTUEES SUR    :</strong></p></td>
  </tr>
  <tr>
    <td width="307" valign="top"><p>&nbsp;</p></td>
    <td width="48" valign="top"><p>&nbsp;</p></td>
    <td width="113" valign="top" align="center"><p>&nbsp;</p>
      <p><strong>Montant    Brut</strong></p></td>
    <td width="113" valign="top" align="center"><p>&nbsp;</p>
      <p><strong>Retenue</strong></p></td>
    <td width="113" valign="top" align="center"><p>&nbsp;</p>
      <p><strong>Montant    Net</strong></p></td>
  </tr>
  <tr>
    <td width="307" valign="top"><p>- Honoraire, commissions, courtages vacation, <br>
      et loyers</p>
      <p>- Marchés</p>
      <p>- Redevances</p>
      <p>- Revenus des comptes    spéciaux d&rsquo;épargne    ouverts auprès des banques</p>
      <p>- Revenus des capitaux mobiliers</p>
      <p>- Revenus des bons de caisse au    porteur</p>
      <p align="center"><strong>To</strong><strong>ta</strong><strong>l Général :</strong></p></td>
    <td width="48" valign="top" align="center">
      <p>&nbsp;</p>
	  <p><? echo $row["retenue"]; ?> %</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p><p>&nbsp;</p>
      </td>
    <td width="113" valign="top" align="right"><p>&nbsp;</p>
     <p><? echo number_format($mnt,3); ?></p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p><b><? echo number_format($mnt,3); ?></b></p></td>
    <td width="113" valign="top" align="right"><p>&nbsp;</p>
      <p><? echo number_format($row["retenue_montant"],3); ?></p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p><b><? echo number_format($row["retenue_montant"],3); ?></b></p></td>
    <td width="113" valign="top" align="right"><p>&nbsp;</p>
      <p><? echo number_format($mnt-$row["retenue_montant"],3); ?></p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
      <p>&nbsp;</p>
       <p><b><? echo number_format($mnt-$row["retenue_montant"],3); ?></b></p></td>
  </tr>
</table>
<br>
<table border="1" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td width="696" colspan="5" valign="top">
		<p><strong>C- BENEFICIAIRE :</strong></p>
		<p>
		<table width="100%">
			<tr>
				<td>Identifiant / Matricule Fiscal  :</td>
				<td><? echo $tva; ?></td>
			</tr>
			<tr>
				<td>Nom, prénoms ou raison sociale :</td>
				<td><? echo $name; ?></td>
			</tr>
			<tr>
				<td>Adresse :</td>
				<td><? echo $adresse; ?></td>
			</tr>
			<tr>
				<td>Tel :</td>
				<td><? echo $tel; ?></td>
			</tr>
		</table>
		</p>
	</td>
  </tr>
</table>	
<br>
<table border="1" cellspacing="0" cellpadding="0" width="100%">
  <tr>
    <td width="696" colspan="5" >
		<p>
		<table width="100%">
			<tr width="100%">
				<td width="50%">Référence / Facture N° :</td>
				<td width="50%"><? echo $ref_fact; ?> / <? echo $ref_fact_avoir; ?></td>
			</tr>
		</table>
		</p>
	</td>
  </tr>
</table>
<br><br><br>
<table border="0" cellspacing="0" cellpadding="0" width="100%">
	<tr align="center">
		<td width="70%"></td>
		<td width="30%">Numéro Ref : Ret_00<? echo $_GET["id"]; ?><br><br><strong>A TUNIS,  Le <? echo date("d/m/Y", $row["add_date"]); ?></strong>
		<br><strong>Signature et Cachet</strong>
		</td>
	</tr>
</table>

</body>
</html>
