<?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");

include_once("../inc/fic_pdt.inc.php");
include_once("../inc/fic_taxes.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
Archivelog(@$_SESSION["code_log"]);

$td = strip_tags(trim($_GET['td']));
$coderech = strip_tags(trim($_GET['code']));

$pbm_pdt=FALSE;
$puht=0;

if ((@$_SESSION["niv_log"] == 4) OR (@$_SESSION["niv_log"] == 3))
	$td="view";

if ($coderech <> "")
{
	// Récupération des informations produit
	$row = lit_enr_pdt($coderech);
	if (is_array($row))
		extract ($row);
	
	// Récupération des informations TVA
	$row = lit_enr_tva($pdt_codetva,TRUE);
	if (is_array($row))
		extract ($row);

	$txtva=$tva_tx/100;
	$puht=round(($pdt_puttc/(1+$txtva))*100)/100;
}
else
{
	if ($td == "add")
		$pdt_vendu = TRUE;
	else
		$pbm_pdt=TRUE;
}

switch ($td)
{
	case "add" :
		$titre = "CREATION ARTICLE";
		break;
	case "edit" :
		$titre = "MISE A JOUR ARTICLE";
		break;
	case "view" :
		$titre = "DETAIL ARTICLE";
		break;
	case "del" :
		$titre = "SUPPRESSION ARTICLE";
		break;
	default :
		$pbm_pdt=TRUE;
		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 Article</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>

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
<!--
var tab1_champ=new Array(
	"nom:code ; format:alphanum ; req:Y ; txt:<?PHP echo html_entity_decode($txt_chp_code); ?> ; lmx:20 ; stok:frmok ; stbad:frmbad",
<?PHP if (($td == "add") OR ($td == "edit"))
{ 
?>
	"nom:fam ; format:liste ; req:Y ; txt:Famille ; vmn:1 ; stok:frmok ; stbad:frmbad",
<?PHP
}
else
{
?>
	"nom:fam ; format:txt ; req:Y ; txt:Famille ; stok:frmok ; stbad:frmbad",
<?PHP
}
?>
	"nom:des; format:txt ; req:Y ; txt:<?PHP echo html_entity_decode($txt_chp_lib); ?> ; lmx:50 ; stok:frmok ; stbad:frmbad",
	"nom:det; format:txt ; req:N ; txt:Détail ; lmx:70 ; stok:frmok ; stbad:frmbad",
	"nom:tva; format:liste ; req:Y ; txt:TVA ; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:puht; format:reel_ns ; req:Y ; txt:PU HT ; lmx:10 ; vmn:0.01 ; stok:frmok ; stbad:frmbad",
	"nom:pu; format:reel_ns ; req:Y ; txt:PU TTC ; lmx:10 ; vmn:0.01 ; stok:frmok ; stbad:frmbad",
	"nom:uv; format:liste ; req:Y ; txt:Unité de vente ; vmn:1 ; stok:frmok ; stbad:frmbad");
//-->
</script>

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

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

<!-- Scripts Javascript sur les champs -->
<script language="JavaScript" src="../jscript/verif_field.js"></script>

<?PHP cre_js_tpfa($td,$pdt_code,""); ?>

</head>

<?PHP 

switch ($td)
{
	case "add" : 
		echo '<body onLoad="metFocus();">';
		break;
	case "edit" :
		echo '<body onLoad="metFocus();tbtpfa();">';
		break;
	case "del" :
		echo '<body onload="warndel();metReadonly();">';
		break;
}
include_once("../inc/entete.inc.php");
if ($pbm_pdt==FALSE)
{
?>
<p align="center"> <span class="titrepage"><?PHP echo $titre ?></span></p>
<!-- Affiché uniquement s'il y a au moins famille -->
	<?PHP
	if ((nb_fam() > 0) AND (nb_pdtu() > 0))
	{
	?>
<table width="100%"  border="0">
  <tr>
    <td width="33%"><strong><em><a href="pdt_lst.php?ta=m"><?PHP echo $txt_lnk_ret_liste; ?></a></em></strong></td>
    <td width="33%"align="center" class="annot_petit_it">(<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"> : <?PHP echo $txt_div_saisoblg; ?>)</td>
    <td width="33%">&nbsp;</td>
  </tr>
</table>
<form name="pdt_maj" id="pdt_maj" method="post" action="pdt_enr.php" onSubmit="javascript:; return (VForm(this,tab1_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
  <input name="titre" type="hidden" id="titre" value="<?PHP echo $titre ?>">
  <input name="td" type="hidden" id="td" value="<?PHP echo $td ?>">

<table width="100%"  border="0">
    <tr>
      <td width="160" class="rubfrm" id="f_code" align="right"><?PHP echo $txt_chp_code; ?></td>
      <td>
      <?PHP if ($td == "add") { ?>
		  <input name="code" type="text" id="code" value="<?PHP echo $pdt_code ?>" size="20" maxlength="20" onChange="javascript:this.value=this.value.toUpperCase();">
          <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
      <?PHP } else { ?>
		  <input name="code" type="text" id="code" value="<?PHP echo $pdt_code ?>" size="20" maxlength="20" readonly="1">
      <?PHP } ?>
	  </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_fam" align="right">Famille</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
	  		<select name="fam" size="1" id="fam">
          <?PHP
				echo "<option>-- Choisissez une famille --</option>";
				
				// Création de la liste des familles
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rs = mysql_query("SELECT * FROM ".TBFAM." ORDER BY fam_desc");

				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					echo '<option value="'.$fam_code.'"';
					if ($pdt_fam == $fam_code)
						echo ' selected';
					echo '>'.$fam_desc.'</option>';
				}
				mysql_free_result($rs);
				mysql_close($conn);
				?>
        	</select>
      		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	  	<?PHP
		}
		else
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBFAM." WHERE `fam_code`= '".$pdt_fam."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract ($row);
				}
			}
			mysql_free_result($rs);
			mysql_close($conn);
		?>
			<input name="fam" type="text" id="fam" value="<?PHP echo $fam_desc ?>" size="50" maxlength="50" readonly="1">
		<?PHP
		}
		?>
		</td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_des" align="right"><?PHP echo $txt_chp_lib; ?></td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
		<input name="des" type="text" id="des" value="<?PHP echo htmlspecialchars($pdt_des) ?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();"> <img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	  <?PHP
	  }
	  else
	  {
	  ?>
		<input name="des" type="text" id="des" value="<?PHP echo htmlspecialchars($pdt_des) ?>" size="50" readonly="1"> 
	  <?PHP
	  }
	  ?>
	  </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_det" align="right">D&eacute;tail</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
		<input name="det" type="text" id="det" value="<?PHP echo htmlspecialchars($pdt_detail) ?>" size="70">
	  <?PHP
	  }
	  else
	  {
	  ?>
		<input name="det" type="text" id="det" value="<?PHP echo htmlspecialchars($pdt_detail) ?>" size="70" readonly="1"> 
	  <?PHP
	  }
	  ?>
	  </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_pu" align="right">PU HT</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
		<input name="puht" type="text" id="puht" style="text-align:right" value="<?PHP echo format_num($puht,2); ?>" size="10" onChange="formatedeci(this.id,2);ttc_from_ht(this.value,'tva','pu',2);"><?PHP echo $lang['symbole_monnaie']; ?>
		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"><br>
		<span class="annot_petit_it">(La d&eacute;cimale doit &ecirc;tre '.' et pas la virgule)</span>
	  <?PHP
	  }
	  else
	  {
	  ?>
		<input name="puht" type="text" id="puht" style="text-align:right" value="<?PHP echo format_num($puht,2); ?>" size="10" readonly="1"><?PHP echo $lang['symbole_monnaie']; ?> 
	  <?PHP
	  }
	  ?>
      </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_tva" align="right">TVA</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
		<select name="tva" size="1" id="tva" onChange="ttc_from_ht(document.getElementById('puht').value,this.id,'pu',2);">
	  <?PHP
		echo "<option value=\"\">-- Choisissez une TVA --</option>";
		
		// Création de la liste des TVA
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBTVA." WHERE tva_actif=1 ORDER BY tva_desc");

		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			echo '<option value="'.$tva_code.'"';
			if ($pdt_codetva == $tva_code)
				echo ' selected';
			echo '>'.$tva_desc.'</option>';
		}
		mysql_free_result($rs);
		mysql_close($conn);
		?>
		</select>
		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	  <?PHP
	  }
	  else
	  {
		$row=lit_enr_tva($pdt_codetva,TRUE);
		if (is_array($row))
			extract($row);
	  ?>
		<input name="tva" type="hidden" id="tva" value="<?PHP echo $pdt_codetva ?>"> 
		<input name="ntva" type="text" id="ntva" align="right" value="<?PHP echo $tva_desc ?>" size="40" readonly="1"> 
	  <?PHP
	  }
		// Création des taux de TVA (champs cachés)
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rs = mysql_query("SELECT * FROM ".TBTVA." WHERE tva_actif=1 ORDER BY tva_desc");
		
		while ($resultat = mysql_fetch_array($rs)) 
		{
			extract($resultat);
			echo '<input name="'.$tva_code.'" type="hidden" id="'.$tva_code.'" value="'.$tva_tx.'">';
		}
		mysql_free_result($rs);
		mysql_close($conn);
	  ?>
      </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_pu" align="right">PU TTC</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
		<input name="pu" type="text" id="pu" style="text-align:right" value="<?PHP echo  format_num($pdt_puttc,2); ?>" size="10"  onChange="formatedeci(this.id,2);ht_from_ttc(this.value,'tva','puht',2);"><?PHP echo $lang['symbole_monnaie']; ?>
		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>"><br>
		<span class="annot_petit_it">(La d&eacute;cimale doit &ecirc;tre '.' et pas la virgule)</span>
	  <?PHP
	  }
	  else
	  {
	  ?>
		<input name="pu" type="text" id="pu" style="text-align:right" value="<?PHP echo $pdt_puttc ?>" size="10" readonly="1"><?PHP echo $lang['symbole_monnaie']; ?>
	  <?PHP
	  }
	  ?>
      </td>
    </tr>
    <tr>
      <td width="160" class="rubfrm" id="f_uv" align="right">Unit&eacute; de vente</td>
      <td>
      <?PHP if (($td == "add") OR ($td == "edit"))
	  { 
	  ?>
	  		<select name="uv" size="1" id="uv">
          <?PHP
				echo "<option>-- Choisissez une unité de vente --</option>";
				
				// Création de la liste des unités
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rs = mysql_query("SELECT * FROM ".TBPDTU." ORDER BY pdtu_desc");

				while ($resultat = mysql_fetch_array($rs)) 
				{
					extract($resultat);
					echo '<option value="'.$pdtu_code.'"';
					if ($pdt_codepdtu == $pdtu_code)
						echo ' selected';
					 echo '>'.$pdtu_desc.' ('.$pdtu_abr.')</option>';
				}
				mysql_free_result($rs);
				mysql_close($conn);
				?>
        	</select>
      		<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	  	<?PHP
		}
		else
		{
			$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			$rech = "SELECT * FROM ".TBPDTU." WHERE `pdtu_code`= '".$pdt_codepdtu."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs) == 1) // Uniquement UN SEUL enregistrement !
			{
				if ($row = mysql_fetch_array($rs)) 
				{
					extract ($row);
				}
			}
			mysql_free_result($rs);
			mysql_close($conn);
		?>
			<input name="uv" type="text" id="uv" value="<?PHP echo $pdtu_desc ?>" size="50" maxlength="50" readonly="1">
		<?PHP
		}
		?>
		</td>
    </tr>
    <tr>
      <td class="rubfrm" id="f_vente" align="right">Commercialis&eacute;</td>
      <td>
	  <?PHP 
		if (($td == "add") OR ($td == "edit"))
		{ 
			echo "<select name=\"vendu\" size=\"1\" id=\"vendu\">";
			echo "<option value=\"Oui\"";
			if ($pdt_vendu == TRUE)
				echo" selected";
			echo">".$sel_lst_oui."</option>";
			echo "<option value=\"Non\"";
			if ($pdt_vendu == FALSE)
				echo" selected";
			echo ">".$sel_lst_non."</option>";
			echo "</select>";
		?>
			<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	  <?PHP 
	  	}
		
		if (($td == "del") OR ($td == "view"))
		{ 
		?>
		<input name="vendu" type="text" id="vendu" value="<?PHP echo $non_oui[$pdt_vendu]; ?>" size="3" maxlength="3" readonly="1">
		<?PHP
		}
		?>
	</td>
    </tr>
  </table>

