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

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_do.inc.php");
include_once("../inc/fic_st.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_co.inc.php");
include_once("../inc/fic_cocl.inc.php");
include_once("../inc/fic_int.inc.php");
include_once("../inc/fic_ot.inc.php");
include_once("../inc/fic_ret.inc.php");
include_once("../inc/fic_statut.inc.php");
include_once("../inc/fic_tec.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_time.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("../inc/mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

// Variables Formulaire
$titre = stripslashes(strip_tags(trim(@$_POST["titre"])));
$td = strip_tags(trim(@$_POST["td"]));

$datecrea = datefrang(strip_tags(trim(@$_POST["datecreaf"])));
$dateenv = datefrang(strip_tags(trim(@$_POST["dateenvf"])));
$clos = strip_tags(trim(@$_POST["clos"]));
$clore=0;
if ($clos=="ok")
	$clore=1;
$code = str_majuscules(strip_tags(trim(@$_POST["code"])));
$codemque = str_majuscules(strip_tags(trim(@$_POST["codemque"])));
$codeage = str_majuscules(strip_tags(trim(@$_POST["codeage"])));
$codeme = str_majuscules(strip_tags(trim(@$_POST["codeme"])));
$bord = str_majuscules(strip_tags(trim(@$_POST["bord"])));
$nbcol= strip_tags(trim(@$_POST["nbcol"]));
if ($nbcol==0) $nbcol=1;

$codeuser = @$_SESSION["code_log"];

$msg="";
$retourliste = TRUE;

if (strip_tags(trim(@$_POST["enrimp"])) == "Enregistrer & Imprimer")
{
	$retourliste = FALSE;
	$imprim = TRUE;
}


switch ($td)
{
	// Ajout de la fiche envoi
	case "add" :
		$codee = crecode("ER",TRUE);
	
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "SELECT * FROM " .TBRETE. " WHERE `rete_code` = '$codee'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) == 0)
		{
			$rech2 = "INSERT INTO " .TBRETE
			. " (rete_code,rete_datecrea,rete_dateenv,rete_codemque,rete_codeme,rete_bord,rete_nbcol,rete_codeage)"
			. " VALUES"
			. " ('$codee','$datecrea','$dateenv','$codemque','$codeme','$bord','$nbcol','$codeage')";
			$rs2 = mysql_query($rech2) or die(mysql_error());
	
			$msg = "Bon de renvoi enregistr&eacute;";
		}
		else
		{
			$msg = $txt_fiche_exist;
			$retourliste = FALSE;
			$imprim = FALSE;
		}
	
		mysql_free_result($rs);
		mysql_close($conn);
		break;
		
	// MAJ de la fiche envoi
	case "edit" :
		
		$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
		mysql_select_db(DBNAME);
		$rech = "UPDATE `" .TBRETE. "` SET `rete_dateenv`='".$dateenv."', `rete_codemque`='".$codemque."',";
		$rech .=" `rete_codeme`='".$codeme."', `rete_bord`='".$bord."', `rete_nbcol`='".$nbcol."', `rete_codeage`='".$codeage."',";
		$rech .=" `rete_clos`='".$clore."'";
		$rech .=" WHERE `rete_code` = '".$code."'";
		$rs = mysql_query($rech) or die(mysql_error());
				
		// Si clot, on met à jour les fiches produits correspondantes
		if ($clore==1)
		{
			$rech = "SELECT * FROM `".TBRETEP. "`";
			$rech .=" WHERE `retep_codeenv` = '".$code."'";
			$rs = mysql_query($rech) or die(mysql_error());
			if (mysql_num_rows($rs)>0)
			{
				while ($row=mysql_fetch_array($rs))
				{
					extract($row);
					
					$rech2 = "UPDATE `".TBRETP. "` SET `retp_dateenv`='".$dateenv."' ";
					$rech2 .=" WHERE `retp_code` = '".$retep_coderetp."'";
					$rs2 = mysql_query($rech2) or die(mysql_error());
				}
			}
			mysql_free_result($rs);
		}
		$msg = "Bon de renvoi mis &agrave; jour.";
	
		mysql_close($conn);
		break;		


	// Suppression de la fiche envoi
	case "del" :
		if ($clore==0)
		{
			if (nb_art_traites_in_envoi($code,TRUE)==0)
			{
				$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
				mysql_select_db(DBNAME);
				$rech = "DELETE FROM `" .TBRETE. "` WHERE `rete_code` = '$code'";
				$rs = mysql_query($rech) or die(mysql_error());
			
				mysql_close($conn);
				
				$msg = "Bon de renvoi supprim&eacute;";
			}
			else
			{
				$msg = "Il y d&eacute;j&agrave; eu des articles trait&eacute;s par le fournisseur. Impossible de supprimer";
				$retourliste = FALSE;
				$imprim = FALSE;				
			}
		}
		else
		{
			$msg = "Impossible de supprimer une fiche close.";
			$retourliste = FALSE;
			$imprim = FALSE;				
		}
		break;
		
	default :
		break;
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Fiche envoi de produits</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>



</head>
<body>
<?php 
include_once("../inc/entete.inc.php"); 
?>
<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<p align="center">&nbsp;</p>
<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>

<br>
<br>
<?PHP if ($retourliste == FALSE) 
{ 
	if ($imprim == TRUE)
	{
		if ($td == "add")
			$urlimp = "rete_pdf.php?ce=".$codee;
		else
			$urlimp = "rete_pdf.php?ce=".$code;
?>
		<p align="center">
		<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")><img src="../img/print.png" title="Imprimer le bon" border="0" align="absmiddle"></a> 
			<a href="javascript:;" onClick="lanceimp('<?PHP echo $urlimp; ?>');")> Imprimer le bon</a></div>
		</p>
<?PHP
	}
	aff_ret_idx_man($td,"rete_lst.php?ord=DES&tri=Code",$txt_lnk_ret_liste,TRUE);
} 
else 
{
	if ($td<>"add")
		aff_ret_lst_auto("rete_lst.php?ord=DES&tri=Code",2000);
	else
		aff_ret_sais_auto("rete_maj.php?td=edit&ce=".$codee,2000);
}
include_once("pied.php");
?>
