<?
ob_clean();
include("../../startup.php"); 

###########################################  	END  FILTER CONITIONS 	#####################################
$invoice_ids = implode(",",$_REQUEST["invoice_ids"]);

$header = "ContactName,EmailAddress,POAddressLine1,POAddressLine2,POAddressLine3,POAddressLine4,POCity,PORegion,POPostalCode,POCountry,InvoiceNumber,Reference,InvoiceDate,DueDate,Total,InventoryItemCode,Description,Quantity,UnitAmount,Discount,AccountCode,TaxType,TaxAmount,TrackingName1,TrackingOption1,TrackingName2,TrackingOption2,Currency,BrandingTheme\n";

$data  = $header;
$sql = "SELECT * FROM invoice WHERE id IN ($invoice_ids) ORDER BY invoice_date DESC";

$rs = mysqli_query($conn,$sql);
while($row = mysqli_fetch_assoc($rs)){
	$job_id = $row["job_id"];
	$Description = $row["invoice_text"];

	$sql_for_enquiry = "SELECT * FROM job WHERE id = ".$job_id." ";
	$result_for_enquiry = mysqli_query($conn,$sql_for_enquiry);
	$row_for_enquiry = mysqli_fetch_array($result_for_enquiry);

	$county = "";
	if(isset($row_for_enquiry["county"]) && $row_for_enquiry["county"] != 0){
		$county =  $gsi->getCountyById($row_for_enquiry['county']);
	}

	$ContactName = ucfirst($row_for_enquiry['company_name']);
	$email = utility::app_crypt($row_for_enquiry['email'],'d');
	//$ContactName = ucfirst($row_for_enquiry['first_name1'])." ".ucfirst($row_for_enquiry['last_name1']);
	$initial_company_name = $gsi->getInitialOfCompanyName($row_for_enquiry["company_name"]);
	$TrackingOption1 = Keycode::getkeycodeById($row_for_enquiry["source"]);
	//$TrackingName2 = ucfirst($gsi->getJobTypeById($row_for_enquiry["enquiry_type"]));
	$TrackingName2 = "Consultancy/Private";
	if($row_for_enquiry["lead_consultant"] > 0){
		$lead_consultant = ucfirst(utility::getTableFieldById("admin","name","id",$row_for_enquiry["lead_consultant"]));
	}else{
		$consultant_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
		$lead_consultant = ucfirst(utility::getTableFieldById("admin","name","id",$consultant_id));
	}
	$lead_consultant_array = explode(" ",$lead_consultant);
	if($row_for_enquiry["enquiry_type"] == 1){
		$fixed_packages = $gsi->getFixedPackagesById($row_for_enquiry["fixed_packages"]);
		$TrackingOption2 = $lead_consultant_array[0]." - ".$fixed_packages;
	}else{
		$TrackingOption2 = $lead_consultant_array[0]." - Consultancy";
	}

	//$InvoiceNumber = $initial_company_name."/".date("m",strtotime(utility::getTableFieldById("invoice","invoice_date","id",$row["id"])))."/".date("y",strtotime(utility::getTableFieldById("invoice","invoice_date","id",$row["id"])))."/".$row["id"];
	$InvoiceNumber = $row["invoice_number"];
	$InvoiceDate = utility::getFormatedDateReturn(utility::getTableFieldById("invoice","invoice_date","id",$row["id"]));
	$DueDate = utility::getFormatedDateReturn(utility::getTableFieldById("invoice","due_date","id",$row["id"]));
	//$Total = number_format($row['amount'],2);
	$amount = round(($row['amount']* 1.2),2);
	$TaxAmount = round(($amount - $row['amount']),2);
	$Total = $amount - $TaxAmount;
	
	$data .= '"'.$ContactName.'"'.",".'"'.$email.'"'.",".'"'.$row_for_enquiry['address1'].'"'.",".'"'.$row_for_enquiry['address2'].'"'.",".'"'.$row_for_enquiry['address3'].'"'.",,".'"'.$row_for_enquiry['city'].'"'.",".'"'.$county.'"'.",".'"'.$row_for_enquiry['postcode'].'"'.",,".'"'.$InvoiceNumber.'"'.",,".'"'.$InvoiceDate.'"'.",".'"'.$DueDate.'"'.",".'"'.$amount.'"'.",,".'"'.$Description.'"'.",1,".'"'.$Total.'"'.",0,200,20% (VAT on Income),".'"'.$TaxAmount.'"'.",Source,".'"'.$TrackingOption1.'"'.",".'"'.$TrackingName2.'"'.",".'"'.$TrackingOption2.'"'.",GBP,"."\n";
}

$file_name = "xero_export.csv";
ob_clean();
echo $data;
header("Content-type: application/csv"); 
header("Content-Disposition: attachment; filename=$file_name");
header("Pragma: no-cache");
header("Expires: 0");
die;
?>