<?php
session_start();
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
$u_id=$_SESSION['id'];
$total=$_GET['total'];
  $bad_symbols = array(",", ".");
   
    $paid=$_POST['amount_paid'];
    $invoice_number=$_POST['invoice_number'];
    $paid= str_replace($bad_symbols, "", $paid);
    if ($paid=="") {
      $paid=0;
    }

include '../includes/connection.php';
// Create connection


$sql = "SELECT sum(quantity)as orders from getstock where branch_id='$branch' && user_id='$u_id'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $orders=$row['orders'];
    }
} else {
    echo "0 results";
}
//mysqli_close($conn);
?>

<!--insert data-->
<?php
 if (isset($_POST['purchase_date'])) {
    $purchase_date=$_POST['purchase_date'];
     $sql1 = "INSERT INTO purchase_totals (invoice_number,quantity, total_price,paid,branch_id,business_id,AddedOn)
VALUES ('$invoice_number','$orders', '$total','$paid','$branch','$business','$purchase_date')";
$sql_log=mysqli_query($con,$sql1);
if ($sql_log) {

$paym=mysqli_fetch_array(mysqli_query($con,"select max(id) as id from purchase_totals where branch_id='$branch'"));
$purchase_id=$paym['id'];
$Check_amountpaid=mysqli_fetch_array(mysqli_query($con,"select paid from purchase_totals where id ='$purchase_id'"));
$pd=$Check_amountpaid['paid'];
if ($pd>0) {
  

 $tyr = date("Y");
 $tmonth = date("m");
        $sql13 = mysqli_query($con,"SELECT max(voucher) as recno from purchase_payments where year='$tyr' && month='$tmonth' && branch_id='$branch'");
        if(mysqli_num_rows($sql13)>0){
            $row13=mysqli_fetch_array($sql13);
            $rec = $row13['recno'];
        }
        else{
            $rec=0;
        }
        $new_rec = $rec +1;
        if(strlen($new_rec)==1){
            $reciept = "000".$new_rec;
        }
        else if(strlen($new_rec)==2){
            $reciept = "00".$new_rec;
        }
        else if(strlen($new_rec)==3){
            $reciept = "0".$new_rec;
        }
        else{
            $reciept = $new_rec;
        }
        $voucher_no = $tyr.$tmonth.$branch.$reciept;
        $id_user=$_SESSION['id'];
        $sql2 = mysqli_query($con,"INSERT INTO purchase_payments (purchase_id, amount_paid,voucher_no,year,month,voucher,branch_id,business_id,AddedBy,AddedOn) VALUES ('$purchase_id', '$paid','$voucher_no','$tyr','$tmonth','$new_rec','$branch','$business','$id_user','$purchase_date')");
    }
       ?>

          <meta http-equiv="refresh" content="0;URL='process_purchasedetails.php?purchase_date=<?php echo$purchase_date?>'" />
        <?php
} else {
    echo "Error: " . $sql1 . "<br>" . mysqli_error($conn);
}

 }else{
  $sql1 = "INSERT INTO purchase_totals (invoice_number,quantity, total_price,paid,branch_id,business_id)
VALUES ('$invoice_number','$orders', '$total','$paid','$branch','$business')"; 
$sql_log=mysqli_query($con,$sql1);
if ($sql_log) {

$paym=mysqli_fetch_array(mysqli_query($con,"select max(id) as id from purchase_totals where branch_id='$branch'"));
$purchase_id=$paym['id'];
$Check_amountpaid=mysqli_fetch_array(mysqli_query($con,"select paid from purchase_totals where id ='$purchase_id'"));
$pd=$Check_amountpaid['paid'];
if ($pd>0) {
  

 $tyr = date("Y");
 $tmonth = date("m");
        $sql13 = mysqli_query($con,"SELECT max(voucher) as recno from purchase_payments where year='$tyr' && month='$tmonth' && branch_id='$branch'");
        if(mysqli_num_rows($sql13)>0){
            $row13=mysqli_fetch_array($sql13);
            $rec = $row13['recno'];
        }
        else{
            $rec=0;
        }
        $new_rec = $rec +1;
        if(strlen($new_rec)==1){
            $reciept = "000".$new_rec;
        }
        else if(strlen($new_rec)==2){
            $reciept = "00".$new_rec;
        }
        else if(strlen($new_rec)==3){
            $reciept = "0".$new_rec;
        }
        else{
            $reciept = $new_rec;
        }
        $voucher_no = $tyr.$tmonth.$branch.$reciept;
        $id_user=$_SESSION['id']; 
         $sql2 = mysqli_query($con,"INSERT INTO purchase_payments (purchase_id, amount_paid,voucher_no,year,month,voucher,branch_id,business_id,AddedBy) VALUES ('$purchase_id', '$paid','$voucher_no','$tyr','$tmonth','$new_rec','$branch','$business','$id_user')"); 

 }


   ?>

    <meta http-equiv="refresh" content="0;URL='process_purchasedetails.php'" />
    <?php 


} else {
    echo "Error: " . $sql1 . "<br>" . mysqli_error($conn);
}

}
//      $id_user=$_SESSION['id'];
//   $activity="Bought ".$orders." items at Ushs.".$total;
//    $log_sql=mysqli_query($con,"INSERT INTO logs(user_id, activity) VALUES 
// ('$id_user','$activity')");



mysqli_close($conn);
?>
