<?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['maps']) && isset($_REQUEST['in_no'])){

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['in_no']) >0){

       $maps_id = $_REQUEST['maps'];  
       $query_fetch_maps_amount = "SELECT * FROM maps WHERE maps_id = $maps_id";
       $run_fetch_maps_amount = mysqli_query($conn, $query_fetch_maps_amount);

       if(mysqli_num_rows($run_fetch_maps_amount) ==1){
            while($row_fetch_maps_amount = mysqli_fetch_array($run_fetch_maps_amount)){
                $invoice_maps_name = $row_fetch_maps_amount['maps_name'];
            }   
       }        
       $invoice_id = $_REQUEST['in_no'];    
       
       $query_bid_check = "SELECT * FROM invoice WHERE invoice_id = $invoice_id 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);

        $invoice_amount_prev = $row_bid_check['invoice_amount'];
           
       }else{
       
        http_response_code(400);
           
        exit;        

       }

       $query_bid_item_check = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id AND maps_id = $maps_id";

       $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);

        $invoice_item_amount = $row_bid_item_check['invoice_item_amount'];
        $invoice_item_total_prev = $row_bid_item_check['invoice_item_total'];

        $invoice_item_quantity_prev = $row_bid_item_check['invoice_item_quantity'];
    
       }else{
        
        http_response_code(400);
           
            exit;
       }

       $invoice_item_quantity_new = $invoice_item_quantity_prev + 1;
       $invoice_item_total_new = $invoice_item_total_prev + $invoice_item_amount;
       $invoice_item_total_new = number_format($invoice_item_total_new, 2, '.', '');
       $invoice_amount_new = $invoice_amount_prev + $invoice_item_amount;
       $invoice_amount_new = number_format($invoice_amount_new, 2, '.', '');

       if($invoice_amount_prev != $invoice_amount_new){
            $query_bid_amount_update = "UPDATE invoice SET invoice_amount = $invoice_amount_new WHERE invoice_id = $invoice_id";

            $run_bid_amount_update = mysqli_query($conn, $query_bid_amount_update);
    
                if(mysqli_affected_rows($conn) !=1){
                    http_response_code(400);                       
                    exit;
                }
       }
     
       $query_bid_qty_update = "UPDATE invoice_item SET invoice_item_total = $invoice_item_total_new, invoice_item_quantity = $invoice_item_quantity_new WHERE invoice_id = $invoice_id AND maps_id = $maps_id";

        $run_bid_qty_update = mysqli_query($conn, $query_bid_qty_update);

        if(mysqli_affected_rows($conn) !=1){
                http_response_code(400);
                exit;
        }else{
            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name - quantity added to $invoice_item_quantity_new', type = 'invoice', item_id = $invoice_id, time = '$time'"; 

            $run_insert_log = mysqli_query($conn, $query_insert_log);

            $invoice_amount_new = "$ ". $invoice_amount_new;
            $arr = array('bid_amount' => $invoice_amount_new, 'bid_item_total' => $invoice_item_total_new, 'bid_item_quantity' => $invoice_item_quantity_new);
            echo json_encode($arr);
            http_response_code(200);
            exit;
        }  
    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>