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


if(isset($_POST['type']) && $_POST['type']=='create_sales'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $customer_id = $_POST['customer_id'];
        $invoice_no = $_POST['invoice_no'];
        $date = $_POST['date'];
        $total_cross_weight = 0;
        $total_stone_weight = 0;
        $total_net_weight = 0;
        $total_amount = $_POST['total_amount'];
        $gst_amount = $_POST['gst_or_igst_amount'];
        $sales_status = $_POST['sales_status'];
        
        $paid_amount = $_POST['paid_amount'];
        $grand_amount = $_POST['grand_amount'];
        $created_user_id = $_POST['created_user_id'];
        
        $gst_percentage = $_POST['gst_percentage'];
        $tax_type = $_POST['tax_type'];
        $gst_type = $_POST['gst_type'];
        $tax_after_total = $_POST['tax_after_total'];
        
        $discount_type = $_POST['discount_type'];
        $discount = $_POST['discount'];
        $old_product_amount = $_POST['old_product_amount'];
        
        
        //Paid Amount Come From
        $payment_scheme_used = $_POST['payment_scheme_used'];
        $weight_scheme_used = $_POST['weight_scheme_used']; 
        $advance_used = $_POST['advance_used'];
        
        // $payment_scheme_ids = isset($_POST['payment_scheme_ids']) ? : '';
        // $weight_scheme_ids = isset($_POST['weight_scheme_ids'])  ? $_POST['weight_scheme_ids'] : ''; 
        // $advance_ids = isset($_POST['advance_ids']) ? $_POST['advance_ids'] : '';
        if(isset($_POST['payment_scheme_ids'])){
            $payment_scheme_ids = implode(',', $_POST['payment_scheme_ids']);
        }
        if(isset($_POST['weight_scheme_ids'])){
            $weight_scheme_ids = implode(',', $_POST['weight_scheme_ids']);
        }
        if(isset($_POST['advance_ids'])){
            $advance_ids = implode(',', $_POST['advance_ids']);
        }
        
        
         
        if($discount_type==1){
            $discount_amount = $_POST['discount_amount'];
        }else{
            $discount_amount = $_POST['discount'];
        }
        
        $location_id = $_POST['location_id'];
        
        
        
        $unique_id = "10000";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_sales'");
        $statement->execute();
        $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($memeber_id_result as $result) {
	        $unique_id = $unique_id + $result['Auto_increment'];
        }
        $current_date = date("Ymd");
        
        $unique_id = "SPM_".$current_date."_".$unique_id;
        
        
        // Daily New ID Starts Here 
        $statements = $pdo->prepare("SELECT * FROM tbl_sales WHERE date=? and location_id=?");
        $statements->execute(array($date, $location_id));
        $daily_advamce_results = $statements->fetchAll(PDO::FETCH_ASSOC);
        $totalDatas = $statements->rowCount();
        
        $daily_id = $totalDatas+1;
        // Daily New nID 
        $daily_unique_id = "SPM_SA_ET_".$daily_id;
        
    
        
        $statement = $pdo->prepare("INSERT INTO `tbl_sales`(`customer_id`, `date`, `invoice_no`, `total_cross_weight`, `total_amount`, 
                        `gst_percentage`, `discount_amount`, `grand_total`, `paid`, `created_by`,
                        `tax_type`, `gst_type`, `total_amount_after_tax`, `discount_type`, `discount_per_or_amt`,
                        `total_stone_weight`, `total_net_weight`, `status`, `old_product_amount`, `gst_amount`, 
                        `created_user_id`, `sales_id`, `location_id`, `esti_unique_no`, `payment_scheme_ids`, `weight_scheme_ids`, `advance_ids`) VALUES (?,?,?,?,?,
                        ?,?,?,?,?,
                        ?,?,?,?,?,
                        ?,?,?,?,?,
                        ?,?,?,?,?,?,?)");
        $result = $statement->execute(array($customer_id, $date, $unique_id, $total_cross_weight, $total_amount, 
                        $gst_percentage, $discount_amount, $grand_amount, $paid_amount,  $session_id,
                        $tax_type, $gst_type, $tax_after_total, $discount_type, $discount,
                        $total_stone_weight, $total_net_weight, $sales_status, $old_product_amount, $gst_amount, 
                        $created_user_id, $unique_id, $location_id, $daily_unique_id, $payment_scheme_ids, $weight_scheme_ids, $advance_ids)); 
        
        
        if($result){  
            
            
        $sales_id = $pdo->lastInsertId(); 
        
        // update scheme or advance table in sale id
        
        if($payment_scheme_used == '1'){
            
        
            $payment_scheme_ids_array = array_unique(explode(',', $payment_scheme_ids));
            
            // Create placeholders for the query based on the number of payment_scheme_ids
            $payment_placeholders = implode(',', array_fill(0, count($payment_scheme_ids_array), '?'));
            
            // Prepare the SQL statement with the IN clause
            $statement = $pdo->prepare("UPDATE `tbl_customer_scheme` SET `sale_id` = ? WHERE `id` IN ($payment_placeholders) AND type=1");
            
            // Create the values array for the query execution
            $values = array_merge([$sales_id], $payment_scheme_ids_array); // Sale ID comes first, then payment_scheme_ids
            
            // Execute the statement with the values
            $result = $statement->execute($values);
            
            
            
        }elseif($weight_scheme_used == '1'){
            
            $weight_scheme_ids_array = array_unique(explode(',', $weight_scheme_ids));
            
            // Create placeholders for the query based on the number of payment_scheme_ids
            $weight_placeholders = implode(',', array_fill(0, count($weight_scheme_ids_array), '?'));
            
            // Prepare the SQL statement with the IN clause
            $statement = $pdo->prepare("UPDATE `tbl_customer_scheme` SET `sale_id` = ? WHERE `id` IN ($weight_placeholders) AND type=2");
            
            // Create the values array for the query execution
            $values = array_merge([$sales_id], $weight_scheme_ids_array); // Sale ID comes first, then payment_scheme_ids
            
            // Execute the statement with the values
            $result = $statement->execute($values);
            
            
            
        }elseif($advance_used == '1'){
           
            // Convert comma-separated string into an array and then use `array_unique` to remove duplicates
            $advance_ids_array = array_unique(explode(',', $advance_ids));
            
            // Create placeholders for the query based on the number of advance_ids
            $placeholders = implode(',', array_fill(0, count($advance_ids_array), '?'));
            
            // Prepare the SQL statement with the IN clause
            $statement = $pdo->prepare("UPDATE `tbl_advance_voucher` SET `sale_id` = ? WHERE `id` IN ($placeholders)");
            
            // Create the values array for the query execution
            $values = array_merge([$sales_id], $advance_ids_array); // Sale ID comes first, then advance_ids
            
            // Execute the statement with the values
            $result = $statement->execute($values);
            
            
        }
        
            
        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_sale_items`(`sale_id`, `pro_ids`, `product_id`, `product_name`, 
    	            `category`, `sub_category`, `hsn_code`, `type`, `unit`, `cross_weight`, `stone_weight`, `net_weight`, `stone_cost`, 
    	            `quantity`, `purity`, `wastage`, `others_weight`, `purchase_price`, `selling_price`,`created_by`,
    	            `per_gram_amount`,`per_stone_amount`, `total`, `wastage_amount`, `product_cost`) VALUES (?,?,?,?,
                        ?,?,?,?,?,?,?,?,?,
                        ?,?,?,?,?,?,?,
                        ?,?,?,?,?)");
                    $result = $statement->execute(array($sales_id, $pro_ids, $product_id, $product_name, 
                                    $category, $sub_category, $hsn_code, $type, $unit, $cross_weight, $stone_weight, $net_weight, $stone_cost,
                                    $quantity, $purity, $wastage, $others_weight, $purchase_price, $price, $session_id,
                    $product_gm_amount, $stone_gm_amount, $sub_total, $wastage_amount, $product_cost)); 
                                    
                                    
                    // Quantity - Minus Starts Here
                    if($sales_status==1){
                        
                        
                        /*$statement_product = $pdo->prepare("UPDATE `tbl_product_variants` SET `sales_status`=? WHERE id=?");
                        $product_result = $statement_product->execute(array(0, $product_id)); 
                        
                        
                        
                        $statement_product = $pdo->prepare("UPDATE `tbl_product_variants` SET `quantity`=quantity-".$quantity." WHERE id=?");
                        $product_result = $statement_product->execute(array($product_id)); */
                    }
                    // Quantity - Minus Endsss Here
    	            
    	            
                }
                
                
                
                
                for($p=0; $p<count($_POST['f_purity_id']); $p++){
                    
                    $purity_id = $_POST['f_purity_id'][$p];
                    $f_total_weight = $_POST['f_total_weight'][$p];
                    
                // Scheme & Non Scheme Starts Here
                    $scheme_wt_post = "scheme_weight_$purity_id";
                    $scheme_rate_post = "scheme_gold_rate_$purity_id";
                    $scheme_total_post = "scheme_total_$purity_id";
                    
                    $stone_post = "f_stone_$purity_id";
                    
                    $scheme_weight = $_POST[$scheme_wt_post];
                    $scheme_amount = $_POST[$scheme_rate_post];
                    $scheme_total = $_POST[$scheme_total_post];
                    
                    $stone_total = $_POST[$stone_post];
                    
                    $non_scheme_wt_post = "non_scheme_weight_$purity_id";
                    $non_scheme_rate_post = "non_scheme_gold_rate_$purity_id";
                    $non_scheme_total_post = "non_scheme_total_$purity_id";
                    
                    $non_scheme_weight = $_POST[$non_scheme_wt_post];
                    $non_scheme_amount = $_POST[$non_scheme_rate_post];
                    $non_scheme_total = $_POST[$non_scheme_total_post];
                // Scheme & Non Scheme Endsss Here
                
                    $gold_rate_post = "gold_rate_$purity_id";
                    $gold_amount_post = "f_gold_amount_$purity_id";
                    $wastage_amount_post = "f_wastage_$purity_id";
                    $grand_total_post = "f_grand_total_$purity_id";
                    

                    $gold_rate = $_POST[$gold_rate_post];
                    $gold_amount = $_POST[$gold_amount_post];
                    $wastage_amount = $_POST[$wastage_amount_post];
                    $grand_total = $_POST[$grand_total_post];
                    
                    
                    $statement = $pdo->prepare("INSERT INTO `tbl_sales_totals`(`sale_id`, `date`, `purity_id`, `rate`, 
                                            `net_weight`, 
                                            `scheme_wt`, `scheme_amount`, `scheme_total`, 
                                            `non_scheme_wt`, `non_scheme_amount`, `non_scheme_total`, 
                                            `gold_amount`, `wastage_amount`, `grand_total`, `stone_amount`) VALUES (?,?,?,?,
                                            ?,
                                            ?,?,?,
                                            ?,?,?,
                                            ?,?,?,?)");
                    
                    $result = $statement->execute(array($sales_id, $today_date, $purity_id, $gold_rate,
                                            $f_total_weight,
                                            $scheme_weight, $scheme_amount, $scheme_total,
                                            $non_scheme_weight, $non_scheme_amount, $non_scheme_total,
                                            $gold_amount, $wastage_amount, $grand_total, $stone_total)); 
                
                    
                    
                    
                    
                }
                
                
                
                
                
                
                
                
                
                
        }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(isset($_POST['old_product_ids']) && count($_POST['old_product_ids'])>0){
                
    	        for($old=0; $old<count($_POST['old_product_ids']); $old++){
    	
    	            $old_product_ids = (isset($_POST['old_product_ids'][$old])) ? $_POST['old_product_ids'][$old] : "";
    	            $old_purity = (isset($_POST['old_purity'][$old])) ? $_POST['old_purity'][$old] : "";
    	            $old_unit = (isset($_POST['old_unit'][$old])) ? $_POST['old_unit'][$old] : "";
    	            $old_weight = (isset($_POST['old_weight'][$old])) ? $_POST['old_weight'][$old] : "";
    	            $old_price = (isset($_POST['old_price'][$old])) ? $_POST['old_price'][$old] : "";
    	            $old_sub_total = (isset($_POST['old_sub_total'][$old])) ? $_POST['old_sub_total'][$old] : "";
    	            $old_product_type = (isset($_POST['old_product_type'][$old])) ? $_POST['old_product_type'][$old] : "";
    	            
    	            
    	            
    	            
    	            
    	            $statement = $pdo->prepare("INSERT INTO `tbl_sale_old_items`(`sale_id`, `pro_ids`, `purity`, `unit`, 
                                	            `weight`, `price`, `total`, `type`) VALUES (?,?,?,?,
                                                    ?,?,?,?)");
                    $result = $statement->execute(array($sales_id, $old_product_ids, $old_purity, $old_unit, 
                                                $old_weight, $old_price, $old_sub_total, $old_product_type)); 
    	            
    	            
                }
                
                
        }
        
    
        }
        if($result){    
        
        
        
            
        if($sales_status==1){
            $ids = $sales_id;
            $invoice = $sales_id;
        	$message = "Sales - Invoice $invoice Created";
            $category = "Sales";	
        }else if($sales_status==2){
            $ids = $sales_id;
            $invoice = $sales_id;
            $message = "Quotation - Invoice $invoice Created";
            $category = "Quotation";
        
        }
            
        
        
        $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']='Sales Added Successfully...';
            $return_arr['sales_id'] = $sales_id;
            $return_arr['type'] = $sales_status;
            $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']=='update_sales'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $ids = $_POST['ids'];
        $customer_id = $_POST['customer_id'];
        $invoice_no = $_POST['invoice_no'];
        $date = $_POST['date'];
        $total_cross_weight = 0;
        $total_stone_weight = 0;
        $total_net_weight = 0;
        $total_amount = $_POST['total_amount'];
        $gst_amount = $_POST['gst_or_igst_amount'];
        $sales_status = $_POST['sales_status'];
        
        $grand_amount = $_POST['grand_amount'];
        $created_user_id = $_POST['created_user_id'];
        
        $gst_percentage = $_POST['gst_percentage'];
        $tax_type = $_POST['tax_type'];
        $gst_type = $_POST['gst_type'];
        $tax_after_total = $_POST['tax_after_total'];
        
        $discount_type = $_POST['discount_type'];
        $discount = $_POST['discount'];
        $old_product_amount = $_POST['old_product_amount'];
         
         
        if($discount_type==1){
            $discount_amount = $_POST['discount_amount'];
        }else{
            $discount_amount = $_POST['discount'];
        }
        
        $unique_id = "10000";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_sales'");
        $statement->execute();
        $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($memeber_id_result as $result) {
	        $unique_id = $unique_id + $result['Auto_increment'];
        }
        $current_date = date("Ymd");
        
        $unique_id = "SPM_".$current_date."_".$unique_id; 
        
    
        
        $statement = $pdo->prepare("UPDATE `tbl_sales` SET `customer_id`=?, `date`=?,  `total_cross_weight`=?, `total_amount`=?, 
                        `gst_percentage`=?, `discount_amount`=?, `grand_total`=?, `created_by`=?,
                        `tax_type`=?, `gst_type`=?, `total_amount_after_tax`=?, `discount_type`=?, `discount_per_or_amt`=?,
                        `total_stone_weight`=?, `total_net_weight`=?, `status`=?, `old_product_amount`=?, `gst_amount`=?, 
                        `created_user_id`=? WHERE id=?");
        $result = $statement->execute(array($customer_id, $date, $total_cross_weight, $total_amount, 
                        $gst_percentage, $discount_amount, $grand_amount, $session_id,
                        $tax_type, $gst_type, $tax_after_total, $discount_type, $discount,
                        $total_stone_weight, $total_net_weight, $sales_status, $old_product_amount, $gst_amount, 
                        $created_user_id, $ids)); 
        
        
    if($result){  
            
            
            $sales_id = $ids; 
                
            if(isset($_POST['product_id']) && count($_POST['product_id'])>0){
                    
                    
                    
        	        for($pro=0; $pro<count($_POST['product_id']); $pro++){
        	            
        	            
        	            $sale_present_or_not = (isset($_POST['sale_present_or_not'][$pro])) ? $_POST['sale_present_or_not'][$pro] : "0";
        	            $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'];
                        }
        	            
        	            
        	            if($sale_present_or_not==0){
        	                
        	                $statement = $pdo->prepare("INSERT INTO  `tbl_sale_items`(`sale_id`, `pro_ids`, `product_id`, `product_name`, 
                                        	            `category`, `sub_category`, `hsn_code`, `type`, `unit`, `cross_weight`, `stone_weight`, `net_weight`, `stone_cost`, 
                                        	            `quantity`, `purity`, `wastage`, `others_weight`, `purchase_price`, `selling_price`,`created_by`,
                                        	            `per_gram_amount`,`per_stone_amount`, `total`, `wastage_amount`, `product_cost`) VALUES (?,?,?,?,
                                                            ?,?,?,?,?,?,?,?,?,
                                                            ?,?,?,?,?,?,?,
                                                            ?,?,?,?,?)");
                            $result = $statement->execute(array($sales_id, $pro_ids, $product_id, $product_name, 
                                                            $category, $sub_category, $hsn_code, $type, $unit, $cross_weight, $stone_weight, $net_weight, $stone_cost,
                                                            $quantity, $purity, $wastage, $others_weight, $purchase_price, $price, $session_id,
                                                            $product_gm_amount, $stone_gm_amount, $sub_total, $wastage_amount, $product_cost)); 
                                                        
        	            }else{
        	                
        	                $statement = $pdo->prepare("UPDATE   `tbl_sale_items` SET `sale_id`=?, `pro_ids`=?, `product_id`=?, `product_name`=?, 
                                        	            `category`=?, `sub_category`=?, `hsn_code`=?, `type`=?, `unit`=?, `cross_weight`=?, `stone_weight`=?, `net_weight`=?, `stone_cost`=?, 
                                        	            `quantity`=?, `purity`=?, `wastage`=?, `others_weight`=?, `purchase_price`=?, `selling_price`=?,`created_by`=?,
                                        	            `per_gram_amount`=?,`per_stone_amount`=?, `total`=?, `wastage_amount`=?, `product_cost`=? WHERE id=?");
                            $result = $statement->execute(array($sales_id, $pro_ids, $product_id, $product_name, 
                                                            $category, $sub_category, $hsn_code, $type, $unit, $cross_weight, $stone_weight, $net_weight, $stone_cost,
                                                            $quantity, $purity, $wastage, $others_weight, $purchase_price, $price, $session_id,
                                                            $product_gm_amount, $stone_gm_amount, $sub_total, $wastage_amount, $product_cost, $sale_present_or_not));
                                                            
        	            }
        	            
        	            
        	            
        	            
                                        
                                        
                        // Quantity - Minus Starts Here
                        if($sales_status==1){
                            
                            
                            /*$statement_product = $pdo->prepare("UPDATE `tbl_product_variants` SET `sales_status`=? WHERE id=?");
                              $product_result = $statement_product->execute(array(0, $product_id)); 
                            
                            
                            
                            $statement_product = $pdo->prepare("UPDATE `tbl_product_variants` SET `quantity`=quantity-".$quantity." WHERE id=?");
                            $product_result = $statement_product->execute(array($product_id)); */
                        }
                        // Quantity - Minus Endsss Here
        	            
        	            
                    }
                    
                    
            }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(isset($_POST['old_product_ids']) && count($_POST['old_product_ids'])>0){
                    
        	        for($old=0; $old<count($_POST['old_product_ids']); $old++){
        	
        	            $old_product_ids = (isset($_POST['old_product_ids'][$old])) ? $_POST['old_product_ids'][$old] : "";
        	            $old_purity = (isset($_POST['old_purity'][$old])) ? $_POST['old_purity'][$old] : "";
        	            $old_unit = (isset($_POST['old_unit'][$old])) ? $_POST['old_unit'][$old] : "";
        	            $old_weight = (isset($_POST['old_weight'][$old])) ? $_POST['old_weight'][$old] : "";
        	            $old_price = (isset($_POST['old_price'][$old])) ? $_POST['old_price'][$old] : "";
        	            $old_sub_total = (isset($_POST['old_sub_total'][$old])) ? $_POST['old_sub_total'][$old] : "";
        	            $old_product_type = (isset($_POST['old_product_type'][$old])) ? $_POST['old_product_type'][$old] : "";
        	            
        	            
        	            
        	            
        	            
        	            $statement = $pdo->prepare("INSERT INTO `tbl_sale_old_items`(`sale_id`, `pro_ids`, `purity`, `unit`, 
        	            `weight`, `price`, `total`, `type`) VALUES (?,?,?,?,
                            ?,?,?,?)");
                        $result = $statement->execute(array($sales_id, $old_product_ids, $old_purity, $old_unit, 
                                        $old_weight, $old_price, $old_sub_total, $old_product_type)); 
        	            
        	            
                    }
            
            }
        
    
    }
        
        
        
        if($result){    
        
        
        
            
            if($sales_status==1){
                $ids = $sales_id;
                $invoice = $sales_id;
            	$message = "Sales - Invoice $invoice Updated";
                $category = "Sales";	
            }else if($sales_status==2){
                $ids = $sales_id;
                $invoice = $sales_id;
                $message = "Quotation - Invoice $invoice Updated";
                $category = "Quotation";
            }
            
        
        
        $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']='Sales Updated Successfully...';
            $return_arr['sales_id'] = $sales_id;
            $return_arr['type'] = $sales_status;
            $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);
}

?>