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

/***************************************************************/
/* Récupération contrats de déduction en fonction code contrat */
/***************************************************************/

session_start(); 
sleep(1);
if (array_key_exists("nco", $_REQUEST))
{
	$tomorrow = 0;//60*60*24; // en nb de secondes
	header("Cache-Control: max-age=$tomorrow");
	print_plain();
}
else
{
	print "Usage : $_SERVER[PHP_SELF]?nco=client_contrat_temps_int_site";
}

function print_plain()
{
	if (!isset($_SESSION["lng"])) $_SESSION["lng"]="fr";
	include_once("../lng/".$_SESSION["lng"].".inc.php"); // Paramètres de language

	header("Content-type: text/html; charset=windows-1252");
	$nco=$_REQUEST["nco"];
	$tab_nco=explode("_",$nco);
	$ncocl = get_cocl1($tab_nco[0],$tab_nco[1],$tab_nco[2],$tab_nco[3],$tab_nco[4]);

	$result = array();
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	foreach ($ncocl as $row)
	{
		if ((trim($row["cocl_codeco"])<>"") AND ($row["cocl_codeco"]<>"-2") AND ($row["cocl_codeco"]<>"-1") AND ($row["cocl_codeco"]<>"0"))
		{
			$rech2 = "SELECT * FROM " .TBCO. " WHERE co_code = '".$row["cocl_codeco"]."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if ($row2 = mysql_fetch_array($rs2))
			{
				if ($row["cocl_delai"] <> 0)
					$txt_cocl = $row["cocl_code"]." - ".$row2["co_nom"]." (".$row["cocl_delai"]."h";
				else
					$txt_cocl = $row["cocl_code"]." - ".$row2["co_nom"]." (Pas de délai";
				if ($row["cocl_datef"]<>"0000-00-00")
				{
					$split = split("-",$row["cocl_datef"]);
					$annee = $split[0];
					$mois = $split[1];
					$jour = $split[2];
					$txt_cocl.=", ".$txt_div_ech." "."$jour"."/"."$mois"."/"."$annee";
				}
				else
					$txt_cocl.=", ".$txt_div_ssech." ";

				if ($row["cocl_datesig"]<>"0000-00-00")
					$txt_cocl.=", Signé";
				else
					$txt_cocl.=", Non signé";
				$txt_cocl.=")";

				// On vérifie s'il y a du temps travaillé pour le contrat, sur cette intervention
				$tpstrav=0;
				$tpstrav=tps_trav_int($row["cocl_code"],$tab_nco[3],FALSE);
				
				// On calcule le solde réel du contrat, en ne tenant pas compte du temps, pour cette intervention,
				// déjà utilisé dans ce contrat
				$solde_reel=soldes_reel_contrat($row["cocl_code"],FALSE)+$tpstrav;
				$txt_cocl.=" - solde, hors cette intervention -> ".heure_to_hhmm(sec_heure($solde_reel,3,TRUE));

			}
			$result[] = $row["cocl_code"]."=".$txt_cocl;
		}
		else
			$result[] = $row["cocl_code"]."=".$row["cocl_desc"];
	}
	mysql_close($conn);
	print implode(";", $result);
}

