<?php $_REQUEST['src']='ext2';require('../../functions/global.php');require('finance_functions.php');
// === TEST DATA
$_REQUEST['mode']='genNew';
$_REQUEST['clientnumber']='651';
$_REQUEST['filter']='Contract Renewal';
// === INVOICE GENERATOR
if($_REQUEST['mode']=="genNew"){

$_REQUEST['invNo']=genInvoiceCode();
$longNo=(sprintf("%06d", $_REQUEST['invNo']));

$stmt = $GLOBALS['con']->prepare("SELECT * FROM `clientdetails` WHERE `clientnumber`='".$_REQUEST['clientnumber']."'")or die(mysqli_error("GETASSET: ".$GLOBALS['con']));
$stmt->execute();$s=$stmt->get_result();$stmt->close();
$dd=mysqli_fetch_array($s);

$itemArray=array();
array_push($itemArray,array(service=>'Contract Renewal',notes=>'',rate=>$dd['rate_per_hour'],quantity=>'1'));

if(!empty($itemArray)){$_REQUEST['items']=serialize($itemArray);}else{$_REQUEST['items']="";}

if($dd['commission']!='' && $dd['commission']!="0.00"){
$address = array(
'client_name' => $_REQUEST['clientnumber'],
'billing_name'  => $dd['billing_name'],
'billing1'  => $dd['billing1'],
'billing2'  => $dd['billing2'],
'billing3'  => $dd['billing3'],
'billing_town'  => $dd['billing_town'],
'billing_county'  => $dd['billing_county'],
'billing_postcode'  => $dd['billing_postcode'],
'email'  => $dd['pri_contact_email'],
'contract_number'  => $dd['contract_number'],
'agent_number'  => $dd['agent_number'],
'agent_name'  => getAgentName($dd['agent_number']),
'commission'  => number_format($dd['commission'],2),
);
}
else{
$address = array(
'client_name' => $_REQUEST['clientnumber'],
'billing_name'  => $dd['billing_name'],
'billing1'  => $dd['billing1'],
'billing2'  => $dd['billing2'],
'billing3'  => $dd['billing3'],
'billing_town'  => $dd['billing_town'],
'billing_county'  => $dd['billing_county'],
'billing_postcode'  => $dd['billing_postcode'],
'email'  => $dd['pri_contact_email'],
'contract_number'  => $dd['contract_number'],
'agent_number'  => $dd['agent_number'],
'agent_name'  => getAgentName($dd['agent_number']),
'commission'  => '0.00',
);
}
if(!empty($dd['commission'])){
$comrate="0.".$dd['commission'];
if($dd['comType']=='0'){$_REQUEST['comTotal']=$dd['rate_per_hour']*$comrate;}
if($dd['comType']=='1'){$_REQUEST['comTotal']=$dd['commission'];}
$_REQUEST['comTotal']=number_format($_REQUEST['comTotal'],2);
$_REQUEST['brokerCom']=1;
}
$_REQUEST['agentID']=$dd['agent_number'];
$_REQUEST['vatrate']=getCompanyDetails('vatrate');
$_REQUEST['sumTotal']=number_format($dd['rate_per_hour'],2);
$_REQUEST['vatTotal']=number_format($dd['rate_per_hour']*$_REQUEST['vatrate'],2);
$_REQUEST['address']=serialize($address);
$date=strtotime($_REQUEST['date']);
$newDate = new DateTime($_REQUEST['date']);
$newDate->add(new DateInterval('P30D'));
$setDate=$newDate->format('d-m-Y');
$dueDate=strtotime($setDate);

$stmt = $GLOBALS['con']->prepare("INSERT INTO `invoices` VALUES(NULL,'".$_REQUEST['clientID']."','".$_REQUEST['agentID']."','".$_REQUEST['address']."','".$_REQUEST['items']."','".$_REQUEST['sumTotal']."|".$_REQUEST['vatTotal']."','".$_REQUEST['vatrate']."','".$date."','".$dueDate."','','','1','','','".$_REQUEST['comTotal']."','0','".$_REQUEST['filter']."')")or die("1".mysqli_error($GLOBALS['con']));
$stmt->execute();$iv=$stmt->get_result();$stmt->close();
$id=mysqli_insert_id($GLOBALS['con']);
$_REQUEST['invNo']=$id;
}

