
<?php
session_start();
include '../includes/connection.php';
$id_user=$_SESSION['id'];       
$business=$_SESSION['business_id'];
//$branch=$_SESSION['branch_id'];
$sql = "SELECT branch_id as bid,(select max(id) from purchase_totals where branch_id=bid)as id, product_id, quantity, unitprice, supplier, expiry_date, batch_no, business_id from getstock where business_id='$business' and user_id='$id_user'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    
     
    while($row = mysqli_fetch_assoc($result)) {
        $branch = $row['bid'];
        $product_id=$row['product_id'];
        $quantity=$row['quantity'];
        $unitprice=$row['unitprice'];
        $user_id=$_SESSION['id'];
        $purchase_id=$row['id'];
        $supplier=$row['supplier'];
        $expiry_date=$row['expiry_date'];
        $batch_no=$row['batch_no'];
        $bss_id=$row['business_id'];
        $total_price=$quantity * $unitprice;

        $tlogsql=mysqli_fetch_array(mysqli_query($con,"select * from products where id='$product_id'"));
        $pname=$tlogsql['product_name'];
//insert data
  
$sql2 = "INSERT INTO purchases (product_id, quantity,unit_price,total_price,branch_id,business_id,expiry_date,batch_no, AddedBy, purchase_id,supplier)
VALUES ('$product_id', '$quantity','$unitprice','$total_price','$branch','$bss_id','$expiry_date','$batch_no','$user_id', '$purchase_id','$supplier')";
$sqs = mysqli_fetch_array(mysqli_query($con,"select * from stock where product_name='$product_id'"));
$bprice=$sqs['buying_price'];
$quan=abs($sqs['quantity']);
$ntprice=$unitprice*$quantity;

 $nquant=(int)$quan+(int)$quantity;
$average=round((($ntprice +$bprice)/$nquant),3);
$sqlst = mysqli_query($con,"update stock set quantity='$nquant',buying_price='$average' where product_name='$product_id' ");
$sqlps = mysqli_query($con,"update products set quantity='$nquant' where id='$product_id' ");

$action=" Purchased product with details"."<br> product Name: ".$pname."<br> product Unit price: ".$unitprice."<br> product quantity: ".$quantity."<br> total_price: ".$unitprice*$quantity."<br> product supplier: ".$supplier;
 $log_sql1=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$action','$branch','$business')");

if (mysqli_query($con, $sql2)) {
  //  echo "New record created successfully";
} else {
    echo "Error: " . $sql2 . "<br>" . mysqli_error($con);
}

//mysqli_close($con);



//insert data


    }
} else {
    echo "0 results";
}

//mysqli_close($conn);
?>




<?php
//delete everything from temp table

// Create connection

// sql to delete a record
$sql = "DELETE from getstock where branch_id='$branch'";

if (mysqli_query($conn, $sql)) {
    $ccredit=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from purchase_totals where branch_id='$branch'"));
    $mid=$ccredit['mid'];
    $ccredit1=mysqli_fetch_array(mysqli_query($con,"SELECT paid  from purchase_totals where id ='$mid'"));
    $paid=$ccredit1['paid'];
    if ($paid<=0) {
        echo "<script> window.location='new_purchase.php'</script>"; 
    }else{
        
        $check1=mysqli_fetch_array(mysqli_query($conn,"SELECT print_after_purchase from receipt_settings where branch_id='$branch'"));
        $pvalue=$check1['print_after_purchase'];
        if ($pvalue==0) {
           echo "<script> window.location='new_purchase.php'</script>"; 
        }else{
           
          
            
        
       
    $rec =mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as id from purchase_payments where branch_id='$branch'"));
    $rid=$rec['id'];
    $vn=mysqli_fetch_array(mysqli_query($con,"SELECT * from purchase_payments where id='$rid'"));
    $voucher=$vn['voucher_no'];
   echo "<script> window.location='voucher2.php?voucher=".$voucher."&&branch=".$branch."';</script>";
   
  } 
} 
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>