<?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['tarpArea']) && isset($_REQUEST['tarpTime']) && isset($_REQUEST['tarpStorey']) && isset($_REQUEST['tarpPitch'])){

    if(strlen($_REQUEST['in_i']) >0 && strlen($_REQUEST['uom_type']) && strlen($_REQUEST['tarpArea']) >0 && strlen($_REQUEST['tarpTime']) >0  && strlen($_REQUEST['tarpStorey']) >0 && strlen($_REQUEST['tarpPitch']) >0){

       $invoice_item_id = $_REQUEST['in_i'];    
       $uom_type = $_REQUEST['uom_type'];         
       $tarp_area = $_REQUEST['tarpArea']; 
       $tarp_time = $_REQUEST['tarpTime']; 
       $tarp_storey = $_REQUEST['tarpStorey'];  
       $tarp_pitch = $_REQUEST['tarpPitch'];         
       
       $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 == 4){

        $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($tarp_area == $invoice_item_uom_value){
                         $checker = $checker + 1;
                     }
                     $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $tarp_area 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($tarp_time == $invoice_item_uom_value){
                         $checker = $checker + 1;
                     }
 
                     $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $tarp_time 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_val3'){ 
                    $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($tarp_storey == $invoice_item_uom_value){
                        $checker = $checker + 1;
                    }

                    $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $tarp_pitch 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_val4'){ 
                    $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($tarp_pitch == $invoice_item_uom_value){
                        $checker = $checker + 1;
                    }

                    $query_insert_bid_item_uom_value = "UPDATE invoice_item_uom_value SET 	invoice_item_uom_value_amount = $tarp_storey 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 == 4){
            echo "same";
         http_response_code(200);
         exit;
        }
         $formulaType ="tarp";       
        

         
 
         $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; //Installed Square Feet Cost  
                 
             }elseif($uom_field_name == 'maps_uom_val2'){
                $maps_uom_val2 = $maps_uom_value_amount;//Emergency After Hours (5pm-6am)
                
             }elseif($uom_field_name == 'maps_uom_val3'){
                $maps_uom_val3 = $maps_uom_value_amount;  //8/12 Roof Pitch Charge
                  
             }elseif($uom_field_name == 'maps_uom_val4'){
                $maps_uom_val4 = $maps_uom_value_amount;  //9/12 Roof Pitch Charge
                 
             }elseif($uom_field_name == 'maps_uom_val5'){
                $maps_uom_val5 = $maps_uom_value_amount;  // 10/12 Roof Pitch Charge
                
            }elseif($uom_field_name == 'maps_uom_val6'){
                $maps_uom_val6 = $maps_uom_value_amount;  // 11/12 Roof Pitch Charge
                
            }elseif($uom_field_name == 'maps_uom_val7'){
              $maps_uom_val7 = $maps_uom_value_amount;  // 12/12 Roof Pitch Charge
                
            }elseif($uom_field_name == 'maps_uom_val8'){
               $maps_uom_val8 = $maps_uom_value_amount;  // 2 Story Upcharge
                
            }elseif($uom_field_name == 'maps_uom_val9'){
              $maps_uom_val9 = $maps_uom_value_amount;  // 3 Story Upcharge
                
            }
 
         }
 
     }
     
 
     if($formulaType = "tarp"){    
        $invoice_item_total_new = tarpFormula($tarp_area, $tarp_time, $tarp_storey, $tarp_pitch, $maps_uom_val1, $maps_uom_val2, $maps_uom_val3, $maps_uom_val4,$maps_uom_val5,$maps_uom_val6,$maps_uom_val7,$maps_uom_val8,$maps_uom_val9);  
      
     }
 
      /*  */ 
      
      $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 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, 'bid_item_total' => $invoice_item_total_new, 'm_id' => $maps_id);
          
        if($tarp_time == '1'){
            $tarp_time = 'Normal';
        }elseif($tarp_time == '2'){
          $tarp_time = "Emergency";
           
        }
        if($tarp_storey == '1'){
            $tarp_storey = 'No Multi-Storey Upcharge';
        }elseif($tarp_storey == '2'){
            $tarp_storey = "2 Storey Upcharge";
           
        }elseif($tarp_storey == '3'){
            $tarp_storey = "3 Storey Upcharge";
             
          }
        if($tarp_pitch == '1'){
            $tarp_pitch = 'No Steep Pitch Upcharge';
        }elseif($tarp_pitch == '2'){
            $tarp_pitch = "8/12 Roof Pitch";           
        }elseif($tarp_pitch == '3'){
            $tarp_pitch = "9/12 Roof Pitch";           
        }elseif($tarp_pitch == '4'){
            $tarp_pitch = "10/12 Roof Pitch";           
        }elseif($tarp_pitch == '5'){
            $tarp_pitch = "11/12 Roof Pitch";           
        }elseif($tarp_pitch == '6'){
            $tarp_pitch = "12/12 Roof Pitch";           
        }
           $query_insert_log = "INSERT INTO change_log SET $active_user_id change_text = '$invoice_maps_name- TARP dimensions changed to Area SQFT- $tarp_area, Timing- $tarp_time, Roof Pitch- $tarp_pitch, No of Stories- $tarp_storey', 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;
}
?>

