<?php
include "include/db.php";
include "include/auth.php";

if(isset($_REQUEST['in_no']) && isset($_REQUEST['state'])){
    if(strlen($_REQUEST['in_no']) >0 && strlen($_REQUEST['state']) >0){
		$sign_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
		$sign_run = mysqli_query($conn, $sign_query);
		$sign_row = mysqli_fetch_array($sign_run);

		$sign_path = $sign_row['pdf_sign'];
		$sign_path = "pdf_sign.php?i=$sign_path";	

		$sign_path_i = "<br><img src='$sign_path' height='120' width='240' class='sign-img'>";
		$question_bid = '';
		$inv_message_query = "SELECT * FROM pdf_admin_data WHERE pdf_admin_id = 1";
		$inv_message_run = mysqli_query($conn, $inv_message_query);
		$inv_message_row = mysqli_fetch_array($inv_message_run);
		$invoice_message = $inv_message_row['pdf_inv_message'];
		$invoice_logo = $inv_message_row['logo_small'];	

		$invoice_id = $_REQUEST['in_no'];
		$state = $_REQUEST['state'];
        $query_check_wo = "SELECT * FROM invoice WHERE invoice_id = $invoice_id AND is_delete IS NULL";
        $run_check_wo = mysqli_query($conn, $query_check_wo);

        if(mysqli_num_rows($run_check_wo) ==1){
            while($row_check_wo = mysqli_fetch_array($run_check_wo)){
				$invoice_no = $row_check_wo['invoice_no'];
				$invoice_amount = $row_check_wo['invoice_amount'];
				$is_system_bid = $row_check_wo['is_system_bid'];
				$usr_name = '';
				$bid_admin_id = $row_check_wo['admin_id'];
				if(isset($bid_admin_id)){
					if(strlen($bid_admin_id) > 0){
						$fetch_admin_name_query = "SELECT admin_fname,admin_lname FROM admin WHERE admin_id = $bid_admin_id";
						$fetch_admin_name_run = mysqli_query($conn, $fetch_admin_name_query);
						if(mysqli_num_rows($fetch_admin_name_run)==1){
							$fetch_admin_name_row = mysqli_fetch_array($fetch_admin_name_run);
						
							$admin_fname_auth = $fetch_admin_name_row['admin_fname'];
							$admin_lname_auth = $fetch_admin_name_row['admin_lname'];
							$usr_name = $admin_fname_auth. " ". $admin_lname_auth;
						}
					}
				}
				$bid_employee_id = $row_check_wo['employee_id'];
				if(isset($bid_employee_id)){
					if(strlen($bid_employee_id) > 0){
						$fetch_emp_name_query = "SELECT employee_fname,employee_lname FROM employee WHERE employee_id = $bid_employee_id";
						$fetch_emp_name_run = mysqli_query($conn, $fetch_emp_name_query);
						if(mysqli_num_rows($fetch_emp_name_run)==1){
							$fetch_emp_name_row = mysqli_fetch_array($fetch_emp_name_run);
						
							$employee_fname_auth = $fetch_emp_name_row['employee_fname'];
							$employee_lname_auth = $fetch_emp_name_row['employee_lname'];
							$usr_name = $employee_fname_auth. " ". $employee_lname_auth;
						}
					}
				}
				$bid_wo_no = '';
				$bid_wo_no = $row_check_wo['bid_wo_no'];
				$bid_amount = 0;
				$bid_amount = $row_check_wo['bid_amount'];
				$bid_amount = number_format($bid_amount, 2, '.', '');
				$invoice_amount = number_format($invoice_amount	, 2);
				$invoice_amount = "$ ".$invoice_amount;
				$client_id = $row_check_wo['client_id'];
				$date = $row_check_wo['create_time'];
				$timestamp = strtotime($date);
				$date= date('m-d-Y', $timestamp);

				$date1 = $row_check_wo['create_time'];
				$timestamp1 = strtotime ( '30 days' , strtotime ( $date1 ) ) ;
				$date1 = date('m-d-Y', $timestamp1);
				$prop_id = $row_check_wo['prop_id'];
				$query_prop_detail = "SELECT * FROM property WHERE prop_id = $prop_id";
                $run_prop_detail = mysqli_query($conn, $query_prop_detail);

                if(mysqli_num_rows($run_prop_detail) ==1){
                    while($row_prop_detail = mysqli_fetch_array($run_prop_detail)){
						$prop_case_num = $row_prop_detail['prop_case_num'];
						$prop_full_add = $row_prop_detail['prop_full_add'];
						$prop_county = $row_prop_detail['prop_county'];
						$prop_state = $row_prop_detail['prop_state'];
						$prop_city = $row_prop_detail['prop_city'];
						$prop_zip = $row_prop_detail['prop_zip'];
                    }
                }


                $query_client_detail = "SELECT * FROM client WHERE client_id = $client_id";
                $run_client_detail = mysqli_query($conn, $query_client_detail);

                if(mysqli_num_rows($run_client_detail) ==1){
                    while($row_client_detail = mysqli_fetch_array($run_client_detail)){
						$client_alias = $row_client_detail['client_alias'];
						$client_name = $row_client_detail['client_name'];
						$client_address = $row_client_detail['client_address'];
                    }
                }
            }            
        }else{
            header("Location:index.php");  
        }
    }else{
        header("Location:index.php");    
    }
}else{
    header("Location:index.php");
}

