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


if(isset($_POST['type']) && $_POST['type']=='create_purchase'){
     try{
         
         $pdo->beginTransaction(); 
         
        // Details Here
        $supplier_id = $_POST['supplier_id'];
        $invoice_no = $_POST['invoice_no'];
        $date = $_POST['date'];
        $total_cross_weight = $_POST['total_cross_weight'];
        $total_stone_weight = $_POST['total_stone_weight'];
        $total_net_weight = $_POST['total_net_weight'];
        $per_gm_stone_amount = $_POST['per_stone_amount'];
        $total_amount = $_POST['total_amount'];
        
        
        $grand_amount = $_POST['grand_amount'];
        
        $gst_amount = $_POST['gst_amount'];
        $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'];
        $business_locations = $_POST['business_locations'];
         
         
        if($discount_type==1){
            $discount_amount = $_POST['discount_amount'];
        }else{
            $discount_amount = $_POST['discount'];
        }
        
        $product_id = $_POST['product_ids'];
        
        $statement = $pdo->prepare("INSERT INTO `tbl_purchases`(`supplier_id`, `date`, `invoice_no`, `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`, `per_gm_stone_amount`, `gst_amount`, `location_id`, `product_id`) VALUES (?,?,?,?,?,
                        ?,?,?,?,
                        ?,?,?,?,?,
                        ?,?,?,?,?,?)");
        $result = $statement->execute(array($supplier_id, $date, $invoice_no, $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, $per_gm_stone_amount, $gst_amount, $business_locations, $product_id)); 
                        
                        
                        
        
        
        
        if($result){  
            
            
            $purchase_id = $pdo->lastInsertId(); 
             
            $purchase_items_count = count($_POST['product_id']);
            
            
            $statements = $pdo->prepare("INSERT INTO `tbl_barcode`(`barcode_date`, `purchase_id`, `purchase_items_count`, `created_by`) VALUES (?,?,?,?)");
            $results = $statements->execute(array($date, $purchase_id, $purchase_items_count, $session_id)); 
            
    
    
    
    
    
    
    
    
        if(count($_POST['product_id']) ==0){
            $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "Purchase Failed... Purchase Items Not Found!";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
        }
         
         
        for($r=0;$r<count($_POST['product_id']);$r++){
                    
                    
                    $product = $_POST['product'][$r];
                    $product_id = $_POST['product_id'][$r];
                    $product_name = $_POST['product_name'][$r];
                    $category = $_POST['category_name'][$r];
                    $product_type = $_POST['product_type'][$r];
                    $unit = $_POST['unit'][$r];
                    $touch = $_POST['touch'][$r];
                    $purity = $_POST['purity'][$r];
                    $purchase_order_items_id = $_POST['purchase_order_items_id'][$r];
                    $today_gram_amount = $_POST['today_gram_amount'][$r];
                    $total_pro_amount = $_POST['total_pro_amount'][$r];
                    $making_charges = $_POST['making_charges'][$r];
                    
        if($product_type==1){
            $cross_weight = $_POST['cross_weight'][$r];
            $stone_cost = 0;
            $stone_weight = 0;
            $net_weight = $cross_weight;
        }else{
            $cross_weight = $_POST['cross_weight'][$r];
            $stone_weight = $_POST['stone_weight'][$r];
            $net_weight = $_POST['net_weight'][$r];
            $stone_cost = $_POST['stone_cost'][$r];
        }
                        
          error_log("PRO TYPE: ".$product_type);
          error_log("PRO WT: ".$_POST['stone_weight'][$r]);
          
          
          
                    if($_POST['purchase_order_items_id'][$r]!=0){
                        // Update Query Here 
                        
                    }else{
                        // Insert Query Here 
                        $statement_purchase_items = $pdo->prepare("INSERT INTO `tbl_purchases_items` ( `purchase_id`, `product_name`, `category`, `product_type`, `unit`,
                                `cross_weight`, `stone_weight`, `net_weight`, `stone_cost`, `purity`,
                                `product_ids`, `per_gram_amount`, `total_amount`, `touch`, `making_charges`) VALUES (?,?,?,?,?,
                                ?,?,?,?,?,
                                ?,?,?,?,?)");
                                
                        $statement_purchase_items_result = $statement_purchase_items->execute(array($purchase_id, $product_name, $category, $product_type, $unit, 
                                $cross_weight, $stone_weight, $net_weight, $stone_cost, $purity,
                                $product, $today_gram_amount, $total_pro_amount, $touch, $making_charges)); 
                        
                    }
        }
        
         
        
        } 
         
    
        if($result){    
            $pdo->commit();
            $return_arr['message']='Purchase 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);
}










if(isset($_POST['type']) && $_POST['type']=='update_purchase'){
     try{
         
        $pdo->beginTransaction(); 
         
         
         
        // Details Here
        $ids = $_POST['ids'];
        $supplier_id = $_POST['supplier_id'];
        $invoice_no = $_POST['invoice_no'];
        $date = $_POST['date'];
        $total_cross_weight = $_POST['total_cross_weight'];
        $total_stone_weight = $_POST['total_stone_weight'];
        $total_net_weight = $_POST['total_net_weight'];
        $per_gm_stone_amount = $_POST['per_stone_amount'];
        $total_amount = $_POST['total_amount'];
        
        
        $grand_amount = $_POST['grand_amount'];
        
        $gst_amount = $_POST['gst_amount'];
        $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'];
         
         
        if($discount_type==1){
            $discount_amount = $_POST['discount_amount'];
        }else{
            $discount_amount = $_POST['discount'];
        }
        
        $statement = $pdo->prepare("UPDATE `tbl_purchases` SET `supplier_id`=?, `date`=?, `invoice_no`=?, `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`=?, `per_gm_stone_amount`=?, `gst_amount`=? WHERE id=?");
        $result = $statement->execute(array($supplier_id, $date, $invoice_no, $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, $per_gm_stone_amount, $gst_amount, $ids)); 
                        
                        
                        
        
        
        
        if($result){  
            
            
            $purchase_id = $ids; 
             
            $purchase_items_count = count($_POST['product_id']);
            
            
            $statements = $pdo->prepare("UPDATE `tbl_barcode` SET `barcode_date`=?, `purchase_items_count`=?, `updated_by`=? WHERE `purchase_id`=?");
            $results = $statements->execute(array($date, $purchase_items_count, $session_id, $purchase_id)); 
            
    
    
    
    
    
    
    
    
        if(count($_POST['product_id']) ==0){
            $return_arr['status']=400;
            $return_arr['error'] = true;
            $return_arr['message'] = "Purchase Failed... Purchase Items Not Found!";
            print_r(json_encode($return_arr));
            return false;
            exit(); 
        }
         
         
        for($r=0; $r<count($_POST['product_id']); $r++){
                    
                    $product = $_POST['product'][$r];
                    $product_id = $_POST['product_id'][$r];
                    $product_name = $_POST['product_name'][$r];
                    $category = $_POST['category_name'][$r];
                    $product_type = $_POST['product_type'][$r];
                    $unit = $_POST['unit'][$r];
                    $touch = $_POST['touch'][$r];
                    $purity = $_POST['purity'][$r];
                    $purchase_order_items_id = $_POST['purchase_order_items_id'][$r];
                    $today_gram_amount = $_POST['today_gram_amount'][$r];
                    $total_pro_amount = $_POST['total_pro_amount'][$r];
                    $making_charges = $_POST['making_charges'][$r];
                    
        if($product_type==1){
            $cross_weight = $_POST['cross_weight'][$r];
            $stone_cost = 0;
            $stone_weight = 0;
            $net_weight = $cross_weight;
        }else{
            $cross_weight = $_POST['cross_weight'][$r];
            $stone_weight = $_POST['stone_weight'][$r];
            $net_weight = $_POST['net_weight'][$r];
            $stone_cost = $_POST['stone_cost'][$r];
        }
                        
          error_log("PRO TYPE: ".$product_type);
          error_log("PRO WT: ".$_POST['stone_weight'][$r]);
          error_log("Items ID: ".$purchase_order_items_id);
          
          
          
          
          
          
          
                    if($purchase_order_items_id!=0){
                        // Update Query Here 
                        $statement_purchase_items = $pdo->prepare("UPDATE `tbl_purchases_items` SET `purchase_id`=?, `product_name`=?, `category`=?, `product_type`=?, `unit`=?,
                                `cross_weight`=?, `stone_weight`=?, `net_weight`=?, `stone_cost`=?, `purity`=?,
                                `product_ids`=?, `per_gram_amount`=?, `total_amount`=?, `touch`=?, `making_charges`=? WHERE id=?");
                        $statement_purchase_items_result = $statement_purchase_items->execute(array($purchase_id, $product_name, $category, $product_type, $unit, 
                                $cross_weight, $stone_weight, $net_weight, $stone_cost, $purity,
                                $product, $today_gram_amount, $total_pro_amount, $touch, $making_charges, $purchase_order_items_id));
                        
                    }else{
                        // Insert Query Here 
                        $statement_purchase_items = $pdo->prepare("INSERT INTO `tbl_purchases_items` ( `purchase_id`, `product_name`, `category`, `product_type`, `unit`,
                                `cross_weight`, `stone_weight`, `net_weight`, `stone_cost`, `purity`,
                                `product_ids`, `per_gram_amount`, `total_amount`, `touch`, `making_charges`) VALUES (?,?,?,?,?,
                                ?,?,?,?,?,
                                ?,?,?,?,?)");
                        $statement_purchase_items_result = $statement_purchase_items->execute(array($purchase_id, $product_name, $category, $product_type, $unit, 
                                $cross_weight, $stone_weight, $net_weight, $stone_cost, $purity,
                                $product, $today_gram_amount, $total_pro_amount, $touch, $making_charges)); 
                        
                    }
        }
        
         
        
        } 
         
    
        if($result){    
            $pdo->commit();
            $return_arr['message']='Purchase Updated 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);
}

?>