<?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/func.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/fic_age.inc.php");
include_once("../inc/fic_hum.inc.php");
include_once("../inc/lst.inc.php");
Archivelog(@$_SESSION["code_log"]);
$td="";
if (isset($_GET['td']))
	$td = strip_tags(trim(@$_GET['td']));
else
	$td = strip_tags(trim(@$_POST['td']));
$codeage="";
if (isset($_GET['ca']))
	$codeage = strip_tags(trim(@$_GET['ca']));
else
	$codeage = strip_tags(trim(@$_POST['ca']));
$id = "";
if (isset($_GET['id']))
	$id = strip_tags(trim(@$_GET['id']));
else
	$id = strip_tags(trim(@$_POST['id']));

$pbm_age=FALSE;
$pbm_enr_agepr = FALSE;
$msg="";
$lastid=0;
$lastdate="";
$totalpr=0;

if ($codeage <> "")
{
	$row = lit_enr_age($codeage,TRUE);
	if (is_array($row))
	{
		extract ($row);
		
		// Vérification tentative de hack d'une agence
		if ((@$_SESSION["niv_log"] == 4) AND ($age_code <> @$_SESSION["code_age"]))
			$pbm_age = TRUE;

		if ($pbm_age==FALSE)
		{	
			$totalpr=nb_agepr($codeage,TRUE);
			if ($totalpr==0)
				$td="add";
			if (((strip_tags(trim(@$_POST["enr"])) == html_entity_decode($but_add)) OR (strip_tags(trim(@$_POST["enr"])) == html_entity_decode($but_modif))))
			{
				$datesais = strip_tags(trim(@$_POST["date"]));
				if ($datesais <> "")
					$datesaisa = datefrang($datesais);
				else
					$datesaisa ="";
				$couth = strip_tags(trim(@$_POST["couth"]));

				if (($datesaisa <> "") AND (floatval($couth)>0))
				{
					$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
					mysql_select_db(DBNAME);
					if (strip_tags(trim(@$_POST["enr"])) == html_entity_decode($but_add))
					{
						$rech = "INSERT INTO " .TBAGEPR;
						$rech .= " (agepr_codeage,agepr_date,agepr_couth) VALUES";
						$rech .= " ('$codeage','$datesaisa','$couth')";
					}
					if (strip_tags(trim(@$_POST["enr"])) == html_entity_decode($but_modif))
					{
						if ($id <> "")
						{
							$rech = "UPDATE `" .TBAGEPR;
							$rech .= "` SET `agepr_date`='".$datesaisa."',`agepr_couth`='".$couth."' WHERE `agepr_id` = '".$id."'";
						}
					}
					$rs = mysql_query($rech) or die(mysql_error());
					mysql_close($conn);
					unset($_POST);
					$td ="add";
				}
				else
				{
					$pbm_enr_agepr = TRUE;
					$msg = "Probl&egrave;me de saisie.";
				}
			}
			else
			{
				if (strip_tags(trim(@$_POST["enr"])) == html_entity_decode($but_del))
				{
					if ($id <> "")
					{
						$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
						mysql_select_db(DBNAME);
						$rech = "DELETE FROM `" .TBAGEPR. "` WHERE `agepr_id` = '$id'";
						$rs = mysql_query($rech) or die(mysql_error());
						mysql_close($conn);
					}
					unset($_POST);
					$td ="add";
				}
			}
		}
	}
	else
		$pbm_age=TRUE;
}
else
	$pbm_age=TRUE;		

if ($pbm_age == FALSE)
{
	$lastid=last_id_agepr($codeage);
	$lastdate=last_date_agepr($lastid);
	
	if ($td == "add")
	{
		$titre = "Ajouter prix de revient";
		if ($lastid>0)
			$datesais=formatedate(nouv_date_plus_jours($lastdate,1));
		else
			$datesais=formatedate($age_datecrea);
	}
	if ($td == "edit")
	{
		if ($id <> "")
		{
			$row = lit_enr_agepr($id);
			if (is_array($row))
				extract($row);
			$datesais = datefr($agepr_date);	
		}
		$titre = "Editer prix de revient";
	}
	if ($td == "view")
	{
		if ($id <> "")
		{
			$row = lit_enr_agepr($id);
			if (is_array($row))
				extract($row);
			$datesais = datefr($agepr_date);	
		}
		$titre = "Voir prix de revient";
	}
	if ($td == "del")
	{
		if ($id <> "")
		{
			$row = lit_enr_agepr($id);
			if (is_array($row))
				extract($row);
			$datesais = datefr($agepr_date);
		}
		$titre = "Supprimer prix de revient";
	}
}
?>
<!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; ?> : Prix de revient responsable agence</title>

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

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

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<script language="javascript">
<!--
var tab1_champ=new Array(
	"nom:date ; format:date ; masque:<?PHP echo $datemsql; ?> ; req:Y ; txt:Date ; vmn:<?PHP if ($lastid>0) {if ($td=="add") echo formatedate(nouv_date_plus_jours($lastdate,1)); else echo formatedate($lastdate); } else echo formatedate($age_datecrea); ?> ; stok:frmok ; stbad:frmbad",
	"nom:couth; format:reel_ns ; req:Y ; txt:coût horaire ; vmn:0.01 ; stok:frmok ; stbad:frmbad");