<!-- Zone de saisie des taxes parafiscales -->
<?PHP
if (($td == "edit") OR ($td == "del") OR ($td == "view"))
{
	if ((@$_SESSION["niv_log"] <> 2) AND (@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6))
	{
	$urlchoixtpf = "tpf_lst.php?ta=p&td=".$td."&cp=".$coderech;
?>
	<table width="100%"  border="0">
	<tr>
		<td class="rubfrm" colspan="2" align="center">TAXES</td>
	</tr>
	</table>
	<div id="dettpfa">
	</div>

	<!-- Lien d'ajout de taxes-->
	<?PHP
	if ($td <> "del")
	{
		if (($td == "edit") AND (@$_SESSION["niv_log"] <> 2) AND (@$_SESSION["niv_log"] <> 7) AND (@$_SESSION["niv_log"] <> 6))
		{
	?>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $urlchoixtpf; ?>',900,500,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")><img src="../img/taxe.png" border="0" align="absmiddle"></a>
		<a href="javascript:;" onclick="fensaisie('<?PHP echo $urlchoixtpf; ?>',900,500,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no');")> Ajouter une taxe parafiscale</a>
		<br>
	<?PHP
		}
	}
	}
}
?>
  <p align="center">
    <?PHP if (($td == "add") OR ($td == "edit")) { ?>
    <input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo $but_reinit; ?>">
    <input name="submit" type="submit" class="bton_std" id="submit" value="<?PHP echo $but_enr; ?>">
    <?PHP } if ($td == "del") { ?>
    <input name="del" type="submit" class="bton_std" id="del" value="<?PHP echo $but_del; ?>">
    <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_cancel; ?>" onclick="javascript:; history.back();">
    <?PHP } if ($td == "view") { ?>
    <input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_retlst; ?>" onclick="javascript:; history.back();">
    <?PHP } ?>
  </p>
</form>
<!-- Fin Affiché uniquement s'il y a au moins une famille article -->
	<?PHP
	}
	else
	{
		$txt_err="";
		if (nb_fam() == 0)
			$txt_err="Merci de cr&eacute;er au moins une famille.";
		if (nb_pdtu() == 0)
			$txt_err.="<br>Merci de cr&eacute;er au moins une unit&eacute; de vente.";
	?>
		<p align="center" class="annot_std_it"><?PHP echo $txt_err; ?></p>
		<script type="text/javascript">
		<!--
		setTimeout("self.location.href=('index.php');",2000)
		//-->
		</script>
<?PHP 
	}
}
else
	aff_ret_pbm_auto("pdt_lst.php",2000);
include_once("pied.php");
?>
