PDF Invoice creation

This public forum is for user-to-user discussions of PHPMaker. Note that this is not support forum.
Post Reply
bart.gebruers
User
Posts: 23

PDF Invoice creation

Post by bart.gebruers »

Anybody out there who can show/guide me in how I can generate an invoice in PHPMaker. (from my sales & sales_details tables)

Did read somethig about custom file/template but dont really get it how this can be one.
Any hints are appreciated.

Thx
Bart


riverman
User
Posts: 158
Location: Stockholm/Sweden

Post by riverman »

This is an old quick-and-dirty invoice with ugly code. But perhaps you can get ideas from it...

It's using TCPDF as pdf library because of utf-8 support (if I remember correct)...


<?php

// Include the main TCPDF library (search for installation path).
require_once('tcpdf/tcpdf.php');

// Extend the TCPDF class to create custom Header and Footer
class PDF extends TCPDF {

//Page header
public function Header() {
	//////////////////////////////////////////
	// PAGE NO
	//////////////////////////////////////////
	// Set font and color
	$this->SetFont('calibri','',11);


	// Heading and page no
	$this->SetXY(169, 5); 
	$this->Cell(30,8,'Sida: '.trim($this->PageNo()).'('.$this->getAliasNbPages().')',0,0,'R');

	//////////////////////////////////////////
	// LOGO
	//////////////////////////////////////////
	$this->SetXY(10, 10);
	$this->Image('logo.jpg' , 10, 10, 50, 10, 'jpg');

}
 public function OrderIDbox($orderid, $orderdate, $printdate){

	// OrderIDbox($_GET["id"], $row['OrderDate'], date('Y-m-d'))
	//////////////////////////////////////////
	// ORDERNR, DATE + FRAME
	//////////////////////////////////////////
	// Set font size and border color
	$this->SetDrawColor(0, 0, 0);
	$this->SetLineWidth(0.5);
	$this->SetFontSize(12);

	// Orderdatum ram
	$this->SetXY(171, 17.5);
	$this->Rect(139, 13, 60, 20, 'D');

// $this->SetXY(141, 17.5);
$this->SetXY(141, 15.5);
$this->Write(0, 'Ordernr:');

	// Ordernr
	$this->SetX(171);
	$this->Write(0, $orderid);

	// Orderdatum heading
	$this->SetXY(141, $this->GetY()+5);
	$this->Write(0, 'Order date:');

	// Orderdatum
	$this->SetXY(171, $this->GetY());
	$this->Write(0, $orderdate);

	// Utskriftsdatum heading
	$this->SetXY(141, $this->GetY()+5);
	$this->Write(0, 'Print Date:');

	// Utskriftsdatum
	$this->SetXY(171, $this->GetY());
	$this->Write(0, $printdate);		
}

public function OrderCustomerHead($row){

	//////////////////////
	// Leveransadress
	//////////////////////
	// Heading

	$this->SetFontSize(9);
	$this->SetXY(11.5, 29.5);
	$this->Cell(30,8,'Delivery address',0,0,'L');
	$this->SetFontSize(12);

	// Företag
	$this->SetXY(11.5, $this->GetY()+6);
	if ($row['Company'] != "-") {
		$this->Write(0, $row['Company']);
	}

	//Namn
	$this->SetXY(11.5, $this->GetY()+5);
	$this->Write(0, $row['FirstName']." ".$row['LastName']);

	//Gatuadress
	$this->SetXY(11.5, $this->GetY()+5);
	$this->Write(0, $row['ShippingStreet']);

	//Postadress
	$this->SetXY(11.5, $this->GetY()+7);
	$this->Write(0, $row['ShippingPostCode']."  ".strtoupper($row['ShippingTown']));

	//////////////////////
	// Kontaktuppgifter
	//////////////////////
	// Headings

	$this->SetXY(11.5, 71);
	$this->SetFontSize(9);
	$this->Write(0, 'Home');
	$this->SetX(62);
	$this->Write(0, 'Work');
	$this->SetX(106);
	$this->Write(0, 'Mobile');
	$this->SetX(154);
	$this->Write(0, 'Mobile2');

	// Change Font size
	$this->SetFontSize(12);

	// HEM-Telefon
	$this->SetXY(11.5, $this->GetY()+3);
	$this->Write(0, $row['PhoneHome']);

	// ARBETE-Telefon
	$this->SetXY(62, $this->GetY());
	$this->Write(0, $row['PhoneWork']);

	// MOBIL-Telefon
	$this->SetXY(106, $this->GetY());
	$this->Write(0, $row['CellurPhone1']);

	// MOBIL2-Telefon
	$this->SetXY(154, $this->GetY());
	$this->Write(0, $row['CellurPhone2']);

	//Change font size
	$this->SetFontSize(9);

	// E-mail headings
	$this->SetXY(11.5, $this->GetY()+6);
	$this->Write(0, 'e-mail');
	$this->SetX(106);
	$this->Write(0, 'e-mail2');

	// Change Font size
	$this->SetFontSize(12);

	// EPOST
	$this->SetXY(11.5, $this->GetY()+3);
	$this->Write(0, $row['EmailAddress']);

	// EPOST2
	$this->SetX(106);
	$this->Write(0, $row['EmailAddress2']);		
}

public function OrderRowHead($discount){

	///////////////////////
	// ORDER ROWS HEADINGS
	///////////////////////

	$this->SetFont('calibrib','',12);

	// Headings
	$this->SetX(10);
	$this->Cell(11,0,"Qty",0,0,'L',0);
	$this->Cell(50,0,"Manufacturer",0,0,'L',0);
	$this->Cell(80,0,"Product",0,0,'L',0);
	$this->Cell(25,0,"Price/unit",0,0,'R',0);
	$this->Cell(25,0,"Sum",0,0,'R',0);

	// Line
	$this->SetLineWidth(0,75);

	// Line(float x1, float y1, float x2, float y2)
	$this->Line(11, $this->Gety()+5, 200, $this->Gety()+5);
	$this->SetFont('calibri','',12);
}

public function NotesDelivery($row) {
	//////////////////////////////////
	// Notes- och delivery terms
	//////////////////////////////////
	// Line
	$this->SetLineWidth(0,75);
	$this->Line(11.5, 160, 200, 160);

	// Headings
	$this->SetFont('calibrib','',8);
	$this->SetXY(12, 163);
	$this->Cell(17,0,'Notes',0,0,'L',false);
	$this->SetXY(12, 200);
	$this->Cell(17,0,'Delivery time',0,0,'L',false);
	$this->SetX(105);
	$this->Cell(17,0,'Delivery',0,0,'L',false);	
	
	// Data
	$this->SetXY(12, 165);
	$this->SetFont('calibri','',10);
	$this->MultiCell(190,4, $row['ExternalNotes']);		
	$this->SetXY(12, 205);
	$this->Cell(17,0, $row['DeliveryWeek'],0,0,'L',false);
	$this->SetX(105);
	$this->Cell(17,0, $row['DeliveryName'],0,0,'L',false);
}
public function signment($row){

	//////////////////////
	// Underskrift
	//////////////////////
	// Line
	$this->SetLineWidth(0,75);
	$this->Line(11.5, 210, 200, 210);
	// Leverasvillkor
	$this->SetFont('calibri','',10);
	$this->SetXY(12, -82);
	$this->MultiCell(190,5,"Bla, bla, bla...",0,'L',false);

	// Headings
	$this->SetFont('calibri','',11);
	$this->SetXY(12, -64);
	$this->Cell(17,0,'Accepted by:',0,0,'L',false);
	$this->SetX(105);
	$this->Cell(17,0,'Date:',0,0,'L',false);

	// Datum
	$this->SetX(118);
	$this->Cell(17,0,$row['OrderDate'],0,0,'L',false);

	// Namn
	$this->SetXY(12, -45);
	$this->Cell(17,0,'Buyer',0,0,'L',false);
	$this->SetX(104);
	$this->Cell(17,0,$row['CreatedBy'].', Company Inc',0,0,'L',false);

}
public function OrderSum($Row, $OIRow){
	//////////////////////
	// Summor
	//////////////////////
	$VAT			= floatval($Row['OrderSum']) * 0.2;
	$DownPayment	= floatval($Row['DownPayment']);
	
	// Headings
	$this->SetXY(41, -60);
	$this->Cell(17,0,'Down payment',0,0,'R',false);
	$this->SetX(86.5);
	$this->Cell(17,0,'TAX',0,0,'R',false);
	$this->SetX(154);
	$this->Cell(17,0,'Order Sum:',0,0,'R',false);
	$this->SetXY(154, $this->GetY()+5);
	if ($Row['Discount'] != 0) {
		$this->Cell(17,0,'Discount:',0,0,'R',false);
	}else{
		$this->Cell(17,0,' ',0,0,'R',false);
	}
	$this->SetXY(154, $this->GetY()+5);
	$this->Cell(17,0,'Betalt:',0,0,'R',false);
	$this->SetXY(154, $this->GetY()+5);
	$this->SetFont('calibrib','',11);
	$this->Cell(17,0,'Att betala:',0,0,'R',false);
	$this->SetFont('calibri','',11);

	// Handpenning
	$this->SetXY(41, -55);
	$this->Cell(17,0,number_format($DownPayment, 2, ',', ' '),0,0,'R',false);

	// Moms
	$this->SetX(86.5);
	$this->Cell(17,0,number_format($VAT, 2, ',', ' '),0,0,'R',false);

	// Ordersumma
	$this->SetXY(180, $this->GetY()-5);
	$this->Cell(17,0,number_format($Row['OrderSum']+$Row['Discount'], 2, ',', ' '),0,0,'R',false);

	// Rabatt
	$this->SetXY(180, $this->GetY()+5);
	if ($Row['Discount'] != 0) {
		$this->Cell(17,0,'- '.number_format($Row['Discount'], 2, ',', ' '),0,0,'R',false);
	}else{
		$this->Cell(17,0,' ',0,0,'R',false);
	}

	// Betalt
	$this->SetXY(180, $this->GetY()+5);
	$this->Cell(17,0,number_format($Row['tot_ledger'], 2, ',', ' '),0,0,'R',false);

	// Att Betala
	$this->SetFont('calibrib','',11);
	$this->SetXY(180, $this->GetY()+5);
	$this->Cell(17,0,number_format($Row['RemainingAmount'], 2, ',', ' '),0,0,'R',false);		
}
// Page footer
public function Footer() {
	//////////////////////
	// Adressfoten
	//////////////////////
	//$this->SetFont('helvetica','',8);
	$this->SetFont('calibri','',8);
	$this->SetY(-25);
	$this->Cell(190,5,"Company Inc",0,0,'C',0);
	$this->SetY(-20);
	$this->Cell(190,5,"Street 123| 123 45 | BIGCITY | BIGCOUNTRY | PHONE 123412341234 | FAX 123412341234",0,0,'C',0);
	$this->SetY(-15);
	$this->Cell(190,5,"BANK ACCOUNT 12341243-1 | AA12341234142",0,0,'C',0);
	$this->SetY(-10);
	$this->Cell(190,5,"info @ companyinc.com | www . companyinc . com",0,0,'C',0);	
}

}

