<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Paiement facture</title>
</head>
    <body>
      <link rel="stylesheet" href="listepaie.css" media="all" />
			
       <form action="index.html">
               <input class="form-control" id="btst" type="submit" value="HOME" />
	            </form>
    
    <table border="0px">
       <tbody>
    
<?php
			
       


// on se connecte à notre base pour facture

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

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

	 if(isset($_POST['update'])) {
		 $sql = "SELECT * FROM tete WHERE ";
		 
		 
	 		

		 	if($positionradio==0) {	
				$sqletat = " etat>0 ";
				 } else {
 				$sqletat = " etat='".$positionradio."' ";
				}

		 $sql=$sql.$sqletat;
		 
		 print $sql.'<br>';
	 } else {
										
									
$sql = "SELECT * FROM tete WHERE etat>='0'";
	// print "<br>".$sql;	
		 }

$result = $conn->query($sql);

if ($result->num_rows > 0) {
    // output data of each row
    while($row = $result->fetch_assoc()) {
				
		$NUM=$row["num"];
$IDX=$row["idx"];
$DATE=$row["date"];
$OBJET=$row["objet"];
$IDXCLI=$row["idxcli"];
$MEMO=$row["memo"];
$NumeroIdxClient=$row["idxcli"];;
$CHANTIER=$row["chantier"];
$TVA=$row["tva"];
$BANQUE=$row["banque"];
$REMARQUE=$row["remarque"];
$ETATC=$row["etat"];
$MONTANTHTVA=$row["montant"];
$MONTANTTVAC=$row["commande"];
$VRAIIDXDEVIS=$row["idxdevis"];
        
        
         print "<tr>";
            print " <td class='no' style='font-size: 15px'  ></td>";
         // print "<td  class='desc'></td><td class='unit'></td></tr>";
		
		// *********************    connection clients *************************
		
								$servername1 = "localhost:8889";
								$username1 = "root";
								$password1 = "root";
								$dbname1 = "Clients";

								// Create connection
								$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
								// Check connection
								if ($conn1->connect_error) {
								die("Connection failed: " . $conn1->connect_error);
								} 

			
								$sql1 = "SELECT * FROM data WHERE id=".$NumeroIdxClient." ";
								//print "<br>".$sql1;	
		 
								$result1 = $conn1->query($sql1);
		
								if ($result1->num_rows > 0) {
    							// output data of each row
								while($row1 = $result1->fetch_assoc()) {
								//print "echo <br>".$row1['societe']."  ".$row1['nom1'];
									$tempsoc=$row1['societe'];
									$tempnom1=$row1['nom1'];
                                    $tempnom2=$row1['nom2'];
                                    
                                    //$tempnom1=$tempnom1." ".$row1['nom2'];
									    } 
    
	
									} else {
										// echo "0 results";
									}




$conn1->close();
        $tempnom1=str_replace("'","", $tempnom1);
        $posnomsearch=strpos($tempnom1, " ");
        $tempnomsearch=$tempnom1;
        if ($posnomsearch>0) {
    	$tempnomsearch=substr ($tempnom1,0,$posnomsearch);
        $tempnomsearch1=substr ($tempnom1,$posnomsearch+1,strlen($tempnom1));  
          	}
		
									
					// *********************    connection devis *************************
		
								$servername1 = "localhost:8889";
								$username1 = "root";
								$password1 = "root";
								$dbname1 = "devis";

								// Create connection
								$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
								// Check connection
								if ($conn1->connect_error) {
								die("Connection failed: " . $conn1->connect_error);
								} 

			
								$sql1 = "SELECT num,montant FROM tete WHERE idx=".$VRAIIDXDEVIS." ";
								//print "<br>".$sql1;	
		 
								$result1 = $conn1->query($sql1);
		
								if ($result1->num_rows > 0) {
    							// output data of each row
								while($row1 = $result1->fetch_assoc()) {
								//print "echo <br>".$row1['societe']."  ".$row1['nom1'];
									$NUMEROSDEREFDEVIS=$row1['num'];
									$MONTANTDEREFDEVIS=$row1['montant'];
								
									    } 
    
	
									} else {
										// echo "0 results";
									}




$conn1->close();			
        
        
             
		    print "<td  class='desc'>";
       
        	print"<h4>";
          print "<div style='float:right'>";
		 print "<form method='post' action='textedit.php' name='demande'>";
		    print "<button name='dnumdevis' id='liensubmit' type='submit' value='". $VRAIIDXDEVIS."'><p style='font-size:14px'>Devis N° : ".$NUMEROSDEREFDEVIS." de ".$MONTANTDEREFDEVIS."</p></button>";
			print "</form></div>";
        print "<div style='float:left'>";
         print "<form method='post' action='texteditfac.php' name='demande'> ";
       print "<button style='font-size:20px' name='dnumdevis' id='liensubmit' type='submit' value='". $IDX."'>Facture N° ".$NUM." </button>";
		
			print "</form> </div>"; 
		  print"</h4>";                           
    
        print"<div style='clear:both'></div>" ;                  
		   
                                      
      
	
       
         print "<form method='post' action='texteditfac.php' name='demande'>";
        print"<div style='float:left'> <button name='dnumdevis' id='liensubmit' type='submit' value='". $IDX."'>".$tempsoc."  ".$tempnom1." ".$row1['nom1']."<p style='font-size:10px'>". $OBJET. " </p></div>";
        print "<div style='float:right'><p style='font-size:10px'> ".$DATE."</p></button></div>";print "</form>";
        
      echo "</td>";
        
		// print "<td class='unit'></td></tr>";    
  
       
   
				
       
       //  print "<tr> <td class='no' style='font-size: 16px'  > ". $NUM."</td>";
         print "<tr> <td class='no' style='font-size: 16px'  > </td>";
         print "<td  class='desc'>";
        
       
     //  print "<p style='font-size:14px'> Montant facture HTVA ".$MONTANTHTVA." TVAC : ".$MONTANTTVAC." ";
        $MONTANTEMP=substr($MONTANTTVAC, 0, (strlen($MONTANTTVAC))-3); 
        $MONTANTEMP=str_replace(" ", "", $MONTANTEMP);
        $MONTANTEMP=str_replace(",", ".", $MONTANTEMP);
      //   print "<p style='font-size:14px;color:red;'><strong> Montant facture : </strong>".$MONTANTEMP." ";
        
		//print "+".substr($MONTANTTVAC, 0, (strlen($MONTANTTVAC))-3); 
        $RECHBANQUE="+".substr($MONTANTTVAC, 0, (strlen($MONTANTTVAC))-3); 
        $RECHBANQUE=str_replace(" ", "", $RECHBANQUE);
        //print $RECHBANQUE;
        
		// *********************    connection banque *************************
		
								$servername1 = "localhost:8889";
								$username1 = "root";
								$password1 = "root";
								$dbname1 = "banque";

								// Create connection
								$conn1 = new mysqli($servername1, $username1, $password1, $dbname1);
								// Check connection
								if ($conn1->connect_error) {
								die("Connection failed: " . $conn1->connect_error);
								} 

        //*********************** Calcul du solde ****************************************
        $sql1 = "SELECT * FROM `TABLE3` WHERE `COL 30`= '".$NUM."'  ";
       //print $sql1;
        $SOLDE=0;
                
        	$resultS = $conn1->query($sql1);
            if ($resultS->num_rows > 0) {
                while($row1 = $resultS->fetch_assoc()) {
                    
                    $SOLDE=$SOLDE+round(floatval(str_replace(",", ".", $row1['COL 8']))*1,2);
                     }
                $RECHBANQUEQAL=str_replace(",", ".", $RECHBANQUE);
                $SOLDE=round($RECHBANQUEQAL,2)-round($SOLDE,2);
                $PAIEMENT=round($RECHBANQUEQAL,2)-round($SOLDE,2);
                if ($SOLDE == 0) {
                 print "<p style='font-size:14px;color:black;'><strong> Montant facture : </strong>".$MONTANTEMP." ";
                 echo " <strong>Paiement : </strong>".$PAIEMENT." <strong>Solde : </strong>".$SOLDE."</p>";
                
                } else {
                    print "<p style='font-size:14px;color:red;'><strong> Montant facture : </strong>".$MONTANTEMP." ";
                     echo " <strong>Paiement : </strong>".$PAIEMENT." <strong>Solde : </strong>".$SOLDE."</p>";
                    }
                
            } else {
                $PAIEMENT='0';
                 print "<p style='font-size:14px;color:red;'><strong> Montant facture : </strong>".$MONTANTEMP." ";
                 echo " <strong>Paiement : </strong>".$PAIEMENT." <strong>Solde : </strong>".$MONTANTEMP."</p>";
                 }
                 //*********************** Recherche un paiement déjà effecuté ****************************************    
      
                                
			$sql1 = "SELECT * FROM `TABLE3` WHERE `COL 30`= '".$NUM."'  ";
        	$resultB = $conn1->query($sql1);
            if ($resultB->num_rows > 0) {
            print "<p style='font-size:12px'>Détail paiement(s):";
                while($row1 = $resultB->fetch_assoc()) {
                   echo "<br><strong> ".$row1['COL 8']."</strong><br> ".$row1['COL 10']." ".$row1['COL 11']." ".$row1['COL 14']." ".$row1['COL 15']." du ".$row1['COL 6'];
                 }
            }    
                //    } else {

               echo "</p>";

        
        
								$sql1 = "SELECT * FROM `TABLE3` WHERE `COL 8`= '".$RECHBANQUE."' AND `COL 30` IS NULL ";
								//print "<br>".$sql1;	
		 
								$result1 = $conn1->query($sql1);
		
								if ($result1->num_rows > 0) {
    							// output data of each row
								while($row1 = $result1->fetch_assoc()) {
								//print "echo <br>".$row1['societe']."  ".$row1['nom1'];
									$BANQUE1=$row1['COL 8'];
									$BANQUE2=$row1['COL 11'];
                                    $BANQUEDATE=$row1['COL 6'];
                                     $BANQUEREF=$row1['COL 1'];
							//	print $BANQUE1."  ".$BANQUE2." ".$BANQUEDATE."<br>";
                                    
                        
		  
        //    print " banque : ".$BANQUE1."  ".$BANQUE2." du ".$BANQUEDATE."<br> ";
                                    
                                print "<form method='post' action='listepaiecheck.php' name='demande'> ";
                                   print " <input type='hidden' id='numfac' name='numfac' value='".$NUM."'>";
       print "<button name='idxref' id='idxref' type='submit' value='". $BANQUEREF."'>banque : ".$BANQUE1."  ".$BANQUE2." du ".$BANQUEDATE." ".$row1['COL 14']." ".$row1['COL 15']."</button></form>";
		
                                    
									    } 
    
	
									} else {
										 //echo "Pas de paiement correspondant";
									}

                                  
		//	$sql1 = "SELECT * FROM `TABLE3` WHERE `COL 8`= '".$tempnom1."'  ";
								$sql1 = "SELECT * FROM `TABLE3` WHERE (`COL 8` LIKE '%".$tempnomsearch."%' OR `COL 11` LIKE '%".$tempnomsearch."%' OR `COL 14` LIKE '%".$tempnomsearch."%' OR `COL 15` LIKE '%".$tempnomsearch."%' ) AND `COL 30` IS NULL ";
        
        	$sql1 = "SELECT * FROM `TABLE3` WHERE (`COL 8` LIKE '%".$tempnomsearch1."%' OR `COL 11` LIKE '%".$tempnomsearch1."%' OR `COL 14` LIKE '%".$tempnomsearch1."%' OR `COL 15` LIKE '%".$tempnomsearch1."%' OR `COL 8` LIKE '%".$tempnomsearch."%' OR `COL 11` LIKE '%".$tempnomsearch."%' OR `COL 14` LIKE '%".$tempnomsearch."%' OR `COL 15` LIKE '%".$tempnomsearch."%' ) AND `COL 30` IS NULL AND `COL 8` LIKE '%+%' ";
								//print "<br>".$sql1;	
		 
								$result1 = $conn1->query($sql1);
		
								if ($result1->num_rows > 0) {
    							// output data of each row
								while($row1 = $result1->fetch_assoc()) {
								//print "echo <br>".$row1['societe']."  ".$row1['nom1'];
									$BANQUE1=$row1['COL 8'];
									$BANQUE2=$row1['COL 11'];
                                    $BANQUEDATE=$row1['COL 6'];
                                     $BANQUEREF=$row1['COL 1'];
						
                                    
                                print "<form method='post' action='listepaiecheck.php' name='demande'> ";
                                   print " <input type='hidden' id='numfac' name='numfac' value='".$NUM."'>";
       print "<button name='idxref' id='idxref' type='submit' value='". $BANQUEREF."'>banque : ".$BANQUE1."  ".$BANQUE2." du ".$BANQUEDATE." ".$row1['COL 14']." ".$row1['COL 15']." </button></form>";
		
                                    
									    } 
    
	
									} else {
										// echo "Pas de nom correspondant";
									}


									 
      
$conn1->close();						
		 print "<form method='post' action='listepaiereset.php' name='demande'> ";
                                   print " <input type='hidden' id='numfac' name='numfac' value='".$NUM."'>";
       print "<button name='idxref' id='idxref' type='submit'>RESET </button></form>";
        
         
		   print "</td>";
            print "</tr>";
        //********************     ligne de separation *******************
            print "<tr>";
            print " <td class='sepa' style='font-size: 20px'  ></td>";
            print "<td  class='sepa'></td>";//<td class='unit'></td></tr>";     
            print "</tr>";
 

    } 
    
	
	} else {
   // echo "0 results";
}




$conn->close();
		
          
           

		?>	
    </tbody></table>
     
 

       <form action="index.html">
               <input class="form-control" id="btst" type="submit" value="HOME" />
	            </form>
</body>
</html>