<?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['in_i']) && isset($_REQUEST['uom_type']) && isset($_REQUEST['length']) && isset($_REQUEST['width'])){

    if(strlen($_REQUEST['in_i']) >0 && strlen($_REQUEST['uom_type']) > 0 && strlen($_REQUEST['length']) > 0 && strlen($_REQUEST['width']) >0){

       $invoice_item_id = $_REQUEST['in_i'];      
       $uom_type = $_REQUEST['uom_type'];        
       $length = $_REQUEST['length'];
       $width = $_REQUEST['width'];  
       
       $query_fetch_maps_id = "SELECT * FROM invoice_item WHERE invoice_item_id = $invoice_item_id AND is_delete IS NULL";
       
       $run_fetch_maps_id = mysqli_query($conn, $query_fetch_maps_id);      

       if(mysqli_num_rows($run_fetch_maps_id) ==1){
        $row_bid_item_check = mysqli_fetch_array($run_fetch_maps_id);
        $invoice_item_amount = $row_bid_item_check['invoice_item_amount']; 
        $invoice_item_total_prev = $row_bid_item_check['invoice_item_total']; 
        $maps_id = $row_bid_item_check['maps_id'];
        $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 = $row_bid_item_check['invoice_id'];
  
       }else{
        
        http_response_code(400);
           
        exit;
       }
       $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;        

       }
        
       include "include/uomFormulaLib.php";

       if($uom_type == 1){

       $query_uom_field = "SELECT * FROM uom_field WHERE uom_id = $uom_type AND uom_field_type = 'bidin'"; 

       $run_uom_field = mysqli_query($conn, $query_uom_field);
       $checker = 0;
       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'];
              $uom_field_name = $row_uom_field['uom_field_name'];              
               if($uom_field_name == 'bidin_uom_val1'){
                    $query_select_bid_item_uom_value = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $uom_field_id"; 

                    $run_select_bid_item_uom_value = mysqli_query($conn, $query_select_bid_item_uom_value);

                    $row_select_bid_item_uom_value = mysqli_fetch_array($run_select_bid_item_uom_value);

                    $invoice_item_uom_value = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
                    if($length == $invoice_item_uom_value){
                        $checker = $checker + 1;
                    }
                    $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $length WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $uom_field_id"; 
                    $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);                    
               }

               if($uom_field_name == 'bidin_uom_val2'){ 
                    $query_select_bid_item_uom_value = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $uom_field_id"; 

                    $run_select_bid_item_uom_value = mysqli_query($conn, $query_select_bid_item_uom_value);

                    $row_select_bid_item_uom_value = mysqli_fetch_array($run_select_bid_item_uom_value);

                    $invoice_item_uom_value = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
                    if($width == $invoice_item_uom_value){
                        $checker = $checker + 1;
                    }

                    $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $width WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $uom_field_id";
                    $run_insert_bid_item_uom_value = mysqli_query($conn, $query_insert_bid_item_uom_value);
               }    
              

           }
                         
       }
       if($checker == 2){
           echo "same";
        http_response_code(200);
        exit;
       }
       $formulaType ="SF";
       $bidin_uom_val1 = $length;
       $bidin_uom_val2 = $width;     
        

        $query_fetch_maps_uom_field = "SELECT * FROM uom_field WHERE uom_id = $uom_type 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_id";

            $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; //fixed base price  
            }

        }

    }

    if($formulaType = "SF"){    
        $invoice_item_total_new = sfFormula($bidin_uom_val1, $bidin_uom_val2, $maps_uom_val1);
    }     
     $invoice_item_total_new = number_format($invoice_item_total_new, 2, '.', '');
     $invoice_amount_temp = $invoice_amount_prev - $invoice_item_total_prev;
     $invoice_amount_new = $invoice_amount_temp + $invoice_item_total_new;
     $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_amount = $maps_uom_val1 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{
          $invoice_amount_new = "$ ". $invoice_amount_new;
          $arr = array('bid_amount' => $invoice_amount_new, 'm_id' => $maps_id,"bid_item_total"=>"$invoice_item_total_new","bid_item"=>"$maps_uom_val1");
          $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name- SF dimensions changed to Length- $length, Width- $width', 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{
        http_response_code(400);
        exit;
    }    
}else{
   
    http_response_code(400);
    exit;
}
?>
