<?php
include("../include/startup.php");
/*error_reporting(E_ALL);
if(!isset($_SESSION['login'])){
	die("Please login to admin panel first");
}*/

session_start();
//$orderDetails = $orders->getOrder($id);

require('fpdf.php');
class PDF extends FPDF
{
protected $id =1;
protected $fontName ="Times";
protected $fontSize =12;
protected $tableHeaderHeight = 8;
protected $tableDescriptionHeight = 6;
//protected $orderDetails = $orders->getOrder($id);

function Header()
{	
	if($this->page == 1){
		// Logo
		$this->Image('../admin/images/logo.png',160,7,40);
		$this->SetXY(170, 27);
		$this->SetFont('Arial','',10);
		$row = utility::getTableRowByField("sites","id",1);
		$company_address = $row["company_address"];
		$company_address = str_replace("&nbsp;"," ",$company_address);
		$company_address = str_replace("â€™","'",$company_address);
		$company_address = str_replace("&lsquo;"," ",$company_address);
		$company_address = str_replace("&rsquo;"," ",$company_address);
		$this->MultiCell(30,5,$company_address,0,"R");
		
		$company_phone = $row["company_phone"];
		$sales_email = $row["sales_email"];
		$company_website = $row["company_website"];
		$this->SetXY(174, 59);
		$this->Cell(30,5,$company_phone,0,"R");
		$this->Ln();
		$this->SetXY(155, 64);
		$this->Cell(30,5,$sales_email,0);
		$this->Ln();
		$this->SetXY(155, 69);
		$this->Cell(30,5,$company_website,0);
		// hr line
		//$this->SetFillColor(122,138,144);
		//$this->SetY($this->GetY() + 10);
		//$this->SetX(5);
		//$this->Cell(200,0,"",0,1,'L',true);
	}	
	// Line break
	$this->Ln(1);
	$this->Rect(5, 5, 200, 286);
}
function normalInvoice($job_id)
{
	global $config,$bebcrm;
	$this->SetFont('Arial','',10);
	$row = utility::getTableRowByField("job","id",$job_id);
	$q_no = $row["id"];
	$quotation_date = utility::getFormatedDateReturn($row["quotation_date"]);
	$first_name = $row["first_name1"];
	$last_name = $row["last_name1"];
	$company_name = $row["company_name"];
	$email = $row["email"];
	
	$address = "";
	if(isset($row["address1"]) && $row["address1"] !=""){
		$address .= $row["address1"];
	}
	if(isset($row["address2"]) && $row["address2"] !=""){
		$address .= " ".$row["address2"];
	}
	if(isset($row["address3"]) && $row["address3"] !=""){
		$address .= " ".$row["address3"];
	}
	if(isset($row["city"]) && $row["city"] !=""){
		$address .= " ".$row["city"];
	}
	if(isset($row["county"]) && $row["county"] !=""){
		$address .= " ".$gsi->getCountyById($row['county']);
	}
	if(isset($row["postcode"]) && $row["postcode"] !=""){
		$address .= " ".$row['postcode'];
	}
	
	if($row["enquiry_type"] == 1){
		$package_name = $gsi->getFixedPackagesById($row["fixed_packages"])." Package";
		$package_month_support = "12 months";
		if($row["fixed_packages"] == 4){
			$package_month_support = "2 years";
		}
		$fixed_amount = $row["fixed_amount"];
		$package_amount = number_format($row["fixed_amount"],2);
		$first_stage_payment = floor($row["fixed_amount"]/3);
		$vat = ($first_stage_payment * 0.2);
		$first_stage_payment_with_vat = $first_stage_payment + $vat;
		$last_stage_payment = $fixed_amount - (2*$first_stage_payment);
		if($row["fixed_packages"] == 4){
			$first_stage_payment = floor($row["fixed_amount"]/4);
			$vat = ($first_stage_payment * 0.2);
			$first_stage_payment_with_vat = $first_stage_payment + $vat;
			$last_stage_payment = $fixed_amount - (3*$first_stage_payment);
		}
	}else{
		$package_name = $gsi->getHourlyPackagesById($row["hourly_packages"])." months";
		$package_amount = number_format($row["hourly_amount"],2);
		$package_month_support = $gsi->getHourlyPackagesById($row["hourly_packages"])." months";
		$hourly_amount = $row["hourly_amount"];
		$first_stage_payment = floor($row["hourly_amount"]/3);
		$vat = ($first_stage_payment * 0.2);
		$first_stage_payment_with_vat = $first_stage_payment + $vat;
		$last_stage_payment = $hourly_amount - (2*$first_stage_payment);
	}
	$first_stage_payment = number_format($first_stage_payment,2);
	$last_stage_payment = number_format($last_stage_payment,2);
	$first_stage_payment_with_vat = number_format($first_stage_payment_with_vat,2);
	
	$this->AddPage();
	$this->SetLeftMargin(10);
	$height = 6;
	
	$this->Cell(150,5,$company_name,0);
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(40,5,$address,0,"L");
	$this->SetY($this->GetY());
	$this->Cell(150,5,$email,0);
	$this->SetY($this->GetY()+10);
	$this->Cell(150,5,"Date : ".$quotation_date,0);
	
	$this->SetY($this->GetY()+7);
	$this->Cell(150,5,"Dear ".ucfirst($first_name)." ".ucfirst($last_name).",",0);
	
	$this->SetY($this->GetY()+7);
	$this->MultiCell(195,5,"Many thanks for returning our instruction form duly signed and for your kind instructions.",0,"L");
	$consultant_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$consultant_name = utility::getTableFieldById("admin","name","id",$consultant_id);
	if(strtolower($consultant_name) == "bev brazier"){
		$string_to_print = "a Graduate Member of the Chartered Institute of Legal Executives";
	}else{
		$string_to_print = "an Affiliate Member of the Chartered Institute of Legal Executives";
	}
	$this->SetY($this->GetY()+2);
	$this->MultiCell(195,5,"I confirm that I shall be carrying out and managing these works and that I am $string_to_print , whose members are independently regulated by CILEx Regulation.",0,"L");
	$this->SetY($this->GetY()+2);
	$this->MultiCell(195,5,"I confirm that I shall be carrying out the following works as instructed by you : ",0,"L");
	$package_information_details = $row["package_information_details"];
	$this->SetY($this->GetY() + 3);
	$this->SetX($this->GetX() + 3);
	$this->MultiCell(195,5,$package_information_details,0,"L");
	$this->SetY($this->GetY()+2);
	$this->MultiCell(195,5,"I confirm that we have agreed fees as follows : ",0,"L");
	$this->SetY($this->GetY()+5);
	$this->SetFont('Arial','B',10);
	$this->Cell(150,5,$package_name,0);
	
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY()+7);
	$this->MultiCell(195,5,"Preparation of bespoke terms & conditions and * further documents as detailed above",0,"L");
	
