<?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 report 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=un-code-contrat";
}

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");
	$ncocl = get_cocl3($_REQUEST["nco"]);
	$result = array();
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	foreach ($ncocl as $row)
	{
		if (($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))
			{
				$txt_cocl = $row["cocl_code"]." - ".$row2["co_nom"];
				$rech3 ="SELECT * FROM ".TBCLI." WHERE `cli_code` = '".$row["cocl_codecli"]."'";
				$rs3 = mysql_query($rech3) or die(mysql_error());
				if ($row3 = mysql_fetch_array($rs3))
				{
					if ($row3["cli_ste"] <> "")
						$txt_cocl.= " (".$row3["cli_ste"];
					else
						$txt_cocl.= " (".$row3["cli_nom"];
					if ($row["cocl_delai"] <> 0)
						$txt_cocl.= " - ".$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." ";
					$txt_cocl.=", solde réel ".$row["soldeh"];
					if ($row["cocl_datesig"]<>"0000-00-00")
						$txt_cocl.=", Signé";
					else
						$txt_cocl.=", Non signé";
					$txt_cocl.=")";
				}
			}
			$result[] = $row["cocl_code"]."=".$txt_cocl;
		}
		else
			$result[] = $row["cocl_code"]."=".$row["cocl_desc"];
	}
	mysql_close($conn);
	print implode(";", $result);
}

// Compare deux dates : vrai si $d1<=$d2
// Les dates sont au format anglais (aaaa-mm-jj)
function comp_date($d1,$d2)
{
	if (strtotime($d1) <= strtotime($d2))
		$result = TRUE;
	else
		$result = FALSE;
		
	return $result;
}

function get_cocl3($nco)
{
	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_txt.inc.php");
	include_once("../inc/func_time.inc.php");
	$result = array();
	// On recupere les infos sur le contrat en cours
	if (trim($nco)<>"")
	{
		// On se connecte a la base
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rnum="";
		$rech = "SELECT * FROM ".TBCOCL." WHERE cocl_code='".$nco."'";
		$rs = mysql_query($rech) or die(mysql_error());
		if ($row = mysql_fetch_array($rs))
		{
			extract($row);
			$ncli=$cocl_codecli;
			$rnum=$cocl_rnum;
			if ($cocl_typh==1)
			{
				if ($cocl_nbsnc==1)
				{
					$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 report");
					$result[] = $tab;
			
					$tab = array();
					if ($cocl_rnum=="")
					{
						if ($cocl_renauto==TRUE)
						{
							$tab = array(
								"cocl_code" => "-1",
								"cocl_codeco" => "-1",
								"cocl_desc" => "Report sur renouvellement (création automatique)");
						}
					}
					else
					{
						$tab = array(
							"cocl_code" => "-1",
							"cocl_codeco" => "-1",
							"cocl_desc" => "Report sur renouvellement (".$cocl_rnum.")");
						$rnum=$cocl_rnum;
					}
					if (count($tab)>0)
						$result[] = $tab;
				}
			}
		}
			
		if ($cocl_typh==1)
		{
			if ($cocl_nbsnc==1)
			{
				// On recupere les enregistrements
				$rech = "SELECT * FROM ".TBCOCL." WHERE cocl_code<>'".$nco."' AND cocl_codecli='".$ncli."'";
				$rech.= " AND cocl_typh=1 AND (cocl_datef>='".$datea."' OR cocl_datef='0000-00-00')";
				$rech.=" AND cocl_datefin='0000-00-00'";
				$rech.= " ORDER BY cocl_code";
				$rs = mysql_query($rech) or die(mysql_error());
				// On cree le resultat
				while($row = mysql_fetch_array($rs))
				{
					if ($rnum<>$row["cocl_code"])
					{
						// Calcul du montant à sortir de ce contrat
						$solde_reel=soldes_reel_contrat($row["cocl_code"],FALSE);
							$txt_cocl.=", solde réel ".heure_to_hhmm(sec_heure($solde_reel,3,TRUE));
						$result[] = $row;
					}
				}
			}
			else
			{
				$tab = array(
					"cocl_code" => "0",
					"cocl_codeco" => "0",
					"cocl_desc" => "Paramètre 'Report non consommé' à 'Non' (Cloture simple)");
				$result[] = $tab;
			}	
		}
		else
		{
			$tab = array(
				"cocl_code" => "0",
				"cocl_codeco" => "0",
				"cocl_desc" => "Aucun report horaire car ce contrat n'est pas de type 'Quota Horaire' (Cloture simple)");
			$result[] = $tab;
		}	
		mysql_close();
	}
	return $result;
}

?>