<?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()
{	
	// Logo
	$images = "logo.png";
	$this->Image('../admin/images/'.$images,130,5,50);
	// Arial bold 15
	$this->SetFont('Arial','B',13);
	// Title
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 20);
	$this->SetX(5);
	//$this->Cell(200,0,"",0,1,'L',true);
	// Line break
	$this->Ln(1);
	//$this->Rect(5, 5, 200, 286);
}

function InvoiceDetails($id)
{
	global $config,$gsi;
	$this->AddPage();
	$this->SetLeftMargin(20);
	$height = 5;
	$this->SetTextColor(0,0,0);
	$y = $this->GetY() + $height;
	$this->SetY($this->GetY() + $height);
	$this->SetX($this->GetX());
	$this->SetFont('Arial','',16);
	//////////////////// Customer Details ////////////////////
	$job_id = utility::getTableFieldById("invoice","job_id","id",$id);
	$row = utility::getTableRowByField("job","id",$job_id);
	
	$this->MultiCell(0,$height+5,"TAX INVOICE");
	$this->SetFont('Arial','',9);
	//$this->SetX($this->GetX() + 5);
	if(isset($row["company_name"]) && $row["company_name"] !=""){
		$this->MultiCell(0,$height,$row["company_name"]);
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["address1"]) && $row["address1"] !=""){
		$this->MultiCell(0,$height,$row["address1"]);
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["address2"]) && $row["address2"] !=""){
		$this->MultiCell(0,$height,$row["address2"]);
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["address3"]) && $row["address3"] !=""){
		$this->MultiCell(0,$height,$row["address3"]);
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["city"]) && $row["city"] !=""){
		$this->MultiCell(0,$height,$row["city"]);
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["county"]) && $row["county"] !=0){
		$this->MultiCell(0,$height,$gsi->getCountyById($row["county"]));
	}
	//$this->SetX($this->GetX() + 5);
	if(isset($row["postcode"]) && $row["postcode"] !=""){
		$this->MultiCell(0,$height,$row["postcode"]);
	}
	
	//////////////////// Invoice Details ////////////////////
	$invoice_date = utility::getFormatedDateReturn(utility::getTableFieldById("invoice","invoice_date","id",$id));
	$invoice_number = utility::getTableFieldById("invoice","invoice_number","id",$id);
	$this->SetY($y);
	$this->SetFont('Arial','B',9);
	$this->SetX($this->GetX() + 95);
	$this->MultiCell(0,$height,"Invoice Date");
	$this->SetFont('Arial','',9);
	$this->SetX($this->GetX() + 95);
	$this->MultiCell(0,$height,$invoice_date);
	$this->SetFont('Arial','B',9);
	$this->SetX($this->GetX() + 95);
	$this->MultiCell(0,$height,"Invoice Number");
	$this->SetFont('Arial','',9);
	$this->SetX($this->GetX() + 95);
	$initial_company_name = $gsi->getInitialOfCompanyName($row["company_name"]);
	//$this->MultiCell(0,$height,$initial_company_name."/".date("m")."/".date("y")."/".$id);
	if($invoice_number == ""){
		$invoice_number = $initial_company_name."/".date("m",strtotime(utility::getTableFieldById("invoice","invoice_date","id",$id)))."/".date("y",strtotime(utility::getTableFieldById("invoice","invoice_date","id",$id)))."/".$id;
	}
	$this->MultiCell(0,$height,$invoice_number);
	$this->SetFont('Arial','B',9);
	$this->SetX($this->GetX() + 95);
	$this->MultiCell(0,$height,"VAT Number");
	$this->SetFont('Arial','',9);
	$this->SetX($this->GetX() + 95);
	$vat_reg_number = utility::getTableFieldById("sites","vat_reg_number","id",1);
	$this->MultiCell(0,$height,$vat_reg_number);
	$this->SetFont('Arial','B',9);
	$this->SetX($this->GetX() + 95);
	$this->MultiCell(0,$height,"Company Reg. Number");
	$this->SetFont('Arial','',9);
	$this->SetX($this->GetX() + 95);
	$company_reg_number = utility::getTableFieldById("sites","company_reg_number","id",1);
	$this->MultiCell(0,$height,$company_reg_number);
	
	//////////////////// BEB Details ////////////////////
	$this->SetY($y);
	$this->SetFont('Arial','',9);
	$this->SetX($this->GetX() + 140);
	$sites_row = utility::getTableRowByField("sites","id",1);
		$company_address = $sites_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,"L");
	/*$this->MultiCell(0,$height,"BEB Consultancy (UK) Ltd");
	$this->SetX($this->GetX() + 140);
	$this->MultiCell(0,$height,"Office 3, St Luke's Centre");
	$this->SetX($this->GetX() + 140);
	$this->MultiCell(0,$height,"Mail Road");
	$this->SetX($this->GetX() + 140);
	$this->MultiCell(0,$height,"Duston");
	$this->SetX($this->GetX() + 140);
	$this->MultiCell(0,$height,"Northampton");
	$this->SetX($this->GetX() + 140);
	$this->MultiCell(0,$height,"NN5 6JB");*/
}

