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

/*****************************************/
/* Diverses fonctions utilisées dans SMI */
/*****************************************/

/********************************************************/
/* Stockage et récupérations diverses dans les fichiers */
/********************************************************/

// Fonction qui renvoie la civilité en fonction du code civilité
function Retournecivilite($code)
{
	$civilite = "";
	
	$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rciv = "SELECT * FROM " .TBCIV. " WHERE civ_code='".$code."'";
	$rsciv = mysql_query($rciv) or die(mysql_error());
	if ($rowciv = mysql_fetch_array($rsciv)) 
	{
		extract ($rowciv);
	}
	mysql_free_result($rsciv);
	mysql_close($conn2);  

	$civilite = str_majuscules($civ_desc);

	return $civilite;
}

// Fonction qui renvoie un nom de créateur
function Retournecreateur($code,$code_admin)
{
	$createur = "";
	$cdg = FALSE;
	
	if ($code <> $code_admin)
	{
		$racine = substr($code,0,1);
		switch ($racine)
		{
			case "A" : $rech2 = "SELECT * FROM " .TBAGE. " WHERE age_code='".$code."'";
					   break;
			case "C" : $rech2 = "SELECT * FROM " .TBCLI. " WHERE cli_code='".$code."'";
					   break;
			case "D" : $rech2 = "SELECT * FROM " .TBDO. " WHERE do_code='".$code."'";
					   break;
			case "T" : $rech2 = "SELECT * FROM " .TBTEC. " WHERE tec_code='".$code."'";
					   break;
			case "O" : $rech2 = "SELECT * FROM " .TBOPE. " WHERE ope_code='".$code."'";
					   break;
					   
			default  : $cdg = TRUE;
					   break;
		}

		if ($cdg == FALSE)
		{
			$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
			mysql_select_db(DBNAME);
			
			$rs2 = mysql_query($rech2) or die(mysql_error());
			if (mysql_num_rows($rs2) == 1) // Il ne peut y avoir qu'un seul résultat
			{
				if ($row2 = mysql_fetch_array($rs2)) 
					extract ($row2);
				
				switch ($racine)
				{
					case "A" : $createur = $age_nom." (Agence)";
							   break;
					case "C" : $createur = $cli_civilite." ".$cli_prenom." ".$cli_nom." (Client)";
							   break;
					case "D" : $createur = $do_nom." (Donneur d'ordres)";
							   break;
					case "T" : $createur = $tec_civilite." ".$tec_prenom." ".$tec_nom." (Technicien)";
							   break;
					case "O" : $createur = $ope_civilite." ".$ope_prenom." ".$ope_nom." (Opérateur)";
							   break;
				}
			}
			else // S'il n'y a pas de résultat, c'est le centre de gestion
				 // (Code centre commence par A/C/D/T/O)
			{
				$createur = "le centre de gestion";
			}
			mysql_free_result($rs2);
			mysql_close($conn2);  
		}
		else
		{
			$createur = "le centre de gestion";
		}
	}
	else
	{
		$createur = "l'administrateur";
	}

	return $createur;
}

/***************************************/
/* Gestion des codes et mots de passes */
/***************************************/

// Création d'un code pour les Clients, Interventions, Techniciens, Opérateurs, Contrats, Demandes, Relevés lors de l'enregistrement
// de la fiche 