require "invoicr.php";
$invoice = new Invoicr();
$logo_url = "images/". $invoice_logo;


$invoice->set("company", [
	"$logo_url",
	"$logo_url", 
	"<span class='comp-name'><b>Complete Property Restoration</b></span>", 
	"<span class='comp-add'>92 Cornerstone #148 Cary NC 27513<span>",
	"<span class='comp-add'>Phone: 919.651.0240<span>",
	"<span class='comp-add'>https://www.propertycpr.com<span>",
	"<span class='comp-add'>admin@propertycpr.com<span>"
]);

// 2B - INVOICE INFO
$invoice->set("invoice", [
	["Created by", "$usr_name"],
	["INVOICE #", "$invoice_no"],
	["Date Created", "$date"],
	["Expiration Date", "$date1"],
	
	
]);
/* add to top array["P.O. #", "CB-789-123"],
	["Due Date", "2011-12-12"] 
	,
	"zfsdfsd fsfsdfadsa sdads sasas",*/

// 2C - BILL TO
$str_client = strlen($client_name);

if($str_client > 30){
	$str_client_set = "set";
}else{
	$str_client_set = "";
}

$invoice->set("billto", [
	"$client_name",
	"$client_address"
]);

// 2D - SHIP TO
$invoice->set("shipto", [
	"$client_name",
	"$prop_full_add, $prop_county", 
	"$prop_city, $prop_state, $prop_zip"
]);

// 2E - ITEMS
// YOU CAN JUST DUMP THE ENTIRE ARRAY IN USING SET, BUT HERE IS HOW TO ADD ONE AT A TIME... 
$query_fetch_items = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id";
$run_fetch_items = mysqli_query($conn, $query_fetch_items);


$query_fetch_items = "SELECT * FROM invoice_item WHERE invoice_id = $invoice_id";
$run_fetch_items = mysqli_query($conn, $query_fetch_items);
$items = array();

