<?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;
	$row = utility::getTableRowByField("job","id",$job_id);
	$q_no = $row["quotation_no"];
	$quotation_date = utility::getFormatedDateReturn($row["quotation_date"]);
	$package_information_details = $row["package_information_details"];
	$first_name = $row["first_name1"];
	$last_name = $row["last_name1"];
	$company_name = $row["company_name"];
	$job_start_date = utility::getFormatedDateReturn($row["job_start_date"]);
	$job_end_date = utility::getFormatedDateReturn($row["job_end_date"]);
	if($row["enquiry_type"] == 1){
		$package_name = $gsi->getFixedPackagesById($row["fixed_packages"]);
		$package_month_support = "12 months";
		if($row["fixed_packages"] == 4){
			$package_month_support = "2 years";
		}
		$fixed_amount = number_format($row["fixed_amount"],2)." + VAT";
		$no_of_hours_to_be_worked = "Fixed price";
		$hourly_rate = "N/A";
	}else{
		$package_name = $gsi->getHourlyPackagesById($row["hourly_packages"])." months";
		$no_of_hours_to_be_worked = $gsi->getHourlyPackagesById($row["hourly_packages"])." months";
		$fixed_amount = "N/A";
		$total_hours = $row["total_hours"];
		$hourly_amount = $row["hourly_amount"];
		$hourly_rate_without_format = ($hourly_amount/$total_hours);
		$hourly_rate = number_format(($hourly_amount/$total_hours),2);
	}
	$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'];
	}
	
	$consultant_id = utility::getTableFieldById("job_admin_mapping","admin_id","job_id",$job_id);
	$consultant_name = utility::getTableFieldById("admin","name","id",$consultant_id);
	
	
	$this->AddPage();
	$this->SetLeftMargin(10);
	$height = 6;
	$this->SetFont('Arial','B',10);
	
	$pound_sign = utf8_decode("£");
	$heading = $total_hours.' HOURS OVER '.strtoupper($no_of_hours_to_be_worked).' - '.$pound_sign.number_format($hourly_amount,2).' + VAT';
	$this->Cell(191,$height,$heading,0,0,'C');
	$this->SetY($this->GetY() + 6);
	$this->SetFont('Arial','B',10);
	$this->Cell(191,$height,'CONSULTANCY SERVICES AGREEMENT',0,0,'C');
	
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY() + 6);
	$this->Cell(191,$height,$quotation_date,0,0,'C');
	
	$this->SetY($this->GetY() + 10);
	$this->Cell(70,$height,'THE CLIENT : ',0,0,'L');
	$this->MultiCell(121,$height,ucfirst($company_name)."\n".$address,0,'L');
	
	$this->SetY($this->GetY() + 2);
	$this->Cell(70,$height,'PERIOD COVERED : ',0,0,'L');
	$this->MultiCell(121,$height,"$job_start_date To $job_end_date INCLUSIVE",0,'L');
	$this->SetY($this->GetY() + 2);
	$this->Cell(70,$height,'REVIEW / EXTENSION DATE : ',0,0,'L');
	$this->Cell(121,$height,"April 2017",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'NO. OF HOURS : ',0,0,'L');
	$h = $total_hours - 16;
	$this->Cell(121,$height,"$h hours x $pound_sign 60 + VAT per hour + 16 hours FOC = $total_hours hours",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'WORKSTO BE CARRIED OUT : ',0,0,'L');
	$this->Cell(121,$height,"The client's normal day to day contractual and legal business",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'PREMISES ATTENDED : ',0,0,'L');
	$this->Cell(121,$height,"Client's place of Business and/or BEB",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'HOURS OF WORK WHEN WITH CLIENT : ',0,0,'L');
	$this->Cell(121,$height,"9 am - 5 pm",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'TRAVELLING TIME : ',0,0,'L');
	$this->Cell(121,$height,"Included in fee",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'MILEAGE : ',0,0,'L');
	$this->Cell(121,$height,"Charged at ".$pound_sign."0.45 per mile",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'EXCLUSIONS : ',0,0,'L');
	$this->MultiCell(121,$height,"Other activities requested by the Client outside of the scope of the Client's day to day Contractual and Legal Business - To be agreed between the parties prior to commencement of the works",0,'L');
	
	$this->SetY($this->GetY() + 5);
	$this->SetFont('Arial','B',10);
	$this->Cell(191,$height,'SCHEDULE OF RATES',0,0,'C');
	
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'Monthly Rate : ',0,0,'L');
	$hourly_amount_in_num_format = number_format($hourly_amount,2);
	$this->MultiCell(121,$height,"$package_name payments of ".$pound_sign."$hourly_amount_in_num_format + VAT\nTravel costs where BEB has travelled to the Client or elsewhere at the Client's request",0,'L');
	$this->SetY($this->GetY() + 2);
	$this->Cell(70,$height,'Payment Terms : ',0,0,'L');
	$this->Cell(121,$height,"Strictly 30 days from invoice date.",0,0,'L');
	
	$this->SetY($this->GetY() + 10);
	$this->SetFont('Arial','B',10);
	$this->Cell(191,$height,'SCHEDULE OF RATES FOR WORKS THAT FALL OUTSIDE OF THIS AGREEMENT',0,0,'C');
	
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'Hourly Rate : ',0,0,'L');
	$this->Cell(121,$height,$pound_sign.$hourly_rate." + VAT per hour",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'Daily Rate : ',0,0,'L');
	$daily_rate = number_format($hourly_rate_without_format * 8,2);
	$this->Cell(121,$height,$pound_sign.$daily_rate." + VAT (".$pound_sign.$hourly_rate." per hour based on a 8 hour day)",0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'Travelling Rate : ',0,0,'L');
	$this->Cell(121,$height,'Charged at '.$pound_sign.'0.45 per mile',0,0,'L');
	$this->SetY($this->GetY() + 7);
	$this->Cell(70,$height,'Hotels & Subsistence : ',0,0,'L');
	$this->MultiCell(121,$height,'When requested by the client, expenses associated with staying away will be charged at cost together with an overnight fee of '.$pound_sign.'80.',0,'L');
	
	
	$this->SetY($this->GetY() + 10);
	$this->Cell(70,$height,'SIGNED BY '.strtoupper($company_name).' : ',0,0,'L');
	$y = $this->GetY();
		
	$this->SetX($this->SetX() + 140);
	$this->Cell(20,$height,'NAME : ',0,0,'L');
	$this->Cell(70,$height,ucfirst($first_name)." ".ucfirst($last_name),0,0,'L');
	$y = $this->GetY()+5;
	$this->Line(85,$y,135,$y);
	
	$this->SetY($this->GetY() + 10);
	$this->Cell(70,$height,'SIGNED BY BEB CONSULTANCY (UK) LTD : ',0,0,'L');
	$y = $this->GetY()-3;
	$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,100,$y,25);
		}
	}
	
	$this->SetX($this->SetX() + 140);
	$this->Cell(20,$height,'NAME : ',0,0,'L');
	$this->Cell(70,$height,$consultant_name,0,0,'L');
	$y = $this->GetY()+5;
	//$this->Line(85,$y,135,$y);
}
function Footer(){
	global $config;
	$vat_reg_number = utility::getTableFieldById("sites","vat_reg_number","id",1);
	$height = 10;
	$this->SetTextColor(0,0,0);
	$this->SetY(260);
	$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');
	$this->SetY($this->GetY() + 10);
	$this->SetX($this->GetX() + 10);
	$this->SetFillColor(152,251,152);
	$this->Cell(170,$height,'Subject to our terms and conditions, available upon request.',0,0,'C',true);
}
}
$pdf = new PDF();
$title = 'Retainer Agreement';
$pdf->SetTitle($title);
$pdf->SetAuthor('BEBCRM');
$job_id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));
$pdf->normalInvoice($job_id);
$pdf->Output("retainer_agreement","I");
/*if($_SERVER['HTTP_HOST'] == "localhost"){
	$filename="../quotation_template/instruction_to_proceed_".$job_id;
}else{
	$filename="/home4/easysolu/public_html/{$other_fields["project_folder_name"]}quotation_template/instruction_to_proceed_".$job_id;
}
$pdf->Output($filename.'.pdf','F');*/
?>