<?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");
Archivelog(@$_SESSION["code_log"]);

$titre = "REAFFECTATION TVA";
$retourliste=FALSE;
if (strip_tags(trim(@$_POST["val"])) == html_entity_decode($but_valider))
{
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$txtva1=0;
	$codetva1=strip_tags(trim(@$_POST["tva1"]));
	$rech = "SELECT tva_tx FROM " .TBTVA. " WHERE `tva_code` = '$codetva1'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) == 1)
	{
		if ($resultat = mysql_fetch_array($rs))
		{
			extract($resultat);
			$txtva1=$tva_tx/100;
		}
	}
	$txtva2=0;
	$codetva2=strip_tags(trim(@$_POST["tva2"]));
	$rech = "SELECT tva_tx FROM " .TBTVA. " WHERE `tva_code` = '$codetva2'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) == 1)
	{
		if ($resultat = mysql_fetch_array($rs))
		{
			extract($resultat);
			$txtva2=$tva_tx/100;
		}
	}
	$modeTTC=intval(strip_tags(trim(@$_POST["brec"])));
	$codeuser = @$_SESSION["code_log"];
	
	// Recalcul des produits
	$rech = "SELECT * FROM " .TBPDT. " WHERE pdt_codetva = '$codetva1'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) > 0)
	{
		while ($resultat = mysql_fetch_array($rs))
		{
			extract($resultat);
			$puttc=$pdt_puttc;
			switch ($modeTTC)
			{
				// mode HT
				case 0 :
					$puht=round($puttc/(1+$txtva1),2);
					$puttc=round($puht*(1+$txtva2),2);
					break;
				// Mode TTC
				case 1 :
					break;
			}
			$rech2 = "UPDATE `" .TBPDT. "` SET `pdt_datemod`='".$datea."', `pdt_codemod`='".$codeuser."',";
			$rech2.= " `pdt_puttc`='".$puttc."',";
			$rech2.= " `pdt_codetva`='".$codetva2."'   WHERE `pdt_id` = '".$pdt_id."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
		}
	}
	
	// Recalcul des taxes parafiscales
	$rech = "SELECT * FROM " .TBTPF. " WHERE tpf_codetva = '$codetva1'";
	$rs = mysql_query($rech) or die(mysql_error());
	if (mysql_num_rows($rs) > 0)
	{
		while ($resultat = mysql_fetch_array($rs))
		{
			extract($resultat);
			$rech2 = "UPDATE `" .TBTPF. "` SET ";
			if ($tpf_pour==FALSE) // On ne modifie la valeur que s'il ne s'agit pas d'un pourcentage
			{
				$valttc=$tpf_valeur;
				switch ($modeTTC)
				{
					// mode HT
					case 0 :
						$valht=round($valttc/(1+$txtva1),2);
						$valtc=round($valht*(1+$txtva2),2);
						break;
					// Mode TTC
					case 1 :
						break;
				}
				$rech2.= " `tpf_valeur`='".$valttc."',";
			}
			$rech2.= " `tpf_codetva`='".$codetva2."'   WHERE `tpf_id` = '".$tpf_id."'";
			$rs2 = mysql_query($rech2) or die(mysql_error());
		}
	}
	
	// Recalcul des interventions en cours
	$modint=intval(strip_tags(trim(@$_POST["modi"])));
	if ($modint==1)
	{
		$rech = "SELECT int_code FROM " .TBINT. " WHERE int_datefin = '0000-00-00'";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) > 0)
		{
			while ($resultat = mysql_fetch_array($rs))
			{
				extract($resultat);
				
				// Produits sur interventions
				$rech2 = "SELECT pdti_id FROM " .TBPDTI. " WHERE pdti_codeint = '".$int_code."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if (mysql_num_rows($rs2) > 0)
				{
					while($resultat2 = mysql_fetch_array($rs2))
					{
						extract($resultat2);
						$rech3 = "UPDATE `" .TBPDTI. "` SET ";
						$rech3.= " `pdti_codetva`='".$codetva2."'   WHERE `pdti_id` = '".$pdti_id."'";
						$rs3 = mysql_query($rech3) or die(mysql_error());
					}
				}

				// Taxes parafiscales globales sur interventions
				$rech2 = "SELECT tpfi_id,tpfi_codetpf,tpfi_valeur FROM " .TBTPFI. " WHERE tpfi_codeint = '".$int_code."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
				if (mysql_num_rows($rs2) > 0)
				{
					while ($resultat2 = mysql_fetch_array($rs2))
					{
						extract($resultat2);
						
						$rech3 = "SELECT * FROM " .TBTPF. " WHERE tpf_code = '$tpfi_codetpf'";
						$rs3 = mysql_query($rech3) or die(mysql_error());
						if (mysql_num_rows($rs3) > 0)
						{
							while ($resultat3 = mysql_fetch_array($rs3))
							{
								extract($resultat3);
								$rech4 = "UPDATE `" .TBTPFI. "` SET ";
								if ($tpf_pour==FALSE) // On ne modifie la valeur que s'il ne s'agit pas d'un pourcentage
								{
									$valttc=$tpfi_valeur;
									switch ($modeTTC)
									{
										// mode HT
										case 0 :
											$valht=round($valttc/(1+$txtva1),2);
											$valttc=round($valht*(1+$txtva2),2);
											break;
										// Mode TTC
										case 1 :
											break;
									}
									$rech4.= " `tpfi_valeur`='".$valttc."',";
								}
								$rech4.= " `tpfi_codetva`='".$codetva2."'   WHERE `tpfi_id` = '".$tpfi_id."'";
								$rs4 = mysql_query($rech4) or die(mysql_error());
							}
						}
					}
				}
			}
		}
	}
		
	// Recalcul des contrat non signés
	$modcocl=intval(strip_tags(trim(@$_POST["modc"])));
	if ($modcocl==1)
	{
		$rech = "SELECT cocl_id,cocl_pvht FROM " .TBCOCL. " WHERE cocl_datesig = '0000-00-00' AND cocl_codetva = '".$codetva1."' ";
		$rs = mysql_query($rech) or die(mysql_error());
		if (mysql_num_rows($rs) > 0)
		{
			while ($resultat = mysql_fetch_array($rs))
			{
				extract($resultat);
				
				$pvht=$cocl_pvht;
				switch ($modeTTC)
				{
					// mode HT
					case 0 :
						break;
					// Mode TTC
					case 1 :
						$pvttc=round($pvht*(1+$txtva1),2);
						$pvht=round($pvttc/(1+$txtva2),2);
						break;
				}
				$rech2 = "UPDATE `" .TBCOCL. "` SET `cocl_datemod`='".$datea."', `cocl_codemod`='".$codeuser."',";
				$rech2.= " `cocl_pvht`='".$pvht."',";
				$rech2.= " `cocl_codetva`='".$codetva2."'   WHERE `cocl_id` = '".$cocl_id."'";
				$rs2 = mysql_query($rech2) or die(mysql_error());
			}
		}
	}
		
	mysql_free_result($rs);
	mysql_close($conn);

	unset($_POST["val"]);
	$retourliste=TRUE;
}
?>
<!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; ?> : R&eacute;affecter TVA</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>

