<?php 
ob_start();
session_start();
include "../config/config.php";
$session_id = $_SESSION['spm_gold']['id'];
$today_date = date('Y-m-d');

if(isset($_POST['type']) && $_POST['type']=='purchase_return'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $purchase_id = $_POST['ids'];
        $return_status = 3;
        
        
$statement = $pdo->prepare("SELECT * FROM tbl_barcode_items WHERE barcode_id=?");
$result = $statement->execute(array($purchase_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();


if($totalData>0){
    foreach($query as $res){
        
        $barcode_items_id = $res['id'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_product_variants WHERE barcode_items_id=?");
        $result = $statement->execute(array($barcode_items_id)); 
        $query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalData = $statement->rowCount();
        if($totalData>0){
            foreach($query as $pro_variant_id){
                $quantity = $pro_variant_id['quantity'];
                $purchase_quantity = $pro_variant_id['purchase_quantity'];
                $sales_status = $pro_variant_id['sales_status'];
                
                if($sales_status==0){
                    $result="FALSE";
                }else{
                    $result="TRUE";
                }
            }
        }else{
                    $result="TRUE";
        }
        
        if($result=="FALSE"){
            $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "Purchase Return is Failed! Because Purchase Items are Saled...";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
        }
        
        
        
    }
}
        
        
        
    if($result=="TRUE"){
         $statement = $pdo->prepare("UPDATE `tbl_purchases` SET `status`=? WHERE id=?");
         $results = $statement->execute(array($return_status, $purchase_id));
    }
        
        
         
         
         
        
        
        
    
        if($results){    
            $pdo->commit();
            $return_arr['message']='Purchase Return Completed';
            $return_arr['status']=200;
        } 
        else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
    }catch(PDOException $e){
        $pdo->rollBack();
        error_log($e);
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}




if(isset($_POST['type']) && $_POST['type']=='sales_return'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $sales_id = $_POST['ids'];
        $return_status = 3;
        
        
$statement = $pdo->prepare("SELECT * FROM tbl_sale_items WHERE sale_id=?");
$result = $statement->execute(array($sales_id)); 
$query = $statement->fetchAll(PDO::FETCH_ASSOC);
$totalData = $statement->rowCount();

if($totalData>0){
    foreach($query as $res){
        
        $sale_items_id = $res['id'];
        $product_id = $res['product_id'];
        
        $statement = $pdo->prepare("SELECT * FROM tbl_product_variants WHERE id=?");
        $result = $statement->execute(array($product_id)); 
        $query = $statement->fetchAll(PDO::FETCH_ASSOC);
        $totalData = $statement->rowCount();
        if($totalData>0){
            foreach($query as $pro_variant_id){
                
                $product_variant_id = $pro_variant_id['id'];
                $quantity = $pro_variant_id['quantity'];
                $purchase_quantity = $pro_variant_id['purchase_quantity'];
                $sales_status = 1;
                
                
                $statement = $pdo->prepare("UPDATE `tbl_product_variants` SET `status`=? WHERE id=?");
                $result = $statement->execute(array($return_status, $product_variant_id));

            }
        }
    }
}
        
        
         $statement = $pdo->prepare("UPDATE `tbl_sales` SET `status`=? WHERE id=?");
         $result = $statement->execute(array($return_status, $sales_id));
        
        
        
        if($result){    
            $pdo->commit();
            $return_arr['message']='Sales Return Completed';
            $return_arr['status']=200;
        } 
        else{
            $return_arr['message']='Something Went Wrong..';
            $return_arr['status']=400;
        }
    }catch(PDOException $e){
        $pdo->rollBack();
        error_log($e);
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}

?>