<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Untitled Document</title>
</head>
	<script src="/js/jquery-2.1.0.min.js"></script>
	<script src="/js/jquery.redirect.js"></script>
	<script type="text/javascript">   
function Redirect() 
{  
	

$.redirect('texteditnc.php', {'dnumdevis': document.getElementById("dnumdevis").value , 'didxcli': document.getElementById("didxcli").value });

	
} 
//document.write("You will be redirected to a new page in 5 seconds"); 
setTimeout('Redirect()', 250);   
</script>
	
	<style>
	
.attente{
	width:110px;
	height:110px;
	position:absolute;
	left:50%;
	top:50%;
	margin:-75px 0 0 -135px;
}
	</style>
	<img class="attente" src="spinner.gif">
	<?php 

	
	//recuperation des documents condition generale
		
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "credit";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
			 
												// dernier devis enregistre
 
$sql = "SELECT * FROM `documents` ";
	//$sql = " SELECT * FROM `tete`  ";		 
//print $sql;
$result = $conn->query($sql);
	 
if ($result->num_rows > 0) {
    // output data of each row
	
    while($Mdocdrow = $result->fetch_assoc()) {
		$CONDGENE=$Mdocdrow['condgene'];
		$LETTRTE=$Mdocdrow['accompagne'];
		$RAPPEL=$Mdocdrow['rappel'];
	
		
	echo "<br>1-Mdocdrow: " . $Mdocdrow["num"]. "  ".$Mdrow["idx"]."<br>";
	//echo "<br>3-Num: " . $Maxnumerodevis." <br>"; 
		
			}
	} else {
    echo "0 results";
}
	
			
 
	
$conn->close();
		
	
	
	
	
	
	
	
	
		//creation du devis tete
		
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "credit";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
			 
												// dernier devis enregistre
 
$sql = "SELECT `num`, idx FROM `tete` WHERE idx=(SELECT MAX(idx) FROM `tete`)";
	//$sql = " SELECT * FROM `tete`  ";		 
//print $sql;
$result = $conn->query($sql);
	 
if ($result->num_rows > 0) {
    // output data of each row
	
    while($Mdrow = $result->fetch_assoc()) {
		$Maxnumerodevis=$Mdrow['num'];
		$MaxIdxnumerodevis=$Mdrow['idx'];
	
		
	echo "<br>1-Num: " . $Mdrow["num"]. "  ".$Mdrow["idx"]."<br>";
	//echo "<br>3-Num: " . $Maxnumerodevis." <br>"; 
		
			}
	} else {
    echo "0 results";
}
	
			
     // creation devis tete	
		
		$Maxnumerodevis++;		

	$sql = "INSERT INTO `tete`(`num`) VALUES ('$Maxnumerodevis')";
	//print $sql;
	$result = $conn->query($sql);
if ($result->num_rows > 0) {	
	
	
	} else {
    echo "0 results";
}

		
															// recuperer le dernier devis enregistre de nouveau
 
$sql = "SELECT `num`, idx FROM `tete` WHERE idx=(SELECT MAX(idx) FROM `tete`)";
	//$sql = " SELECT * FROM `tete`  ";		 
//print $sql;
$result = $conn->query($sql);
	 
if ($result->num_rows > 0) {
    // output data of each row
	
    while($Mdrow = $result->fetch_assoc()) {
		$Maxnumerodevis=$Mdrow['num'];
		$MaxIdxnumerodevis=$Mdrow['idx'];
	
		
	echo "<br>1-Num: " . $Mdrow["num"]. "  ".$Mdrow["idx"]."<br>";
	//echo "<br>3-Num: " . $Maxnumerodevis." <br>"; 
		
			}
	} else {
    echo "0 results";
}
	
			
	
$conn->close();
		
	
	
	
	
	
	
	
	
	
	$debug=0;

 $myboxes = $_POST['DES'];
 $myboxes1 = $_POST['QTE'];  
 $myboxes2 = $_POST['PRIX']; 
 $myboxes3 = $_POST['UNITE'];  
 $myboxes4 = $_POST['ORDRELIGNE'];  
	$NumeroIndexDevis=$MaxIdxnumerodevis;
	$NumeroIdxClient=$_POST['didxcli'];
	
 $ordl = $_POST['ligne'];
 $RechIdx = $NumeroIndexDevis;//$_POST['dnum'];	
	
	if ($debug==1)   { 
	print "idex devis ".$_POST['dnum']; //numeros idxdevis
	print "<br> Idx client ".$_POST['didxcli'];
	print "<br> date devis ".$_POST['ddate'];
	print "<br> Objet ".$_POST['object'];
	print "<br> chantier ".$_POST['dchantier'];
	print "<br> tva ".$_POST['dtva'];
	print "<br> banque ".$_POST['dbanque'];
	print "<br> Remarque ".$_POST['dremarque'];
	print "<br> Memo ".$_POST['dmemo'];
	
	} 
	if ($debug==1)   { 
	print "<br>ligne devis insertion ".$ordl; 
	print "<br>ligne devis insertion ".$_POST['ligne']; 
	print "<br>index devis ".$RechIdx."  <br>";
        $ETATCLOT=$_POST['etatcloture'];  
    print "<br>Clotuere devis (2) facture (3) cloture  devis ".$ETATCLOT."  <br>";
        
		}
	
	//update tete
	
