<?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");
if(isset($_REQUEST['invoice_vendor_pay']) && isset($_REQUEST['check_bid_item_id']) && isset($_REQUEST['check_bid_id'])){

    if(strlen($_REQUEST['invoice_vendor_pay']) >0 && strlen($_REQUEST['check_bid_item_id'] && strlen($_REQUEST['check_bid_id'])) >0){

       $maps_id = $_REQUEST['check_bid_item_id'];      
       $wo_no = $_REQUEST['check_bid_id'];        
       $bid_item_amount_new = $_REQUEST['invoice_vendor_pay'];
       $bid_item_amount_new = number_format($bid_item_amount_new, 2, '.', '');

       if(is_numeric($bid_item_amount_new)){            
          $query_bid_check = "SELECT * FROM bid WHERE bid_id = $wo_no AND is_delete IS NULL";
            $run_bid_check = mysqli_query($conn, $query_bid_check);
            if(mysqli_num_rows($run_bid_check) ==1){
                $row_bid_check = mysqli_fetch_array($run_bid_check);
                $bid_amount_prev = $row_bid_check['vendor_amount'];
                $work_order_number = $row_bid_check['bid_wo_no'];
                $int_bid_amount = $row_bid_check['bid_amount']; 
                if($bid_amount_prev == NULL){
                    $bid_amount_prev = 0;
                }               
            }else{  
                             
                http_response_code(400);                    
                exit;        
            }
            $query_bid_item_check = "SELECT * FROM bid_item WHERE bid_id = $wo_no AND bid_item_id = $maps_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_item_amount_prev = $row_bid_item_check['vendor_item_amount'];
                $bid_item_total = $row_bid_item_check['bid_item_total'];

                if($bid_item_amount_prev  == NULL){
                    $bid_item_amount_prev  = 0;
                }

                if($bid_item_total  == NULL){
                    $bid_item_total  = 0;
                }
               

            }else{
                http_response_code(400);                
                exit;
            }

            if($bid_item_amount_new == $bid_item_amount_prev){
                $bid_amount_prev = "$ ". $bid_amount_prev;
                $arr = array('message' => 'no');


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

            if($bid_item_amount_new > $int_bid_amount){
                $time = date("Y-m-d H:i:s");
            
                $alert_string = "Vendor pay of $bid_amount_new. Vendor overpay in Bid# $work_order_number.";    
                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL,admin_alert_time = '$time', admin_alert_link = 'estimate_finalize.php?wo_no=$wo_no',$active_user_id admin_alert_activity = '$alert_string'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
            }

            $query_bid_item_update = "UPDATE bid_item SET vendor_item_amount = $bid_item_amount_new WHERE bid_item_id = $maps_id";

            $run_bid_item_update = mysqli_query($conn, $query_bid_item_update);

            if(mysqli_affected_rows($conn) !=1){                
                http_response_code(400);                
                exit;
            }

            $bid_item_color_new = '';

            $diff = $bid_item_total - $bid_item_amount_new;

            if($diff < 0){
                $bid_item_color_new = 'red';

            }else{
                $pro_percent = $diff/$bid_item_amount_new;
                $pro_percent = $pro_percent * 100;

                if($pro_percent > 44){
                    $bid_item_color_new = 'green';
                }elseif($pro_percent > 34){
                    $bid_item_color_new = 'yellow';
                }else{
                    $bid_item_color_new = '#BEBEBE';
                }

            }

            $bid_amount_new = $bid_amount_prev - $bid_item_amount_prev + $bid_item_amount_new;
            $bid_amount_new = number_format($bid_amount_new, 2, '.', '');
            if($bid_amount_new > $bid_item_total){
                $time = date("Y-m-d H:i:s");
            
                $alert_string = "Vendor pay of item more than Bid amount. Bid# $work_order_number.";    
                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL,admin_alert_time = '$time', admin_alert_link = 'estimate_finalize.php?wo_no=$wo_no',$active_user_id admin_alert_activity = '$alert_string'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
            }

            $query_bid_update = "UPDATE bid SET vendor_amount = $bid_amount_new WHERE bid_id = $wo_no AND is_delete IS NULL";
            $run_bid_update = mysqli_query($conn, $query_bid_update);
            if(mysqli_affected_rows($conn) !=1){
               
                http_response_code(400);                    
                exit;        
            }

            $bid_amount_new = "$ ". $bid_amount_new;
            $arr = array('message' => 'change','bid_amount' => $bid_amount_new, 'bid_item_total' => $bid_item_amount_new, 'color' => $bid_item_color_new);

            
            echo json_encode($arr);
            http_response_code(200);
            exit;
            
        }else{
            //numeric
            
            http_response_code(400);
            exit;
        }            
       
    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