//////////////////////////////////////////
// Read order data to $row
//////////////////////////////////////////

//Connect to database
$dbsrv = "127.0.0.1";
$db = "aasdafsdf";
$dbuser = "asdfasdf";
$dbpass = "asdfasfd";

$con = mysqli_connect($dbsrv, $dbuser, $dbpass, $db);
mysqli_set_charset($con, 'utf8');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

if (!mysqli_set_charset($con, "utf8")) {
printf("Error loading character set utf8: %s\n", mysqli_error($con));
exit();
}

// Execute SQL query: ORDER
//SELECT * FROM order INNER JOIN delivery ON order.DeliveryID = delivery.DeliveryID INNER JOIN orderstatus ON order.OrderStatusID = orderstatus.OrderStatusID WHERE OrderID = '210612'
$sql = "SELECT * FROM ".$db.".order INNER JOIN delivery ON order.DeliveryID = delivery.DeliveryID INNER JOIN orderstatus ON order.OrderStatusID = orderstatus.OrderStatusID WHERE OrderID = ".$_GET["id"]." LIMIT 1";
$result = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($result);
$OrderStatusName = $row['OrderStatusName'];

// Execute SQL query: ORDERITEMS
$sql = "SELECT sum(discount) AS TotDiscount FROM ".$db.".orderitems WHERE OrderID = ".$_GET["id"]." LIMIT 1";
$result = mysqli_query($con, $sql);
$DiscountRow = mysqli_fetch_assoc($result);

