
<?php
session_start();
include '../includes/connection.php';
$staffcode=$_GET['staffcode'];
$id_sale=$_GET['id'];

$id_user=$_SESSION['id'];
$branch=$_SESSION['branch_id'];
$business=$_SESSION['business_id'];
include '../settings/check_business_settings.php';

$sql_rec=mysqli_fetch_array(mysqli_query($con,"SELECT * from business where business_id='$business'"));
$receipt_type=$sql_rec['receipt_type'];

$sql = "SELECT (select max(id) from sales)as id, product_id, (select buying_price from stock where product_name=temp.product_id ORDER BY id DESC limit 1)as buying_price, unit_price as selling_price, quantity, business_id,saletype,taken from temp where branch_id='$branch' AND user='$id_user'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result) > 0) {
    // output data of each row
    while($row = mysqli_fetch_assoc($result)) {
        $product_id=$row['product_id'];
        $quantity=$row['quantity'];
        $user_id=$_SESSION['id'];
        $order_id=$id_sale;
        $bss_id=$row['business_id'];
        $buying_price=$row['buying_price'];
        $selling_price=$row['selling_price'];
        $saletype=$row['saletype'];
        $taken=$row['taken'];
        $quantity_remaining=$quantity-$taken;
        $tlogsql=mysqli_fetch_array(mysqli_query($con,"select * from products where id='$product_id'"));
        $pname=$tlogsql['product_name'];
        $has_stock=$tlogsql['has_stock'];
//insert data
            if ($saletype==1) {
                $sst="Wholesale";
            }else{
                $sst="Retail";
            }
            $date=date('Y-m-d');
            $ttl=$selling_price*$quantity;
    if (isset($_GET['sale_date'])) {
                $sale_date=$_GET['sale_date'];
        $sql2 = "INSERT INTO sales_details (product_id, quantity,quantity_remaining,buying_price, selling_price, user_id, order_id,branch_id,business_id,date_time) VALUES ('$product_id', '$quantity','$quantity_remaining','$buying_price','$selling_price', '$user_id', '$order_id','$branch','$business','$sale_date')";
    }else{
        $sql2 = "INSERT INTO sales_details (product_id, quantity,quantity_remaining,buying_price, selling_price, user_id, order_id,branch_id,business_id) VALUES ('$product_id', '$quantity','$quantity_remaining','$buying_price','$selling_price', '$user_id', '$order_id','$branch','$business')";
    }
    if ($has_stock==1) {
        $sql3="UPDATE products set quantity=quantity-$quantity WHERE id='$product_id'";
     $sql4="UPDATE stock set quantity=quantity-$quantity WHERE product_name='$product_id'";
    }
    else{
        $sql3="UPDATE products set quantity=quantity-0 WHERE id='$product_id'";
       $sql4="UPDATE stock set quantity=quantity-0 WHERE product_name='$product_id'";
    }

$action=$sst." sale: Sold product with details"."<br> product Name: ".$pname."<br> product Unit price: ".$selling_price."<br> product quantity: ".$quantity."<br> total_price: ".$selling_price*$quantity;
 $log_sql1=mysqli_query($con,"INSERT INTO logs(user_id, activity,branch_id,business_id) VALUES 
('$id_user','$action','$branch','$business')");
 $reason=$pname." sales";
 $log_sqtt=mysqli_query($con,"INSERT INTO transactions(money_in,reason,branch_id,business_id,date,added_by) VALUES 
('$ttl','$reason','$branch','$business','$date','$id_user')");

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

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

//mysqli_close($con);



//insert data

$notific=mysqli_fetch_assoc(mysqli_query($con,"SELECT quantity,min_quantity,(select product_name from products where id=stock.product_name)as pdname,(select has_stock from products where id=stock.product_name)as hastock from stock where product_name='$product_id' && branch_id='$branch'"));
$q=$notific['quantity'];
$m=$notific['min_quantity'];
$n=$notific['pdname'];
$hstock=$notific['hastock'];
if ($m>=$q) {
    if ($hstock==1) {
        $shortmessage="Product $n is getting out of Stock";
    $fullmessage="Product $n is getting out of Stock, the quantity remaining is $q , please restock";
    $notify=mysqli_query($con,"INSERT into notifications(shortmessage,fullmessage,branch_id,business_id) VALUES('$shortmessage','$fullmessage','$branch','$business')");
       
    }
    
}
    }
} else {
    echo "0 results";
}

//mysqli_close($conn);
?>




<?php
//delete everything from temp table

// Create connection

// sql to delete a record
$sql = "DELETE from temp where branch_id='$branch' && user='$id_user'";

if (mysqli_query($conn, $sql)) {

    $check1=mysqli_fetch_array(mysqli_query($conn,"SELECT print_after_sale from receipt_settings where branch_id='$branch'"));
        $svalue=$check1['print_after_sale'];
        if ($svalue==0) {
           ?>
           <meta http-equiv="refresh" content="0;URL='new_order.php'" />
           <?php 
        }else{
            $chpaid=mysqli_fetch_array(mysqli_query($con,"SELECT max(id) as mid from sales where branch_id='$branch'"));
    $mid=$chpaid['mid'];
    $ccredit1=mysqli_fetch_array(mysqli_query($con,"SELECT paid  from sales where id ='$mid'"));
    $paid=$ccredit1['paid'];
    if ($paid<=0) {
        if ($gbusiness_settings['print_order_bills']==1) {
            if ($receipt_type==1) {
           ?>
    <meta http-equiv="refresh" content="0;URL='print_receipta4.php?staffcode=<?php echo $staffcode; ?>'" />

           <?php
        }else{
            ?>
    <meta http-equiv="refresh" content="0;URL='print_receipt.php?staffcode=<?php echo $staffcode; ?>'" />


    <?php   

        }
        }else{
          ?>
        <meta http-equiv="refresh" content="0;URL='new_order.php'" />
       <?php  
        }
       
    }else{
        if ($receipt_type==1) {
           ?>
    <meta http-equiv="refresh" content="0;URL='print_receipta4.php?staffcode=<?php echo $staffcode; ?>'" />

           <?php
        }else{
            ?>
    <meta http-equiv="refresh" content="0;URL='print_receipt.php?staffcode=<?php echo $staffcode; ?>'" />


    <?php   

        }
            
           
        
    
}
    }
} else {
    echo "Error deleting record: " . mysqli_error($conn);
}

mysqli_close($conn);
?>