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

    if(strlen($_REQUEST['maps']) >0 && strlen($_REQUEST['cl_id']) >0 && strlen($_REQUEST['state']) >0 && strlen($_REQUEST['in_no']) >0 && strlen($_REQUEST['invoice_no']) >0){
        $maps_added = $_REQUEST['maps'];
        $cl_id = $_REQUEST['cl_id'];
        $invoice_id = $_REQUEST['in_no'];
        $invoice_no = $_REQUEST['invoice_no'];
        $state = $_REQUEST['state'];
        $suc_count = 0;
        $query_check_bid = "SELECT * FROM invoice WHERE invoice_id = $invoice_id AND client_id = $cl_id AND invoice_type = $state AND invoice_no = '$invoice_no'";

        $run_check_bid = mysqli_query($conn, $query_check_bid);

        if(mysqli_num_rows($run_check_bid) !=1){
            http_response_code(400);
            exit;
        }
        $row_check_bid = mysqli_fetch_array($run_check_bid);
        $prop_id = $row_check_bid['prop_id'];
        $invoice_amount_prev = $row_check_bid['invoice_amount'];
        $invoice_amount_prev = number_format($invoice_amount_prev, 2, '.', '');

        $maps_arr = array();
        $query_fetch_maps_added = "SELECT * FROM maps WHERE maps_id = $maps_added";
        $run_fetch_maps_added = mysqli_query($conn, $query_fetch_maps_added);

        if(mysqli_num_rows($run_fetch_maps_added) ==1){
            array_push($maps_arr, $maps_added); 
            $check_maps_group_query = "SELECT * FROM maps_group WHERE parent_id = $maps_added AND is_delete IS NULL AND is_approve = 1";   
            $check_maps_group_run = mysqli_query($conn, $check_maps_group_query); 
            if(mysqli_num_rows($check_maps_group_run) > 0){
                while($check_maps_group_row = mysqli_fetch_array($check_maps_group_run)){
                    $sub_id = $check_maps_group_row['sub_id'];
                    array_push($maps_arr, $sub_id);
                }
            }                   
        }

        $item_count = count($maps_arr);
        foreach($maps_arr as $maps){
            $query_fetch_maps_amount = "SELECT * FROM maps WHERE maps_id = $maps";
            $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_item_amount = $row_fetch_maps_amount['maps_min_bid_price'];
                        $invoice_item_uom = $row_fetch_maps_amount['maps_crew_unit_uom_id'];
                        if(!isset($invoice_item_uom) || strlen($invoice_item_uom) == 0){
                            $invoice_item_uom = 0;
                        }                


                        if(!isset($invoice_item_amount)){
                        $invoice_item_amount = 0.00;

                        }
                        if($invoice_item_amount == NULL){
                            $invoice_item_amount = 0.00;  
                        }
                        $invoice_item_amount = number_format($invoice_item_amount, 2, '.', '');
                    } 

            }else{
                continue; 
            }       

            if(isset($invoice_item_amount)){
                    $time = date("Y-m-d H:i:s");
                    $query_check2_bid_item = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id AND maps_id = $maps AND is_delete IS NULL"; 
                    $run_check2_bid_item = mysqli_query($conn, $query_check2_bid_item);
                    if(mysqli_num_rows($run_check2_bid_item)>0){
                        continue;                 
                    }

                    $invoice_amount_new = $invoice_amount_prev + $invoice_item_amount;
                    $invoice_amount_new = number_format($invoice_amount_new, 2, '.', '');

                    $query_insert_bid_item = "INSERT INTO invoice_item SET invoice_id = $invoice_id, maps_id = $maps, invoice_item_amount = $invoice_item_amount, invoice_item_uom = $invoice_item_uom,invoice_item_quantity = 1, invoice_item_total = $invoice_item_amount, create_time = '$time'"; 

                    $run_insert_bid_item = mysqli_query($conn, $query_insert_bid_item);
                    $invoice_item_id_u = mysqli_insert_id($conn);
                    if(mysqli_affected_rows($conn) > 0){  
                        $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = 'Added item $invoice_maps_name', type = 'invoice', item_id = $invoice_id, time = '$time'"; 

                        $run_insert_log = mysqli_query($conn, $query_insert_log);
                        if($invoice_amount_new != $invoice_amount_prev){    
                            $query_update_bid = "UPDATE invoice SET invoice_amount = $invoice_amount_new WHERE invoice_id = $invoice_id AND client_id = $cl_id AND invoice_type = $state AND invoice_no = '$invoice_no'"; 

                            $run_update_bid = mysqli_query($conn, $query_update_bid);

                            if(mysqli_affected_rows($conn) > 0){         
                                $suc_count++;
                                if($item_count > 1){
                                    $invoice_amount_prev = $invoice_amount_new;
                                }
                            }
                        }else{
                            $suc_count++;
                        }

                    }else{ 
                        continue;
                    }

                    if($invoice_item_uom == 4){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }

                        }
                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        if(mysqli_num_rows($run_uom_field)>0){
                            while($row_uom_field = mysqli_fetch_array($run_uom_field)){
                                $uom_field_id = $row_uom_field['uom_field_id'];
                                
                                $query_insert_bid_item_uom_value = "INSERT INTO invoice_item_uom_value SET invoice_item_id = $invoice_item_id_u, invoice_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);

                            }
                                      
                        } 
                        
                        if($p_lot_size_set == 'yes'){
                            $query_uom_field2 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'bidin' AND uom_field_name = 'bidin_uom_val1'"; 
                            $run_uom_field2 = mysqli_query($conn, $query_uom_field2);

                            if(mysqli_num_rows($run_uom_field2) == 1){
                                $row_uom_field2 = mysqli_fetch_array($run_uom_field2);
                                $uom2_field_id = $row_uom_field2['uom_field_id'];

                                $update_uom2_query = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = '$p_lot_size' WHERE invoice_item_id = $invoice_item_id_u AND invoice_item_uom_field_id = $uom2_field_id";
                                $update_uom2_run = mysqli_query($conn, $update_uom2_query);

                            }else{
                                continue; 
                            }
                        }
                        
                    }

                    if($invoice_item_uom == 1 || $invoice_item_uom == 5 || $invoice_item_uom == 6 || $invoice_item_uom == 7 || $invoice_item_uom == 8){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }
                        }

                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        if(mysqli_num_rows($run_uom_field)>0){
                            while($row_uom_field = mysqli_fetch_array($run_uom_field)){
                                $uom_field_id = $row_uom_field['uom_field_id'];
                                
                                $query_insert_bid_item_uom_value = "INSERT INTO invoice_item_uom_value SET invoice_item_id = $invoice_item_id_u, invoice_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);

                            }
                                    
                        } 
                        
                        $query_fetch_maps_uom_field = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'maps'"; 
                        $run_fetch_maps_uom_field = mysqli_query($conn, $query_fetch_maps_uom_field);
                
                        while($row_fetch_maps_uom_field = mysqli_fetch_array($run_fetch_maps_uom_field)){
                            $uom_field_name = $row_fetch_maps_uom_field['uom_field_name'];
                
                            $uom_field_id = $row_fetch_maps_uom_field['uom_field_id'];
                
                            $query_fetch_maps_uom_value = "SELECT * FROM maps_uom_value WHERE uom_field_id = $uom_field_id AND maps_id = $maps";
                
                            $run_fetch_maps_uom_value = mysqli_query($conn, $query_fetch_maps_uom_value);
                
                            $row_fetch_maps_uom_value = mysqli_fetch_array($run_fetch_maps_uom_value);
                
                            $maps_uom_value_amount = $row_fetch_maps_uom_value['maps_uom_value_amount'];
                
                            if($uom_field_name == 'maps_uom_val1'){
                                $maps_uom_val1 = $maps_uom_value_amount;  
                            }
                
                        }
                       
                        $query_insert_bid_item = "UPDATE invoice_item SET invoice_item_amount = $maps_uom_val1, invoice_item_total = 0.00 WHERE invoice_item_id = $invoice_item_id_u";

                        $run_insert_bid_item = mysqli_query($conn, $query_insert_bid_item);
                        $invoice_amount_new_adjust = $invoice_amount_new - $invoice_item_amount;
                        
                        $query_update_bid = "UPDATE invoice SET invoice_amount = $invoice_amount_new_adjust WHERE invoice_id = $invoice_id AND client_id = $cl_id AND invoice_type = $state AND invoice_no = '$invoice_no'"; 

                        $run_update_bid = mysqli_query($conn, $query_update_bid);
                        
                    }

                    if($invoice_item_uom == 2){
                        $fetch_prop_area_query = "SELECT * FROM prop_assign WHERE is_approve = 1 AND is_delete IS NULL AND client_id = $cl_id AND prop_id = $prop_id";
                        $fetch_prop_area_run = mysqli_query($conn, $fetch_prop_area_query);
                        if(mysqli_num_rows($fetch_prop_area_run) > 0){
                            $fetch_prop_area_row = mysqli_fetch_array($fetch_prop_area_run);
                            $p_lot_size = $fetch_prop_area_row['p_lot_size'];
                            if(!isset($p_lot_size)){
                                $p_lot_size = '';
                            }
                            if($p_lot_size == NULL){
                                $p_lot_size = '';
                            }

                            if(strlen($p_lot_size) > 0){
                                $p_lot_size_set = 'yes';
                            }

                        }

                        $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'bidin'"; 
                        $run_uom_field = mysqli_query($conn, $query_uom_field);
                        if(mysqli_num_rows($run_uom_field)>0){
                            while($row_uom_field = mysqli_fetch_array($run_uom_field)){
                                $uom_field_id = $row_uom_field['uom_field_id'];
                                
                                $query_insert_bid_item_uom_value = "INSERT INTO invoice_item_uom_value SET invoice_item_id = $invoice_item_id_u, invoice_item_uom_field_id = $uom_field_id";
                                $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);

                                
                            }                                       
                        }

                        if($p_lot_size_set == 'yes'){
                            $query_uom_field2 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'bidin' AND uom_field_name = 'bidin_uom_val1'"; 
                            $run_uom_field2 = mysqli_query($conn, $query_uom_field2);

                            if(mysqli_num_rows($run_uom_field2) == 1){
                                $row_uom_field2 = mysqli_fetch_array($run_uom_field2);
                                $uom2_field_id = $row_uom_field2['uom_field_id'];

                                $update_uom2_query = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = '$p_lot_size' WHERE invoice_item_id = $invoice_item_id_u AND invoice_item_uom_field_id = $uom2_field_id";
                                $update_uom2_run = mysqli_query($conn, $update_uom2_query);

                            }else{
                                continue; 
                            }
                        }
                    }
                }else{
                    continue; 
                }
        }
        if($suc_count > 0){
            http_response_code(200);
            exit;
        }else{
            http_response_code(400);
            exit;            
        }  

    }else{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>