$DDNUM=$_POST['devisnum'];
$DDATE=$_POST["ddate"];
$DOBJET=$_POST["object"];
$DIDXCLI=$_POST["didxcli"];
$DCHANTIER=$_POST["dchantier"];
$DTVA=$_POST["dtva"];
$DBANQUE=$_POST["dbanque"];
$DREMARQUE=$_POST["dremarque"];
$MEMO=$_POST['dmemo'];
$MONTANTHTVA=$_POST['montant'];
$ETATC=$_POST['etat'];
$VRAIINDEXDEVIS=$_POST['idxref'];
$COMMANDE=$_POST['commande'];
		
					 
				
			        // on se connecte à notre base credit

$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "credit";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

 //$sql = "SELECT * FROM tete";
	$DCHANTIER=str_replace("'","\'",$DCHANTIER);
	$DOBJET=str_replace("'","\'",$DOBJET);
	$MEMO=str_replace("'","\'",$MEMO);
	$CONDGENE=str_replace("'","\'",$CONDGENE);
		$LETTRTE=str_replace("'","\'",$LETTRTE);
		$LETTRTE=str_replace("@","\@",$LETTRTE);
	$ETATC='1';
//$sql = "UPDATE tete SET num='$DNUM', date='$DDATE', objet='$DOBJET', idxcli='$DIDXCLI', chantier='$DCHANTIER', tva='$DTVA', banque='$DBANQUE', remarque='$DREMARQUE',memo='$MEMO',live='1',montant='$MONTANTHTVA',etat='$ETATC' WHERE idx='$NumeroIndexDevis' ";
    
	$sql = "UPDATE tete SET num='$Maxnumerodevis', date='$DDATE', objet='$DOBJET', idxcli='$DIDXCLI', chantier='$DCHANTIER', tva='$DTVA', banque='$DBANQUE',memo='$MEMO', live='1',montant='$MONTANTHTVA',etat='$ETATC',commande='$COMMANDE', remarque='$CONDGENE',lettre='$LETTRTE', idxdevis='$VRAIINDEXDEVIS' WHERE idx='$NumeroIndexDevis' ";
		 
if ($debug==1)   { print $sql;  print "<br>"; } 
$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {

        //echo "Num: " . $row["dnum"]. " - Date: " . $row["ddate"]. " - Objet: " . $row["objet"]. " Idex" . $row["didxcli"]. "<br>";
		//echo "Soc: " . $row["societe"]. " - Nom: " . $row["nom1"]. "  " . $row["nom2"]. "<br>";
    } 
	
	
	
	} else {
   if ($debug==1)   { echo "0 results";} 
}

$conn->close();
			 
	
	
	
	
	// ***********************************            update corps
	
	if(empty($myboxes))
	 
	{
	 
	 // echo("You didn't select any boxes.");
  }
  else
  {
    $i = count($myboxes);
    echo("<br>You have $i box(es): <br>");
    for($j = 0; $j < $i; $j++)
    {
      echo($myboxes[$j] . " ".$myboxes1[$j] ."  ".$myboxes2[$j] ." ".$myboxes3[$j] ."<br>");
    }
  }
	
	
	

		
