<?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,";
}
if(isset($_REQUEST['maps']) && isset($_REQUEST['in_no']) && isset($_REQUEST['new_price'])){

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

       $maps_id = $_REQUEST['maps'];      
       $invoice_id = $_REQUEST['in_no'];        
       $invoice_item_amount_new = $_REQUEST['new_price'];
       $invoice_item_amount_new = number_format($invoice_item_amount_new, 2, '.', '');

       if(is_numeric($invoice_item_amount_new)){            
          $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']; 
                $invoice_number = $row_bid_check['invoice_no'];
                if($invoice_amount_prev == NULL){
                    $invoice_amount_prev = 0;
                }               
            }else{  
                             
                http_response_code(400);                    
                exit;        
            }
            $query_bid_item_check = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id AND maps_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);
                $invoice_item_amount_prev = $row_bid_item_check['invoice_item_amount'];
                if($invoice_item_amount_prev  == NULL){
                    $invoice_item_amount_prev  = 0;
                }
                $invoice_item_total_prev = $row_bid_item_check['invoice_item_total'];
                if($invoice_item_total_prev == NULL){
                    $invoice_item_total_prev = 0;
                }
                $invoice_item_quantity = $row_bid_item_check['invoice_item_quantity'];
                if($invoice_item_quantity == NULL){
                    $invoice_item_quantity = 1;
                }

            }else{
                http_response_code(400);                
                exit;
            }

            if($invoice_item_amount_new == $invoice_item_amount_prev){
                $invoice_amount_prev = "$ ". $invoice_amount_prev;
                $arr = array('bid_amount' => $invoice_amount_prev, 'bid_item_total' => $invoice_item_total_prev, 'price' => $invoice_item_amount_new);


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

            $invoice_item_total_new = $invoice_item_amount_new * $invoice_item_quantity;
            $invoice_item_total_new = number_format($invoice_item_total_new, 2, '.', '');

            $query_bid_item_update = "UPDATE invoice_item SET invoice_item_total = $invoice_item_total_new, invoice_item_amount = $invoice_item_amount_new WHERE invoice_id = $invoice_id AND maps_id = $maps_id AND is_delete IS NULL";

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

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


            $invoice_amount_new = $invoice_amount_prev - $invoice_item_total_prev + $invoice_item_total_new;
            $invoice_amount_new = number_format($invoice_amount_new, 2, '.', '');

            $query_bid_update = "UPDATE invoice SET invoice_amount = $invoice_amount_new WHERE invoice_id = $invoice_id 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;        
            }

            $invoice_amount_new = "$ ". $invoice_amount_new;

            $arr = array('bid_amount' => $invoice_amount_new, 'bid_item_total' => $invoice_item_total_new, 'price' => $invoice_item_amount_new);
            $time = date("Y-m-d H:i:s");
            if($user_type == 'staff'){
                $query_fetch_maps_name = "SELECT * FROM maps WHERE maps_id = $maps_id";
                $run_fetch_maps_name = mysqli_query($conn, $query_fetch_maps_name);
                $row_fetch_maps_name = mysqli_fetch_array($run_fetch_maps_name);
                $item_name = $row_fetch_maps_name['maps_name'];
                $time = date("Y-m-d H:i:s");
            
                $alert_string = "has changed price of $item_name to $$invoice_item_amount_new in Invoice# $invoice_number.";    
                $query_cat_verify_alert = "INSERT INTO admin_alert SET admin_alert_id = NULL,admin_alert_time = '$time', admin_alert_link = 'invoice_finalize.php?in_no=$invoice_id',employee_id= $employee_id, admin_alert_activity = '$alert_string'";
                $run_cat_verify_alert = mysqli_query($conn, $query_cat_verify_alert);
                
            }
            $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Changed price of $item_name to $$invoice_item_amount_new', type = 'invoice', item_id = $invoice_id, time = '$time'"; 

            $run_insert_log = mysqli_query($conn, $query_insert_log);
            
            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;
}
?>

