<?php 
ob_start();
session_start();
include "../config/config.php";
include "../config/session.php";


if(isset($_POST['type']) && $_POST['type']=='create_stock_transfer'){
     try{
         
         $pdo->beginTransaction(); 
         
        $date = $_POST['date'];
        $reference_no = $_POST['reference_no'];
        $transfer_status = $_POST['transfer_status'];
        $sales_man_code = $_POST['sales_man_code'];
        $from_location_id = $_POST['from_location_id'];
        $to_location_id = $_POST['to_location_id'];
        $total_amount = $_POST['total_amount'];
        $paid = 0;
        
        
        $unique_id = "10000";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_stock_transfer'");
        $statement->execute();
        $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($memeber_id_result as $result) {
	        $unique_id = $unique_id + $result['Auto_increment'];
        }
        $unique_id = "ST_".$unique_id; 
        
        
        if ($from_location_id==$to_location_id){

                $return_arr['status']=400;
                $return_arr['error'] = true;
                $return_arr['message'] = "Can't Transfer Same Locations";
                print_r(json_encode($return_arr));
                return false;
                exit(); 
        }
        
        
    
        
        $statement = $pdo->prepare("INSERT INTO `tbl_stock_transfer`(`date`, `reference_no`, `transfer_status`, `grand_total`, `paid`, 
                                    `from_location`, `to_location`,
                                    `created_by`, `updated_by`, `created_at`, `updated_at`) VALUES (?,?,?,?,?,
                                    ?,?,
                                    ?,?,?,?)");
        $result = $statement->execute(array($date, $reference_no, $transfer_status, $total_amount, $paid,
                                    $from_location_id, $to_location_id,
                                    $session_id, 0, $updated_at, $updated_at)); 
        
        
        if($result){  
            
            
        $stock_transfer_id = $pdo->lastInsertId(); 
            
            
            
            
            
        if(isset($_POST['product_id']) && count($_POST['product_id'])>0){
               for($pro=0;$pro<count($_POST['product_id']);$pro++){
    	
    	            $product_id = (isset($_POST['product_id'][$pro])) ? $_POST['product_id'][$pro] : "";
    	            $product_name = (isset($_POST['product_name'][$pro])) ? $_POST['product_name'][$pro] : "";
    	            $price = (isset($_POST['amount'][$pro])) ? $_POST['amount'][$pro] : "";
    	            $product_name = (isset($_POST['sub_total'][$pro])) ? $_POST['sub_total'][$pro] : "";
    	            $quantity = (isset($_POST['quantity'][$pro])) ? $_POST['quantity'][$pro] : "";
    	            
    	            // Product Price & Stone Price
    	            $product_gm_amount = (isset($_POST['product_gm_amount'][$pro])) ? $_POST['product_gm_amount'][$pro] : "0";
    	            $stone_gm_amount = (isset($_POST['stone_gm_amount'][$pro])) ? $_POST['stone_gm_amount'][$pro] : "0";
    	            
    	            // Others Product Details
    	            $sub_total = (isset($_POST['sub_total'][$pro])) ? $_POST['sub_total'][$pro] : "0";
    	            $wastage_amount = (isset($_POST['wastage_amount'][$pro])) ? $_POST['wastage_amount'][$pro] : "0";
    	            
    	            $product_cost = (isset($_POST['gold_amount'][$pro])) ? $_POST['gold_amount'][$pro] : "0";
    	            $stone_amount = (isset($_POST['stone_amount'][$pro])) ? $_POST['stone_amount'][$pro] : "0";
    	            
    	            
    	            
    	            
    	            
    	            $statement_products = $pdo->prepare("SELECT * FROM tbl_product_variants e where e.id=?");
                    $statement_products->execute(array($product_id));
                    $products_datas = $statement_products->fetchAll(PDO::FETCH_ASSOC);
                    $products_totalData = $statement_products->rowCount();
                    if($products_totalData>0){
                        $pro_ids = $products_datas[0]['pro_ids'];
        	            $category = $products_datas[0]['category'];
        	            $sub_category = $products_datas[0]['sub_category'];
        	            $hsn_code = $products_datas[0]['hsn_code'];
        	            $type = $products_datas[0]['type'];
        	            $unit = $products_datas[0]['unit'];
        	            $cross_weight = $products_datas[0]['cross_weight'];
        	            $stone_weight = $products_datas[0]['stone_weight'];
        	            $net_weight = $products_datas[0]['net_weight'];
        	            $stone_cost = $products_datas[0]['stone_cost'];
        	            $purity = $products_datas[0]['purity'];
        	            $wastage = $products_datas[0]['wastage'];
        	            $others_weight = $products_datas[0]['others_weight'];
        	            $purchase_price = $products_datas[0]['purchase_price'];
                    }
    	            
    	            
    	            
    	            
    	            
    	            
    	            $statement = $pdo->prepare("INSERT INTO `tbl_stock_transfer_items`(`product_id`, `transfer_id`, `amount`, `updated_at`) VALUES (?,?,?,?)");
                    $result = $statement->execute(array($product_id, $stock_transfer_id, $sub_total, $updated_at)); 
                    
                    $statement = $pdo->prepare("UPDATE tbl_product_variants SET location_id=? WHERE id=?");
                    $result = $statement->execute(array($to_location_id, $product_id)); 
                                    

    	            
    	            
        }
                
                
                

                
        }else{
            $pdo->rollBack();
            $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "No Products Found!";
            print_r(json_encode($return_arr));
            return false;
            exit();
        }
         
        

        
    
        }
        
        
        
        if($result){    
        
        
        
            

            $ids = $stock_transfer_id;
        	$message = "Stock Transfer - Invoice $unique_id Created Successfully";
            $category = "Stock Transfer";	

            
        
        
        $statement = $pdo->prepare("INSERT INTO `tbl_notifications`(`message`, `ids`, `date`, `user_id`, `category`) VALUES (?,?,?,?,?)");
        $result = $statement->execute(array($message, $ids, $today_date, $session_id, $category));
        
        
            $pdo->commit();
            $return_arr['message']='Stock Transfer Added Successfully...';
            $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);
}





?>