function OrderDetail($id)
{
	global $config;
	$row = utility::getTableRowByField("invoice","id",$id);
	$height = 7;
	$this->SetY($this->GetY() + 30);
	$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','B',8);
	$this->Cell(100,$height,'Description',0);
	$this->Cell(25,$height,'Quantity',0);
	$this->Cell(25,$height,'Unit Price',0);
	$this->Cell(20,$height,'VAT',0);
	$this->Cell(22,$height,'Amount GBP',0,0,"R");
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 6);
	$this->SetX(5);
	$this->Cell(200,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(80,$height,$row["invoice_text"],0);
	$H = $this->GetY();
	$this->SetY($y-8);
	$this->SetX($this->GetX()+90);
	$this->Cell(25,$height,'1.00',0);
	$this->Cell(25,$height,number_format($row["amount"],2),0);
	$this->Cell(20,$height,'20%',0);
	$this->Cell(22,$height,number_format($row["amount"],2),0,0,"R");
	
	/*$this->SetY($y+16);
	$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',8);
	$this->Cell(80,$height,'INVOICE 6 of 12',0);
	
	$this->SetY($y+23);
	$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',8);
	$this->Cell(80,$height,'4.25 hours remainaing',0);*/
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($H + 5);
	$this->SetX(5);
	$this->Cell(200,0,"",0,1,'L',true);
	
	$this->SetY($this->GetY() + 2);
	$this->SetX($this->GetX() + 131);
	$this->SetFont('Arial','',9);
	$this->Cell(30,$height,'Subtotal',0);
	$this->Cell(22,$height,number_format($row["amount"],2),0,0,"R");
	
	$this->SetY($this->GetY() + 8);
	$this->SetX($this->GetX() + 131);
	$this->Cell(30,$height,'TOTAL VAT 20%',0);
	$vat = $row["amount"]/5;
	$this->Cell(22,$height,number_format(($row["amount"]/5),2),0,0,"R");
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 7);
	$this->SetX($this->GetX() + 85);
	$this->Cell(100,0,"",0,1,'R',true);
	
	$this->SetY($this->GetY() + 2);
	$this->SetX($this->GetX() + 131);
	$this->SetFont('Arial','B',9);
	$this->Cell(30,$height,'Total',0);
	$this->Cell(22,$height,number_format($row["amount"] + $vat,2),0,0,"R");
	
	$this->SetY($this->GetY()+14);
	$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','B',9);
	$due_date = $row["due_date"];
	if($due_date != "0000-00-00 00:00:00"){
		$this->MultiCell(80,$height,"Due Date: ".date("d M Y",strtotime($due_date)),0);
	}else{
		$this->MultiCell(80,$height,"Due Date: ",0);
	}
	$this->SetX($this->GetX()-10);
	$this->SetFont('Arial','',8);
	$this->MultiCell(80,$height,"Bank: Lloyds Bank",0);
	$this->SetX($this->GetX()-10);
	$this->MultiCell(80,$height,"Sort code: 30 96 09",0);
	$this->SetX($this->GetX()-10);
	$this->MultiCell(80,$height,"Account no: 00313717",0);
}
function PaymentAdvice($id)
{
	global $gsi;
	$height = 5;
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 15);
	$this->SetX(5);
	$this->Cell(200,0,"",0,1,'L',true);
	
	//////////////////// BEB Details ////////////////////
	$this->SetY($this->GetY() + 5);
	$this->SetFont('Arial','',13);
	$this->SetX($this->GetX() - 5);
	$this->MultiCell(0,$height,"PAYMENT ADVICE");
	
	$this->SetFont('Arial','',9);
	$this->SetY($this->GetY() + 5);
	$this->SetX($this->GetX() - 5);
	$this->Cell(0,$height,"To:");
	
	$y = $this->GetY();
	$this->SetY($this->GetY());
	$this->SetX($this->GetX() + 6);
	$this->MultiCell(0,$height,"Web Alliance Limited");
	$this->SetX($this->GetX() + 6);
	$this->MultiCell(0,$height,"32 Thruxton Drive");
	$this->SetX($this->GetX() + 6);
	$this->MultiCell(0,$height,"Northampton");
	$this->SetX($this->GetX() + 6);
	$this->MultiCell(0,$height,"NN3 6ES,");
	$this->SetX($this->GetX() + 6);
	$this->MultiCell(0,$height,"United Kingdom");
	
	$this->SetY($y-10);
	$this->SetX($this->GetX() + 100);
	$this->SetFont('Arial','B',8);
	$this->Cell(40,$height,"Customer",0);
	$this->SetFont('Arial','',8);
	
	$job_id = utility::getTableFieldById("invoice","job_id","id",$id);
	$row = utility::getTableRowByField("job","id",$job_id);
	$this->Cell(40,$height,$row["company_name"],0);
	
	$this->SetY($y-5);
	$this->SetX($this->GetX() + 100);
	$this->SetFont('Arial','B',8);
	$this->Cell(40,$height,"Invoice Number",0);
	$this->SetFont('Arial','',8);
	$initial_company_name = $gsi->getInitialOfCompanyName($row["company_name"]);
	//$this->Cell(40,$height,$initial_company_name."/".date("m")."/".date("y")."/".$id,0);
	$invoice_number = utility::getTableFieldById("invoice","invoice_number","id",$id);
	if($invoice_number == ""){
		$invoice_number = $initial_company_name."/".date("m")."/".date("y")."/".$id;
	}
	$this->Cell(40,$height,$invoice_number,0);
	
	$this->SetY($y);
	$this->SetX($this->GetX() + 100);
	$this->SetFont('Arial','B',8);
	$this->Cell(40,$height,"Amount Due",0);
	$this->SetFont('Arial','B',8);
	$amount = utility::getTableFieldById("invoice","amount","id",$id);
	$vat = $amount/5;
	$total = $amount + $vat;
	$this->Cell(40,$height,number_format($total,2),0);
	
	$this->SetY($y+5);
	$this->SetX($this->GetX() + 100);
	$this->SetFont('Arial','B',8);
	$this->Cell(40,$height,"Due Date",0);
	$this->SetFont('Arial','',8);
	$due_date = utility::getTableFieldById("invoice","due_date","id",$id);
	if($due_date != "0000-00-00 00:00:00"){
		$this->Cell(40,$height,date("d M Y",strtotime($due_date)),0);
	}else{
		$this->Cell(40,$height,"",0);
	}
	
	$this->SetY($y+10);
	$this->SetX($this->GetX() + 100);
	$this->SetFont('Arial','B',8);
	$this->Cell(40,$height,"Amount Enclosed",0);
	
	// hr line
	$this->SetFillColor(122,138,144);
	$this->SetY($this->GetY() + 7);
	$this->SetX($this->GetX() + 135);
	$this->Cell(50,0,"",0,1,'R',true);
	
	$this->SetY($y+17);
	$this->SetX($this->GetX() + 140);
	$this->SetFont('Arial','',7);
	$this->Cell(40,$height,"Enter the amount you are paying above",0);
}
}
$pdf = new PDF();
$title = 'Procon Insurance Invoice';
$pdf->SetTitle($title);
$pdf->SetAuthor('Procon Insurance');
$id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));

$pdf->InvoiceDetails($id);
$pdf->OrderDetail($id);
$pdf->PaymentAdvice($id);
$pdf_genrate = $_REQUEST['pdf_genrate'];
if($pdf_genrate == 1){
	if($_SERVER['HTTP_HOST'] == "localhost"){
		$filename="../payment_invoice/invoice_".$id;
	}else{
		$filename="/home/bebconsultancyco/public_html/beb/payment_invoice/invoice_".$id;
	}
	$pdf->Output($filename.'.pdf','F');
}else{
	$pdf->Output("Invoice","I");
}
?>