<!-- Scripts Javascript pour contrôle de formulaires -->
<script	type="text/javascript" src="../jscript/verif_form.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:tva1 ; format:liste ; req:Y ; txt:TVA à réaffecter ; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:tva2 ; format:liste ; req:Y ; txt:Vers la TVA ; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:brec ; format:liste ; req:Y ; txt:Base de recalcul ; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:modi ; format:liste ; req:Y ; txt:Modifier interventions en cours; vmn:1 ; stok:frmok ; stbad:frmbad",
	"nom:modc ; format:liste ; req:Y ; txt:Modifier contrats non signés; vmn:1 ; stok:frmok ; stbad:frmbad");
//-->
</script>

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

</head>

<body onLoad="metFocus();">

<?php 
include_once("../inc/entete.inc.php"); 
?>

<p align="center"><span class="titrepage"><?PHP echo $titre ?></span></p>
<?PHP if ($retourliste==FALSE) { ?>
<form name="tvarea_maj" id="tvarea_maj" method="post" action="tvarea_maj.php" onSubmit="javascript:; return (verif_tvarea(this,tab1_champ,'',''));">
<table width="60%"  border="0" align="center">
<tr>
	<td width="50%" class="rubfrm" id="f_tva1">R&eacute;affecter la TVA</td>
	<td width="50%">
	<select name="tva1" id="tva1">
	<option value="">--- Choisissez un code TVA ---</option>
	<?PHP
	// Création de la liste des TVA
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs = mysql_query("SELECT * FROM ".TBTVA." ORDER BY tva_code");

	while ($resultat = mysql_fetch_array($rs)) 
	{
		extract($resultat);
		echo '<option value="'.$tva_code.'"';
		echo '>'.$tva_code.' ('.$tva_desc.')</option>';
	}
	mysql_free_result($rs);
	mysql_close($conn);
	?>
	</select>
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	</td>
</tr>
<tr>
	<td width="50%" class="rubfrm" id="f_tva2">Vers la TVA</td>
	<td width="50%">
	<select name="tva2" id="tva2">
	<option value="">--- Choisissez un code TVA ---</option>
	<?PHP
	// Création de la liste des TVA
	$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
	mysql_select_db(DBNAME);
	$rs = mysql_query("SELECT * FROM ".TBTVA." WHERE tva_actif=1 ORDER BY tva_code");

	while ($resultat = mysql_fetch_array($rs)) 
	{
		extract($resultat);
		echo '<option value="'.$tva_code.'"';
		echo '>'.$tva_code.' ('.$tva_desc.')</option>';
	}
	mysql_free_result($rs);
	mysql_close($conn);
	?>
	</select>
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	<br><span class="annot_petit_it">Code TVA qui remplacera le pr&eacute;c&eacute;dent.</span>
	</td>
</tr>
<tr>
	<td width="50%" class="rubfrm" id="f_brec">Base de recalcul</td>
	<td width="50%">
	<select name="brec" id="brec">
	<option value="">--- Choisissez un mode tarifaire ---</option>
	<option value="0">Tarif HT (le TTC sera recalculé)</option>
	<option value="1">Tarif TTC (le HT sera recalculé)</option>
	</select>
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	</td>
</tr>
<tr>
	<td width="50%" class="rubfrm" id="f_modi">Modifier interventions en cours ?</td>
	<td width="50%">
	<select name="modi" id="modi">
	<option value="">--- Choisissez ---</option>
	<option value="0"><?PHP echo $sel_lst_non; ?></option>
	<option value="1"><?PHP echo $sel_lst_oui; ?></option>
	</select>
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	<br><span class="annot_petit_it">Les produits et les taxes parafiscales sont concern&eacute;s.</span>
	</td>
</tr>
<tr>
	<td width="50%" class="rubfrm" id="f_modi">Modifier contrats non signés ?</td>
	<td width="50%">
	<select name="modc" id="modc">
	<option value="">--- Choisissez ---</option>
	<option value="0"><?PHP echo $sel_lst_non; ?></option>
	<option value="1"><?PHP echo $sel_lst_oui; ?></option>
	</select>
	<img src="../img/oblig.png" align="absmiddle" alt="<?PHP echo $alt_chp_oblig; ?>">
	</td>
</tr>
</table>
<p align="center">
<input name="RAZ" type="reset" class="bton_std" id="RAZ" value="<?PHP echo $but_reinit; ?>">
<input name="val" type="submit" class="bton_std" id="val" value="<?PHP echo $but_valider; ?>">
</p>
</form>
<?PHP
} else {
	aff_ret_idx_man($td,"tva_lst.php",$txt_lnk_ret_liste,FALSE);
}
include_once("pied.php");
?>