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






if(isset($_POST['type']) && $_POST['type']=='add_advance_voucher'){
    
     try{
         
        $pdo->beginTransaction(); 
        
        $customer_id = $_POST['customer_id'];
        $date = $_POST['date'];
        $product_type = $_POST['product_type'];
        
        $created_user_id = $_POST['created_user_id'];
        
        
        
        $location_id = $_POST['location_id'];
        $product_ids = $_POST['product_ids'];
        $product_name = $_POST['product_name'];
        $product_variant_id = $_POST['product_variant_id'];
        
        $purity = $_POST['purity'];
        $per_product_gram_amount = $_POST['per_product_gram_amount'];
        $weight_gram = $_POST['weight_gram'];
        $order_size = $_POST['order_size'];
        $order_length = $_POST['order_length'];
        $advance_policy = $_POST['advance_policy'];
        $remarks = $_POST['remarks'];
        
        $expected_purchase_date = $_POST['expected_purchase_date'];
        $advance_amount = $_POST['advance_amount'];
        $total_amount = $_POST['total_amount'];
        $advance_total_weight = $_POST['advance_total_weight'];
        $type = "Credit";
        $accounts = 4;
        
        

        
        
        $unique_id = "SPM";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_advance_voucher'");
        $statement->execute();
        $memeber_id_result = $statement->fetchAll(PDO::FETCH_ASSOC); 
        foreach ($memeber_id_result as $result) {
	        $unique_id = $unique_id + $result['Auto_increment'];
	        $auto_increment_id = $result['Auto_increment'];
        }
        
        $advance_no = "SPM_AD_".$auto_increment_id; 
        
        
        // Daily New ID Starts Here 
        $statements = $pdo->prepare("SELECT * FROM tbl_advance_voucher 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_AD_ET_".$daily_id;
     
        
        
        // Advance Policy - Here
                $statement = $pdo->prepare("SELECT * FROM `tbl_advance_policy` WHERE id=1");
                $statement->execute(array($advance_policy));
                $resultsss = $statement->fetchAll(PDO::FETCH_ASSOC);
                $validity_days = $resultsss[0]['days'];
        
        
        $statement = $pdo->prepare("INSERT INTO `tbl_advance_voucher`( `customer_id`, `advance_no`, `estimate_unique_no`, `type`, `date`, `expected_date`, 
                            `product_ids`, `product_variant_id`, `purity`, `advance_amount`, `per_gram_amount`, 
                            `weight`, `size`, `length`, `advance_policy`, `validity_days`, `created_by`,
                            `total_amount`, `advance_weight`, `remarks`,
                            `created_user_id`, `location_id`, `product_name`) VALUES (?,?,?,?,?,?,
                            ?,?,?,?,?,
                            ?,?,?,?,?,?,
                            ?,?,?,
                            ?,?,?)");
                       
        $result = $statement->execute(array($customer_id,  $advance_no, $daily_unique_id, $product_type, $date, $expected_purchase_date, 
                            $product_ids, $product_variant_id, $purity, $advance_amount, $per_product_gram_amount,
                            $weight_gram, $order_size, $order_length, $advance_policy, $validity_days, $session_id,
                            $total_amount, $advance_total_weight, $remarks,
                            $created_user_id, $location_id, $product_name)); 
        
        
        
    if($result){
        $advance_id = $pdo->lastInsertId(); 
        
        
        
        $statement = $pdo->prepare("INSERT INTO `tbl_advance_voucher_days`( `advance_voucher_id`, `date`, `policy_id`, `days`, `expected_date`, 
                                            `per_gram_amount`, `advance_amount`,  `created_by`, `updated_by`) 
                                            VALUES (?,?,?,?,?,
                                            ?,?,?,?)");
                       
        $result = $statement->execute(array($advance_id,  $date, $advance_policy, $validity_days, $expected_purchase_date, 
                            $per_product_gram_amount, $advance_amount, $session_id, $session_id));
        
        
        
        
    }
        
        
        
        
        
        
        
        
        
        
        for($i=0; $i<count($_POST['payment_id']); $i++){
            
            $payment_id = $_POST['payment_id'][$i];
            $paid_amount = $_POST['paid_amount'][$i];
            $payment_type = $_POST['payment_type'][$i];
            $reference = $_POST['reference'][$i];
            $notes = $_POST['notes'][$i];
            
            
            if($paid_amount!=0){
                
                
                
                    if($payment_id==0){
                        // Insert Here
                        
                        
                        $statement = $pdo->prepare("INSERT INTO `tbl_payments` (`date`, `type`, `amount`, `advance_id`, `customer_id`, 
                       `accounts`, `payment_type`, `reference_no`, `created_date`, `created_by`, `notes`) VALUES (?,?,?,?,?,
                       ?,?,?,?,?,?)");
                       $result = $statement->execute(array($date, $type, $paid_amount, $advance_id,  $customer_id,
                       $accounts, $payment_type, $reference, $today_date, $session_id, $notes)); 
                        
                    }else{
                        // Update Here
                        
                    }
            
            
            
            }else{
                
            }
            
            
            
            
        }
        
        

        
   
        

            
                        
            
            
           if($result){
               

               
                $pdo->commit();
                $return_arr['advance_id'] = $advance_id;
                $return_arr['message']='Advance Voucher Added Successfully...';
                $return_arr['status']=200;
           }else{
                $return_arr['message']='Something Went Wrong..';
                $return_arr['status']=400;
            }
            
            
             
     }catch(PDOException $e){
         $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
        }   
    echo json_encode($return_arr);
}

if(isset($_POST['field_type']) && $_POST['field_type'] == 'update_advance_voucher'){


    try{
         
        $pdo->beginTransaction();
        
        
        $advance_id = $_POST['ids'];
        $customer_id = $_POST['customer_id'];
        $date = $_POST['date'];
        // $product_type = $_POST['product_type'];
        // $created_user_id = $_POST['created_user_id'];
        
        // $location_id = $_POST['location_id'];
        // $product_ids = $_POST['product_ids'];
        $product_variant_id = $_POST['product_variant_id'];
        // $product_name = $_POST['product_name'];
        
        // $purity = $_POST['purity'];
        $per_product_gram_amount = $_POST['per_product_gram_amount'];
        // $weight_gram = $_POST['weight_gram'];
        // $order_size = $_POST['order_size'];
        // $order_length = $_POST['order_length'];
        // $advance_policy = $_POST['advance_policy'];
        // $remarks = $_POST['remarks'];
        
        $expected_purchase_date = $_POST['expected_purchase_date'];
        // $advance_amount = $_POST['advance_amount'];
        // $total_amount = $_POST['total_amount'];
        // $advance_total_weight = $_POST['advance_total_weight'];
        $payment_date = $today_date = date('Y-m-d');
        $type = "Credit";
        $accounts = 4;
        
        
        $statement = $pdo->prepare("UPDATE `tbl_advance_voucher` SET `customer_id`=?, `date`=?, `expected_date`=?,
                                     `product_variant_id`=?, `per_gram_amount`=?  WHERE id=?");
                       
        $result = $statement->execute(array($customer_id, $date, $expected_purchase_date, 
                                    $product_variant_id, $per_product_gram_amount, $advance_id)); 
        
    
        
        for($i=0; $i<count($_POST['payment_id']); $i++){
            
            $payment_id = $_POST['payment_id'][$i];
            $paid_amount = $_POST['paid_amount'][$i];
            $payment_type = $_POST['payment_type'][$i];
            $reference = $_POST['reference'][$i];
            $notes = $_POST['notes'][$i];
            
            
            if($paid_amount!=0){
                
                    if($payment_id==0){
                        // Insert Here
                        
                        $statement = $pdo->prepare("INSERT INTO `tbl_payments` (`date`, `type`, `amount`, `advance_id`, `customer_id`, 
                                    `accounts`, `payment_type`, `reference_no`, `created_date`, `created_by`, `notes`) VALUES (?,?,?,?,?,
                                    ?,?,?,?,?,?)");
                       $result = $statement->execute(array($payment_date, $type, $paid_amount, $advance_id,  $customer_id,
                                    $accounts, $payment_type, $reference, $today_date, $session_id, $notes)); 
                        
                    }else{
                        // Update Here
                        
                    }
            
            }else{
            }
            
        }
            
           if($result){
                $pdo->commit();
                $return_arr['message']='Advance Voucher Payment Added Successfully...';
                $return_arr['status']=200;
           }else{
                $return_arr['message']='Something Went Wrong..';
                $return_arr['status']=400;
            }
            
    }catch(PDOException $e){
         $pdo->rollBack();
        $return_arr['message']="$e";
        $return_arr['status']=500;
    }   
    echo json_encode($return_arr);

}


?>