<?php
$sup=$_GET['supplier'];
$dfrom=$_GET['dfrom'];
$dto=$_GET['dto'];

include '../includes/connection.php';
session_start();

$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
                                   // Create connection


if(strlen($sup)>0 && strlen($dfrom)==0 && strlen($dto)==0){
$supl=mysqli_query($con,"SELECT purchases.purchase_id as id1, purchase_payments.purchase_id from purchases,purchase_payments where purchases.supplier='$sup' AND purchases.purchase_id=purchase_payments.purchase_id && purchases.branch_id='$branch'");
$n=0;
while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['id1']; 


$sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,(select name from suppliers where supplier_id='$sup') as supplier,(select name from users where id=purchase_payments.AddedBy)as user from purchase_payments where purchase_id='$supid' && branch_id='$branch'");

if (mysqli_num_rows($sql) > 0) {
	
	$row = mysqli_fetch_array($sql);
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>
                                                <td><?php echo $row['voucher_no']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                                    <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    
} else {
    echo "0 resultz".mysqli_error($con);
}
//}





}
}

if(strlen($sup)>0 && strlen($dfrom)>0 && strlen($dto)==0){
$supl=mysqli_query($con,"SELECT purchases.purchase_id as id1, purchase_payments.purchase_id from purchases,purchase_payments where purchases.supplier='$sup' AND purchases.purchase_id=purchase_payments.purchase_id AND DATE_FORMAT(purchases.date_time, '%Y-%m-%d')>='$dfrom' && purchases.branch_id='$branch'");
$n=0;
  while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['purchase_id']; 


$sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,(select  name from suppliers where supplier_id='$sup')as supplier,(select name from users where id=purchase_payments.AddedBy)as user from purchase_payments where purchase_id='$supid' && branch_id='$branch' ");

if (mysqli_num_rows($sql) > 0) {
	
	$row = mysqli_fetch_array($sql);
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>
                                                <td><?php echo $row['voucher_no']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                                    <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    
} else {
    echo "0 resultz".mysqli_error($con);
}




}
}


if(strlen($sup)>0 && strlen($dfrom)>0 && strlen($dto)>0){
$supl=mysqli_query($con,"SELECT purchases.purchase_id as id1, purchase_payments.purchase_id from purchases,purchase_payments where purchases.supplier='$sup' AND purchases.purchase_id=purchase_payments.purchase_id AND DATE_FORMAT(purchases.date_time, '%Y-%m-%d')>='$dfrom' AND DATE_FORMAT(purchases.date_time, '%Y-%m-%d')<='$dto' && purchases.branch_id='$branch'");
$n=0;
while ($rowsup=mysqli_fetch_array($supl)) {
  $supid=$rowsup['id1']; 

$sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,(select  name from suppliers where supplier_id='$sup')as supplier,(select name from users where id=purchase_payments.AddedBy)as user from purchase_payments where purchase_id='$supid' && branch_id='$branch'");

if (mysqli_num_rows($sql) > 0) {
	
$row = mysqli_fetch_assoc($sql);
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>
                                                <td><?php echo $row['voucher_no']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                                    <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    
} else {
    echo "0 resultz".mysqli_error($con);
}
//}




}

}

if(strlen($sup)==0 && strlen($dfrom)>0 && strlen($dto)==0){



$sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,( select name from suppliers where supplier_id=(select distinct supplier from purchases where purchase_id=purchase_payments.purchase_id))as supplier,(select name from users where id=purchase_payments.AddedBy)as user from purchase_payments where DATE_FORMAT(AddedOn, '%Y-%m-%d')>='$dfrom' && branch_id='$branch'");

if (mysqli_num_rows($sql) > 0) {
	$n=0;
	while($row = mysqli_fetch_assoc($sql)) {
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>
                                                <td><?php echo $row['voucher_no']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                                    <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    }
} else {
    echo "0 resultz".mysqli_error($con);
}







}

if(strlen($sup)==0 && strlen($dfrom)>0 && strlen($dto)>0){



$sql= mysqli_query($con,"SELECT id,amount_paid,voucher_no,AddedOn,purchase_id,( select name from suppliers where supplier_id=(select distinct supplier from purchases where purchase_id=purchase_payments.purchase_id))as supplier,(select name from users where id=purchase_payments.AddedBy)as user from purchase_payments where DATE_FORMAT(AddedOn, '%Y-%m-%d')>='$dfrom' AND DATE_FORMAT(AddedOn, '%Y-%m-%d')<='$dto' && branch_id='$branch'");

if (mysqli_num_rows($sql) > 0) {
	$n=0;
	while($row = mysqli_fetch_assoc($sql)) {
                
                       $n++; 
                    ?>
                                            <tr class="odd gradeX">
                                                <td><?php echo $n; ?></td>
                                                <td><?php echo $row['AddedOn']; ?></td>
                                                <td><?php echo $row['amount_paid']; ?></td>
                                                <td><?php echo $row['voucher_no']; ?></td>
                                                <td><?php echo $row['supplier']; ?></td>
                                                <td><?php echo $row['user'];  ?></td>
                                                <td>
                                                    <?php
                                                    $purchid=$row['purchase_id'];
                                                    $selpid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_payments where purchase_id='$purchid'"));
                                                    $mid=$selpid['mid'];
                                                    $selmxvoucher=mysqli_fetch_array(mysqli_query($con,"SELECT voucher_no from purchase_payments where id='$mid'"));
                                                    ?>
                                                    <a href="reprint_voucher.php?voucher=<?php echo $selmxvoucher['voucher_no']; ?>"><button class="btn btn-primary"><i class="icon-print icon-white"></i></button></a>
                                                </td>
                                                
                                                
                                                
                                            </tr>
               <?php                             
    }
} else {
    echo "0 resultz".mysqli_error($con);
}
//}






}



?>