function crecode($racine)
	{
		$conn2 = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rcons = "SELECT * FROM " .TBCONS;
		$rscons = mysql_query($rcons) or die(mysql_error());
		if (mysql_num_rows($rscons) == 1) // Uniquement UN SEUL enregistrement !
		{
			 while ($rowcons = mysql_fetch_array($rscons)) 
			{
				extract ($rowcons);

				// Code Client
				if ($racine == "C")
				{
					$rcli = strip_tags(trim($rowcons["cons_rcli"]));
					$ncli = $rowcons["cons_ncli"];
					$ncli++;
					settype ($ncli, "string");
					$format = "%'07s";
					$nclif = sprintf ($format,$ncli);
					$codecree = $rcli.$nclif;

					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_ncli`='".$ncli."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Technicien
				if ($racine == "T")
				{
					$rtec = strip_tags(trim($rowcons["cons_rtec"]));
					$ntec = $rowcons["cons_ntec"];
					$ntec++;
					settype ($ntec, "string");
					$format = "%'07s";
					$ntecf = sprintf ($format,$ntec);
					$codecree = $rtec.$ntecf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_ntec`='".$ntec."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Intervention

				if ($racine == "I")
				{
					$rint = strip_tags(trim($rowcons["cons_rint"]));
					$nint = $rowcons["cons_nint"];
					$nint++;
					settype ($nint, "string");
					$format = "%'07s";
					$nintf = sprintf ($format,$nint);
					$codecree = $rint.$nintf;

					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nint`='".$nint."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}

				// Code Opérateur
				if ($racine == "O")
				{
					$rope = strip_tags(trim($rowcons["cons_rope"]));
					$nope = $rowcons["cons_nope"];
					$nope++;
					settype ($nope, "string");
					$format = "%'07s";
					$nopef = sprintf ($format,$nope);
					$codecree = $rope.$nopef;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nope`='".$nope."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Contrat
				if ($racine == "CO")
				{
					$rcon = strip_tags(trim($rowcons["cons_rcon"]));
					$ncon = $rowcons["cons_ncon"];
					$ncon++;
					settype ($ncon, "string");
					$format = "%'07s";
					$nconf = sprintf ($format,$ncon);
					$codecree = $rcon.$nconf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_ncon`='".$ncon."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Certificat de destruction
				if ($racine == "CD")
				{
					$rcer = strip_tags(trim($rowcons["cons_rcer"]));
					$ncer = $rowcons["cons_ncer"];
					$ncer++;
					settype ($ncer, "string");
					$format = "%'07s";
					$ncerf = sprintf ($format,$ncer);
					$codecree = $rcer.$ncerf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_ncer`='".$ncer."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Demande
				if ($racine == "D")
				{
					$rdde = strip_tags(trim($rowcons["cons_rdde"]));
					$ndde = $rowcons["cons_ndde"];
					$ndde++;
					settype ($ndde, "string");
					$format = "%'07s";
					$nddef = sprintf ($format,$ndde);
					$codecree = $rdde.$nddef;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_ndde`='".$ndde."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}
		
				// Code Relevé
				if ($racine == "R")
				{
					$rrel = strip_tags(trim($rowcons["cons_rrel"]));
					$nrel = $rowcons["cons_nrel"];
					$nrel++;
					settype ($nrel, "string");
					$format = "%'07s";
					$nrelf = sprintf ($format,$nrel);
					$codecree = $rrel.$nrelf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nrel`='".$nrel."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}


				// Code Relevé de Licences
				if ($racine == "RL")
				{
					$rrlic = strip_tags(trim($rowcons["cons_rrlic"]));
					$nrlic = $rowcons["cons_nrlic"];
					$nrlic++;
					settype ($nrlic, "string");
					$format = "%'07s";
					$nrlicf = sprintf ($format,$nrlic);
					$codecree = $rrlic.$nrlicf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nrlic`='".$nrlic."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}

				// Code Relevé de Matériels
				if ($racine == "RM")
				{
					$rrpdt = strip_tags(trim($rowcons["cons_rrpdt"]));
					$nrpdt = $rowcons["cons_nrpdt"];
					$nrpdt++;
					settype ($nrpdt, "string");
					$format = "%'07s";
					$nrpdtf = sprintf ($format,$nrpdt);
					$codecree = $rrpdt.$nrpdtf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nrpdt`='".$nrpdt."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}

				// Code Ordre
				if ($racine == "OR")
				{
					$rord = strip_tags(trim($rowcons["cons_rord"]));
					$nord = $rowcons["cons_nord"];
					$nord++;
					settype ($nord, "string");
					$format = "%'07s";
					$nordf = sprintf ($format,$nord);
					$codecree = $rord.$nordf;
					
					$rcmaj = "UPDATE `" .TBCONS. "` SET `cons_nord`='".$nord."'  WHERE `cons_id` = '".$rowcons["cons_id"]."'";
					$rscmaj = mysql_query($rcmaj) or die(mysql_error());
				}

			}
		}

		mysql_free_result($rscons);
		mysql_close($conn2);  

		return $codecree;
	}

// Création d'un code session pour création de fiche Client ("C"), Technicien ("T"), Intervention ("I"), etc...
// Fonction utilisée pendant la saisie d'un formulaire

function crecodesession($racine)
	{
		$codecree = $racine.time();
		return $codecree;
	}

// Création du mot de passe

function cremdp($aveccryptage)
{
    mt_srand();
    $longueur = mt_rand(8,10);

    $mdpcree = "";
    for($i=0; $i<$longueur; $i++)
	{
        $quoi= mt_rand(1,3);
        switch($quoi){
            case 1: $mdpcree .= mt_rand(0,9); break;
            case 2: $mdpcree .= chr(mt_rand(65,90)); break;
            case 3: $mdpcree .= chr(mt_rand(97,122)); break;
        }
    }

	if ($aveccryptage == TRUE)	
		$mdpcree = md5($mdpcree); // Cryptage du MDP

	return $mdpcree;
}

// Affichage des erreurs MYSQL

function erreur_bdd($argTexte,$argSql = '')
{
 echo "<table width=\"100%\"  border=\"0\">";
 echo "<tr>";
 echo "<td width=\"30%\"><div align=\"right\"><font size=\"10\" color=\"red\"><b>Erreur MYSQL</b></font></div></td>";
 echo "<td width=\"70%\">&nbsp;</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td width=\"30%\"><div align=\"right\"><b>Type : </b></div></td>";
 echo "<td width=\"70%\"><b>$argTexte </b>.</td>";
 echo "</tr>";
 echo "<tr>";
 echo "<td width=\"30%\"><div align=\"right\"><b>Numéro : </b></div></td>";
 $strCorps = @mysql_errno()." --> ".@mysql_error();
 echo "<td width=\"70%\"><b>$strCorps</b>. </td>";
 echo "</tr>";
 echo "</table>";

 // envoi email à admin
 // $strCorps = "[$argTexte]\n";
 // $strCorps .= "(".@mysql_errno()." : ".@mysql_error().")\n";
 // if(!empty($argSql)) $strCorps .= "\nSQL : ".$argSql;
 // @mail(
 // "email@administrateur",
 // "Erreur Bdd",
 // $strCorps
 // );
}

/***************************/
/* Gestion des impressions */
/***************************/

//	Entête des impressions des contrats

function entimpcon($titre)
{
	echo "<p align=\"center\"><span class=\"titre_impression\">$titre</span></p>";
}

//	Entête des impressions des fiches Agences, Clients, Techniciens

function entimpfic($titre)
{
	echo "<p align=\"center\"><span class=\"titre_impression\">$titre</span></p>";
	echo "<span class=\"annot_petit_it\">";
	echo "Imprimée le : ";
	echo "</span>";
	echo "<span class=\"annot_gras_it\">";
	echo date("d-m-Y H:i:s")."<br><br>";
	echo "</span>";
}

//	Entête des impressions des fiches Interventions

function entimpint($titre,$inter,$dateinter,$daterdv,$url,$agence)
{
	echo "<table width=\"740\" border=\"0\">";
    echo "<tr>";

    echo "<td rowspan=\"2\">";
	$urlimg = "";
	if (agence <> "")
	{
		$urlimg = $url."/log".strtolower($agence).".png"; 
	}
	echo "<div align=\"left\">";
	echo "<img src=\"".$urlimg."\"><br>";

	$rowage = lit_enr_age($agence);
	extract ($rowage);

	echo "<span class=\"annot_petit_it\">";
	echo $age_rcs."<br>";
	echo "</span>";
	echo $age_adr1."<br>";
	if ($age_adr2 <> "")
		echo $age_adr2."<br>";
	echo Retournecp_openId($age_ville,TRUE)." ".Retourneville_openId($age_ville,TRUE)."<br>";
	echo "Tel : ";
	echo $age_telf;
	echo "<span class=\"annot_petit_it\">";
	echo " ".$age_txttelf."<br>";
	echo "</span>";
	echo "Fax : ";
	echo $age_fax;
	echo "<span class=\"annot_petit_it\">";
	echo " ".$age_txtfax."<br>";
	echo "</span>";
	echo "<span class=\"annot_petit_it\">";
	if ($age_web <> "")
	{
		echo " Site Web : ";
		echo $age_web." / ";
	}
	echo "Email : ";
	echo $age_email."<br>";
	echo "Horaires : ";
	echo $age_heures."<br>";
	echo formatetxt($age_txtent);
	echo "</span>";
	echo "</div>";
	echo "</td>";

    echo "<td valign=\"top\">";
	echo "<div align=\"center\">";
	echo "<span class=\"titre_impression\">$titre</span>";
	echo "<br>";
	echo "Numéro : ";
	echo "<b>";
	echo $inter."<br>";
	echo "</b>";
	echo "Date création : ";
	echo "<b>";
	echo $dateinter."<br>";
	echo "</b>";
	if ($daterdv <> "")
	{
		echo "<span class=\"annot_petit_it\">";
		echo "Rendez-vous le : ";
		echo "</span>";
		echo "<span class=\"annot_gras_it\">";
		echo $daterdv."<br>";
		echo "</span>";
	}
	echo "</div>";
	echo "</td>";

    echo "</tr>";

    echo "<tr>";
	echo "<td>";
	echo "<div valign =\"bottom\" align=\"right\">";
	echo "<span class=\"annot_petit_it\">";
	echo "Imprimée le : ";
	echo "</span>";
	echo "<span class=\"annot_gras_it\">";
	echo date("d-m-Y H:i:s")."<br>";
	echo "</span>";
	echo "</div>";
	echo "</td>";
    echo "</tr>";

	echo "</table>";
}

//	Entête des impressions complémentaires (contacts, articles, etc...) des fiches
function compimpfic($titre)
{
	echo "<p align=\"center\"><span class=\"titre_gras_soul\">$titre</span></p>";
}

// Affichage des images avec mise à l'échelle et lien éventuel
function aff_img($lnkimg,$lnkhref,$lmax,$hmax,$title,$alt,$return)
{
	if (trim($lnkimg)<>"")
	{
		if (file_exists(trim($lnkimg)))
		{
			$infos_img = getimagesize($lnkimg); 
			$largeur = $infos_img[0];
			$hauteur = $infos_img[1];
			$coeffl=1;
			$coeffh=1;
			if ($lmax>0)
			{
				if ($largeur>$lmax)
					$coeffl=($largeur/$lmax);
			}
			if ($hmax>0)
			{
				if ($hauteur>$hmax)
					$coeffh=($hauteur/$hmax);
			}
			if ($coeffl>$coeffh)
				$coeff=$coeffl;
			else
				$coeff=$coeffh;
			$hauteur=round($hauteur/$coeff);
			$largeur=round($largeur/$coeff);
			if (trim($lnkhref)<>"")
				echo "<a href=\"".$lnkhref."\">";
			echo "<img src=\"".$lnkimg."\"  title=\"".$title."\" alt=\"".$alt."\" width=\"".$largeur."\" height=\"".$hauteur."\" border=\"0\" align=\"absmiddle\">";
			if (trim($lnkhref)<>"")
				echo "</a>";
			echo "\n";
			if ($return==TRUE)
				echo "<br>";
		}
	}
}

?> 