while($row_fetch_items = mysqli_fetch_array($run_fetch_items)){
	$invoice_item_id = $row_fetch_items['invoice_item_id'];
	$maps_id = $row_fetch_items['maps_id'];
	$invoice_item_quantity = '';
	$invoice_item_amount = '';
	
    $pic1 = "";
	$pic2 = "";
	$pic3 = "";
	$pic4 = "";

	$isset_uom2_eq = "";
	$isset_uom2_cut = "";

	$uom2_eq_1 = "";
	$uom2_eq_2 = "";
	$uom2_eq_3 = "";

	$uom2_cut_1 = "";
	$uom2_cut_2 = "";
	$uom2_cut_3 = "";
	$query_maps = "SELECT * FROM maps WHERE maps_id = $maps_id";
	$run_maps = mysqli_query($conn, $query_maps);
	$row_maps = mysqli_fetch_array($run_maps);
	$maps_name = $row_maps['maps_name'];
	$maps_name = strtoupper($maps_name);
            

    $invoice_item_desc = $row_fetch_items['invoice_item_desc'];

    if($invoice_item_desc == NULL){          
    	$invoice_item_desc = $row_maps['maps_desc'];
    }
    
	$invoice_item_uom = $row_fetch_items['invoice_item_uom'];       
	 
	if($invoice_item_uom == 0 || $invoice_item_uom == 1 || $invoice_item_uom == 5 || $invoice_item_uom == 6 || $invoice_item_uom == 7 || $invoice_item_uom == 8){
		$invoice_item_amount = $row_fetch_items['invoice_item_amount'];       
	}
	if($invoice_item_uom == 0){
		$invoice_item_quantity = $row_fetch_items['invoice_item_quantity'];
		if($invoice_item_quantity == NULL){
			$invoice_item_quantity = 1;
		}       
	}
	if($invoice_item_uom == 5){
		$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'];
				$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'];
					$invoice_item_uom_value = number_format($invoice_item_uom_value, 2, '.', '');
					$invoice_item_quantity = $invoice_item_uom_value.' HR';
				}

			}
							
		}      
	}

	if($invoice_item_uom == 1){
		$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'];
				$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);

					$length = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];               
				}

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

					$width = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
				} 
				$sf= $length * $width;
				$sf = number_format($sf, 2, '.', '');
				$invoice_item_quantity = $sf . ' SF';   
			}				
		}
	}

	if($invoice_item_uom == 6){
		$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'];
				$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);

					$length = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];}
				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);

					$width = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];

				}  
				$ui= $length * $width;
				$ui = number_format($ui, 2, '.', '');
				$invoice_item_quantity = $ui . ' UI';
			} 			
		}
	}

	if($invoice_item_uom == 7){
		$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'];
				$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);

					$length = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];                  
				}

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

					$width = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
				}
				
				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);

					$height = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
				}
				$cyd= $length * $width * $height;
				$cyd= $cyd / 27;
				$cyd = number_format($cyd, 2, '.', '');
				$invoice_item_quantity = $cyd . ' CYD';  

			}
							
		}
	}

	if($invoice_item_uom == 8){
		$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'];
				$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);

					$inches = $row_select_bid_item_uom_value['invoice_item_uom_value_amount'];
					$lf = $inches / 12;  
					$lf = number_format($lf, 2, '.', '');
					$invoice_item_quantity = $lf . ' LF';                  
				}

			}
							
		}
	}
	    $uom2_name1 = '';
		$uom2_name2 = '';
		$uom2_val1 = '';
		$uom2_val2 = '';
		

	if($invoice_item_uom == 2){
		$invoice_item_amount = $row_fetch_items['invoice_item_amount'];
		$invoice_item_total = $row_fetch_items['invoice_item_total'];
		if($invoice_item_total > $invoice_item_amount){
			$query22 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'maps' AND uom_field_name = 'maps_uom_val3'";
            $run22 = mysqli_query($conn, $query22);

            if(mysqli_num_rows($run22) > 0){
                $row22 = mysqli_fetch_array($run22);
                $invoice_uom2_maps_uom_val3 = $row22['uom_field_id'];
			}
			
			$query222 = "SELECT * FROM maps_uom_value WHERE maps_id = $maps_id AND uom_field_id = $invoice_uom2_maps_uom_val3";
            $run222 = mysqli_query($conn, $query222);

            if(mysqli_num_rows($run222) > 0){
                $row222 = mysqli_fetch_array($run222);
                $maps_uom_value_amount3 = $row222['maps_uom_value_amount'];
                if($maps_uom_value_amount3 > 0){
                    $isset_uom2_eq = "set";

					$uom2_eq_1 = "SPECIAL EQUIPMENT:";
					$uom2_eq_2 = "Grass is over 3ft tall and will require additional special equipment in order to be cut.";
					$maps_uom_value_amount3 = number_format($maps_uom_value_amount3, 2, '.', '');
					$uom2_eq_3 = "$maps_uom_value_amount3";
                }      
            }
			
			$query33 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_type = 'maps' AND uom_field_name = 'maps_uom_val4'";
            $run33 = mysqli_query($conn, $query33);

            if(mysqli_num_rows($run33) > 0){
                $row33 = mysqli_fetch_array($run33);
                $invoice_uom2_maps_uom_val4 = $row33['uom_field_id'];
            }

           
            $query333 = "SELECT * FROM maps_uom_value WHERE maps_id = $maps_id AND uom_field_id = $invoice_uom2_maps_uom_val4";
            $run333 = mysqli_query($conn, $query333);

            if(mysqli_num_rows($run333) > 0){
                $row333 = mysqli_fetch_array($run333);
                $maps_uom_value_amount4 = $row333['maps_uom_value_amount'];
                if($maps_uom_value_amount4 > 0){
                    $isset_uom2_cut = "set";

					$uom2_cut_1 = "CLIPPINGS:";
					$uom2_cut_2 = "Pick up Grass remnant will need to be raked and bagged due to height of grass and excessive clippings.";
					$maps_uom_value_amount4 = number_format($maps_uom_value_amount4, 2, '.', '');
					$uom2_cut_3 = "$maps_uom_value_amount4";
                }
                
            }	
			
		}

		$uom2_name1 = 'Lot Size (SQF)';
		$uom2_name2 = 'Grass height (inches)';	
		
		$fetch_uom2_query1 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val1' AND uom_field_type = 'bidin'";
		$fetch_uom2_run1 = mysqli_query($conn, $fetch_uom2_query1);
		if(mysqli_num_rows($fetch_uom2_run1) == 1){
			$fetch_uom2_row1 = mysqli_fetch_array($fetch_uom2_run1);
			$fetch_uom2_id1 = $fetch_uom2_row1['uom_field_id'];
		}

		$fetch_uom2_val_query1 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom2_id1";
		$fetch_uom2_val_run1 = mysqli_query($conn, $fetch_uom2_val_query1);
		$num_uom_value1 = mysqli_num_rows($fetch_uom2_val_run1);
		$fetch_uom2_val_row1 = mysqli_fetch_array($fetch_uom2_val_run1);
		$uom2_val1 = $fetch_uom2_val_row1['invoice_item_uom_value_amount'];
		if($uom2_val1 == NULL){
			$uom2_val1 = '';
		}
		if($uom2_val1 == 0){
			$uom2_val1 = '';
		}

		$fetch_uom2_query2 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val2' AND uom_field_type = 'bidin'";
		$fetch_uom2_run2 = mysqli_query($conn, $fetch_uom2_query2);
		if(mysqli_num_rows($fetch_uom2_run2) == 1){
			$fetch_uom2_row2 = mysqli_fetch_array($fetch_uom2_run2);
			$fetch_uom2_id2 = $fetch_uom2_row2['uom_field_id'];
		}

		$fetch_uom2_val_query2 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom2_id2";
		$fetch_uom2_val_run2 = mysqli_query($conn, $fetch_uom2_val_query2);
		$num_uom_value2 = mysqli_num_rows($fetch_uom2_val_run2);
		$fetch_uom2_val_row2 = mysqli_fetch_array($fetch_uom2_val_run2);
		$uom2_val2 = $fetch_uom2_val_row2['invoice_item_uom_value_amount'];
		if($uom2_val2 == NULL){
			$uom2_val2 = '';
		}
		if($uom2_val2 == 0){
			$uom2_val2 = '';
		}		
       
	}
	$uom4_name1 = '';
	$uom4_name2 = '';
	$uom4_name3 = '';
	$uom4_name4 = '';
	$uom4_val1 = '';
	$uom4_val2 = '';
	$uom4_val3 = '';
	$uom4_val4 = '';


	if($invoice_item_uom == 4){
		$uom4_name1 = 'Area SQFT';
		$uom4_name2 = 'Timing';
		$uom4_name3 = 'Roof Pitch';
		$uom4_name4 = 'No of Stories';	

		
		$fetch_uom4_query1 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val1' AND uom_field_type = 'bidin'";
		$fetch_uom4_run1 = mysqli_query($conn, $fetch_uom4_query1);
		if(mysqli_num_rows($fetch_uom4_run1) == 1){
			$fetch_uom4_row1 = mysqli_fetch_array($fetch_uom4_run1);
			$fetch_uom4_id1 = $fetch_uom4_row1['uom_field_id'];
		}

		$fetch_uom4_val_query1 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom4_id1";
		$fetch_uom4_val_run1 = mysqli_query($conn, $fetch_uom4_val_query1);
		$num_uom_value1 = mysqli_num_rows($fetch_uom4_val_run1);
		$fetch_uom4_val_row1 = mysqli_fetch_array($fetch_uom4_val_run1);
		$uom4_val1 = $fetch_uom4_val_row1['invoice_item_uom_value_amount'];
		if($uom4_val1 == NULL){
			$uom4_val1 = '';
		}
		if($uom4_val1 == 0){
			$uom4_val1 = '';
		}
//
		$fetch_uom4_query2 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val2' AND uom_field_type = 'bidin'";
		$fetch_uom4_run2 = mysqli_query($conn, $fetch_uom4_query2);
		if(mysqli_num_rows($fetch_uom4_run2) == 1){
			$fetch_uom4_row2 = mysqli_fetch_array($fetch_uom4_run2);
			$fetch_uom4_id2 = $fetch_uom4_row2['uom_field_id'];
		}

		$fetch_uom4_val_query2 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom4_id2";
		$fetch_uom4_val_run2 = mysqli_query($conn, $fetch_uom4_val_query2);
		$num_uom_value2 = mysqli_num_rows($fetch_uom4_val_run2);
		$fetch_uom4_val_row2 = mysqli_fetch_array($fetch_uom4_val_run2);
		$uom4_val22 = $fetch_uom4_val_row2['invoice_item_uom_value_amount'];

		if($uom4_val22 == '1'){ 
			$uom4_val2 = 'Normal Hours (6am-5pm)';			
		}elseif($uom4_val22 == '2'){ 
			$uom4_val2 = "Emergency After Hours (5pm-6am)";
	    }
		if($uom4_val22 == NULL){
			$uom4_val2 = '';
		}
		if($uom4_val22 == 0){
			$uom4_val2 = '';
		}
		//
		$fetch_uom4_query3 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val3' AND uom_field_type = 'bidin'";
		$fetch_uom4_run3 = mysqli_query($conn, $fetch_uom4_query3);
		if(mysqli_num_rows($fetch_uom4_run3) == 1){
			$fetch_uom4_row3 = mysqli_fetch_array($fetch_uom4_run3);
			$fetch_uom4_id3 = $fetch_uom4_row3['uom_field_id'];
		}

		$fetch_uom4_val_query3 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom4_id3";
		$fetch_uom4_val_run3 = mysqli_query($conn, $fetch_uom4_val_query3);
		$num_uom_value3 = mysqli_num_rows($fetch_uom4_val_run3);
		$fetch_uom4_val_row3 = mysqli_fetch_array($fetch_uom4_val_run3);
		$uom4_val33 = $fetch_uom4_val_row3['invoice_item_uom_value_amount'];

		if($uom4_val33 == '1'){ 
			$uom4_val3 = "No Steep Pitch Upcharge";			
		}elseif($uom4_val33 == '2'){ 
			$uom4_val3 = "8/12 Roof Pitch";
		}elseif($uom4_val33 == '3'){ 
			$uom4_val3 = "9/12 Roof Pitch";
		}elseif($uom4_val33 == '4'){ 
			$uom4_val3 = "10/12 Roof Pitch";
		}elseif($uom4_val33 == '5'){ 
			$uom4_val3 = "11/12 Roof Pitch";
		}elseif($uom4_val33 == '6'){ 
			$uom4_val3 = "12/12 Roof Pitch";
		}
		
		if($uom4_val33 == NULL){
			$uom4_val3 = '';
		}
		if($uom4_val33 == 0){
			$uom4_val3 = '';
		}
//
		$fetch_uom4_query4 = "SELECT * FROM uom_field WHERE uom_id = $invoice_item_uom AND uom_field_name = 'bidin_uom_val4' AND uom_field_type = 'bidin'";
		$fetch_uom4_run4 = mysqli_query($conn, $fetch_uom4_query4);
		if(mysqli_num_rows($fetch_uom4_run4) == 1){
			$fetch_uom4_row4 = mysqli_fetch_array($fetch_uom4_run4);
			$fetch_uom4_id4 = $fetch_uom4_row4['uom_field_id'];
		}

		$fetch_uom4_val_query4 = "SELECT * FROM invoice_item_uom_value WHERE invoice_item_id = $invoice_item_id AND invoice_item_uom_field_id = $fetch_uom4_id4";
		$fetch_uom4_val_run4 = mysqli_query($conn, $fetch_uom4_val_query4);
		$num_uom_value4 = mysqli_num_rows($fetch_uom4_val_run4);
		$fetch_uom4_val_row4 = mysqli_fetch_array($fetch_uom4_val_run4);
		$uom4_val44 = $fetch_uom4_val_row4['invoice_item_uom_value_amount'];

		if($uom4_val44 == '1'){ 
			$uom4_val4 = "No Multi-Storey Upcharge";			
		}elseif($uom4_val44 == '2'){ 
			$uom4_val4 = "2 Storey Upcharge";
		}elseif($uom4_val44 == '3'){ 
			$uom4_val4 = "3 Storey Upcharge";
		}

		if($uom4_val44 == NULL){
			$uom4_val4 = '';
		}
		if($uom4_val44 == 0){
			$uom4_val4 = '';
		}
//		
		
       
	}

	$invoice_item_total = $row_fetch_items['invoice_item_total']; 
	$invoice_item_total = number_format($invoice_item_total, 2);
	




