
<?php
session_start();
include '../includes/connection.php';
        
$business=$_SESSION['business_id'];
$branch=$_SESSION['branch_id'];
$u_id=$_SESSION['id'];

$sql = "SELECT product_id, quantity, business_id from getstore where  user_id='$u_id'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    $id_user=$_SESSION['id'];
     
    while($row = mysqli_fetch_assoc($result)) {
        $product_id=$row['product_id'];
        $quantity=$row['quantity'];
        
        $user_id=$_SESSION['id'];
        
        $bss_id=$row['business_id'];
        


        $tlogsql=mysqli_fetch_array(mysqli_query($con,"select * from stores where id='$product_id'"));
        $pname=$tlogsql['product_name'];
//insert data
  
$sql2 = "INSERT INTO storedetails (product_id, quantity,business_id, AddedBy)
VALUES ('$product_id', '$quantity','$bss_id','$user_id')";

   $sqlps = mysqli_query($con,"UPDATE stores set quantity=quantity+$quantity where id='$product_id' && business_id='$business' ");



$action=" Purchased product with details"."<br> product Name: ".$pname."<br> product quantity: ".$quantity;
 $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 getstore where  user_id='$u_id'";

if (mysqli_query($conn, $sql)) {
    
           echo "<script> window.location='add_tostore.php'</script>"; 
       
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>