<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language
include_once("../inc/config.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_log.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));
$codeint = strip_tags(trim(@$_POST["codeint"]));

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$des = str_majuscules(str_escape(trim(@$_POST["des"])));
$qte = strip_tags(trim(@$_POST["qte"]));
$tva = strip_tags(trim(@$_POST["tva"]));
$pu = strip_tags(trim(@$_POST["pu"]));
$ns = str_escape(trim(@$_POST["ns"]));
$texte = str_escape(trim(@$_POST["texte"]));

$retourliste = TRUE;

$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);

switch ($td)
{
	// Ajout de la fiche Produit Intervention
	case "add" :
		$rech = "SELECT * FROM " .TBPDTI. " WHERE pdti_codeint = '$codeint' AND pdti_codepdt = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBPDTI;
			$rech2 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
			$rech2 .= " ('$codeint','$code',NULL,'$pu','$tva','$qte','$ns','$texte')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			
			// Ajout des taxes parafiscales rattachées
			$rech2 = "SELECT * FROM " .TBTPFA. " WHERE tpfa_codepdt = '$code' ORDER BY tpfa_id";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2) > 0)
			{
				while ($row2 = mysql_fetch_assoc($rs2)) 
				{
					extract($row2);

					$rech3 = "SELECT * FROM " .TBTPF. " WHERE tpf_code = '$tpfa_codetpf'";
					$rs3 = mysql_query($rech3) or die(mysql_error());
					if (mysql_num_rows($rs3) > 0)
					{
						if ($row3=mysql_fetch_assoc($rs3))
						{
							extract($row3);
							
							$txtcompl="";
	
							// Calcul du code TVA
							$codetva_tpf="";
							switch ($tpf_codetva)
							{
								// -2 : Pas de TVA
								case "-2" :
									$codetva_tpf="TVA0";
									break;
								// -1 : TVA du produit
								case "-1" :
									$codetva_tpf=$tva;
									break;
								// Autre : TVA définie
								default :
									$codetva_tpf=$tpf_codetva;
									break;
							}
							
							// Calcul du taux de TVA
							$txtva_tpf=0;
							$rech4 = "SELECT * FROM " .TBTVA. " WHERE tva_code = '$codetva_tpf'";
							$rs4 = mysql_query($rech4) or die(mysql_error());
							if (mysql_num_rows($rs4) > 0)
							{
								if ($row4=mysql_fetch_assoc($rs4))
								{
									extract($row4);
									$txtva_tpf=$tva_tx;
								}
							}

							// Calcul du nombre de taxes
							$qte_tpf=0;
							switch ($tpf_typmt)
							{
								// 0 : Proportionnel au nombre de produits
								case "0" :
									$qte_tpf=$qte;
									$txtcompl.=" Taxe unitaire.";
									break;
								// 1 : Fixe quel que soit le nombre de produits
								case "1" :
									$qte_tpf=1;
									$txtcompl.=" Taxe forfaitaire.";
									break;
							}

							// Calcul du montant TTC unitaire de la taxe
							$putttc_tpf=0;
							switch ($tpf_pour)
							{
								// 0 : Valeur
								case "0" :
									$puttc_tpf=$tpfa_mt;
									break;
								// 1 : Pourcentage
								case "1" :
									$puttc_tpf=$pu*round($tpfa_mt/100,2);
									$txtcompl.=" ".format_num($tpfa_mt,2)."% sur prix unitaire";
									if ($codetva_tpf=="TVA0")
										$txtcompl.=" TTC";
									$txtcompl.=".";
									break;
							}
							
							$rech4 = "INSERT INTO " .TBPDTI;
							$rech4 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
							$rech4 .= " ('$codeint','$tpf_code','$code','$puttc_tpf','$codetva_tpf','$qte_tpf','','$txtcompl')";
							$rs4 = mysql_query($rech4) or die(mysql_error());
						}
					}
				}
			}
			$msg = "Produit ajout&eacute;";
		}
		else
		{
			$msg = "Ce produit est d&eacute;j&agrave; saisi dans cette intervention !";
			$retourliste = FALSE;
		}
		mysql_free_result($rs);
		break;
		
	// MAJ de la fiche Produit Intervention
	case "edit" :
		$rech = "UPDATE `" .TBPDTI. "` SET `pdti_codeint`='".$codeint."', `pdti_codepdt`='".$code."', `pdti_puttc`='".$pu."', `pdti_qte`='".$qte."', `pdti_ns`='".$ns."', `pdti_texte`='".$texte."'";
		$rech .= " WHERE `pdti_codeint` = '".$codeint."' AND `pdti_codepdt` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
			
		// On supprime d'abord les taxes parafiscales rattachées à ce produit, dans cette intervention
		$rech = "DELETE FROM `" .TBPDTI. "` WHERE `pdti_codeint` = '$codeint' AND `pdti_frompdt` = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());

		// Ajout des taxes parafiscales rattachées
		$rech2 = "SELECT * FROM " .TBTPFA. " WHERE tpfa_codepdt = '$code' ORDER BY tpfa_id";
		$rs2 = mysql_query($rech2) or die(mysql_error());
		if (mysql_num_rows($rs2) > 0)
		{
			while ($row2 = mysql_fetch_assoc($rs2)) 
			{
				extract($row2);

				$rech3 = "SELECT * FROM " .TBTPF. " WHERE tpf_code = '$tpfa_codetpf'";
				$rs3 = mysql_query($rech3) or die(mysql_error());
				if (mysql_num_rows($rs3) > 0)
				{
					if ($row3=mysql_fetch_assoc($rs3))
					{
						extract($row3);
						
						$txtcompl="";

						// Calcul du code TVA
						$codetva_tpf="";
						switch ($tpf_codetva)
						{
							// -2 : Pas de TVA
							case "-2" :
								$codetva_tpf="TVA0";
								break;
							// -1 : TVA du produit
							case "-1" :
								$codetva_tpf=$tva;
								break;
							// Autre : TVA définie
							default :
								$codetva_tpf=$tpf_codetva;
								break;
						}
						
						// Calcul du taux de TVA
						$txtva_tpf=0;
						$rech4 = "SELECT * FROM " .TBTVA. " WHERE tva_code = '$codetva_tpf'";
						$rs4 = mysql_query($rech4) or die(mysql_error());
						if (mysql_num_rows($rs4) > 0)
						{
							if ($row4=mysql_fetch_assoc($rs4))
							{
								extract($row4);
								$txtva_tpf=$tva_tx;
							}
						}

						// Calcul du nombre de taxes
						$qte_tpf=0;
						switch ($tpf_typmt)
						{
							// 0 : Proportionnel au nombre de produits
							case "0" :
								$qte_tpf=$qte;
								$txtcompl.=" Taxe unitaire.";
								break;
							// 1 : Fixe quel que soit le nombre de produits
							case "1" :
								$qte_tpf=1;
								$txtcompl.=" Taxe forfaitaire.";
								break;
						}

						// Calcul du montant TTC unitaire de la taxe
						$putttc_tpf=0;
						switch ($tpf_pour)
						{
							// 0 : Valeur
							case "0" :
								$puttc_tpf=$tpfa_mt;
								break;
							// 1 : Pourcentage
							case "1" :
								$puttc_tpf=$pu*round($tpfa_mt/100,2);
								$txtcompl.=" ".format_num($tpfa_mt,2)."% sur prix unitaire";
								if ($codetva_tpf=="TVA0")
									$txtcompl.=" TTC";
								$txtcompl.=".";
								break;
						}
						
						$rech4 = "INSERT INTO " .TBPDTI;
						$rech4 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
						$rech4 .= " ('$codeint','$tpf_code','$code','$puttc_tpf','$codetva_tpf','$qte_tpf','','$txtcompl')";
						$rs4 = mysql_query($rech4) or die(mysql_error());
					}
				}
			}
		}

		$msg = "Produit mis &agrave; jour";
		break;

	// Suppression de la fiche Produit de l'intervention
	case "del" :
		$rech = "DELETE FROM `" .TBPDTI. "` WHERE `pdti_codeint` = '$codeint' AND `pdti_codepdt` = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());

		$rech = "DELETE FROM `" .TBPDTI. "` WHERE `pdti_codeint` = '$codeint' AND `pdti_frompdt` = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());

		$msg = "Produit supprim&eacute;";
		break;
		
	default :
		break;
}
mysql_close($conn);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Article Intervention</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

</head>
<body>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>


<!-- Fermeture temporisée -->
<?PHP 
$row=lit_enr_int($codeint);
if (is_array($row))
	extract($row);
cre_js_pdti("edit",$codeint,$int_acpte,"window.opener."); 
?>
<script type="text/javascript">
<!--
updatint();
self.close('fscomp');
//-->
</script>
<p align="center"><?PHP echo $txt_div_sinon; ?>, <a href="javascript:void(0)" onClick="javascript:; self.close('fscomp');"><?PHP echo $txt_lnk_clic_ici; ?></a> </p>

</body>
</html>