$image_array = array();
	
$bid_item_image = "SELECT * FROM invoice_actual_image WHERE item_id = $invoice_item_id ORDER BY image_id ASC";
$bid_item_image_run = mysqli_query($conn, $bid_item_image);

$existing = mysqli_affected_rows($conn);
$remaining = 30 - $existing;

while($bid_item_image_row = mysqli_fetch_array($bid_item_image_run)){
	$image_name = $bid_item_image_row['image_name'];
	$image_path_i = 'maps_act_image.php?i='.$image_name;
	array_push($image_array, "$image_path_i");
}
for($i=0; $i<$remaining; $i++){
	
	$image_path = "";
	
	array_push($image_array, $image_path);
}	

if($is_system_bid == 1){
   if(isset($bid_wo_no) && isset($bid_amount)){
		if(strlen($bid_wo_no) > 0 && $bid_amount > 0){
			$question_bid = 'yes';	
		}
   }  

}				
				
$items_r = ["$maps_name", "$invoice_item_desc", $invoice_item_quantity, "$invoice_item_amount" , "$invoice_item_total", "$pic1","$pic2","$pic3","$pic4","$invoice_item_uom","$uom4_name1", "$uom4_name2","$uom4_name3","$uom4_name4","$uom4_val1","$uom4_val2","$uom4_val3","$uom4_val4","$uom2_name1","$uom2_name2","$uom2_val1","$uom2_val2","$isset_uom2_eq","$isset_uom2_cut","$uom2_eq_1","$uom2_eq_2","$uom2_eq_3","$uom2_cut_1","$uom2_cut_2","$uom2_cut_3","$sign_path_i","$str_client_set"];

