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

$today_date = date('Y-m-d');
$updated_at = date("Y-m-d H:i:s");






if(isset($_POST['type']) && $_POST['type']=='add_repair_voucher'){
    
     try{
         
        $pdo->beginTransaction(); 
        
        $customer_id = $_POST['customer_id'];
        $date = $_POST['date'];
        $repair_type = $_POST['repair_type'];
        $location_id = $_POST['location_id'];
        $product_name = $_POST['product_name'];
        
        
        $product_ids = $_POST['product_ids'];
        $product_variant_id = $_POST['product_variant_id'];
        
        $weight_gram = $_POST['weight_gram'];
        $remarks = $_POST['remarks'];
        
        $due_date = $_POST['due_date'];
        $sales_man = $_POST['sales_man_id'];
        $type = "Credit";
        $accounts = 6;
        
        

        
        
        $unique_id = "SPM";
	    $statement = $pdo->prepare("SHOW TABLE STATUS WHERE `name` = 'tbl_repair_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'];
        }
        
        $repair_no = "SPM_RV_".$auto_increment_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_repair_voucher`(`customer_id`, `product_ids`, `weight`, `due_date`, `type`, 
                            `sales_man`, `bill_no`, `date`,
                            `created_by`, `created_at`, `updated_at`, `remarks`, `location_id`, `product_name`) VALUES (?,?,?,?,?,
                            ?,?,?,
                            ?,?,?,?,?,?)");
                       
        $result = $statement->execute(array($customer_id,  $product_ids, $weight_gram, $due_date, $repair_type, 
                            $sales_man, $repair_no, $date,
                            $session_id, $updated_at, $updated_at, $remarks, $location_id, $product_name)); 
        
        
        
        if($result){
            $repair_id = $pdo->lastInsertId(); 
        }
        
        if(!empty($_POST['payment_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`, `repair_id`, `customer_id`, 
                       `accounts`, `payment_type`, `reference_no`, `created_date`, `created_by`, `notes`) VALUES (?,?,?,?,?,
                       ?,?,?,?,?,?)");
                $result = $statement->execute(array($date, $type, $paid_amount, $repair_id,  $customer_id,
                       $accounts, $payment_type, $reference, $today_date, $session_id, $notes)); 
                        
                    }else{
                        // Update Here
                        
                    }
            
            
            
            }else{
                
            }
            
            
            
            
        }
        }
        
        

        
   
        

            
                        
            
            
           if($result){
               

               
                $pdo->commit();
                
                $return_arr['message']='Repair Voucher Added Successfully...';
                $return_arr['status']=200;
           }else{
                $pdo->rollBack();
                $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['type']) && $_POST['type']=='update_repair_voucher'){
    
     try{
         
        $pdo->beginTransaction(); 
        
        
        $ids = $_POST['ids'];
        $customer_id = $_POST['customer_id'];
        $date = $_POST['date'];
        $repair_type = $_POST['repair_type'];
        
        $location_id = $_POST['location_id'];
        $product_name = $_POST['product_name'];
        
        
        $product_ids = $_POST['product_ids'];
        $product_variant_id = $_POST['product_variant_id'];
        
        $weight_gram = $_POST['weight_gram'];
        $remarks = $_POST['remarks'];
        
        // $total_amount = $_POST['total_amount'];
        $due_date = $_POST['due_date'];
        $sales_man = $_POST['sales_man_id'];
        $type = "Credit";
        $accounts = 6;
        
        $repair_completed_date = isset($_POST['repair_completed_date']) ? $_POST['repair_completed_date'] : '' ;
 
	    $pre_statement = $pdo->prepare("SELECT rv.bill_no FROM tbl_repair_voucher AS rv WHERE rv.id=?");
        $pre_statement->execute(array($ids));
        $pre_data = $pre_statement->fetchAll(PDO::FETCH_ASSOC); 
        
        $repair_no = $pre_data[0]['bill_no']; 
        
        
        
        $statement = $pdo->prepare("UPDATE `tbl_repair_voucher` SET `customer_id`=?, `product_ids`=?, `weight`=?, `due_date`=?, `type`=?, 
                            `sales_man`=?, `bill_no`=?, `date`=?, `location_id`=?, `product_name`=?, `repair_completed_date`=?,
                            `updated_by`=?, `updated_at`=?, `remarks`=? WHERE id=?");
                       
        $result = $statement->execute(array($customer_id,  $product_ids, $weight_gram, $due_date, $repair_type, 
                            $sales_man, $repair_no, $date, $location_id, $product_name,$repair_completed_date,
                            $session_id, $updated_at, $remarks, $ids)); 
        

        $repair_id = $ids; 
        
        if(!empty($_POST['payment_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`, `repair_id`, `customer_id`, 
                                   `accounts`, `payment_type`, `reference_no`, `created_date`, `created_by`, `notes`) VALUES (?,?,?,?,?,
                                   ?,?,?,?,?,?)");
                            $result = $statement->execute(array($date, $type, $paid_amount, $repair_id,  $customer_id,
                                   $accounts, $payment_type, $reference, $today_date, $session_id, $notes)); 
                        
                    }else{
                        // Update Here
                        
                    }
            
            
            
            }else{
                
            }
            
            
            
            
        }
        }
        

        
       
            
           if($result){
               

               
                $pdo->commit();
                
                $return_arr['message']='Repair Voucher Updated Successfully...';
                $return_arr['status']=200;
           }else{
                $pdo->rollBack();
                $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);
}
?>