// $solde_nec : temps, en secondes, nécessaires pour cette intervention ($nint)
function get_cocl1($ncli,$nco,$solde_nec,$nint,$idsite)
{
	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/fic_cocl.inc.php");
	include_once("../inc/fic_int.inc.php");
	include_once("../inc/func_time.inc.php");
	include_once("../inc/func_txt.inc.php");
	
	// Temps travaillé sur l'intervention, pour le contrat choisi
	$tpstrav=0;
	$tpstrav=tps_trav_int($nco,$nint,TRUE);
	
	// On se connecte a la base
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$result = array();

	// On recupere les infos sur le contrat en cours
	$rech = "SELECT * FROM ".TBCOCL." WHERE cocl_code='".$nco."'";
	$rs = mysql_query($rech) or die(mysql_error());
	
	// Solde du contrat choisi
	$solde_ce_contrat=0;
	if ($row = mysql_fetch_array($rs))
	{
		extract($row);
		if ($cocl_typh==1)
		{
			if ($cocl_nbsdep==1)
			{
				// Solde du contrat choisi = solde réel du contrat (hors intervention en cours)
				$solde_ce_contrat=soldes_reel_contrat($nco,FALSE)+$tpstrav;
				
				// si le solde de ce contrat (hors temps travaillé de cette intervention) est inférieur
				// au temps travaillé sur cette intervention, on essaye de trouver des contrats de report
				if ($solde_ce_contrat<$solde_nec)
				{
					$tab = array(
						"cocl_code" => "",
						"cocl_codeco" => "",
						"cocl_desc" => "-- Faites votre choix --");
					$result[] = $tab;
			
					$tab = array(
						"cocl_code" => "-2",
						"cocl_codeco" => "-2",
						"cocl_desc" => "Pas de déduction");
					$result[] = $tab;
				
					// On recupere les infos sur le contrat en cours
					$rnum="";
					$tab = array();
					if (trim($cocl_rnum)=="")
					{
						if ($cocl_renauto==TRUE)
						{
							$tab = array(
								"cocl_code" => "-1",
								"cocl_codeco" => "-1",
								"cocl_desc" => "Déduction sur renouvellement (automatique)");
						}
					}
					else
					{
						$tab = array(
							"cocl_code" => "-1",
							"cocl_codeco" => "-1",
							"cocl_desc" => "Déduction sur renouvellement (".$cocl_rnum.")");
						$rnum=$cocl_rnum;
					}
					if (count($tab)>0)
						$result[] = $tab;
						
					// On recupere les enregistrements des contrats 'quota horaire' du client avec un solde
					// suffisant (> au temps déjà passé sur cette intervention), et non terminés
					// Bien évidemment, on exclu le contrat choisi
					$rech2 = "SELECT * FROM ".TBCOCL." WHERE cocl_code<>'".$nco."' AND cocl_codecli='".$ncli."'";
					$rech2.= " AND cocl_typh=1 AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00')";
					$rech2.= " AND cocl_datefin='0000-00-00'";
					$rech2.= " ORDER BY cocl_code";
					$rs2 = mysql_query($rech2) or die(mysql_error());
					// On crée le resultat
					while($row2 = mysql_fetch_array($rs2))
					{
						$addok=TRUE;
						if ($idiste>0)
						{
							$rech3 = "SELECT * FROM " .TBSCLIC. " WHERE sclic_codecocl = '".$cocl_code."' AND sclic_idscli='".$idsite."'";
							$rs3 = mysql_query($rech3) or die(mysql_error());
							if (mysql_num_rows($rs3)==0)
								$addok=FALSE;
						}
						else
						{
							if ($cocl_sitep==FALSE)
								$addok=FALSE;
						}

						if ($addok==TRUE)
						{
							if ($rnum<>$row2["cocl_code"])
							{
								// On vérifie s'il y a du temps travaillé pour le contrat, sur cette intervention
								$tpstrav=0;
								$tpstrav=tps_trav_int($row2["cocl_code"],$nint,FALSE);

								// On ne prend que les contrats ayant plus de temps dispo que le temps minimum nécessaire
								// par rapport au temps déjà passé sur l'intervention
								$solde_reel=soldes_reel_contrat($row2["cocl_code"],FALSE);
								$solde_reel=$solde_reel+$tpstrav;
								if ($solde_reel>=$solde_nec)
									$result[] = $row2;
							}
						}
					}
				}
				else
				{
					$tab = array(
						"cocl_code" => "0",
						"cocl_codeco" => "0",
						"cocl_desc" => "Solde ".$nco." suffisant");
					$result[] = $tab;
				}
			}
			else
			{
				$tab = array(
					"cocl_code" => "0",
					"cocl_codeco" => "0",
					"cocl_desc" => "Paramètre 'Report sur-consommé' à 'Non'");
				$result[] = $tab;
			}	
		}
		else
		{
			$tab = array(
				"cocl_code" => "0",
				"cocl_codeco" => "0",
				"cocl_desc" => "Ce contrat n'est pas de type 'Quota Horaire'");
			$result[] = $tab;
		}	
	}

	mysql_close();
	return $result;
}

?>