$pdf = new PDF('P', 'mm', 'A4', true, 'UTF-8', false);

$pdf->SetCreator('Riverman');
$pdf->SetAuthor($row['CreatedBy']);
$pdf->SetTitle('Order: '.$_GET["id"]);
$pdf->SetSubject('Order utskrift');
$pdf->SetKeywords('Company Inc');

// convert TTF font to TCPDF format and store it on the fonts folder
//$fontname = TCPDF_FONTS::addTTFfont('Calibri.ttf', 'TrueTypeUnicode', '', 96);
$fontname = TCPDF_FONTS::addTTFfont('C:\wamp\www\ekero2d\tcpdf\fonts\calibri.ttf', 'TrueTypeUnicode', '', 96);
$fontname1 = TCPDF_FONTS::addTTFfont('C:\wamp\www\ekero2d\tcpdf\fonts\calibrii.ttf', 'TrueTypeUnicode', '', 96);
$fontname2 = TCPDF_FONTS::addTTFfont('C:\wamp\www\ekero2d\tcpdf\fonts\calibrib.ttf', 'TrueTypeUnicode', '', 96);
/var_dump($fontname1);
exit(0);
/

$pdf->SetFont($fontname, '', 14, '', true);

// New page
$pdf->AddPage();

// Set margins
$pdf->SetMargins(11.5, 10, 10);