echo $id;

// === EOF INVOICE GENERATOR

// === QR GENERATOR
function cleaner($i){
$i=str_replace("'","&#39;",$i);
return $i;
}

function base64ToImage($imageData,$invoiceCode){
$data = 'data:image/png;base64,AAAFBfj42Pj4';
list($type, $imageData) = explode(';', $imageData);
list(,$extension) = explode('/',$type);
list(,$imageData)      = explode(',', $imageData);
$fileName = $invoiceCode.'.'.$extension;
$imageData = base64_decode($imageData);
file_put_contents('../../invoices/'.$fileName, $imageData);
}

function get_tiny_url($url)  {  
$ch = curl_init();  
$timeout = 5;  
curl_setopt($ch,CURLOPT_URL,'http://tinyurl.com/api-create.php?url='.$url);  
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);  
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);  
$data = curl_exec($ch);  
curl_close($ch);  
return $data;  
}

function genurl(){
$thurl='https://'.getCompanyDetails('web').'/payments/paybounce.php?code=ENREP PAYMENT&invoice='.$_REQUEST['invNo'].'&amount='.$_REQUEST['sumTotal'];
$string=rawurlencode($thurl);
$url=get_tiny_url($string);
return $url;
}

$imgname ="qr.png";
$data =  genurl();
$logo =  '../../images/logo_payment.png';
$sdir = explode("/",$_SERVER['REQUEST_URI']);
$dir = str_replace($sdir[count($sdir)-1],"",$_SERVER['REQUEST_URI']);
// === Create qrcode image
include('../../packages/phpqrcode/qrlib.php');
QRcode::png($data,$imgname,QR_ECLEVEL_H,11.45,0);
// === Adding image to qrcode
$QR = imagecreatefrompng($imgname);
if($logo !== FALSE){
$logopng = imagecreatefrompng($logo);
$QR_width = imagesx($QR);
$QR_height = imagesy($QR);
$logo_width = imagesx($logopng);
$logo_height = imagesy($logopng);
	
list($newwidth, $newheight) = getimagesize($logo);
$out = imagecreatetruecolor($QR_width, $QR_width);
imagecopyresampled($out, $QR, 0, 0, 0, 0, $QR_width, $QR_height, $QR_width, $QR_height);
imagecopyresampled($out, $logopng, $QR_width/8.25, $QR_height/3.15, 0, 0, $QR_width/1.2, $QR_height/2.5, $newwidth, $newheight);
//imagecopyresampled($out, $logopng, $QR_width/3.05, $QR_height/2.25, 0, 0, $QR_width/2.8, $QR_height/6, $newwidth, $newheight);
}
imagepng($out,$imgname);
imagedestroy($out);

// === Change image color
$im = imagecreatefrompng($imgname);
$r = 44;$g = 62;$b = 80;
for($x=0;$x<imagesx($im);++$x){
for($y=0;$y<imagesy($im);++$y){
$index 	= imagecolorat($im, $x, $y);
$c= imagecolorsforindex($im, $index);
if(($c['red'] < 100) && ($c['green'] < 100) && ($c['blue'] < 100)) { // dark colors
// here we use the new color, but the original alpha channel
$colorB = imagecolorallocatealpha($im, 0x12, 0x2E, 0x31, $c['alpha']);
imagesetpixel($im, $x, $y, $colorB);
}
}
}
imagepng($im,$imgname);
imagedestroy($im);

// === Convert Image to base64
$type = pathinfo($imgname, PATHINFO_EXTENSION);
$data = file_get_contents($imgname);
$imgbase64 = 'data:image/' . $type . ';base64,' . base64_encode($data);

// === Show image
//echo "<img src='$imgbase64' style='position:relative;display:block;width:240px;height:240px;margin:160px auto;'>";

// === Save image
base64ToImage($imgbase64,$_REQUEST['invNo']);

// === EOF QR GENERATOR

// === PDF GENERATOR
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `invoices` WHERE `invNo`=?")or die(mysqli_error($GLOBALS['con']));
$stmt->bind_param('i',$_REQUEST['invNo']);$stmt->execute();$iv=$stmt->get_result();$stmt->close();
$cd=mysqli_fetch_array($iv);
$address = unserialize($cd['address']);
$totals=explode('|',$cd['totals']);