	if($row["enquiry_type"] == 1){
		$this->SetY($this->GetY() + 3);
		$this->Cell(125,5,"Total for all works",0,"L");
		$this->Cell(60,5,"= £".$package_amount." + VAT",0,"L");
	}else{
		$total_hours = $row["total_hours"];
		$hourly_amount = $row["hourly_amount"];
		$hourly_rate = number_format(($hourly_amount/$total_hours),2);
		$this->SetY($this->GetY() + 3);
		$this->MultiCell(195,5,"Fees will be charged at an hourly rate of £$hourly_rate p/h + VAT and we estimate an initial number of hours of £$package_amount.",0,"L");
	}
	$this->SetFont('Arial','B',10);
	$this->SetY($this->GetY()+7);
	$this->MultiCell(195,5,"Additional Flexible Support Package",0,"L");
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY()+2);
	$this->MultiCell(195,5,"12 hours of contract and legal support during a 12 month period ",0,"L");
	$this->SetY($this->GetY() + 1);
	$this->Cell(125,5,"",0,"L");
	$this->Cell(60,5,"=£69 + VAT per month",0,"L");
	
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(195,5,"What is unique to us is that we can also provide on-going assistance in the form of a 12 month support package for just £69 + VAT per month.  This allows you to refer any queries on any of the documentation that we have prepared, by either you or your clients, back to us, giving you peace of mind that any issues will be resolved and that you will be supported through the entire implementation stage.  Should any alterations be required at any stage during this 12 month period, you will have priority service over our non-contract clients.",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->MultiCell(195,5,"As agreed, our payment terms will be £$first_stage_payment + VAT deposit, £$first_stage_payment + VAT invoiced on issue of the first drafts and £$last_stage_payment + VAT invoiced on the issue of the final documents.",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->MultiCell(195,5,"If you have opted to add our flexible support to your package, this gives you 12 hours of contract and legal support to be used as and when required within a 12 month period which commences from when your final documentation is issued. Payment terms are £69 + VAT to be paid monthly by direct debit, for a minimum term of 12 months.",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->MultiCell(195,5,"All works are subject to our terms and conditions, copies of which are available upon request.",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->MultiCell(195,5,"We are registered with the Information Commissioner’s Office under registration reference Z1467723. All of our Client information is held in compliance with the Data Protection Act 1998.",0,"L");
	
	$this->SetY($this->GetY() + 3);
	$this->MultiCell(195,5,"We do hold Professional Indemnity Insurance for our Legal Consultancy as follows : ",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->Cell(100,5,"Policy Number : ",0);
	$this->Cell(50,5,"10218712",0);
	$this->SetY($this->GetY() + 7);
	$this->Cell(100,5,"Professional Indemnity Limit : ",0);
	$this->Cell(50,5,"£250,000",0);
	
	$this->SetY($this->GetY() + 7);
	$this->Cell(100,5,"Insurance Company : ",0);
	$this->MultiCell(50,5,"CNA / HARDY\n13th Floor\n20 Fenchurch Street\nLondon\nEC3M 3BY\n\nTel: +44 (0)20 7743 6800",0,"L");
	$this->SetY($this->GetY() + 7);
	$this->Cell(100,5,"Insurance Broker : ",0);
	$this->MultiCell(50,5,"Stuart & Co.\nStuart House\nRamsay Court\nHinchingbrooke Business Park\nHuntingdon\nCambridgeshire\nPE29 6FY\n\nTel: 01480 433443",0,"L");
	$this->SetY($this->GetY() + 3);
	$this->Cell(100,5,"Expiry date : ",0);
	$this->Cell(50,5,"31st July 2017",0);
	$this->SetY($this->GetY() + 7);
	$this->MultiCell(195,5,"Should for any reason you not be happy with the service that we provide, please follow our Customer Complaints Procedure, details of which we enclose.",0,"L");
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(195,5,"I am confident that you will be happy with the service I provide.  Therefore please tell my regulator what you think of the service you have received.  Visit http://www.cilexregulation.org.uk then click on the link entitled “For Consumers” and take the client survey.",0,"L");
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(195,5,"I shall of course keep you up to date on the progress of these works but should you have any queries, please do not hesitate to give me a call.  Our normal office hours are 8 am – 5 pm Monday – Friday. Outside of these hours you may contact me on 07930 408157 / 07983 415690.  Should I not be available, I shall endeavour to make contact with you the following morning.",0,"L");
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(195,5,"I look forward to working with you.",0,"L");
	
	$consultant_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$consultant_name = utility::getTableFieldById("admin","name","id",$consultant_id);
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(195,5,"Yours sincerely",0,"L");
	
	$y = $this->GetY();
	$consultant_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$signature = utility::getTableFieldById("admin","signature","id",$consultant_id);
	if(isset($signature) && $signature !=""){
		if (is_file('../signature/'.$signature)) {
			$this->Image('../signature/'.$signature,7,$y,40);
		}
	}
	
	$this->SetY($this->GetY()+ 35);
	$this->MultiCell(195,5,ucfirst($consultant_name),0,"L");
	$this->SetY($this->GetY());
	$designation = utility::getTableFieldById("admin","designation","id",$consultant_id);
	$this->MultiCell(195,5,ucfirst($designation),0,"L");
	$this->SetY($this->GetY());
	$phone = utility::getTableFieldById("admin","phone","id",$consultant_id);
	if(isset($phone) && $phone !=""){
		$this->MultiCell(195,5,ucfirst($phone),0,"L");
	}
	$this->SetY($this->GetY());
	$phone1 = utility::getTableFieldById("admin","phone1","id",$consultant_id);
	if(isset($phone1) && $phone1 !=""){
		$this->MultiCell(195,5,ucfirst($phone1),0,"L");		
	}
	$this->SetY($this->GetY());
	$email_admin = utility::getTableFieldById("admin","email_admin","id",$consultant_id);
	if(isset($email_admin) && $email_admin !=""){
		$this->MultiCell(195,5,ucfirst($email_admin),0,"L");
	}
	$this->SetY($this->GetY());
	$this->MultiCell(195,5,"For and on behalf of",0,"L");
	$this->SetY($this->GetY());
	$this->MultiCell(195,5,"BEB Consultancy (UK) Ltd",0,"L");
}
function Footer(){
	global $config;
	$vat_reg_number = utility::getTableFieldById("sites","vat_reg_number","id",1);
	$height = 10;
	$this->SetTextColor(0,0,0);
	$this->SetY(270);
	$this->SetX($this->GetX() + 2);
	$this->SetFont('Arial','',10);
	
	$this->SetY($this->GetY() + 6);
	$this->SetX(5);
	$this->Cell(200,0,"",0,1,'L',true);
	$this->SetY($this->GetY() + 2);
	$this->SetFont('Arial','',10);
	$this->Cell(191,$height,'BEB Consultancy (UK) Ltd    Registered in England & Wales no 6663021   VAT No. '.$vat_reg_number,0,0,'C');
}
}
$pdf = new PDF();
$title = 'Client Care Letter';
$pdf->SetTitle($title);
$pdf->SetAuthor('BEBCRM');
//$job_id = $_REQUEST['id'];
$job_id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));
$pdf->normalInvoice($job_id);
$pdf->Output("client_care_letter","I");
/*if($_SERVER['HTTP_HOST'] == "localhost"){
	$filename="../quotation_template/client_care_letter_".$job_id;
}else{
	$filename="/home4/easysolu/public_html/{$other_fields["project_folder_name"]}quotation_template/client_care_letter_".$job_id;
}
$pdf->Output($filename.'.pdf','F');*/
?>