$servername = "localhost:8889";
$username = "root";
$password = "root";
$dbname = "credit";

	// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
			 
	

			 
 $myboxes = $_POST['DES'];
 $myboxes1 = $_POST['QTE'];  
 $myboxes2 = $_POST['PRIX']; 
 $myboxes3 = $_POST['UNITE'];  
 $myboxes4 = $_POST['ORDRELIGNE'];  
	
	
	
	foreach($myboxes1 as $array_key=>$array_item)
{
  if($myboxes1[$array_key] == '0')
  {
      unset($myboxes[$array_key]);
	  unset($myboxes1[$array_key]);
	  unset($myboxes2[$array_key]);
	  unset($myboxes3[$array_key]);
	  unset($myboxes4[$array_key]);
	
  }
}
	$myboxes=array_values($myboxes);
	$myboxes1=array_values($myboxes1);
	$myboxes2=array_values($myboxes2);
	$myboxes3=array_values($myboxes3);
 
	
	
	
	
	
	if(empty($myboxes))
	 
	{
	 
	// echo("You didn't select any boxes.");
  }
  else
  {
	 
	  
    $ic = count($myboxes);
	$ordl=0;
	$jc=0;
    echo("You have $ic box(es): <br>");
	  
	  //*******************     compte le nombre de ligne existantes ******************
	  
	  $sql = "SELECT 0 FROM corps  WHERE numidxdevis='$NumeroIndexDevis' ";
	  $result = $conn->query($sql);
	  if ($result->num_rows > 0) {
    // output data of each row
    echo "<br> nombre enregistrement ".$result->num_rows."<br>";
	
	} else {
    if ($debug==1)   { echo "0 results";} 
}
	  // ********************************************************************************* 
	  $icrc=$result->num_rows;
	   if ($ic >= $icrc)   {
	 // echo " plus de lignes<br>".$ic."  ".$icrc." ";
	  $jcirtemp=$icrc;
	   for($jcir = $icrc; $jcir < $ic; $jcir++)
    {
     $jcirtemp++;
		   $sql = "INSERT INTO `corps`(`numidxdevis`, `ordre`) VALUES ('$NumeroIndexDevis',' $jcirtemp')";
		   if ($debug==1)   { print $sql;  print "<br>"; } 
	  $result = $conn->query($sql);
	  } 
		  } else {
    if ($debug==1)   { echo "effacer et reecrire";} 
		    $sql = "DELETE FROM corps WHERE numidxdevis='$NumeroIndexDevis' ";
		   if ($debug==1)   { print $sql;  print "<br>"; } 
	  $result = $conn->query($sql);
		   
		   
		   
		   

	   // ********************************************************************************* 
	  
	  
	 
    for($jc = 0; $jc < $ic; $jc++)
    {
echo "<br>".$myboxes1[$jc]."  ".floatval($myboxes1[$jc]."<br>" );
		
		if (floatval($myboxes1[$jc] ) <> 0) {

			$myboxes[$jc]=str_replace("'","\'",$myboxes[$jc]);		
	
		//echo($myboxes[$j] . " ".$myboxes1[$j] ."  ".$myboxes2[$j] ." ".$myboxes3[$j] ."<br>");
		// $sql = "SELECT * FROM tete WHERE idx=1";
	
		
		$sql = "INSERT INTO `corps`(`des`, `qt`, `pu`, `um`, `numidxdevis`, `ordre`) VALUES ('$myboxes[$jc]','$myboxes1[$jc]','$myboxes2[$jc]','$myboxes3[$jc]','$NumeroIndexDevis','$myboxes4[$jc]')";
			
		if ($debug==1)   { print $sql;  print "<br>"; } 
   $result = $conn->query($sql);
			
			
			
		    }
		
    }
   }
		   
		     for($jc = 0; $jc < $ic; $jc++)
    {
//echo "<br>".$myboxes1[$jc]."  ".floatval($myboxes1[$jc]."<br>" );
		echo floatval($myboxes1[$jc] )."<br>";

        //         if (floatval($myboxes1[$jc] ) <> 0) {

			$myboxes[$jc]=str_replace("'","\'",$myboxes[$jc]);		
	
		//echo($myboxes[$j] . " ".$myboxes1[$j] ."  ".$myboxes2[$j] ." ".$myboxes3[$j] ."<br>");
		// $sql = "SELECT * FROM tete WHERE idx=1";
	
		
	//	$myboxes3[$jc]=trim($myboxes3[$jc],"\x00..\x1F");
		//	$myboxes1[$jc]=trim($myboxes1[$jc],"\x00..\x1F");
		//	$myboxes1[$jc]=str_replace(' ', '', $myboxes1[$jc]);
			$myboxes3[$jc]=str_replace(' ', '', $myboxes3[$jc]);
		//	$myboxes3[$jc]=substr($myboxes3[$jc],1,3);
		//	print "<br>";
//print_r(unpack("C*", $myboxes3[$jc]));
	//		print $myboxes3[$jc]."<br>";
			$jctr=$jc;
			$jctr++;
				$sql = "UPDATE corps SET des='$myboxes[$jc]', qt='$myboxes1[$jc]', pu='$myboxes2[$jc]', um='$myboxes3[$jc]' WHERE numidxdevis='$NumeroIndexDevis' AND ordre='$jctr'";
				if ($debug==1)   { print $sql;  print "laaaaaaaa   <br>"; } 
  $result = $conn->query($sql);
			
		  //  }
		
    }
		   
		   
		   
		   
		   
 

			 
if ($result->num_rows > 0) {
    // output data of each row
    while($drow = $result->fetch_assoc()) {
	//  
			
    } 
	
	} else {
    if ($debug==1)   { echo "0 results";} 
}
	}		 
$conn->close();		
	

		$sql = "UPDATE `corps` SET ordre= ordre+1 WHERE  ordre>=".$ordl." AND numidxdevis=".$RechIdx." ";
	
	
// /***********************************   fin insertion ligne *********************************************************************
	
	
   
	
		
// /***********************************    suppression ligne *********************************************************************
	


		$sql = "DELETE FROM `corps` WHERE ordre=".$ordl." AND numidxdevis=".$RechIdx." ";
	
	  
	// /***********************************    fin suppression ligne *********************************************************************
	
?>
	
			<form method = "post" action = "indexdevlecture.php">	

			<input type="hidden" size="60" name="dnumdevis" id="dnumdevis" value="<?php echo $NumeroIndexDevis; ?>" > 
		
       		<input type="hidden" size="60" name="didxcli" id="didxcli" value="<?php echo $NumeroIdxClient; ?>" >
			
			<input type = "hidden" type = "submit" id = "update" value = "suivant"><br> 
	

<body>
</body>
</html>