//$pdf->AliasNbPages('{nb}');

// Set font and color
/* $pdf->AddFont('helvetica','','helvetica.php');
$pdf->AddFont('helvetica','I','helveticai.php');
$pdf->AddFont('helvetica','B','helveticai.php'); */
//$pdf->($fontname,'',11);
$pdf->SetTextColor(0, 0, 0);
$pdf->SetAutoPageBreak(0, 30);
$pdf->setViewerPreferences(array('Duplex' => 'Simplex'));

//////////////////////////////////////////
// HEADING
//////////////////////////////////////////
$Heading = '';

switch ($OrderStatusName) {
case "Offer":
$Heading = 'Offer';
break;

case "Order":
$Heading = 'Order';
break;

case "Delivery":
$Heading = 'Delivery';
break;

case "Delivered":
$Heading = 'Delivered';
break;

case "Invoice":
$Heading = 'Invoice';
break;

case "Invoiced":
$Heading = 'Invoice (Copy)';
break;

default:
$Heading = 'Order';
break;

}
// Set font size and border color

$pdf->SetFontSize(18);
$pdf->SetXY(102, 13);
$pdf->Cell(30,8,$Heading,0,0,'L');

// ORDERNR, DATE + FRAME
$pdf->OrderIDbox($_GET["id"], $row['OrderDate'], date('Y-m-d'));

// ORDER CUSTOMER HEADINGS
$pdf->OrderCustomerHead($row);

// ORDER ROWS HEADINGS
$pdf->SetY($pdf->GetY()+10, 1);
$pdf->OrderRowHead($row['Discount']);

// ORDER ROWS (OrderItems)
$pdf->SetY($pdf->GetY()+5, 1);

// ORDER ROWS
//$sql = "SELECT Quantity, Supplier, Text, Discount, ItemPrice, Sums, Info FROM orderitems INNER JOIN suppliers ON orderitems.SuppliersID = suppliers.SuppliersID WHERE OrderID = ".$GET["id"];
$sql = "SELECT orderitems.Quantity, suppliers.Supplier, orderitems.Text, orderitems.Discount, orderitems.ItemPrice, orderitems.Sums, orderitems.Info ";
$sql = $sql."FROM orderitems ";
$sql = $sql."INNER JOIN suppliers ON orderitems.SuppliersID = suppliers.SuppliersID ";
$sql = $sql."WHERE OrderID = ".$
GET["id"];
$result = mysqli_query($con, $sql);
$OIRow = mysqli_fetch_assoc($result);

// Convert UTF-8 to ISO-8859-1 for pdf
foreach(array_keys($OIRow) as $key){
$OIRow[$key] = iconv('UTF-8','ISO-8859-1', $OIRow[$key]);
}

// Reset key and Row...
mysqli_data_seek($result, 0);
$OrderPos = 95;

