<?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);
$_SESSION['job_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()
{	
	//$this->Image('../admin/images/beb_background.jpg', 0, 0, $this->w, $this->h);
	 if($this->page == 1){
		// Logo
		$this->Image('../admin/images/logo.png',125,15,60);
		$this->SetXY(170, 40);
		$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(32,5,$company_address,0,"R");
						
		$company_phone = $row["company_phone"];
		$sales_email = $row["sales_email"];
		$company_website = $row["company_website"];
		$this->SetXY(174, 70);
		$this->Cell(25,5,$company_phone,0,"R");
		$this->Ln();
		$this->SetXY(152, 75);
		$this->Cell(30,5,$sales_email,0);
		$this->Ln();
		$this->SetXY(157, 80);
		$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);
		
		$height = 5;
		$this->SetTextColor(0,0,0);
		$y = $this->GetY() + $height;
		$this->SetY($this->GetY() + $height);
		$this->SetX($this->GetX());
		
		$this->SetFont('Arial','B',9);
		$this->SetY($this->GetY() - 44);
		$this->SetX($this->GetX() + 110);
		$this->MultiCell(0,$height,"Date:");
		
		
		$row1 = utility::getTableRowByField("job","id",$_SESSION['job_id']);
	
		$row1=utility::validate_crypt_data($row1);
		
		$quotation_date = date("jS M Y",strtotime($row1['quotation_date']));
		if($row1['expiry_date']=='0000-00-00 00:00:00'){
			$expiry_date = date("jS M Y",strtotime('+1 month',strtotime($row1['quotation_date'])));
		}else{
			$expiry_date = date("jS M Y",strtotime($row1['expiry_date']));
		}
		
		$this->SetFont('Arial','',9);
		$this->SetX($this->GetX() + 110);		
		$this->MultiCell(0,$height,$quotation_date);
		
		$this->SetFont('Arial','B',9);
		$this->SetY($this->GetY() + 2);
		$this->SetX($this->GetX() + 110);
		$this->MultiCell(0,$height,"Expiry Date:");
		
		$this->SetFont('Arial','',9);
		$this->SetX($this->GetX() + 110);		
		$this->MultiCell(0,$height,$expiry_date);
		
		$this->SetFont('Arial','B',9);
		$this->SetY($this->GetY() + 2);
		$this->SetX($this->GetX() + 110);
		$this->MultiCell(0,$height,"");
	
		$this->SetFont('Arial','',9);
		$this->SetX($this->GetX() + 110);		
		$this->MultiCell(0,$height,"");
		
		$this->SetFont('Arial','B',9);
		$this->SetY($this->GetY() + 2);
		$this->SetX($this->GetX() + 110);
		$this->MultiCell(0,$height,"VAT Number:");
		
		$this->SetFont('Arial','',9);
		$this->SetX($this->GetX() + 110);		
		$this->MultiCell(0,$height,"290268393");
		
		
	}	
		// Line break
		$this->Ln(1);
		//$this->Rect(5, 5, 200, 286);
}
function normalInvoice($job_id)
{
	global $config,$gsi;
	$this->SetFont('Arial','',10);
	$row = utility::getTableRowByField("job","id",$job_id);
	$row=utility::validate_crypt_data($row);
	$q_no = $row["quotation_no"];
	//$quotation_date = utility::getFormatedDateReturn($row["quotation_date"]);
	$quotation_date = date("jS F Y",strtotime($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"]."\n";
	}
	if(isset($row["address2"]) && $row["address2"] !=""){
		$address .= $row["address2"]."\n";
	}
	if(isset($row["address3"]) && $row["address3"] !=""){
		$address .= $row["address3"]."\n";
	}
	if(isset($row["city"]) && $row["city"] !=""){
		$address .= $row["city"]."\n";
	}
	if(isset($row["county"]) && $row["county"] !=""){
		$address .= $gsi->getCountyById($row['county'])."\n";
	}
	if(isset($row["postcode"]) && $row["postcode"] !=""){
		$address .= $row['postcode'];
	}
	$this->AddPage();
	$this->SetLeftMargin(10);
	$height = 6;
	
	$hhh = 5;
	$this->SetFont('Arial','B',9);
	$this->SetY($this->GetY() - 23);
	$this->SetX($this->GetX() + 110);
	$this->MultiCell(0,$hhh,"Quote Number:");


	$this->SetFont('Arial','',9);
	$this->SetY($this->GetY() + 0);
	$this->SetX($this->GetX() + 110);		
	$this->MultiCell(0,$hhh,$row['quotation_no_with_initial']);
	
	$this->SetFont('Arial','',21);
	$this->SetY($this->GetY() - 35);
	$this->MultiCell(195,15,"QUOTE",0);
	
	$this->SetFont('Arial','',10);
	$this->Cell(150,5,$company_name,0);
	$this->SetY($this->GetY() + 5);
	$this->MultiCell(70,5,$address,0,"L");
	
	
	
	
		
	$this->SetY($this->GetY() + 28);
	//$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','B',9);
	$this->Cell(100,$height,'Description',0);
	$this->Cell(20,$height,'Quantity',0);
	$this->Cell(35,$height,'Unit Price',0);
	$this->Cell(15,$height,'VAT',0);
	//$this->Cell(20,$height,'',0);
	$this->Cell(22,$height,'Amount GBP',0,0,"R");
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 6);
	$this->SetX(10);
	$this->Cell(193,0,"",0,1,'L',true);
	
	
	/* $this->SetY($this->GetY()+4);
	$y = $this->GetY()+8;
	//$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',9);
	$this->MultiCell(100,$height,$row["job_description"],0); */
	
	$this->SetY($this->GetY()+1);
	$y = $this->GetY()+8;
	//$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',9);
	/* $sql_for_form_element = "SELECT * FROM quotation_items_mapping WHERE job_id = '$job_id' ORDER BY id ";
	$rs_for_form_element = mysqli_query($conn,$sql_for_form_element);
	if(mysqli_num_rows($rs_for_form_element) > 0){
		while($row_for_form_element = mysqli_fetch_assoc($rs_for_form_element)){
	$this->MultiCell(100,$height,$row_for_form_element["quotation_items_descrip"],0);
	}} */
	
	$H = $this->GetY();
	$vat = $gsi->getvatTypeById($row["vat"]);
	if($row["vat"] == 1){
		$vat_value = 0;
	}else if($row["vat"] == 2){
		$vat_value = 5;
	}else if($row["vat"] == 3){
		$vat_value = 20;
	}
	$sql_for_form_element = "SELECT * FROM quotation_items_mapping WHERE job_id = '$job_id' ORDER BY id ";
	$rs_for_form_element = mysqli_query($conn,$sql_for_form_element);
	if(mysqli_num_rows($rs_for_form_element) > 0){
		while($row_for_form_element = mysqli_fetch_assoc($rs_for_form_element)){
			$total = $row_for_form_element["qty_list"] * $row_for_form_element["quotation_items_price"];
			$vat_amount = ($total * $vat_value)/100;
			$total_with_vat = $total + $vat_amount;
			$final_total = $final_total + $total + $vat_amount;
			
			$this->SetY($this->GetY()+4);
			//$y = $this->GetY()+8;
			//$quotation_items_descrip = str_replace('','',$row_for_form_element["quotation_items_descrip"]);
			$this->MultiCell(90,$height-1.5,$row_for_form_element["quotation_items_descrip"],0);
			
			//$this->SetY($y-8);
			$height22 = $height - 10;
			$this->SetX($this->GetX()+100);
			$this->Cell(20,$height22,$row_for_form_element["qty_list"],0);
			$this->Cell(35,$height22,number_format($row_for_form_element["quotation_items_price"],2),0);
			$this->Cell(15,$height22,$vat,0);
			$this->Cell(22,$height22,number_format($total_with_vat,2),0,0,"R");
			//$y = $y + 6;
			
			// hr line
	$this->SetFillColor(212, 212, 212);
	$this->SetY($this->GetY() + 6);
	$this->SetX(10);
	$this->Cell(193,0,"",0,1,'L',true);
		}
	}
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 7);
	$this->SetX($this->GetX() + 101);
	$this->Cell(92,0,"",0,1,'R',true);
	
	$this->SetY($this->GetY() + 2);
	$this->SetX($this->GetX() + 140);
	$this->SetFont('Arial','',9);
	$this->Cell(30,$height,'Subtotal',0);
	$this->Cell(22,$height,number_format($final_total,2),0,0,"R");
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 8);
	$this->SetX($this->GetX() + 101);
	$this->Cell(92,0,"",0,1,'R',true);
	
	$this->SetY($this->GetY() + 2);
	$this->SetX($this->GetX() + 140);
	$this->SetFont('Arial','B',9);
	$this->Cell(30,$height,'TOTAL GBP',0);
	$this->Cell(22,$height,number_format($final_total,2),0,0,"R");
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 7);
	$this->SetX($this->GetX() + 101);
	$this->Cell(92,0,"",0,1,'R',true);
	
	$this->SetY($this->GetY() + 50);
	//$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','B',9);
	$this->Cell(100,$height,'Terms',0);
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 6);
	$this->SetX(10);
	$this->Cell(193,0,"",0,1,'L',true);
	
	
	$this->SetY($this->GetY()+4);
	$y = $this->GetY()+8;
	//$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',9);
	$terms = utility::getTableFieldById('term','terms','term_type',$row["term_type"]);
	$this->MultiCell(100,$height,$terms,0);
	$sql_doc = "SELECT * FROM documents WHERE job_id = '$job_id' AND move_doc_status = '1' ORDER BY id ";
	$rs_doc = mysqli_query($conn,$sql_doc);
	$a = 0;
	$b = 1;
	if(mysqli_num_rows($rs_doc) > 0){
		$this->AddPage();
		while($d = mysqli_fetch_assoc($rs_doc)){
			$row_no = intval($a/4) + 1;
			$col_no = $b%4;
			/*if($row_no == 1){
				$y_axis = 10;
			}elseif($row_no == 2){
				$y_axis = 50;
			}elseif($row_no == 3){
				$y_axis = 90;
			}elseif($row_no == 4){
				$y_axis = 130;
			}*/
			$y_axis = 40*$row_no - 30;
			if($col_no == 1){
				$x_axis = 10;
			}else{
				$x_axis = $x_axis + 50;
			}
			$document_name = $d["document_name"];
			//echo $y_axis."-->".$x_axis."<br>";
			$this->Image('../document/'.$document_name,$x_axis,$y_axis,40);
			$a++;
			$b++;
		}
	}
	/*$this->MultiCell(100,$height,'We also offer finance please get in touch for details
All quotes are fixed price unless significant issues arise that could not be seen beforehand
This quote is subject to our terms and conditions
All quotes are valid for 30 days',0);*/
	
}
function Footer(){
	global $config;
	$vat_reg_number = utility::getTableFieldById("sites","vat_reg_number","id",1);
	$height = 10;
	$this->SetTextColor(0,0,0);
	$this->SetY(280);
	$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->Image('../admin/images/footer.jpg',5, $this->GetY()+7, 200,22);
	
	$this->SetFont('Arial','',8);
	$this->Cell(191,$height,'Registered Office: 155 The Drive, Kingsley, Northampton, NN1 4SW',0,0,'L');
}
}
$pdf = new PDF();
$title = 'Quote';
$pdf->SetTitle($title);
$pdf->SetAuthor('krscrm');
//$job_id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));
$di 	= utility::app_crypt($_REQUEST["di"],'d');
$job_id = $_REQUEST['id'];
if($di != $job_id){
	die('invalid url');
}
$_SESSION['job_id']=$job_id;
$pdf->normalInvoice($job_id);
$pdf->Output("quotation.pdf","I");
if($_SERVER['HTTP_HOST'] == "localhost"){
	$filename="../quotation_template/quotation_template_".$job_id;
}else{
	$filename="../quotation_template/quotation_template_".$job_id;
}
$pdf->Output($filename.'.pdf','F');
unset($_SESSION['job_id']);
?>