$sub=str_replace(',','',$totals[0]);
$vat=str_replace(',','',$totals[1]);
$com=str_replace(',','',$cd['commission']);
$gtotal=$sub+$vat;

$agentGtotal=$sub-$vat;




function getAgentDetails($type,$agent){
$stmt = $GLOBALS['con']->prepare("SELECT * FROM `agents` WHERE `agent_number`='".$agent."'")or die(mysqli_error($GLOBALS['con']));
$stmt->execute();$s=$stmt->get_result();$stmt->close();
$xd=mysqli_fetch_array($s);
if($type=="bankName"){return $xd['bankName'];}
if($type=="accountNo"){return $xd['accountNo'];}
if($type=="sortCode"){return $xd['sortCode'];}
if($type=="name"){return $xd['company'];}
if($type=="vatno"){return $xd['vatno'];}
if($type=="web"){return $xd['web'];}
if($type=="name_short"){return $xd['company_short'];}
if($type=="email"){return $xd['pri_contact_email'];}
if($type=="address"){return '<tr><td>'.$xd['agent_name'].'</td></tr><tr><td>'.$xd['address1'].'</td></tr><tr><td>'.$xd['address2'].'</td></tr><tr><td>'.$xd['town'].'</td></tr><tr><td>'.$xd['post'].'</td></tr>';}
if($type=="contact"){return '<p>'.$xd['tel'].'<br>'.$xd['mob'].'<br>'.$xd['main_email'].'<br>'.$xd['web'].'</p>';}
}

function invType($type){
    if($type=="proforma"){return('<span style="color:#2a3f54">PROFORMA</span>');}
    if($type=="client"){return('<span style="color:#2a3f54">INVOICE</span>');}
    if($type=="agent"){return('<span style="color:#2a3f54">AGENT INVOICE</span>');}
    if($type=="broker"){return('<span style="color:#2a3f54">BROKER INVOICE</span>');}
    if($type=="remit"){return('<span style="color:#257609">REMITTANCE ADVICE</span>');}
    if($type=="remind"){return('<span style="color:#bd0404">REMINDER</span>');}
}


$clientAddress='<table class="invtable table table-sm" style="text-align:right"><tbody>';
$clientAddress.=getCompanyDetails('address');
$clientAddress.='</tbody></table>';

$agentAddress='<table class="invtable table table-sm"><thead><tr><th style="background-color:#2a3f54!important; color:#ffffff!important">To</th></tr></thead><tbody>';
$agentAddress.=getAgentDetails('address',$cd['agent']);
$agentAddress.='</tbody></table>';

$deliveryAddress='<table class="invtable table table-sm"><thead><tr><th style="background-color:#2a3f54!important; color:#ffffff!important">To</th></tr></thead><tbody>';
$deliveryAddress.='<tr><td>'.$address['billing_name'].'</td></tr>';
if($address['billing1']!="" && $address['billing1']!="null"){$deliveryAddress.='<tr><td>'.$address['billing1'].'</td></tr>';}
if($address['billing2']!="" && $address['billing2']!="null"){$deliveryAddress.='<tr><td>'.$address['billing2'].'</td></tr>';}
if($address['billing3']!="" && $address['billing3']!="null"){$deliveryAddress.='<tr><td>'.$address['billing3'].'</td></tr>';}
if($address['billing_town']!="" && $address['billing_town']!="null"){$deliveryAddress.='<tr><td>'.$address['billing_town'].'</td></tr>';}
if($address['billing_county']!="" && $address['billing_county']!="null"){$deliveryAddress.='<tr><td>'.$address['billing_county'].'</td></tr>';}
if($address['billing_postcode']!="" && $address['billing1']!="null"){$deliveryAddress.='<tr><td>'.$address['billing_postcode'].'</td></tr>';}
$deliveryAddress.='</tbody></table>';

$items = unserialize($cd['items']);
foreach($items as $item){
$rate=str_replace(',','',$item['rate']);
$net=$rate*$item['quantity'];
$itemRow.='<tr><td>'.$item['service'].'</td><td>'.$item['notes'].'</td><td>'.$item['rate'].'</td><td>'.$item['quantity'].'</td><td style="background-color:#eaeaec!important;" align="right">'.number_format($net,2).'</td></tr>';
}


