<?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 TVA      */
/* et taxes parafiscales                          */
/**************************************************/

/** TVA **/

// Vérification du nombre de TVA
function nb_tva()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBTVA;
	$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 TVA
function lit_enr_tva($code,$open)
{
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech2 = "SELECT * FROM " .TBTVA. " WHERE tva_code = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)==1)
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
		}
	}
	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return $row2;
}


// Vérifie si une TVA est modifiable
// Non, si déjà utilisée  :
//     dans une intervention
//     dans une fiche produit intervention
//     dans un contrat signé
function tva_modifiable($code,$open)
{
	$used=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	// Dans une fiche produit intervention
	$rech2 = "SELECT * FROM " .TBPDTI. " WHERE pdti_codetva = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	// Dans une intervention
	$rech2 = "SELECT * FROM " .TBTPFI. " WHERE tpfi_codetva = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	// Dans un contrat signé
	$rech2 = "SELECT * FROM " .TBCOCL. " WHERE cocl_codetva = '$code' AND cocl_datesig <> '0000-00-00' ";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return !$used;
}


// Vérifie si une TVA est désactivable
// Non, si déjà utilisée  :
//     dans une fiche produit d'une intervention non close
//     dans un contrat non terminé
function tva_desactivable($code,$open)
{
	$used=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	// Dans une fiche produit d'une intervention non close
	$rech2 = "SELECT * FROM " .TBINT.", ".TBPDTI. " WHERE ".TBINT.".int_code = ".TBPDTI.".pdti_codeint AND ".TBINT.".int_datefin = '0000-00-00' AND ".TBPDTI.".pdti_codetva = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	// Dans une intervention non close
	$rech2 = "SELECT * FROM " .TBINT.", ".TBTPFI. " WHERE ".TBINT.".int_code = ".TBTPFI.".tpfi_codeint AND ".TBINT.".int_datefin = '0000-00-00' AND ".TBTPFI.".tpfi_codetva = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	// Dans un contrat non clot
	$rech2 = "SELECT * FROM " .TBCOCL. " WHERE cocl_codetva = '$code' AND cocl_datefin = '0000-00-00' ";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return !$used;
}


/** Taxes Parafiscales **/

// Vérification du nombre de taxes
function nb_tpf()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBTPF;
	$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 Taxe
function lit_enr_tpf($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM " .TBTPF. " WHERE tpf_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;
}

//  Donne le nombre de taxes dans une fiche article
function nb_tpf_in_pdt($codep)
{
	$nbtpf=0;
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTPFA. "` WHERE `tpfa_codepdt` = '$codep'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	$nbtpf = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $nbtpf;
}

//  Lit une taxe dans une fiche article
function lit_enr_tpf_in_pdt($codet,$codep)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTPFA. "` WHERE `tpfa_codetpf` = '$codet' AND `tpfa_codepdt` = '$codep'";
	$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'une taxe dans une fiche intervention
function exist_tpf_in_int($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBTPFI. "` WHERE `tpfi_codetpf` = '$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 une taxe dans une fiche intervention
function lit_enr_tpf_in_int($codet,$codei)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBTPFI. "` WHERE `tpfi_codetpf` = '$codet' AND `tpfi_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'une taxe dans une fiche intervention non close
function exist_tpf_in_int_ouv($code)
{

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

	$rech2 = "SELECT ".TBTPFI.".tpfi_codetpf, ".TBTPFI.".tpfi_codeint, ".TBINT.".int_code, ";
	$rech2.= TBINT.".int_datefin FROM ".TBTPFI.", ".TBINT." WHERE (".TBTPFI.".tpfi_codetpf = '$code' AND ".TBTPFI.".tpfi_codeint = ".TBINT.".int_code AND 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;
}

// Vérifie si une taxe est modifiable
// Non, si déjà utilisée  :
//     dans une fiche produit intervention
function tpf_modifiable($code,$open)
{
	$used=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	// Dans une fiche produit intervention
	$rech2 = "SELECT * FROM " .TBTPFA. " WHERE tpfa_codetpf = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	// Dans une intervention
	$rech2 = "SELECT * FROM " .TBTPFI. " WHERE tpfi_codetpf = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return !$used;
}


// Vérifie si une taxe est désactivable
// Non, si déjà utilisée  :
//     dans une fiche produit d'une intervention non close
//     dans une intervention non close
function tpf_desactivable($code,$open)
{
	$used=FALSE;
	if ($open==TRUE)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}

	// Dans une fiche produit d'une intervention non close
	$rech2 = "SELECT * FROM " .TBINT.", ".TBPDTI. " WHERE ".TBINT.".int_code = ".TBPDTI.".pdti_codeint";
	$rech2.= " AND ".TBINT.".int_datefin = '0000-00-00'";
	$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 " .TBTPFA. " WHERE tpfa_codetpf = '$code' AND tpfa_codepdt = '$pdti_codepdt'";
			$rs3 = mysql_query($rech3)or die(mysql_error());
			if (mysql_num_rows($rs2)>0)
			{
				$used=TRUE;
				break;
			}
		}
	}

	// Dans une intervention non close
	$rech2 = "SELECT * FROM " .TBINT.", ".TBTPFI. " WHERE ".TBINT.".int_code = ".TBTPFI.".tpfi_codeint";
	$rech2.= " AND ".TBINT.".int_datefin = '0000-00-00' AND ".TBTPFI.".tpfi_codetpf = '$code'";
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2)>0)
		$used=TRUE;

	mysql_free_result($rs2);
	if ($open==TRUE)
		mysql_close($conn2);
	
	return !$used;
}


?>