<?php
include "include/db.php";
include "include/auth.php";
if($user_type == 'admin'){
    $active_user_id = "admin_id = $admin_id,";
}elseif($user_type == 'staff'){
    $active_user_id = "employee_id = $employee_id,";
}
$time = date("Y-m-d H:i:s");

function clean_input_data($cellVal){	
    include "include/db.php";
    $cellVal = trim($cellVal);
    $cellVal = mysqli_real_escape_string($conn, $cellVal);            
    return $cellVal;
}

if(isset($_REQUEST['bid_created_number']) && isset($_REQUEST['invoice_number'])){
    if(strlen($_REQUEST['bid_created_number']) >0 && strlen($_REQUEST['invoice_number']) >0){
       $bid_created_number = clean_input_data($_REQUEST['bid_created_number']); 
       $invoice_number = clean_input_data($_REQUEST['invoice_number']);
       $query_invoice_item_check = "SELECT * FROM invoice WHERE invoice_id = $invoice_number AND is_delete IS NULL";
       $run_invoice_item_check = mysqli_query($conn, $query_invoice_item_check);
       if(mysqli_num_rows($run_invoice_item_check) == 1){
            $row_invoice_item_check = mysqli_fetch_array($run_invoice_item_check);
            $vendor_pay = $row_invoice_item_check['vendor_pay'];
            $is_vendor_comment = $row_invoice_item_check['is_vendor_comment'];
            if(!isset($vendor_pay)){
                $vendor_pay = 0;
            }

            if(!isset($is_vendor_comment)){
                $is_vendor_comment = 0;
            }            
            $int_client_id = $row_invoice_item_check['client_id'];
            $int_property_id = $row_invoice_item_check['prop_id'];
            $query_bid_item_check = "SELECT * FROM bid WHERE bid_wo_no = '$bid_created_number' AND prop_id = $int_property_id AND client_id = $int_client_id AND is_delete IS NULL";
            $run_bid_item_check = mysqli_query($conn, $query_bid_item_check);
            if(mysqli_num_rows($run_bid_item_check) ==1){
                $row_bid_item_check = mysqli_fetch_array($run_bid_item_check); 
                $bid_amount = $row_bid_item_check['bid_amount']; 
                $query = "UPDATE bid SET bid_is_invoiced = 1 WHERE bid_wo_no = '$bid_created_number' AND is_delete IS NULL";
                $run = mysqli_query($conn, $query);
                if($run){
                    if($is_vendor_comment == 1 || $vendor_pay > 0){
                        $query = "UPDATE invoice SET is_system_bid = 1,invoice_type = 2, bid_wo_no = '$bid_created_number',bid_amount = $bid_amount,alert_type = 1 WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                        $run = mysqli_query($conn, $query);
                        if($run){
                            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added Bid Number- $bid_created_number', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                            $run_insert_log = mysqli_query($conn, $query_insert_log);
                            $arr = array('message' => 'found','amount'=>"$bid_amount");
                            echo json_encode($arr);
                            http_response_code(200);
                            exit;
                        }
                    }else{
                        $query = "UPDATE invoice SET is_system_bid = 1, bid_wo_no = '$bid_created_number',bid_amount = $bid_amount WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                        $run = mysqli_query($conn, $query);
                        if($run){
                            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added Bid Number- $bid_created_number', type = 'invoice', item_id = $invoice_number, time = '$time'";  
                            $run_insert_log = mysqli_query($conn, $query_insert_log);
                            $arr = array('message' => 'found','amount'=>"$bid_amount");
                            echo json_encode($arr);
                            http_response_code(200);
                            exit;
                        }                        
                    }
                }
            }else{        
                $arr = array('message' => 'missing');
                echo json_encode($arr);
                http_response_code(200);
                exit;
            }
       }else{        
        $arr = array('message' => 'missing');

        echo json_encode($arr);
        http_response_code(200);
        exit;
    }

       
    }else{
        http_response_code(400);
        exit;
    }    
}else if(isset($_REQUEST['no_bid_reason']) && isset($_REQUEST['invoice_number'])){
    if(strlen($_REQUEST['no_bid_reason']) >0 && strlen($_REQUEST['invoice_number']) >0){
       $no_bid_reason = clean_input_data($_REQUEST['no_bid_reason']); 
       $invoice_number = clean_input_data($_REQUEST['invoice_number']);

       $query_invoice_item_check = "SELECT * FROM invoice WHERE invoice_id = $invoice_number AND is_delete IS NULL";
       $run_invoice_item_check = mysqli_query($conn, $query_invoice_item_check);
       if(mysqli_num_rows($run_invoice_item_check) == 1){  
            $row_invoice_item_check = mysqli_fetch_array($run_invoice_item_check);
            $vendor_pay = $row_invoice_item_check['vendor_pay'];
            $is_vendor_comment = $row_invoice_item_check['is_vendor_comment'];
            if(!isset($vendor_pay)){
                $vendor_pay = 0;
            }

            if(!isset($is_vendor_comment)){
                $is_vendor_comment = 0;
            }
            if($is_vendor_comment == 1 || $vendor_pay > 0){
                $query = "UPDATE invoice SET is_system_bid = 0, bid_wo_no = '', invoice_type = 2, alert_type = 1, bid_amount = NULL, no_bid_reason = '$no_bid_reason' WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                $run = mysqli_query($conn, $query);
                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added reason for no bid- \"$no_bid_reason\"', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                    $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'reason_set','reason'=>"$no_bid_reason");
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }
            }else{
                $query = "UPDATE invoice SET is_system_bid = 0, bid_wo_no = '', bid_amount = NULL, no_bid_reason = '$no_bid_reason' WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                $run = mysqli_query($conn, $query);
                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added reason for no bid- \"$no_bid_reason\"', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'reason_set','reason'=>"$no_bid_reason");
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }
            }                    
        } 

    }else{
        http_response_code(400);
        exit;
    }    
}else if(isset($_REQUEST['no_vendor_comment']) && isset($_REQUEST['invoice_number'])){
    if(strlen($_REQUEST['no_vendor_comment']) >0 && strlen($_REQUEST['invoice_number']) >0){
       $no_vendor_comment = clean_input_data($_REQUEST['no_vendor_comment']); 
       $invoice_number = clean_input_data($_REQUEST['invoice_number']);
       $query_invoice_item_check = "SELECT * FROM invoice WHERE invoice_id = $invoice_number AND is_delete IS NULL";
       $run_invoice_item_check = mysqli_query($conn, $query_invoice_item_check);
       if(mysqli_num_rows($run_invoice_item_check) == 1){  
            $row_invoice_item_check = mysqli_fetch_array($run_invoice_item_check);
            $is_system_bid = '';
            $is_system_bid = $row_invoice_item_check['is_system_bid'];
            if($is_system_bid == 1 || $is_system_bid ==0){
                $query = "UPDATE invoice SET is_vendor_comment = 1, vendor_comment = '$no_vendor_comment',invoice_type = 2, alert_type = 1 WHERE invoice_id = $invoice_number AND is_delete IS NULL";

                $run = mysqli_query($conn, $query);

                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added comment- \"$no_vendor_comment\"', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                    $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'comment_set','reason'=>"$no_vendor_comment");
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }
            }else{
                $query = "UPDATE invoice SET is_vendor_comment = 1, vendor_comment = '$no_vendor_comment' WHERE invoice_id = $invoice_number AND is_delete IS NULL";

                $run = mysqli_query($conn, $query);

                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added comment- \"$no_vendor_comment\"', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'comment_set','reason'=>"$no_vendor_comment");
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }
            }
        }                      
    }else{
        http_response_code(400);
        exit;
    }    
}else if(isset($_REQUEST['invoice_vendor_pay']) && isset($_REQUEST['invoice_number'])){
    if(strlen($_REQUEST['invoice_vendor_pay']) >0 && strlen($_REQUEST['invoice_number']) >0){
       $vendor_pay = clean_input_data($_REQUEST['invoice_vendor_pay']); 
       $invoice_number = clean_input_data($_REQUEST['invoice_number']);
       $query_invoice_item_check = "SELECT * FROM invoice WHERE invoice_id = $invoice_number AND is_delete IS NULL";
       $run_invoice_item_check = mysqli_query($conn, $query_invoice_item_check);
       if(mysqli_num_rows($run_invoice_item_check) == 1){  
            $row_invoice_item_check = mysqli_fetch_array($run_invoice_item_check);
            $is_system_bid = '';
            $is_system_bid = $row_invoice_item_check['is_system_bid'];

            $no_bid_reason = '';
            $no_bid_reason = $row_invoice_item_check['no_bid_reason'];
       }
       $time = date("Y-m-d H:i:s");

       if($vendor_pay > 0){
            if($is_system_bid == 1 || strlen($no_bid_reason) > 0){           
                $query = "UPDATE invoice SET vendor_pay = $vendor_pay, vendor_pay_date= '$time',vendor_is_settle = 1, is_vendor_comment = 0,invoice_type = 2, alert_type = 1 WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                $run = mysqli_query($conn, $query);
                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added vendor pay - $$vendor_pay', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                    $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'vendor_pay_set');
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }
            }else{
                $query = "UPDATE invoice SET vendor_pay = $vendor_pay, vendor_pay_date= '$time',vendor_is_settle = 1, is_vendor_comment = 0 WHERE invoice_id = $invoice_number AND is_delete IS NULL";
                $run = mysqli_query($conn, $query);
                if($run){
                    $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added vendor pay - $$vendor_pay', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                    $run_insert_log = mysqli_query($conn, $query_insert_log);
                    $arr = array('message' => 'vendor_pay_set');
                    echo json_encode($arr);
                    http_response_code(200);
                    exit;
                }                
            }
       }else{
            $query = "UPDATE invoice SET vendor_pay = $vendor_pay, vendor_pay_date= '$time',vendor_is_settle = 1, is_vendor_comment = 2 WHERE invoice_id = $invoice_number AND is_delete IS NULL";
            $run = mysqli_query($conn, $query);
            if($run){
                $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added vendor pay - $$vendor_pay', type = 'invoice', item_id = $invoice_number, time = '$time'"; 
                $run_insert_log = mysqli_query($conn, $query_insert_log);
                $arr = array('message' => 'vendor_pay_set');
                echo json_encode($arr);
                http_response_code(200);
                exit;
            }
       } 
       
       
    }else{
        http_response_code(400);
        exit;
    }    
}else if(isset($_REQUEST['invoice_client_pay']) && isset($_REQUEST['invoice_number'])){
    if(strlen($_REQUEST['invoice_client_pay']) >0 && strlen($_REQUEST['invoice_number']) >0){
       $invoice_client_pay = clean_input_data($_REQUEST['invoice_client_pay']); 
       $invoice_number = clean_input_data($_REQUEST['invoice_number']);
       $time = date("Y-m-d H:i:s");
        $query = "UPDATE invoice SET client_pay = $invoice_client_pay,client_pay_date= '$time',is_settle = 1 WHERE invoice_id = $invoice_number AND is_delete IS NULL";
        $run = mysqli_query($conn, $query);
        if($run){
            $arr = array('message' => 'client_pay_set');
            echo json_encode($arr);
            http_response_code(200);
            exit;
        }                      
    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

