<?php
include("../../../include/startup.php"); 
if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_SI_item'){
	$returnArray = array();
	$si_item_mapping_id = $_REQUEST['si_item_mapping_id'];
	$delete_sql = "DELETE FROM sales_invoice_mapping WHERE id = '$si_item_mapping_id'";
	if(mysqli_query($conn,$delete_sql)){
		$returnArray["status"] = "success";
	}else{
		$returnArray["status"] = "failure";
	}
	
	echo json_encode($returnArray);
	die;
}elseif(isset($_REQUEST['action']) && $_REQUEST['action']=='get_supplier_address_details'){
	$returnArray = array();
	$supplier_id = $_REQUEST['supplier_id'];
    $sql = "SELECT * FROM supplier_management WHERE id = $supplier_id ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row = utility::validate_crypt_data($row);
		
		$subcontractor_detail = "";
		if(isset($row["address1"]) && $row["address1"] !=""){
			$subcontractor_detail .= $row["address1"].", ";
		}
		if(isset($row["address2"]) && $row["address2"] !=""){
			$subcontractor_detail .= $row["address2"].", ";
		}
		if(isset($row["address3"]) && $row["address3"] !=""){
			$subcontractor_detail .= $row["address3"].", ";
		}
		if(isset($row["town_city"]) && $row["town_city"] !=""){
			$subcontractor_detail .= $row["town_city"].", ";
		}
		if(isset($row["postcode"]) && $row["postcode"] !=""){
			$subcontractor_detail .= $row['postcode'].", ";
		}
		if(isset($row["country"]) && $row["country"] !=""){
			$subcontractor_detail .= $gsi->getCountryById($row['country']);
		}
		$returnArray["status"] = "success";
		$returnArray["sub_contractor_telephone"] = $row['account_manager_telephone'];
		$returnArray["sub_contractor_address"] = $subcontractor_detail;
		
	}
	echo json_encode($returnArray);
	die;
}elseif(isset($_REQUEST['action']) && $_REQUEST['action']=='get_site_address_by_site_id'){
	$returnArray = array();
	//$site_sub_contractor_check = $_REQUEST['site_sub_contractor_check'];
	$site_id = $_REQUEST['site_id'];
	$org_id = $_REQUEST['org_id'];
	$sql = "SELECT * FROM site_management WHERE id = $site_id ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row = utility::validate_crypt_data($row);
		//$priority_site = $row['priority_site'];
		$site_address_detail = "";
		if(isset($row["address1"]) && $row["address1"] !=""){
			$site_address_detail .= $row["address1"].", ";
		}
		if(isset($row["address2"]) && $row["address2"] !=""){
			$site_address_detail .= $row["address2"].", ";
		}
		if(isset($row["address3"]) && $row["address3"] !=""){
			$site_address_detail .= $row["address3"].", ";
		}
		if(isset($row["address4"]) && $row["address4"] !=""){
			$site_address_detail .= $row["address4"].", ";
		}
		if(isset($row["postcode"]) && $row["postcode"] !=""){
			$site_address_detail .= $row['postcode'].",";
		}
		if(isset($row["country"]) && $row["country"] !=""){
			$site_address_detail .= $gsi->getCountryById($row['country']);
		}
		$returnArray["status"] = "success";
		$returnArray["site_address"] = $site_address_detail;
		$returnArray["is_site_needs_po"] = $row['is_needs_po'];
		$returnArray["contact_name"] = $row['contact_name'];
		$returnArray["telephone"] = $row['telephone'];
		$returnArray["email"] = $row['email'];
		$returnArray["access_times"] = $row['access_times'];
		$returnArray["parking"] = $row['parking'];
		$returnArray["site_encrypt_id"] = urlencode(utility::app_crypt($row['id'],'e'));
		$returnArray["site_priority_site_exist"] = $row['priority_site'];
		
		if($org_id !=""){
			$returnArray["is_client_needs_po"] = utility::getTableFieldById("organisation_management","is_needs_po","id",$org_id);
		}
		
	}
	
	$site_notes_qry = "SELECT id FROM notes WHERE site_id = '$site_id' AND mark_as_important = '1'";
	$site_notes_sql = mysqli_query($conn,$site_notes_qry);
	if(mysqli_num_rows($site_notes_sql) > 0){
		$returnArray["site_important_notes_exist"] = "1";
	}
	echo json_encode($returnArray);
	die;
		
}elseif(isset($_REQUEST['action']) && $_REQUEST['action']=='get_sub_contract_address_by_contract_id'){
	$returnArray = array();
	$sub_contractor_id = $_REQUEST['sub_contractor_id'];
	
	$supplier_sql = "SELECT id,address1,address2,address3,town_city,postcode,country FROM supplier_management WHERE id = '$sub_contractor_id' ";
	$supplier_rs = mysqli_query($conn,$supplier_sql);
	if(mysqli_num_rows($supplier_rs) > 0){
		$supplier_row = mysqli_fetch_assoc($supplier_rs);
		$supplier_row = utility::validate_crypt_data($supplier_row);
		
		$sub_contractor_address = "";
		if(isset($supplier_row["address1"]) && $supplier_row["address1"] !=""){
			$sub_contractor_address .= $supplier_row["address1"].", ";
		}
		if(isset($supplier_row["address2"]) && $supplier_row["address2"] !=""){
			$sub_contractor_address .= $supplier_row["address2"].", ";
		}
		if(isset($supplier_row["address3"]) && $supplier_row["address3"] !=""){
			$sub_contractor_address .= $supplier_row["address3"].", ";
		}
		if(isset($supplier_row["town_city"]) && $supplier_row["town_city"] !=""){
			$sub_contractor_address .= $supplier_row["town_city"].", ";
		}
		if(isset($supplier_row["postcode"]) && $supplier_row["postcode"] !=""){
			$sub_contractor_address .= $supplier_row['postcode'].", ";
		}
		if(isset($supplier_row["country"]) && $supplier_row["country"] !=""){
			$sub_contractor_address .= $gsi->getCountryById($supplier_row['country']);
		}
		$returnArray["status"] = "success";
		$returnArray["sub_contractor_address"] = $sub_contractor_address;
	}
	echo json_encode($returnArray);
	die;
		
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='delete_PO'){
	$po_id = $_REQUEST['po_id'];
	mysqli_query($conn,"DELETE FROM purchase_order WHERE id = '$po_id' ");
	mysqli_query($conn,"DELETE FROM purchase_order_items_mapping WHERE po_id = '".$po_id."' "); 
	
	$returnArray["status"] = "success";
	echo json_encode($returnArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_part_description_by_part_number'){
	$dataArray = array();
	$dataArray["status"] = "failure";
	$part_number = $_REQUEST['part_number'];
    $sql = "SELECT id,product_description FROM item_management WHERE id = '$part_number' ";
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$dataArray["status"] = "success";
		$dataArray["product_description"] = $row['product_description'];
	}
	echo json_encode($dataArray);
}else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'get_address_dropdown') {
	$dataArray = array();
	$dataArray["delivery_add_id"]=array();
	$dataArray["status"] = 0;
	$customer_id = $_REQUEST['customer_id'];
	$sql = "SELECT id,delivery_address_1 FROM client_delivery_address_mapping  WHERE customer_id = '$customer_id'";
	$rs = mysqli_query($conn, $sql);
	if (mysqli_num_rows($rs) > 0) {

		while ($row = mysqli_fetch_array($rs)) {
			$row = utility::validate_crypt_data($row);
			
			$dataArray["delivery_add_id"][] = array("delivery_address_1" => $row['delivery_address_1'], "id" => $row["id"]);
		}
		$dataArray["status"] = 1;
	}
	echo json_encode($dataArray);
}
else if (isset($_REQUEST['action']) && $_REQUEST['action'] == 'get_customer_drodown') {
	$dataArray = array();
	$dataArray["customer_id"]=array();
	$dataArray["status"] = 0;
	$order_id = $_REQUEST['order_id'];
	$customer_id =  utility::getTableFieldById("work_order","customer_id","id",$order_id);
	$sql = "SELECT id,customer_name FROM client_management  WHERE id = '$customer_id'";
	$rs = mysqli_query($conn, $sql);
	if (mysqli_num_rows($rs) > 0) {

		while ($row = mysqli_fetch_array($rs)) {
			$row = utility::validate_crypt_data($row);
			
			$dataArray["customer_id"][] = array("customer_name" => $row['customer_name'], "id" => $row["id"]);
		}
		$dataArray["status"] = 1;
	}
	echo json_encode($dataArray);
}else if(isset($_REQUEST['action']) && $_REQUEST['action']=='get_customer_id_address'){
	$dataArray = array();
	$dataArray["status"] = 0;
	$delivery_add_id = $_REQUEST['delivery_add_id'];
	$sql = "SELECT id,customer_id,delivery_address_1 ,delivery_address_2, delivery_address_3,delivery_add_postcode FROM client_delivery_address_mapping WHERE id = '$delivery_add_id' ";
	
	
	$rs = mysqli_query($conn,$sql);
	if(mysqli_num_rows($rs) > 0){
		$row = mysqli_fetch_assoc($rs);
		$row=utility::validate_crypt_data($row);
		$invoice_address_1 =  utility::getTableFieldById("client_management","invoice_address_1","id",$row['customer_id']);
    	$invoice_address_2 =  utility::getTableFieldById("client_management","invoice_address_2","id",$row['customer_id']);
	    $invoice_address_3 =  utility::getTableFieldById("client_management","invoice_address_3","id",$row['customer_id']);
	    $invoice_add_postcode =  utility::getTableFieldById("client_management","invoice_add_postcode","id",$row['customer_id']);
	
		$dataArray["delivery_address_1"]  = $row['delivery_address_1'];
		$dataArray["delivery_address_2"]  = $row['delivery_address_2'];
		$dataArray["delivery_address_3"]  = $row['delivery_address_3'];
		$dataArray["delivery_add_postcode"]  = $row['delivery_add_postcode'];
		$dataArray["invoice_address_1"]  = $invoice_address_1;
		$dataArray["invoice_address_2"]  = $invoice_address_2;
		$dataArray["invoice_address_3"]  = $invoice_address_3;
		$dataArray["invoice_add_postcode"]  = $invoice_add_postcode;
		$dataArray["status"]		= 1;
	}
	echo json_encode($dataArray);

}

die;
?>