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

/**************************************************/
/* Fonctions pour les accès aux fichiers produits */
/* et fichiers rattachés                          */
/**************************************************/

// Nombre d'articles
function nb_pdt()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDT;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Lecture d'un enregistrement produit
function lit_enr_pdt($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDT. "` WHERE `pdt_code` = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérification de la présence d'un article dans une fiche intervention
function exist_pdt_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTI. "` WHERE `pdti_codepdt` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist = TRUE;
	else
		$exist = FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $exist;
}

//  Lit un article dans une fiche intervention
function lit_enr_pdt_in_int($codep,$codei)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBPDTI. "` WHERE `pdti_codepdt` = '$codep' AND `pdti_codeint` = '$codei'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $row2;
}

// Vérification de la présence d'un produit dans une fiche intervention non close
function exist_pdt_in_int_ouv($code)
{

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

	$rech2 = "SELECT ".TBPDTI.".pdti_codepdt, ".TBPDTI.".pdti_codeint, ".TBINT.".int_code, ";
	$rech2.= TBINT.".int_datefin FROM ".TBPDTI.", ".TBINT." WHERE (".TBPDTI.".pdti_codepdt = '$code' AND ".TBPDTI.".pdti_codeint = ".TBINT.".int_code AND ".TBINT.".int_datefin = '0000-00-00')";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) <> 0)
		$exist=TRUE;
	else
		$exist=FALSE;
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $exist;
}

// Création du code Javascript pour affichage des taxes du produit
function cre_js_tpfa($td,$code,$from)
{
	global
		$lang,
		$txt_ent_lst_clic;
		
	echo "<script language=\"javascript\">\n";
	
	echo "function tbtpfa() {\n";
	echo "var myid = ".$from."document.getElementById(\"dettpfa\");\n";
	echo "myid.innerHTML = '';\n";

	if (nb_tpf_in_pdt($code)>0)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);

		$str_html = "<table class=\"Ltable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">";     

		// Création entête
		$str_html.="<th>";
		$str_html.="Code";
		$str_html.="</th>";
		
		$str_html.="<th>";
		$str_html.="Désignation";
		$str_html.="</th>";

		$str_html.="<th>";
		$str_html.="Valeur TTC";
		$str_html.="</th>";

		$str_html.="<th>";
		$str_html.="Forfait ?";
		$str_html.="</th>";

		if ($td=="edit")
		{
			$str_html.="<th>";
			$str_html.=$txt_ent_lst_clic;
			$str_html.="</th>";
		}

		// Lignes du tableau
		$rech = "SELECT * FROM ".TBTPFA." WHERE tpfa_codepdt='".$code."' ORDER BY 'tpfa_id'";
		$rs = mysql_query($rech) or die(mysql_error());
			
		$i = 0;
		
		while ($row = mysql_fetch_assoc($rs))
		{
			extract($row);
			
			if($i%2 == 0)  
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter1'\" class=\"TRalter1\">";
			else
			   $str_html.="<tr onmouseover=\"this.className='TRsel'\" onmouseout=\"this.className='TRalter2'\" class=\"TRalter2\">";

			// Code Taxe
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$tpfa_codetpf;
			$str_html.="</td>";
	
			$rech2 = "SELECT tpf_desc,tpf_pour,tpf_typmt,tpf_asaisir FROM ".TBTPF." WHERE tpf_code='".$row["tpfa_codetpf"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			
			if ($row2 = mysql_fetch_assoc($rs2)) 
				extract($row2);
			
			// Désignation
			$str_html.="<td class=\"tdlst\" valign=\"top\">";
			$str_html.=$tpf_desc;
			$str_html.="</td>";
	
			// Valeur
			echo "mycurrent_cell = ".$from."document.createElement(\"td\");\n";
			$txt=format_num($tpfa_mt,2);
			if ($tpf_pour==TRUE)
				$txt.="%";
			else
				$txt.=" ".$lang['symbole_monnaie_txt'];
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"right\">";
			$str_html.=$txt;
			$str_html.="</td>";
	
			// Unique ?
			if ($tpf_typmt==FALSE)
				$txt="Non";
			else
				$txt="Oui";
			$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
			$str_html.=$txt;
			$str_html.="</td>";
	
			if ($td == "edit")
			{
				$str_html.="<td class=\"tdlst\" valign=\"top\" align=\"center\">";
				if ($tpf_asaisir==TRUE)
				{
					$urledittpf = "tpfa_maj.php?td=edit&cp=".$tpfa_codepdt."&tpf=".$tpfa_codetpf;
					$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urledittpf."',900,500,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
					$str_html.="<img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\" title=\"Modifier la ligne taxe\"></a>";
					$str_html.=" ";
				}
				$urldeltpf = "tpfa_maj.php?td=del&cp=".$row["tpfa_codepdt"]."&tpf=".$row["tpfa_codetpf"];
				$str_html.="<a href=\"javascript:;\" onclick=\"fensaisie('".$urldeltpf."',900,500,'status=no,scrollbars=yes,menubar=no,statusbar=no,location=no,locationbar=no')\")>";
				$str_html.="<img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\" title=\"Supprimer la ligne taxe\"></a>";
				$str_html.=" ";
				$str_html.="</td>";
			}

			$str_html.="</tr>";
			$i++;
		}
		// Fermeture de la table
		$str_html.="</table>";
	
		// MAJ de l'id
		echo "var str_html = '".addslashes($str_html)."';\n";
		// echo "alert(str_html);\n";
		echo "myid.innerHTML=str_html;\n";
	}
	echo "}\n";
	
	echo "</script>\n";
}

/** Familles **/

// Vérification du nombre de familles produits
function nb_fam()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBFAM;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

/** Unités de vente **/

// Vérification du nombre d'unités de vente
function nb_pdtu()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBPDTU;
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	return $nbexist;
}

?>