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

Version 0.9.7

Copyright (C) 2006-2007  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 contrats clients */
/* et fichiers rattachés                                  */
/**********************************************************/

// Vérification du nombre de contrats gérés par une agence
function nb_cocl($agence)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	if ($agence <> "")
		$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codeage` = '".$agence."'";
	else
		$rexist = "SELECT * FROM `" .TBCOCL. "`";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre de contrats pour un client
function nb_cocl_in_cli($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_codecli` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification du nombre de contrats clients dans l'historique contrats
function nb_cocl_in_coclh($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCLH. "` WHERE `coclh_codecocl` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	$nbexist = mysql_num_rows($rs2);
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $nbexist;
}

// Vérification qu'un contrat contrat-client est signé
function cocl_signed($code)
{
	$signe = FALSE;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract ($row2);
		
		if ($cocl_signe <> "")
			$signe = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $signe;
}

// Vérification qu'un contrat est de type "quota d'heure"
function cocl_quota($code)
{
	$quota = FALSE;

	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rexist = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_code` = '$code'";
	$rs2 = mysql_query($rexist) or die(mysql_error());
	if ($row2 = mysql_fetch_array($rs2))
	{
		extract ($row2);
		
		if ($cocl_typh == 1)
			$quota = TRUE;
	}
	mysql_free_result($rs2);
	mysql_close($conn2);

	return $quota;
}

// Crée une chaîne qui contient la liste des sites rattachés à un contrat
function cree_str_lstsites_cocl($co,$open)
{
	// Construction de la chaîne des sites sélectionnés
	$lstsites = "";
	
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	$rech = "SELECT scli_id FROM ".TBSCLI.",".TBSCLIC." WHERE ".TBSCLIC.".sclic_codecocl = '".$co."' AND ".TBSCLI.".scli_id = ".TBSCLIC.".sclic_idscli";
	$rs = mysql_query($rech);
	while ($row = mysql_fetch_assoc($rs)) 
	{
		extract($row);
		$lstsites .= $scli_id."-";
	}
	mysql_free_result($rs);
	if ($open==TRUE)
		mysql_close($conn);
	
	if ($lstsites!="")
		$lstsites = substr($lstsites,0,strlen($lstsites)-1);
	
	return $lstsites;
}

// Crée le texte des matériels (contrats) sur la base de la string des matériel/licences choisis
function cree_txt_matos_cocl($lstrlp)
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtmatos="";
	$tab_lstrlp=explode("-",$lstrlp);
	
	if (count($tab_lstrlp>0))
	{
		// Injection des matériels
		$j=1;
		$titregen=TRUE;
		$titre="=== DETAIL DU MATERIEL CONCERNE ===%%%%";
		for($i=0; $i<count($tab_lstrlp); $i++)
		{
			$rech = "SELECT * FROM `" .TBRPDT. "` WHERE `rpdt_code`= '".$tab_lstrlp[$i]."' ORDER BY `rpdt_idsite`";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);
					
					if ($titregen==TRUE)
					{
						$txtmatos_titre =$titre."---- MATERIEL N°".sprintf("%'010s",$j)." ----";
						$titregen=FALSE;
					}
					else
						$txtmatos_titre ="---- MATERIEL N°".sprintf("%'010s",$j)." ----";
					$nom_site="";
					switch ($rpdt_idsite)
					{
						case -1 :
							$nom_site="Tous les sites";
							break;
						case 0 :
							$nom_site="Site principal";
							break;
						default :
							$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$rpdt_idsite."'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if ($row2 = mysql_fetch_array($rs2))
							{
								extract($row2);
								$nom_site.= $scli_ste;
								$nom_site.= " (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
							}
							mysql_free_result($rs2);
					}
					$rech2 = "SELECT * FROM ".TBRPCAT." WHERE `rpcat_code` = '".$rpdt_codecat."'";
					$rs2 = mysql_query($rech2);
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rpdt_codema."'";
					$rs2 = mysql_query($rech2);
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$saut = chr(13).chr(10);
					$txtmatos_corps1 = "Site : ".$nom_site."%%Relevé ".$rpdt_code." (".$rpdt_codep.") : ".$rpcat_desc." / ".$mque_ste." ".$rpdt_modele.", NS N°".$rpdt_ns.", situé ".$rpdt_empl.".";
					$txtmatos_corps1 = ereg_replace($saut,"%%",htmlspecialchars_decode($txtmatos_corps1));
					mysql_free_result($rs2);

					$txtmatos_pied = "---- FIN DETAIL MATERIEL N°".sprintf("%'010s",$j)." ----";
					$txtmatos.=$txtmatos_titre."%%".$txtmatos_corps1."%%".$txtmatos_pied."%%%%";
				}
				$j++;
			}
			mysql_free_result($rs);
		}
		// Injection des licences
		$j=1;
		$titregen=TRUE;
		$titre="%%=== DETAIL DES LICENCES CONCERNEES ===%%%%";
		for($i=0; $i<count($tab_lstrlp); $i++)
		{
			$rech = "SELECT * FROM `" .TBRLIC. "` WHERE `rlic_code`= '".$tab_lstrlp[$i]."'  ORDER BY `rlic_idsite`";
			$rs = mysql_query($rech);
			if (mysql_num_rows($rs) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row = mysql_fetch_array($rs))
				{
					extract ($row);

					if ($titregen==TRUE)
					{
						$txtmatos_titre =$titre."---- LICENCE N°".sprintf("%'010s",$j)." ----";
						$titregen=FALSE;
					}
					else
						$txtmatos_titre ="---- LICENCE N°".sprintf("%'010s",$j)." ----";
					$nom_site="";
					switch ($rlic_idsite)
					{
						case -1 :
							$nom_site="Tous les sites";
							break;
						case 0 :
							$nom_site="Site principal";
							break;
						default :
							$rech2 = "SELECT * FROM `" .TBSCLI. "` WHERE `scli_id` = '".$rpdt_idsite."'";
							$rs2 = mysql_query($rech2)or die(mysql_error());
							if ($row2 = mysql_fetch_array($rs2))
							{
								extract($row2);
								$nom_site.= $scli_ste;
								$nom_site.= " (".Retournecp_openId($scli_ville,FALSE)." ".Retourneville_openId($scli_ville,FALSE).")";
							}
							mysql_free_result($rs2);
					}
					$rech2 = "SELECT * FROM ".TBMQUE." WHERE `mque_code` = '".$rlic_codema."'";
					$rs2 = mysql_query($rech2);
					if ($row2 = mysql_fetch_array($rs2))
					{
						extract ($row2);
					}
					$saut = chr(13).chr(10);
					$txtmatos_corps1 = "Site : ".$nom_site."%%Relevé ".$rlic_code." : ".$mque_ste." ".$rlic_modele." ".$rlic_version.".";
					$txtmatos_corps1 = ereg_replace($saut,"%%",htmlspecialchars_decode($txtmatos_corps1));
					mysql_free_result($rs2);

					$txtmatos_pied = "---- FIN DETAIL LICENCE N°".sprintf("%'010s",$j)." ----";
					$txtmatos.=$txtmatos_titre."%%".$txtmatos_corps1."%%".$txtmatos_pied."%%%%";
				}
				$j++;
			}
			mysql_free_result($rs);
		}
	}
	mysql_close($conn);
	
	if ($txtmatos!="")
		$txtmatos = substr($txtmatos,0,strlen($txtmatos)-4);
	return $txtmatos;
}

// Lecture d'un enregistrement contrat client
function lit_enr_cocl($code)
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM `" .TBCOCL. "` WHERE `cocl_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;
}