$header='<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta name="viewport" content="width=device-width" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
$header.='</head> 
<body bgcolor="#FFFFFF" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" style="font-size:10px!important">';
$header.=file_get_contents('../../includes/engines/invoice_structure/header_view.php');

// CLIENT ///////////////////
$client_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$client_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
$client_body.='<h2 style="text-align:right; color:#2a3f54">INVOICE</h2>';
$client_body.='
<div style="width:200px; float:left">'.$deliveryAddress.'</div>
<div style="width:200px; float:right">'.$clientAddress.'</div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE NUMBER</th><td>'.sprintf("%06d", $_REQUEST['invNo']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>VAT REGISTRATION NUMBER</th><td>'.getCompanyDetails('vatno').'</td></tr>
</table></div>
<div style="clear:both"></div>
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE DATE</th><td>'.date('d-m-Y',$cd['date']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>TERMS</th><td>Stricly 30 days</td></tr>
</table></div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<table class="invtable table table-sm"><thead>
<tr>
<th style="background-color:#2a3f54!important; color:#ffffff!important">SERVICE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">REFERENCE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">RATE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">QUANTITY</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">NET</th>
</tr></thead>
<tbody>
'.$itemRow.'
<tr><td colspan="5"><hr style="color:#2a3f54"></td></tr>
</tbody>
<tfoot>
<tr><td colspan="3"></td><td>SUBTOTAL</td><td style="background-color:#eaeaec!important;"  align="right">'.$totals[0].'</td></tr>
<tr><td colspan="3"></td><td>VAT</td><td align="right">'.$totals[1].'</td></tr>
<tr><td colspan="3"></td><td colspan="2"><hr style="color:#2a3f54"></td></tr>
<tr><td colspan="3"></td><td>TOTAL</td><td style="background-color:#2a3f54; color:#ffffff;" align="right">&pound;'.number_format($gtotal,2).'</td></tr>

</tfoot>
</table>
';

// AGENT ///////////////////
if($_REQUEST['brokerCom']==1){
$broker_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$broker_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
$broker_body.='<h2 style="text-align:right; color:#2a3f54">INVOICE</h2>';
$broker_body.='
<div style="width:200px; float:left">'.$agentAddress.'</div>
<div style="width:200px; float:right">'.$clientAddress.'</div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE NUMBER</th><td>'.sprintf("%06d", $_REQUEST['invNo']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>VAT REGISTRATION NUMBER</th><td>'.getCompanyDetails('vatno').'</td></tr>
</table></div>
<div style="clear:both"></div>
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE DATE</th><td>'.date('d-m-Y',$cd['date']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>TERMS</th><td>Stricly 30 days</td></tr>
</table></div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<table class="invtable table table-sm"><thead>
<tr>
<th style="background-color:#2a3f54!important; color:#ffffff!important">SERVICE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">REFERENCE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">RATE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">QUANTITY</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">NET</th>
</tr></thead>
<tbody>
'.$itemRow.'
<tr><td colspan="5"><hr style="color:#2a3f54"></td></tr>
</tbody>
<tfoot>
<tr><td colspan="3"></td><td>SUBTOTAL</td><td style="background-color:#eaeaec!important;"  align="right">'.$totals[0].'</td></tr>
<tr><td colspan="3"></td><td>COMMISSION</td><td style="background-color:#eaeaec!important;"  align="right">'.$com.'</td></tr>
<tr><td colspan="3"></td><td colspan="2"><hr style="color:#2a3f54"></td></tr>
<tr><td colspan="3"></td><td>TOTAL</td><td style="background-color:#2a3f54; color:#ffffff;" align="right">&pound;'.number_format($agentGtotal,2).'</td></tr>

</tfoot>
</table>
';

}

