File: D:/HostingSpaces/SBogers10/deensekroon.komma-mediadesign.nl/wwwroot/invoice/invoice.php
<?php
//define('DB_SERVER', 'localhost');
//define('DB_USER', 'deens_nl_shop');
//define('DB_PASS', 'Danishcr0wn!');
//define('DB_NAME', 'deensekroon_nl_shop');
date_default_timezone_set('Europe/Amsterdam');
require_once '../config/errorReport.php';
require_once '../admin/php/connect.php';
require_once '../admin/php/functions2.php';
require_once '../php/mailformats.php';
if(isset($_GET['invoice']) && isset($_GET['orderId'])) {
$invoice = $_GET['invoice'];
$orderId = $_GET['orderId'];
}
require('../fpdf17/fpdf.php');
//////////// GET INFO \\\\\\\\\\\\\\\\
$output = '';
if($result = $mysqli->query('SELECT DISTINCT oi.id AS orderId, oi.orderCode, oi.customerId, oi.orderDate, oi.amount, oi.shippingCosts, oi.paymentMethod, oi.paymentStatus, oi.shippingMethod, oi.orderStatus, oi.lastUpdate,
oc.customerCode, oc.shipping_title, oc.shipping_firstName, oc.shipping_lastName, oc.shipping_postal, oc.shipping_adres, oc.shipping_city, oc.shipping_country,
oc.invoice_firstName, oc.invoice_lastName, oc.invoice_postal, oc.invoice_adres, oc.invoice_city, oc.invoice_country, oc.email, oc.phone, oc.notes, oi.amount_info
FROM shop_order_info AS oi, shop_order_customer AS oc
WHERE oi.id = oc.orderId
AND oi.id = '.$orderId.'
LIMIT 1')){
if($result->num_rows == 1){
$record = $result->fetch_assoc();
$orderId = $record['orderId'];
$lastUpdate = $record['lastUpdate'];
$orderTimest = $record['orderDate'];
$orderDate = date('d / m / Y',$record['orderDate']);
$paymentMethod = $record['paymentMethod'];
$paymentStatus = $record['paymentStatus'];
$orderCode = $record['orderCode'];
$notes = fromDatabase($record['notes']);
$amountInfo = fromDatabase($record['amount_info']);
$status = '';
switch ($paymentStatus) {
case "completed": // payment complete
$status = 'Je hebt je betaling<br />voldaan via '.$paymentMethod;
break;
case "initialized": // waiting
break;
case "uncleared": // waiting (credit cards or direct debit)
break;
}
//get info instances
$totalProductPrice = 0;
$productlist = '';
$pricelist = '';
// klant details
$customerCode = $record['customerCode'];
$shippingMethod = $record['shippingMethod'];
$shipping_title = $record['shipping_title'];
$shipping_firstname = $record['shipping_firstName'];
$shipping_firstname = iconv('UTF-8', 'windows-1252', $shipping_firstname);
$shipping_lastname = $record['shipping_lastName'];
$shipping_lastname = iconv('UTF-8', 'windows-1252', $shipping_lastname);
$invoice_firstname = $record['invoice_firstName'];
$invoice_firstname = iconv('UTF-8', 'windows-1252', $invoice_firstname);
$invoice_lastname = $record['invoice_lastName'];
$invoice_lastname = iconv('UTF-8', 'windows-1252', $invoice_lastname);
$email = $record['email'];
$phone = $record['phone'];
$shipping_adres = ucwords($record['shipping_adres']);
$shipping_adres = iconv('UTF-8', 'windows-1252', $shipping_adres);
$shipping_postal = strtoupper($record['shipping_postal']);
$shipping_city = ucwords($record['shipping_city']);
$shipping_city = iconv('UTF-8', 'windows-1252', $shipping_city);
$shipping_country = $record['shipping_country'];
$shipping_country = iconv('UTF-8', 'windows-1252', $shipping_country);
$invoice_postal = strtoupper($record['invoice_postal']);
$invoice_adres = ucwords($record['invoice_adres']);
$invoice_adres = iconv('UTF-8', 'windows-1252', $invoice_adres);
$invoice_city = ucwords($record['invoice_city']);
$invoice_city = iconv('UTF-8', 'windows-1252', $invoice_city);
$invoice_country = ucwords($record['invoice_country']);
$invoice_country = iconv('UTF-8', 'windows-1252', $invoice_country);
}
//////////// CREATE PDF INVOICE \\\\\\\\\\
# create pdf
$pdf = new FPDF('P','mm','A4');
$pdf->AddPage();
# write content
$pdf->SetY(20);
$pdf->SetX(20);
// Main thanks
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',10);
$pdf->Cell(100,5,'Hartelijk bedankt voor je bestelling bij Deense Kroon!');
// Logo
$pdf->Image('./invoice-logo.jpg',170,20,20,20);
// Deense kroon NAW
$pdf->SetY(46);
$pdf->SetX(150);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',10);
$pdf->Cell(40,5,'Deense Kroon',0,2,'R');
$pdf->SetY(52);
$pdf->SetX(150);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',8.5);
$pdf->Cell(40,5,'Willemstraat 17',0,2,'R');
$pdf->Cell(40,5,'5611 HB Eindhoven',0,2,'R');
$pdf->SetY(65);
$pdf->SetX(150);
$pdf->Cell(40,5,'NL74 INGB 0006 0825 28',0,2,'R');
$pdf->Cell(40,5,'tnv Deense Kroon',0,2,'R');
$pdf->SetY(78);
$pdf->SetX(150);
$pdf->Cell(40,5,'kvk: 54829992',0,2,'R');
$pdf->Cell(40,5,'btw: NL821456467B01',0,2,'R');
// Title
$pdf->SetY(43);
$pdf->SetX(20);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',14);
$pdf->Cell(20,10,'Factuur');
// Invoice details
$pdf->SetY(52);
$pdf->SetX(20);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',8.5);
$pdf->Cell(30,5,'Datum');
$pdf->SetX(70);
$pdf->Cell(30,5, $orderDate);
$pdf->SetY(57);
$pdf->SetX(20);
$pdf->Cell(30,5,'Ordernummer');
$pdf->SetX(70);
$pdf->Cell(30,5, $orderCode);
// Invoice address
$pdf->SetY(65);
$pdf->SetX(20);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',9);
$pdf->Cell(30,5,'Factuur aan');
$pdf->Ln(5);
$pdf->SetX(20);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',8.5);
$pdf->Cell(30,5,$invoice_firstname . ' ' . $invoice_lastname,0,2);
$pdf->Cell(30,5,$invoice_adres,0,2);
$pdf->Cell(30,5,$invoice_postal.' '.$invoice_city,0,2);
$pdf->Cell(30,5,$invoice_country,0,2);
//aflever adres
$pdf->SetY(65);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',9);
$pdf->SetX(70);
if($shippingMethod == 'pickUpFromShopEindhoven')
{
$pdf->Cell(30, 5, 'Afhalen in Eindhoven', 0, 2);
}
else
{
$pdf->Cell(30, 5, 'Verzenden naar', 0, 2);
$pdf->SetTextColor(111, 111, 110);
$pdf->SetFont('Helvetica', '', 8.5);
$pdf->Cell(30, 5, $shipping_firstname . ' ' . $shipping_lastname, 0, 2);
$pdf->Cell(30, 5, $shipping_adres, 0, 2);
$pdf->Cell(30, 5, $shipping_postal . ' ' . $shipping_city, 0, 2);
$pdf->Cell(30, 5, $shipping_country, 0, 2);
}
$pdf->SetDrawColor(220,220,220);
$pdf->Line(20,100,190,100);
//bestelling
// $pdf->SetY(117);
// $pdf->SetX(30);
// $pdf->SetTextColor(87,87,86);
// $pdf->SetFont('Helvetica','B',7);
// $pdf->Cell(30,5,'Uw bestelling');
// $pdf->SetY(105);
// $pdf->SetX(30);
// $pdf->Cell(10,5, '#');
// $pdf->Cell(120,5, 'Naam product');
// //$pdf->Cell(60,5, 'Productcode');
// $pdf->Cell(20,5, 'Prijs' ,0,1,'R');
$instanceResult = $mysqli->query('SELECT DISTINCT title, price_original, price_current, productId, color, numProducts , stockcode
FROM shop_order_products
WHERE orderId = '.$orderId);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',8.5);
$currentY = 105;
$lineHeight = 5;
while($instanceRecord = $instanceResult->fetch_assoc()){
$title = fromDatabase($instanceRecord['title']);
$numProducts = $instanceRecord['numProducts'];
$stockcode = $instanceRecord['stockcode'];
$totalProductPrice += $instanceRecord['price_current']*$numProducts;
$displayPrice = displayPriceForPDF($instanceRecord['price_current']*$numProducts);
$pdf->SetY($currentY);
$pdf->SetX(20);
$pdf->Cell(10,$lineHeight, $numProducts.'x');
$pdf->Cell(120,$lineHeight, ucwords($title));
// Line below
$pdf->SetY($currentY + $lineHeight);
$pdf->SetX(30);
$pdf->Cell(120,$lineHeight, $stockcode);
// Back to price
$pdf->SetY($currentY);
$pdf->SetX(170);
$pdf->Cell(20,$lineHeight, $displayPrice ,0,1,'R');
$currentY += $lineHeight*2.5;
}
$totalPrice = (float) $record['amount'];
$shippingCosts = $record['shippingCosts'];
// check if giftcards where used
$gcAmount = 0;
if($gcresult = $mysqli->query('SELECT amount FROM gc_used WHERE orderId = '.$orderId))
{
while($gcrecord = $gcresult->fetch_assoc())
{
$gcAmount += $gcrecord['amount'];
}
}
// Use invoice service
include_once $_SERVER['DOCUMENT_ROOT'] . '/App/Invoice/InvoiceService.php';
$invoiceService = new \App\Invoice\InvoiceService();
// Fix the old way:
if($shippingCosts === null)
$shippingCosts = $invoiceService->estimateShippingCosts($totalPrice,$totalProductPrice,$gcAmount,$shipping_country);
// Prepare values
$subtotalPrice = (float) $totalProductPrice;
$shippingCosts = (float) $shippingCosts;
$totalPrice = (float) $totalPrice;
// Do we have order-discount?
// Check if the total price matches the subtotal + shipping
// If not the order has a discount
$orderDiscount = $invoiceService->calculateOrderDiscount($totalPrice,$subtotalPrice,$shippingCosts,$gcAmount);
// Create display prices
$displaySubtotalPrice = displayPriceForPDF($subtotalPrice);
$displayDiscountPrice = displayPriceForPDF($orderDiscount);
$displayShippingCost = displayPriceForPDF($shippingCosts);
$displayTotalPrice = displayPriceForPDF($totalPrice);
if($vatresult = $mysqli->query('SELECT VAT FROM countries WHERE isocode = "'.$invoice_country.'" LIMIT 1')){
$vatrecord = $vatresult->fetch_assoc();
$vat = $vatrecord['VAT'];
// if larger then 1 oct 2012
if($orderTimest > 1349054915){
$vat = 21;
}
else{
$vat = 19;
}
$factor = 1 + ($vat / 100);
$exbtw = $totalPrice / $factor;
$btw = $totalPrice - $exbtw;
$displayExBtw = displayPriceForPDF($exbtw);
$displayBtw = displayPriceForPDF($btw);
}
$currentY += 3;
$pdf->Line(20,$currentY,190,$currentY);
$currentY += 3;
// Total
$orderDetailsX = 140;
$orderDetailsWidth = 30;
$pdf->SetY($currentY);
$pdf->SetX($orderDetailsX);
$pdf->Cell($orderDetailsWidth,5,'Subtotaal');
$pdf->Cell(20,5, $displaySubtotalPrice ,0,1,'R');
$pdf->SetX($orderDetailsX);
if($orderDiscount < 0)
{
$pdf->Cell($orderDetailsWidth,5,'Korting');
$pdf->Cell(20,5, '- ' . $displayDiscountPrice ,0,1,'R');
$pdf->SetX($orderDetailsX);
}
$pdf->Cell($orderDetailsWidth,5,'Verzendkosten');
$pdf->Cell(20,5, $displayShippingCost ,0,1,'R');
if($gcAmount > 0)
{
$pdf->SetX($orderDetailsX);
$pdf->Cell($orderDetailsWidth,5,'Giftcards');
$pdf->Cell(20,5, '- '.displayPriceForPDF($gcAmount) ,0,1,'R');
}
$pdf->SetX($orderDetailsX);
$pdf->Cell($orderDetailsWidth,5,'Totaal');
$pdf->Cell(20,5, $displayTotalPrice ,0,1,'R');
// Vat
$pdf->SetY($currentY);
$pdf->SetX(30);
$pdf->SetTextColor(180,180,180);
$pdf->Cell(20,5,'Excl btw');
$pdf->Cell(20,5, $displayExBtw ,0,1,'R');
$pdf->SetX(30);
$pdf->Cell(20,5,$vat.'% btw');
$pdf->Cell(20,5, $displayBtw,0,1,'R');
$pdf->SetY($currentY+30);
// Notes
if( ! empty($notes))
{
$pdf->SetX(20);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','B',9);
$pdf->SetX(20);
$pdf->Cell(20,5,'Opmerking',0,2);
$pdf->SetFont('Helvetica','',9);
$pdf->MultiCell(120, 5, str_replace('<br />','',$notes),0,2);
}
$amountInfo = json_decode($amountInfo);
if($orderDiscount < 0)
{
if(isset($amountInfo->discount_title))
{
// Discount
$pdf->SetTextColor(111, 111, 110);
$pdf->SetFont('Helvetica', 'B', 9);
$pdf->SetX(150);
$pdf->Cell(40, 5, 'Korting', 0, 2, 'R');
$pdf->SetFont('Helvetica','',9);
$pdf->Cell(40, 5, $amountInfo->discount_title, 0, 2, 'R');
$pdf->Cell(40, 5, 'Code: ' . $amountInfo->discount_code, 0, 2, 'R');
}
}
if(isset($amountInfo->gift_card))
{
if(count($amountInfo->gift_card) > 0)
{
$pdf->SetTextColor(111, 111, 110);
$pdf->SetFont('Helvetica', 'B', 9);
$pdf->SetX(150);
$pdf->Cell(40, 5, 'Cadeaubonnen', 0, 2, 'R');
$pdf->SetFont('Helvetica','',9);
foreach($amountInfo->gift_card as $card)
{
// Temp solution for older orders before "usedAmount"
if($card->usedAmount == null)
{
$pdf->Cell(40, 5, $card->code .' (' . displayPriceForPDF($card->amount) . ')', 0, 2, 'R');
}
else
{
$pdf->Cell(40, 5, $card->code .' (' . displayPriceForPDF($card->usedAmount) . ')', 0, 2, 'R');
}
}
}
}
// Shipping sticker
$pdf->SetY(249);
$pdf->SetX(30);
$pdf->Image('./invoice-logo.jpg',110,224,20,20);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',9);
if($shippingMethod == 'pickUpFromShopEindhoven')
{
$pdf->Cell(70,7,'Afhalen in Eindhoven',0,2);
}
else
{
$pdf->Cell(70,7,'Aan:',0,2);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',9);
$pdf->Cell(30,5,$shipping_firstname. ' ' .$shipping_lastname,0,2);
$pdf->Cell(30,5,$shipping_adres,0,2);
$pdf->Cell(30,5,$shipping_postal.' '.$shipping_city,0,2);
$pdf->Cell(30,5,$shipping_country,0,2);
}
$pdf->SetY(249);
$pdf->SetX(110);
$pdf->SetTextColor(87,87,86);
$pdf->SetFont('Helvetica','B',9);
$pdf->Cell(70,7,'Retouradres:',0,2);
$pdf->SetTextColor(111,111,110);
$pdf->SetFont('Helvetica','',9);
$pdf->Cell(70,5,'Deense Kroon',0,2);
$pdf->Cell(70,5,'Willemstraat 17',0,2);
$pdf->Cell(70,5,'5611 HB Eindhoven',0,2);
$pdf->Cell(70,5,'Nederland',0,2);
# output pdf
$pdf->Output();
}
/*
$output .= '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Factuur '.$invoice.'</title>
</head>
<body>';
//$output .= '<a href="javascript:window.print()">Print deze factuur</a><br /><br />';
//$output .= getInvoiceEmail($orderId);
$output .= '</body>
</html>';
echo $output;*/