<?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
****************************************************************************************/

session_start(); 
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/func_log.inc.php");
if (CheckURLLog(FALSE)==FALSE) header("Location: ../index.php");
if (CheckNivLog("5-9")==FALSE) header("Location: ../index.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/fic_pdt.inc.php");
Archivelog(@$_SESSION["code_log"]);

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

$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$fam = str_majuscules(strip_tags(trim(@$_POST["fam"])));
$des = str_majuscules(strip_tags(str_escape(trim(@$_POST["des"]))));
$det = strip_tags(str_escape(trim(@$_POST["det"])));
$tva = strip_tags(trim(@$_POST["tva"]));
$pu = strip_tags(trim(@$_POST["pu"]));
$uv = strip_tags(trim(@$_POST["uv"]));
$vendu = strip_tags(trim(@$_POST["vendu"]));
if ($vendu == $txt_div_oui)
	$venteok = TRUE;
else
	$venteok = FALSE;

$retourliste = TRUE;
$codeuser = @$_SESSION["code_log"];

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

switch ($td)
{
	// Ajout de la fiche Produit
	case "add" :
		$rech = "SELECT * FROM " .TBPDT. " WHERE pdt_code = '$code'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBPDT;
			$rech2 .= " (pdt_datecrea,pdt_codecrea,pdt_datemod,pdt_codemod,pdt_code,pdt_fam,pdt_des,pdt_detail,pdt_puttc,pdt_vendu,pdt_codetva,pdt_codepdtu) VALUES";
			$rech2 .= " ('$datea','$codeuser','$datea','$codeuser','$code','$fam','$des','$det','$pu','$venteok','$tva','$uv')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			$msg = $txt_fiche_add;
		}
		else
		{
			$msg = $txt_fiche_exist;
			$retourliste = FALSE;
		}
		mysql_free_result($rs);
		mysql_close($conn);
		break;
		
	// MAJ de la fiche Produit
	case "edit" :
		mysql_close($conn);
		$existeint = FALSE;
		$majok=TRUE;

		// Vérifie si utilisé dans une fiche intervention non close
		if (exist_pdt_in_int_ouv($code) == TRUE)
		{
			$existeint = TRUE;
			if ($venteok == FALSE)
			{
				$majok=FALSE;
				$msg = $txt_pdt_used_intnc;
				$retourliste = FALSE;
			}
		}
		
		if ($majok == TRUE)
		{	
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			// MAJ de la fiche Produit
			$rech = "UPDATE `" .TBPDT. "` SET `pdt_datemod`='".$datea."', `pdt_codemod`='".$codeuser."', `pdt_code`='".$code."',";
			$rech.= " `pdt_fam`='".$fam."', `pdt_des`='".$des."', `pdt_detail`='".$det."', `pdt_puttc`='".$pu."',";
			$rech.= " `pdt_vendu`='".$venteok."', `pdt_codetva`='".$tva."', `pdt_codepdtu`='".$uv."'   WHERE `pdt_code` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());

			// Ce produit est présent dans une intervention ouverte
			// Il faut effectuer une mise à jour s'il y a des taxes rattachées
			if ($existeint == TRUE)
			{
				// On récupère la liste des interventions non terminées
				$rech = "SELECT int_code FROM " .TBINT. " WHERE int_datefin = '0000-00-00'";
				$rs = mysql_query($rech) or die(mysql_error());
				if (mysql_num_rows($rs) > 0)
				{
					while ($row = mysql_fetch_assoc($rs))
					{
						extract($row);
					
						// On récupère les infos du produit dans cette intervention
						$rech0 = "SELECT * FROM " .TBPDTI. " WHERE pdti_codeint = '$int_code' AND `pdti_codepdt` = '$code'";
						$rs0 = mysql_query($rech0) or die(mysql_error());
						if (mysql_num_rows($rs0) > 0)
						{
							while ($row0 = mysql_fetch_assoc($rs0))
							{
								extract($row0);
								
								$codepdt=$pdti_codepdt;
								$tvapdt=$pdti_codetva;
								$qtepdt=$pdti_qte;
								$pupdt=$pdti_puttc;

								// SUPPRESSION des taxes rattachées à ce produit dans cette intervention
								$rech1 = "DELETE FROM `" .TBPDTI. "` WHERE pdti_codeint = '$int_code' AND `pdti_frompdt` = '$codepdt'";
								$rs1 = mysql_query($rech1) or die(mysql_error());

								// On récupère la liste des taxes utilisée par ce produit
								$rech1 = "SELECT * FROM " .TBTPFA. " WHERE tpfa_codepdt = '$codepdt' ORDER BY tpfa_id";
								$rs1 = mysql_query($rech1) or die(mysql_error());
								if (mysql_num_rows($rs1) > 0)
								{
									while ($row1 = mysql_fetch_assoc($rs1)) 
									{
										extract($row1);

										$txtcompl="";
									
										// On récupère les infos pour cette taxe
										$rech2 = "SELECT * FROM " .TBTPF. " WHERE tpf_code = '$tpfa_codetpf'";
										$rs2 = mysql_query($rech2) or die(mysql_error());
										if (mysql_num_rows($rs2) > 0)
										{
											if ($row2 = mysql_fetch_assoc($rs2)) 
											{
												extract($row2);

												// 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=$tvapdt;
														break;
													// Autre : TVA définie
													default :
														$codetva_tpf=$tpf_codetva;
														break;
												}

												// Calcul du taux de TVA
												$txtva_tpf=0;
												$rech3 = "SELECT * FROM " .TBTVA. " WHERE tva_code = '$codetva_tpf'";
												$rs3 = mysql_query($rech3) or die(mysql_error());
												if (mysql_num_rows($rs3) > 0)
												{
													if ($row3=mysql_fetch_assoc($rs3))
													{
														extract($row3);
														$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=$qtepdt;
														$txtcompl.=" ".$txt_div_taxeu.".";
														break;
													// 1 : Fixe quel que soit le nombre de produits
													case "1" :
														$qte_tpf=1;
														$txtcompl.=" ".$txt_div_taxef.".";
														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=$pupdt*round($tpfa_mt/100,2);
														$txtcompl.=" ".format_num($tpfa_mt,2)."% sur prix unitaire";
														if ($codetva_tpf=="TVA0")
															$txtcompl.=" TTC";
														$txtcompl.=".";
														break;
												}
														
												// Réinjection de la taxe mise à jour pour ce produit dans cette intervention
												$rech4 = "INSERT INTO " .TBPDTI;
												$rech4 .= " (pdti_codeint,pdti_codepdt,pdti_frompdt,pdti_puttc,pdti_codetva,pdti_qte,pdti_ns,pdti_texte) VALUES";
												$rech4 .= " ('$int_code','$tpf_code','$codepdt','$puttc_tpf','$codetva_tpf','$qte_tpf','','$txtcompl')";
												$rs4 = mysql_query($rech4) or die(mysql_error());
											}
										}
									}
								}
							}
						}
					}
				}
			}

			mysql_close($conn);

			$msg = $txt_fiche_mod;
		}
		break;
		
	// Suppression de la fiche Produit
	case "del" :
		mysql_close($conn);
		$existint = FALSE;
	
		// Vérifie si utilisée dans une fiche intervention
		if (exist_pdt_in_int($code) == TRUE)
		{
			$existint = TRUE;
			$msg = $txt_pdt_used_int;
			$retourliste = FALSE;
		}
		
		if ($existint == FALSE)			
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
	
			// SUPPRESSION des taxes rattachées
			$rech = "DELETE FROM `" .TBTPFA. "` WHERE `tpfa_codepdt` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
	
			// SUPPRESSION de la fiche Produit
			$rech = "DELETE FROM `" .TBPDT. "` WHERE `pdt_code` = '$code'";
			$rs = mysql_query($rech) or die(mysql_error());
			mysql_close($conn);
	
			$msg = $txt_fiche_del;
		}
		break;
		
	default :
		break;
}
?>
<!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; ?> : Fiche Produit</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>
<?php 
include_once("../inc/entete.inc.php"); 
?>
<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>
<?PHP 
if ($retourliste == FALSE)
	aff_ret_idx_man($td,"pdt_lst.php?ta=m",$txt_lnk_ret_liste,TRUE);
else
	aff_ret_lst_auto("pdt_lst.php?ta=m",2000);
include_once("pied.php");
?>