// AGENT ///////////////////
if($_REQUEST['agentCom']==1){
    $agent_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
    $agent_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
    $agent_body.='<h2 style="text-align:right; color:#2a3f54">INVOICE</h2>';
    $agent_body.='
    <div style="width:200px; float:left">'.$agentAddress.'</div>
    <div style="width:200px; float:right">'.$clientAddress.'</div>
    <div style="clear:both"></div>
    <hr style="color:#2a3f54">
    <div style="width:30%; float:left"><table class="invtable table table-sm">
    <tr><th>INVOICE NUMBER</th><td>'.sprintf("%06d", $_REQUEST['invNo']).'</td></tr>
    </table></div>
    <div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
    <tr><th>VAT REGISTRATION NUMBER</th><td>'.getCompanyDetails('vatno').'</td></tr>
    </table></div>
    <div style="clear:both"></div>
    <div style="width:30%; float:left"><table class="invtable table table-sm">
    <tr><th>INVOICE DATE</th><td>'.date('d-m-Y',$cd['date']).'</td></tr>
    </table></div>
    <div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
    <tr><th>TERMS</th><td>Stricly 30 days</td></tr>
    </table></div>
    <div style="clear:both"></div>
    <hr style="color:#2a3f54">
    <table class="invtable table table-sm"><thead>
    <tr>
    <th style="background-color:#2a3f54!important; color:#ffffff!important">SERVICE</th>
    <th style="background-color:#2a3f54!important; color:#ffffff!important">REFERENCE</th>
    <th style="background-color:#2a3f54!important; color:#ffffff!important">RATE</th>
    <th style="background-color:#2a3f54!important; color:#ffffff!important">QUANTITY</th>
    <th style="background-color:#2a3f54!important; color:#ffffff!important">NET</th>
    </tr></thead>
    <tbody>
    '.$itemRow.'
    <tr><td colspan="5"><hr style="color:#2a3f54"></td></tr>
    </tbody>
    <tfoot>
    <tr><td colspan="3"></td><td>SUBTOTAL</td><td style="background-color:#eaeaec!important;"  align="right">'.$totals[0].'</td></tr>
    <tr><td colspan="3"></td><td>VAT</td><td align="right">'.$totals[1].'</td></tr>
    <tr><td colspan="3"></td><td colspan="2"><hr style="color:#2a3f54"></td></tr>
    <tr><td colspan="3"></td><td>TOTAL</td><td style="background-color:#2a3f54; color:#ffffff;" align="right">&pound;'.number_format($gtotal,2).'</td></tr>
    
    </tfoot>
    </table>
    ';
    
    }


// REMIT ///////////////////
$remit_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$remit_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
$remit_body.='<h2 style="text-align:right; color:#257609">REMITTANCE ADVICE</h2>';
$remit_body.='
<div style="width:200px; float:left">'.$deliveryAddress.'</div>
<div style="width:200px; float:right">'.$clientAddress.'</div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE NUMBER</th><td>'.sprintf("%06d", $_REQUEST['invNo']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>VAT REGISTRATION NUMBER</th><td>'.getCompanyDetails('vatno').'</td></tr>
</table></div>
<div style="clear:both"></div>
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>INVOICE DATE</th><td>'.date('d-m-Y',$cd['date']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>TERMS</th><td>Stricly 30 days</td></tr>
</table></div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<table class="invtable table table-sm"><thead>
<tr>
<th style="background-color:#2a3f54!important; color:#ffffff!important">SERVICE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">REFERENCE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">RATE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">QUANTITY</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">NET</th>
</tr></thead>
<tbody>
'.$itemRow.'
<tr><td colspan="5"><hr style="color:#2a3f54"></td></tr>
</tbody>
<tfoot>
<tr><td colspan="3"></td><td>SUBTOTAL</td><td style="background-color:#eaeaec!important;"  align="right">'.$totals[0].'</td></tr>
<tr><td colspan="3"></td><td>VAT</td><td align="right">'.$totals[1].'</td></tr>
<tr><td colspan="3"></td><td colspan="2"><hr style="color:#2a3f54"></td></tr>
<tr><td colspan="3"></td><td>TOTAL</td><td style="background-color:#2a3f54; color:#ffffff;" align="right">&pound;'.number_format($gtotal,2).'</td></tr>

</tfoot>
</table>
';