//-->
</script>

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

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

<!-- Scripts Javascript pour vérifications des champs -->
<script language="JavaScript" src="../jscript/verif_field.js"></script>
<!-- Définition Javascript pour calendrier popup -->

  <!-- Feuille de Style -->
  <link rel="stylesheet" type="text/css" media="all" href="../jscript/jscal/skins/aqua/theme.css" title="Aqua" />

  <!-- Script Javascript Principal -->
  <script type="text/javascript" src="../jscript/jscal/calendar.js"></script>

  <!-- Choix du langage -->
  <script type="text/javascript" src="../jscript/jscal/lang/calendar-<?PHP echo $_SESSION["lng"]; ?>.js"></script>

  <!-- the following script defines the Calendar.setup helper function, which makes
       adding a calendar a matter of 1 or 2 lines of code. -->
  <script type="text/javascript" src="../jscript/jscal/calendar-setup.js"></script>

<!-- Fin de la définition Javascript pour calendrier popup -->

</head>
<?PHP 
if ($td <> "edit")
{
	if ($pbm_age == FALSE)
	{
?>
		<body onLoad="javascript:; window.focus();">
<?PHP
	}
	else
	{
?>
		<body>
<?PHP
	}
}
else
{
?>
	<body onLoad="javascript:; window.focus();metFocus();">
<?PHP
}
if (($pbm_age == FALSE) AND ($pbm_enr_agepr == FALSE))
{
?>
<p align="center"> <span class="titrepage">PRIX DE REVIENT</span></p>
<p align="center"><span class="titre_gras">Pour le responsable agence : <?php echo $age_resp_nom." ".$age_resp_prenom; ?> </span></p>
<div align="right">
<input name="quit" type="button" class="bton_std" id="quit" value="<?PHP echo $but_quit; ?>" style="text-decoration: none; font-size: 11px; color:White ;font-weight:bold;background-color: #666666;" onClick="javascript:; self.close('fscomp');">
</div>
<br />
<?PHP
$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
mysql_select_db(DBNAME);
$rech = "SELECT * FROM `" .TBAGEPR. "` WHERE `agepr_codeage`= '".$codeage."' ORDER BY agepr_id";
$rs = mysql_query($rech) or die(mysql_error());

if (mysql_num_rows($rs) <> 0)
{
	echo "<table class=\"MtableD\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">\n";
	 
	// Création de l'entête de la table "détail prix de revient"
	echo "<th>";
	echo "Date";
	echo "</th>\n";
	echo "<th>";
	echo "Co&ucirc;t horaire";
	echo "</th>\n";
	echo "<th>";
	echo $txt_ent_lst_clic;
	echo "</th>\n";
	
	$j = 0;
	
	while ($row = mysql_fetch_assoc($rs)) 
	{         
		// Création de l'entête de ligne (TR)
		creetrlstdet($j);
		
		echo "<td align=\"center\">\n";
		echo formatedate($row["agepr_date"]);
		echo "<td align=\"right\">\n";
		echo format_num($row["agepr_couth"],2).$lang['symbole_monnaie'];
		echo "</td>\n";
		echo "<td align=\"center\">\n";
		if (($td <> "view") AND ($lastid==$row["agepr_id"]))
		{
			$lst_maj = TRUE;
			echo "<a href=\"agepr_maj.php?td=edit&ca=".$codeage."&id=".$row["agepr_id"]."\"><img src=\"../img/edit.png\" border=\"0\" align=\"absmiddle\"></a>";
			echo " ";

			$lst_del = TRUE;
			echo "<a href=\"agepr_maj.php?td=del&ca=".$codeage."&id=".$row["agepr_id"]."\"><img src=\"../img/del.png\" border=\"0\" align=\"absmiddle\"></a>";
		}
		else
			echo "-";
		echo "</td>\n";

		echo "</tr>\n";
	
		$j++;
	}
	
	echo "</table>\n";
	// Fin affichage table détail horaire
}
else
{
?>
	<p class="annot_petit_it">Aucun prix de revient enregistr&eacute;</p>
<?PHP
}
mysql_free_result($rs);
mysql_close($conn);

$taille_piedla="";
include_once("piedla.inc.php"); 

if ($td <> "view")
{
?>
	<br>
	<form name="agepr_maj" class="form_pointilles" method="post" action="agepr_maj.php" onSubmit="javascript:; return (VForm(this,tab1_champ,'','<?PHP echo $_SESSION["lng"]; ?>'));">
	<p align="center" class="annot_std_gras"><?PHP echo $titre; ?></p>
	<p align="center" class="annot_petit_it">Tous les champs sont obligatoires</p>
	<input name="td" type="hidden" id="td" value="<?PHP echo $td; ?>">
	<input name="ca" type="hidden" id="ca" value="<?PHP echo $codeage; ?>">
	<input name="id" type="hidden" id="id" value="<?PHP echo $id; ?>">
	<table width="580"  border="0" align="center">
		<tr>
			<td width="30%" class="rubfrm" id="f_date" align="right">Date</td>
			<td align="left">
				<input name="date" type="text" id="date" value="<?PHP echo $datesais; ?>" size="10" maxlength="10" readonly="1">
				<img src="../img/calendar.png" align="absmiddle" id="f_img_date" title="<?PHP echo $alt_chxdate; ?>">
				<a href="javascript:;" onclick="document.agepr_maj.date.value=''"><img src="../img/raz.png" border="0" align="absmiddle"></a>
				<?PHP 
				if ($td=="add") 
				{
					if ($lastid>0)
					{ 
				?>
				<br /><span class="annot_petit_it">(Strictement sup&eacute;rieur &agrave; derni&egrave;re date de modification)</span>
				<?PHP 
					}
					else
					{
				?>
				<br /><span class="annot_petit_it">(Sup&eacute;rieur ou &eacute;gal &agrave; la date de cr&eacute;ation de l'agence : <?PHP echo formatedate($age_datecrea);?>)</span>
				<?PHP
					}
				}
				
				if ($td=="edit") { ?>
				<br /><span class="annot_petit_it">(Ne peut &ecirc;tre inf&eacute;rieure &agrave; la date pr&eacute;c&eacute;demment saisie)</span>
				<?PHP } ?>
			</td>
		</tr>
		<tr>
			<td width="30%" class="rubfrm" id="f_tec" align="right">Co&ucirc;t horaire</td>
			<td align="left">
			<input name="couth" type="text" id="couth" style="text-align:right" value="<?PHP echo format_num($agepr_couth,2) ?>" onChange="formatedeci(this.id,2);" size="10">
			<?PHP echo $lang['symbole_monnaie']; ?>
			<br /><span class="annot_petit_it">(La d&eacute;cimale doit &ecirc;tre '.' et pas la virgule)</span>
			</td>
		</tr>
	</table>
		<p align="center">
		<input name="annul" type="button" class="bton_std" id="annul" value="<?PHP echo $but_cancel; ?>" onclick="javascript:; self.location.href='agepr_maj.php?td=add&ca=<?PHP echo $codeage; ?>';">
		<?PHP
			if ($td == "add")
			{
		?>
				<input name="enr" type="submit" class="bton_std" id="enr" value="<?PHP echo $but_add; ?>">
		<?PHP
			}
			if ($td == "edit")
			{
		?>
				<input name="enr" type="submit" class="bton_std" id="enr" value="<?PHP echo $but_modif; ?>">
		<?PHP
			}
			if ($td == "del")
			{
		?>
			<input name="enr" type="submit" class="bton_std" id="enr" value="<?PHP echo $but_del; ?>">
		<?PHP
			}
		?>
		</p>
	</form>
	
	<script type="text/javascript">
	<!--
		Calendar.setup({
			inputField     :    "date",     	// id of the input field
			ifFormat       :    "<?PHP echo $datefmtc; ?>",     // format of the input field
			button         :    "f_img_date",  	// trigger for the calendar (button ID)
			align          :    "Ll",           // alignment (defaults to "Bl")
			electric	   :	false,
			singleClick    :    true
		});
	//-->
	</script>
<?PHP
}
}
else
{
	if ($pbm_age == TRUE)
	{
?>
		<script type="text/javascript">
		<!--
		self.close('fscomp');
		//-->
		</script>
<?PHP
	}
	if ($pbm_enr_agepr == TRUE)
	{
?>
		<p align="center" class="annot_std_it"><?PHP echo $msg; ?></p>
		<script type="text/javascript">
		<!--
		window.setTimeout("location=('agepr_maj.php?ca=<?PHP echo $codeage; ?>&td=<?PHP echo $td; ?>&id=<?PHP echo $id; ?>');",3000)
		//-->
		</script>
<?PHP
	}
}
?>

</body>
</html>
