<?php
session_start();
include 'load.php';
$branch_id = $_SESSION['branch_id'];
if(isset($_POST['orderid'])){
    $sql = "DELETE FROM temp_return_qty";
    $conn->query($sql);
    $orderid = $_POST['orderid'];


    $orderitems = mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE orderid='$orderid' AND orderid !='0' AND pro_qty!='0'");

    if ($orderitems->num_rows > 0) {
        while($row = $orderitems->fetch_assoc()) {
            $productid = $row['proid'];
            $return_qty = $row['pro_qty'];
            $return_type = 1;
            $return_reson = "";
            $pro_price = $row['pro_dis_price'];
            $return_status = 0;
            $order_item_id = $row['tempid'];
            $oid = $row['orderid'];
            $sql1 = "INSERT INTO temp_return_qty (order_id, product_id, return_qty, return_type, return_reson, return_amount, return_status, order_item_id,branch_id)VALUES ('$oid', '$productid', '$return_qty', '$return_type', '$return_reson', '$pro_price', '$return_status', '$order_item_id','$branch_id')";
            $conn->query($sql1);
        }
    }
    

    $_SESSION['order_id'] =  $_POST['orderid'];




}

if(isset($_POST['order_item_id']) && isset($_POST['qty'])){

   
    $order_item_id = $_POST['order_item_id'];
    $qty = $_POST['qty'];

    $orderitemsdetails = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobiletemporarycart WHERE tempid='$order_item_id'"));
    $return1 = mysqli_fetch_array(mysqli_query($conn, "SELECT SUM(qty) as qt FROM tbl_return_items WHERE item_id ='$order_item_id'"));
    if(!empty($return1)){
        $rqty = $return1['qt'];
    }else{
        $rqty = 0;
    }
    
    $order_id = $orderitemsdetails['orderid'];
    $orderinfo = mysqli_fetch_array(mysqli_query($conn, "SELECT * FROM mobileorders WHERE order_id ='$order_id'"));
    $orderamount = $orderinfo['amount'];
    $orderdiscount_type = $orderinfo['discount_type'];
    $orderdall_discount = $orderinfo['all_discount']; 
        if($orderdall_discount!=0){
           $disval =  ($orderitemsdetails['pro_dis_price'] / 100) * $orderdall_discount;
            $dis = $orderitemsdetails['pro_dis_price'] - $disval;

        }else{
            $dis = $orderitemsdetails['pro_dis_price'];
        }


       // $pro_price = $orderitemsdetails['pro_dis_price'];
       $pro_price = $dis;
    $orderqty = $orderitemsdetails['pro_qty'];
    $allqty = $rqty + $qty;
    $price = $pro_price * $qty;

    
    if($orderqty >= $allqty){                                                
        $sql = "UPDATE temp_return_qty SET return_qty='$qty',return_amount='$price' WHERE order_item_id='$order_item_id'";
        $conn->query($sql);
        echo $price;
    }else{
        echo 0;
    }
    

}



if(isset($_POST['order_item_id']) && isset($_POST['return_type'])){
    $order_item_id = $_POST['order_item_id'];
    $return_type = $_POST['return_type'];

    $sql = "UPDATE temp_return_qty SET return_type='$return_type' WHERE order_item_id='$order_item_id'";
    $conn->query($sql);
}


if(isset($_POST['order_item_id']) && isset($_POST['return_reson'])){
    $order_item_id = $_POST['order_item_id'];
    $return_reson = $_POST['return_reson'];

    $sql = "UPDATE temp_return_qty SET return_reson='$return_reson' WHERE order_item_id='$order_item_id'";
    $conn->query($sql);
}

if(isset($_POST['order_item_id']) && isset($_POST['return_status'])){
    $order_item_id = $_POST['order_item_id'];
    $return_status = $_POST['return_status'];

    $sql = "UPDATE temp_return_qty SET add_to_return='$return_status' WHERE order_item_id='$order_item_id'";
    $conn->query($sql);
}



?>