// REMIND ///////////////////
$remind_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$remind_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
$remind_body.='<h2 style="text-align:right; color:#bd0404">REMINDER</h2>';
$remind_body.='
<div style="width:200px; float:left">'.$deliveryAddress.'</div>
<div style="width:200px; float:right">'.$clientAddress.'</div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<div style="clear:both"></div>
<p>This is a friendly reminder that an invoice is now overdue.</p>
<p>INVOICE DATE: '.date('d-m-Y',$cd['date']).'<br>
<p>INVOICE NUMBER: '.sprintf("%06d", $_REQUEST['invNo']).'<br>
<p>INVOICE AMOUNT: &pound;'.number_format($gtotal,2).'</p>
<br><br>
<p>We understand that oversights happen but would appreciate prompt payment of this amount. Could you please let me know when I can expect payment? If payment has already been made, please disregard this notice.<br><br>
Thanks in advance for your cooperation.</p>
';

// PROFORMA ///////////////////
$proform_body.='<link href="../../packages/bootstrap/css/bootstrap.min.css" rel="stylesheet">';
$proform_body.=file_get_contents('../../includes/engines/invoice_structure/style.php');
$proform_body.='<h2 style="text-align:right; color#3f3f3f">PRO FORMA INVOICE</h2>';
$proform_body.='
<div style="width:200px; float:left">'.$deliveryAddress.'</div>
<div style="width:200px; float:right">'.$clientAddress.'</div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>REFERENCE NUMBER</th><td>'.sprintf("%06d", $_REQUEST['invNo']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>VAT REGISTRATION NUMBER</th><td>'.getCompanyDetails('vatno').'</td></tr>
</table></div>
<div style="clear:both"></div>
<div style="width:30%; float:left"><table class="invtable table table-sm">
<tr><th>DATE</th><td>'.date('d-m-Y',$cd['date']).'</td></tr>
</table></div>
<div style="width:40%; float:right"><table class="invtable table table-sm" style="text-align:right">
<tr><th>TERMS</th><td>Valid for 30 days</td></tr>
</table></div>
<div style="clear:both"></div>
<hr style="color:#2a3f54">
<table class="invtable table table-sm"><thead>
<tr>
<th style="background-color:#2a3f54!important; color:#ffffff!important">SERVICE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">REFERENCE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">RATE</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">QUANTITY</th>
<th style="background-color:#2a3f54!important; color:#ffffff!important">NET</th>
</tr></thead>
<tbody>
'.$itemRow.'
<tr><td colspan="5"><hr style="color:#2a3f54"></td></tr>
</tbody>
<tfoot>
<tr><td colspan="3"></td><td>SUBTOTAL</td><td style="background-color:#eaeaec!important;"  align="right">'.$totals[0].'</td></tr>
<tr><td colspan="3"></td><td>VAT</td><td align="right">'.$totals[1].'</td></tr>
<tr><td colspan="3"></td><td colspan="2"><hr style="color:#2a3f54"></td></tr>
<tr><td colspan="3"></td><td>TOTAL</td><td style="background-color:#2a3f54; color:#ffffff;" align="right">&pound;'.number_format($gtotal,2).'</td></tr>

</tfoot>
</table>
';

$footer=file_get_contents('../../includes/engines/invoice_structure/footer_view.php');

//// CONTENT REPLACEMENT

if(file_exists('../../images/company/afl_1.jpg')){$header=str_replace('[AF1]','<td><img src="../../images/company/afl_1.jpg" width="50px" /></td>',$header);}
if(file_exists('../../images/company/afl_2.jpg')){$header=str_replace('[AF2]','<td><img src="../../images/company/afl_2.jpg" width="50px" /></td>',$header);}
if(file_exists('../../images/company/afl_3.jpg')){$header=str_replace('[AF3]','<td><img src="../../images/company/afl_3.jpg" width="50px" /></td>',$header);}
if(file_exists('../../images/company/afl_4.jpg')){$header=str_replace('[AF4]','<td><img src="../../images/company/afl_4.jpg" width="50px" /></td>',$header);}
if(file_exists('../../images/company/afl_5.jpg')){$header=str_replace('[AF5]','<td><img src="../../images/company/afl_5.jpg" width="50px" /></td>',$header);}
if(file_exists('../../images/company/afl_6.jpg')){$header=str_replace('[AF6]','<td><img src="../../images/company/afl_6.jpg" width="50px" /></td>',$header);}
$footer=str_replace('[QCODE]','<img src="../../invoices/'.$_REQUEST['invNo'].'.png" width="100px" />',$footer);
$footer=str_replace('[CNAME]',getCompanyDetails('name'),$footer);
$footer=str_replace('[CBANK]',getCompanyDetails('bankName'),$footer);
$footer=str_replace('[CACNO]',getCompanyDetails('accountNo'),$footer);
$footer=str_replace('[CSCODE]',getCompanyDetails('sortCode'),$footer);
$footer=str_replace('[WEB]',getCompanyDetails('web'),$footer);