/** Préavis **/

// Maximum (jours) pour le préavis
function preavis_max()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM ".TBCONS;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $cons_relmax;
}

// Minimum (jours) pour le préavis
function preavis_min()
{
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rech2 = "SELECT * FROM ".TBCONS;
	$rs2 = mysql_query($rech2)or die(mysql_error());
	if (mysql_num_rows($rs2) == 1) // Uniquement UN SEUL enregistrement !
	{
		if ($row2 = mysql_fetch_array($rs2))
		{
			extract($row2);
		}
	}
	mysql_free_result($rs2);
	mysql_close($conn2);
	
	return $cons_relmin;
}

// Récupération du temps travaillé sur un contrat donné
// pour des interventions closes
function tps_trav_clot($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// Calcul des cumuls
	// Récupération du total travaillé
	$nbtotalsec = 0;

	$rech2 = "SELECT ".TBINTH.".inth_heurea, ".TBINTH.".inth_heured, ".TBINTH.".inth_offert ";
	$rech2.= "FROM ".TBINT.", ".TBINTH;
	$rech2.= " WHERE ".TBINT.".int_codecocl = '".$contrat."'";
	$rech2.= " AND ".TBINT.".int_code = ".TBINTH.".inth_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)) 
		{         
			if ($row2["inth_offert"] == 0)
				$nbtotalsec = $nbtotalsec + diff_time_sec($row2["inth_heurea"],$row2["inth_heured"]);
		}
	}
	
	mysql_free_result($rs2);
	if ($open==TRUE)
	{
		mysql_close($conn);
	}
	
	return $nbtotalsec;
}

// Récupération du temps travaillé sur un contrat donné
// pour des interventions closes
function tps_trav_non_clot($contrat,$open)
{
	if ($open==TRUE)
	{
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
	}
	// Calcul des cumuls
	// Récupération du total travaillé
	$nbtotalsec = 0;

	$rech2 = "SELECT ".TBINTH.".inth_heurea, ".TBINTH.".inth_heured, ".TBINTH.".inth_offert ";
	$rech2.= "FROM ".TBINT.", ".TBINTH;
	$rech2.= " WHERE ".TBINT.".int_codecocl = '".$contrat."'";
	$rech2.= " AND ".TBINT.".int_code = ".TBINTH.".inth_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)) 
		{         
			if ($row2["inth_offert"] == 0)
				$nbtotalsec = $nbtotalsec + diff_time_sec($row2["inth_heurea"],$row2["inth_heured"]);
		}
	}
	
	mysql_free_result($rs2);
	if ($open==TRUE)
	{
		mysql_close($conn);
	}
	
	return $nbtotalsec;
}

?>