$items_r = array_merge($items_r, $image_array);
$invoice->add("items", $items_r); 

}

/* $bid_item_a_pic */

// 2F - TOTALS
$invoice->set("totals", [
	
	["GRAND TOTAL", "$invoice_amount","$question_bid","$bid_amount","$bid_wo_no"]
]);

/*above array ["SUB-TOTAL", "$108.00"],
	["DISCOUNT 10%", "-$10.80"], */

// 2G - NOTES, IF ANY
$invoice->set("notes", [
	"<div class='invoice-mess-div'><p class='invoice-mess'><b>$invoice_message</b></p>","$sign_path_i"
	
]);


/* [STEP 3 - OUTPUT] */
// 3A - CHOOSE TEMPLATE, DEFAULTS TO SIMPLE IF NOT SPECIFIED

$invoice->template("blueberry");

/*****************************************************************************/
// 3B - OUTPUT IN HTML
// DEFAULT DISPLAY IN BROWSER | 1 DISPLAY IN BROWSER | 2 FORCE DOWNLOAD | 3 SAVE ON SERVER
 //$invoice->outputHTML();
 if($state == 'download'){
	$invoice->outputPDF(2, "$invoice_no.pdf");
}elseif($state == 'view'){ 
$invoice->outputHTML(1);
}
// $invoice->outputHTML(2, "invoice.html");
// $invoice->outputHTML(3, __DIR__ . DIRECTORY_SEPARATOR . "invoice.html");
/*****************************************************************************/
// 3C - PDF OUTPUT
// DEFAULT DISPLAY IN BROWSER | 1 DISPLAY IN BROWSER | 2 FORCE DOWNLOAD | 3 SAVE ON SERVER
// $invoice->outputPDF();
//$invoice->outputPDF(1);
//$invoice->outputPDF(2, "$invoice_no.pdf");
//$invoice->outputPDF(3, __DIR__ . DIRECTORY_SEPARATOR . "invoice.pdf");
/*****************************************************************************/
// 3D - DOCX OUTPUT
// DEFAULT FORCE DOWNLOAD| 1 FORCE DOWNLOAD | 2 SAVE ON SERVER
// $invoice->outputDOCX();
// $invoice->outputDOCX(1, "invoice.docx");
// $invoice->outputDOCX(2, __DIR__ . DIRECTORY_SEPARATOR . "invoice.docx");
/*****************************************************************************/
?>