include('../../packages/mpd/mpdf.php');
//CLIENT
$filestore="../../invoices/".$_REQUEST['invNo']."_invoice.pdf";
$mpdf=new mPDF();
$mpdf->SetHTMLHeader($header); 
$mpdf->SetHTMLFooter($footer); 
$mpdf->WriteHTML($client_body);
$mpdf->Output($filestore);

//BROKER
if($_REQUEST['brokerCom']==1){
    $filestore="../../invoices/".$_REQUEST['invNo']."_broker.pdf";
    $mpdf=new mPDF();
    $mpdf->SetHTMLHeader($header); 
    $mpdf->SetHTMLFooter($footer); 
    $mpdf->WriteHTML($agent_body);
    $mpdf->Output($filestore);

}

//AGENT
if($_REQUEST['agentCom']==1){
    $filestore="../../invoices/".$_REQUEST['invNo']."_agent.pdf";
    $mpdf=new mPDF();
    $mpdf->SetHTMLHeader($header); 
    $mpdf->SetHTMLFooter($footer); 
    $mpdf->WriteHTML($agent_body);
    $mpdf->Output($filestore);
    
}

//REMIT
$filestore="../../invoices/".$_REQUEST['invNo']."_remit.pdf";
$mpdf=new mPDF();
$mpdf->SetHTMLHeader($header); 
$mpdf->SetHTMLFooter($footer); 
$mpdf->WriteHTML($remit_body);
$mpdf->Output($filestore);

//REMIND
$filestore="../../invoices/".$_REQUEST['invNo']."_remind.pdf";
$mpdf=new mPDF();
$mpdf->SetHTMLHeader($header); 
$mpdf->SetHTMLFooter($footer); 
$mpdf->WriteHTML($remind_body);
$mpdf->Output($filestore);

//PROFORM
$filestore="../../invoices/".$_REQUEST['invNo']."_proforma.pdf";
$mpdf=new mPDF();
$mpdf->SetHTMLHeader($header); 
$mpdf->SetHTMLFooter($footer); 
$mpdf->WriteHTML($proform_body);
$mpdf->Output($filestore);

$path='../../invoices/';

$theZip=$_REQUEST['invNo'].'.zip';
$zip = new ZipArchive;
if ($zip->open($path.$theZip, ZipArchive::CREATE) === TRUE)
{
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_invoice.pdf",$_REQUEST['invNo']."_invoice.pdf");
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_remit.pdf",$_REQUEST['invNo']."_remit.pdf");
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_remind.pdf",$_REQUEST['invNo']."_remind.pdf");
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_proforma.pdf",$_REQUEST['invNo']."_proforma.pdf");
}
$zip->close();
if($_REQUEST['brokerCom']==1){
$theZip=$_REQUEST['invNo'].'_broker.zip';
$zip = new ZipArchive;
if ($zip->open($path.$theZip, ZipArchive::CREATE) === TRUE)
{
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_broker.pdf",$_REQUEST['invNo']."_broker.pdf");
$zip->addFile("../../invoices/".$_REQUEST['invNo']."_invoice.pdf",$_REQUEST['invNo']."_invoice.pdf");
}
$zip->close();
}
if($_REQUEST['agentCom']==1){

}

$stmt = $GLOBALS['con']->prepare("INSERT INTO `finance_reports` VALUES ('NULL','".$cd['client']."','".$_REQUEST['invNo']."','Invoice','".urlencode($client_body)."','".$_SESSION['user']."','".strtotime('now')."','".$cd['date']."','','1')");
$stmt->execute();$stmt->close();
// === EOF PDF GENERATOR