// Print OrderRows...
while($OIRow = mysqli_fetch_array($result)){
// Write first row
$pdf->Cell(10,5,$OIRow['Quantity'],0,0,'L',0);
$pdf->Cell(50,5,iconv('UTF-8','ISO-8859-1',$OIRow['Supplier']),0,0,'L',0);
$pdf->Cell(80,5,iconv('UTF-8','ISO-8859-1',$OIRow['Text']),0,0,'L',0);
// $pdf->Cell(55,5,iconv('UTF-8','ISO-8859-1',$OIRow['Text']),0,0,'L',0);
/* if ($row['Discount'] != 0){
//if ($OIRow['Discount']==0){
$pdf->Cell(25,5,' ',0,0,'R',0);
}else{
$pdf->Cell(25,5,number_format(floatval($OIRow['Discount']), 2, ',', ' '),0,0,'R',0);
}
}else{
$pdf->Cell(25,0,"",0,0,'R',0);
}*/
$pdf->Cell(25,5,number_format($OIRow['ItemPrice'], 2, ',', ' '),0,0,'R',0);
$pdf->Cell(25,5,number_format($OIRow['Sums'], 2, ',', ' '),0,1,'R',0);

$pdf->SetX(21.5);

//MultiCell(float w, float h, string txt [, mixed border [, string align [, boolean fill]]])
$pdf->MultiCell(120,5,iconv('UTF-8','ISO-8859-1',$OIRow['Info']),0,'L',0);
// Cell(float w [, float h [, string txt [, mixed border [, int ln [, string align [, boolean fill [, mixed link]]]]]]])
$pdf->ln();
// If needed do a page break
if ($pdf->gety()> 220){
	$pdf->AddPage('P', 'A4');
	$pdf->SetMargins(11.5, 10, 10);
	if (intval(trim($pdf->PageNo())) % 2 == 0){
		$pdf->AddPage('P', 'A4');
		$pdf->SetMargins(11.5, 10, 10);
		//$pdf->cell(20,5,'Baksida');
	}
	$pdf->SetY(40, 1);
	$pdf->OrderRowHead($row);
	$pdf->SetY($pdf->GetY()+5, 1);
}

}

// ORDER SUM
$pdf->OrderSum($row, $DiscountRow);

// SIDBRYTNING
$pdf->AddPage('P', 'A4');
//$pdf->SetY(40, 1);
if (intval(trim($pdf->PageNo())) % 2 != 0){
$pdf->AddPage('P', 'A4');
$pdf->SetMargins(11.5, 10, 10);
//$pdf->cell(20,5,'Framsida');
}
// LEDGER

// SELECT LedgerID, OrderID, accounts.Name, Amount, Date FROM ledgers INNER JOIN accounts ON ledgers.AccountsID = accounts.ID WHERE OrderID = 210612
$sql = "SELECT LedgerID, OrderID, accounts.Name as AName, Amount, Date FROM ledgers INNER JOIN accounts ON ledgers.AccountsID = accounts.ID WHERE OrderID = ".$_GET["id"];
$result = mysqli_query($con, $sql);
$LRow = mysqli_fetch_assoc($result);
$TotBet = 0;

$pdf->SetY(40, 1);
$pdf->SetFont('calibrib','',14);

$pdf->Cell(10,5,'Payments',0,1,'L');
$pdf->SetFont('calibrib','',11);
$pdf->Cell(10,5,'ID',0,0,'L');
$pdf->Cell(20,5,'Date',0,0,'L');
$pdf->Cell(50,5,'Account',0,0,'L');
$pdf->Cell(40,5,'Sum',0,0,'R');
$pdf->ln();

if ($result->num_rows > 0) {
// Convert UTF-8 to ISO-8859-1 for pdf
foreach(array_keys($LRow) as $key){
$LRow[$key] = iconv('UTF-8','ISO-8859-1', $LRow[$key]);
}

// Reset key and Row...
mysqli_data_seek($result, 0);
$pdf->SetFont('calibri','',11);

while($LRow = mysqli_fetch_array($result)){
	$TotBet = $TotBet + $LRow['Amount'];
	$pdf->Cell(10,5,$LRow['LedgerID'],0,0,'L');
	$pdf->Cell(20,5,$LRow['Date'],0,0,'L');
	$pdf->Cell(50,5,$LRow['AName'],0,0,'L');
	$pdf->Cell(40,5,number_format($LRow['Amount'], 2, ',', ' ').' kr',0,0,'R');
	$pdf->ln();
}

}
$pdf->SetFont('calibrib','',11);
$pdf->Cell(120,5,number_format($TotBet, 2, ',', ' ').' kr',0,0,'R');
$pdf->SetFont('calibri','',11);
//number_format(floatval($OIRow['Discount'])

// NOTES & DELIVERY
$pdf->NotesDelivery($row);

// Underskrift
$pdf->Signment($row);

// Write PDF
$pdf->Output();

?>


Post Reply