<?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,10,40);
		$this->SetXY(170, 27);
		$this->SetFont('Arial','',10);
	}	
	// Line break
	$this->Ln(1);
	//$this->Rect(5, 5, 200, 286);
}
function normalInvoice($id)
{
	global $config,$gsi;
	$this->AddPage();
	$this->SetLeftMargin(7);
	$this->SetFont('Arial','',10);
	$section_ids = utility::getTableFieldById('document_template_job_mapping','section_ids','id',$id);
	$job_id = utility::getTableFieldById('document_template_job_mapping','job_id','id',$id);
	$section_ids_array = explode(",",$section_ids);
	foreach($section_ids_array as $section_id){
		$document_template_section = utility::getTableFieldById('document_template_section','description','id',$section_id);
		$client_field_array = array();
		$client_field_value_array = array();
		foreach(gsi::$clientDetailArray as $client_field){
			if( strpos( $document_template_section, $client_field ) !== false) {
				array_push($client_field_array,"[".$client_field."]");
				$field_value = utility::getTableFieldById('job',$client_field,'id',$job_id);
				$field_value = $gsi->getFieldValueById($field_value,$client_field,$job_id);
				array_push($client_field_value_array,$field_value);
			}
		}
		$document_template_section = str_replace($client_field_array,$client_field_value_array,$document_template_section);
		
		$this->SetY($this->GetY()+5);
		$this->MultiCell(195,5,$document_template_section,0,"J");
	}
}
function Footer(){
	global $config;
	$height = 10;
	$this->SetTextColor(0,0,0);
	$this->SetY(260);
	$this->SetX($this->GetX() + 2);
	$this->SetFont('Arial','',10);
	$this->SetY($this->GetY() + 20);
	$this->SetX($this->GetX() + 10);
	$this->SetFillColor(152,251,152);
	$this->Cell(170,$height,'Web Alliance Ltd.',0,0,'C',true);
}
}
$pdf = new PDF();
$title = 'Document Template';
$pdf->SetTitle($title);
$pdf->SetAuthor('CRM');

//$id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));
$id = $_REQUEST['id'];
//$job_id = base64_decode(base64_decode(base64_decode(base64_decode(base64_decode(base64_decode($_REQUEST['id']))))));
$pdf->normalInvoice($id);
$pdf->Output("client_care_letter.pdf","I");
/*if($_SERVER['HTTP_HOST'] == "localhost"){
	$filename="../quotation_template/client_care_letter_".$job_id;
}else{
	$filename="/home4/easysolu/public_html/beb/quotation_template/client_care_letter_".$job_id;
}
$pdf->Output($filename.'.pdf','F');*/
?>