<?php
require('fpdf181/fpdf.php');

function currency_format($SiteName, $value)
{
	switch($SiteName)
	{
		case "Amazon UK" :
		case "eBay Fixed Price UK" :
			$currency_symbol = "£";
			break;

		case "eBay Fixed Price DE" : 
		case "Amazon Seller Central - DE" : 
		case "eBay Prix Fixe FR" : 
		case "Amazon Seller Central - FR" : 
		case "eBay Tiendas ES" : 
		case "Amazon Seller Central - ES" : 
		case "Amazon Seller Central - IT" : 
		case "Amazon Seller Central - NL" : 
			$currency_symbol = "€";
			break;
		
		default :
			$currency_symbol = "$";
			break;
	}
	return $currency_symbol.number_format((float)$value, 2, '.', '');
}

class InvoicePDF extends FPDF
{
	// Page header
	function Header()
	{
		$this->Image     ('https://sewing-online.com/skin/frontend/default/theme679/images/main_logo.png',10,10,80);     // Logo
		$this->Cell      (95,10,'',0);                    // Move to the right
		$this->SetFont   ('Helvetica','',8);              // Arial regular 8
		$this->SetTextColor(0,0,0);
		$this->MultiCell (95,3.5,'General Merchandise International
VAT Registration Number : GB 983852765
Sewing Online
9 Mallard Rd, Victoria Business Park
Netherfield, Nottingham, NG4 2PE
Tel: +44 (0)115 987 4422
Fax: +44(0)115 987 4001
Email: sales@sewing-online.com',0,'R');
		$this->ln(10);
	}
	
	function Order($order, $db_link)
	{
		switch($order['SiteName'])
	    {
	    	case 'Amazon UK'                  : $site_country_code="UK"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - US' : $site_country_code="US"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - AU' : $site_country_code="AU"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - CA' : $site_country_code="CA"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - DE' : $site_country_code="DE"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - ES' : $site_country_code="ES"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - FR' : $site_country_code="FR"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - IT' : $site_country_code="IT"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - NL' : $site_country_code="NL"; $SiteOrderID = $order['SiteOrderID']; break;
	    	case 'Amazon Seller Central - MX' : $site_country_code="MX"; $SiteOrderID = $order['SiteOrderID']; break;
                                                                                                             
	    	case 'Catch AU'                   : $site_country_code="AU"; $SiteOrderID = $order['SiteOrderID']; break;
                                                                                                             
	    	case 'TradeMe'                    : $site_country_code="NZ"; $SiteOrderID = $order['SiteOrderID']; break;

	    	case 'eBay Fixed Price UK'        : $site_country_code="UK"; $SiteOrderID = "E-"  . $order['SecondarySiteOrderID']; break;
	    	case 'eBay Fixed Price US'        : $site_country_code="US"; $SiteOrderID = "US-" . $order['SecondarySiteOrderID']; break;
	    	case 'eBay Fixed Price AU'        : $site_country_code="AU"; $SiteOrderID = "AU-" . $order['SecondarySiteOrderID']; break;
	    	case 'eBay Fixed Price DE'        : $site_country_code="DE"; $SiteOrderID = "DE-" . $order['SecondarySiteOrderID']; break;
	    	case 'eBay Prix Fixe FR'          : $site_country_code="FR"; $SiteOrderID = "FR-" . $order['SecondarySiteOrderID']; break;
			case 'eBay Tiendas ES'            : $site_country_code="ES"; $SiteOrderID = "ES-" . $order['SecondarySiteOrderID']; break;
	    }

		$ID             = $order['ID'];
		$PaymentDateUtc = $order['PaymentDateUtc'];

		$billing_name = ""; $billing_join = "";
		if($order['BillingTitle'              ]) { $billing_name.=($billing_join.$order['BillingTitle'    ]); $billing_join=" "; }
		if($order['BillingFirstName'          ]) { $billing_name.=($billing_join.$order['BillingFirstName']); $billing_join=" "; }
		if($order['BillingLastName'           ]) { $billing_name.=($billing_join.$order['BillingLastName' ]); $billing_join=" "; }
		if($order['BillingSuffix'             ]) { $billing_name.=($billing_join.$order['BillingSuffix'   ]); $billing_join=" "; }

		$billing_company = ""; $billing_join = "";
		if($order['BillingCompanyJobTitle'    ]) { $billing_company.=($billing_join.$order['BillingCompanyJobTitle']); $billing_join=" "; }
		if($order['BillingCompanyName'        ]) { $billing_company.=($billing_join.$order['BillingCompanyName'    ]); $billing_join=" "; }

		$billing_telephone = ""; $billing_join = "";
		if($order['BillingDaytimePhone'       ]) { $billing_telephone.=($billing_join.'Day: '.$order['BillingDaytimePhone']); $billing_join=" / "; }
		if($order['BillingEveningPhone'       ]) { $billing_telephone.=($billing_join.'Eve: '.$order['BillingEveningPhone'    ]); $billing_join=" "; }

		$billing_address = ""; $billing_join = ""; $billing_blank_rows=9;
		if($billing_name                       ) { $billing_blank_rows--; $billing_address.=($billing_join.$billing_name                       ); $billing_join="
"; }
		if($billing_company                    ) { $billing_blank_rows--; $billing_address.=($billing_join.$billing_company                    ); $billing_join="
"; }
		if($order['BillingAddressLine1'       ]) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingAddressLine1'       ]); $billing_join="
"; }
		if($order['BillingAddressLine2'       ]) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingAddressLine2'       ]); $billing_join="
"; }
		if($order['BillingCity'               ]) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingCity'               ]); $billing_join="
"; }
		if($order['BillingStateOrProvinceName']) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingStateOrProvinceName']); $billing_join="
"; }
		if($order['BillingPostalCode'         ]) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingPostalCode'         ]); $billing_join="
"; }
		if($order['BillingCountryName'        ]) { $billing_blank_rows--; $billing_address.=($billing_join.$order['BillingCountryName'        ]); $billing_join="
"; }

		while($billing_blank_rows<9 && $billing_blank_rows>0)
		{
			$billing_address.="
";
			$billing_blank_rows--;
		}
		
		$shipping_name = ""; $shipping_join = "";
		if($order['ShippingTitle'              ]) { $shipping_name.=($shipping_join.$order['ShippingTitle'    ]); $shipping_join=" "; }
		if($order['ShippingFirstName'          ]) { $shipping_name.=($shipping_join.$order['ShippingFirstName']); $shipping_join=" "; }
		if($order['ShippingLastName'           ]) { $shipping_name.=($shipping_join.$order['ShippingLastName' ]); $shipping_join=" "; }
		if($order['ShippingSuffix'             ]) { $shipping_name.=($shipping_join.$order['ShippingSuffix'   ]); $shipping_join=" "; }

		$shipping_company = ""; $shipping_join = "";
		if($order['ShippingCompanyJobTitle'    ]) { $shipping_company.=($shipping_join.$order['ShippingCompanyJobTitle']); $shipping_join=" "; }
		if($order['ShippingCompanyName'        ]) { $shipping_company.=($shipping_join.$order['ShippingCompanyName'    ]); $shipping_join=" "; }

		$shipping_telephone = ""; $shiping_join = "";
		if($order['ShippingDaytimePhone'       ]) { $shipping_telephone.=($shipping_join.'Day: '.$order['ShippingDaytimePhone']); $shipping_join=" / "; }
		if($order['ShippingEveningPhone'       ]) { $shipping_telephone.=($shipping_join.'Eve: '.$order['ShippingEveningPhone'    ]); $shipping_join=" "; }

		$shipping_address = ""; $shipping_join = ""; $shipping_blank_rows=9;
		if($shipping_name                       ) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$shipping_name                       ); $shipping_join="
"; }
		if($shipping_company                    ) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$shipping_company                    ); $shipping_join="
"; }
		if($order['ShippingAddressLine1'       ]) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingAddressLine1'       ]); $shipping_join="
"; }
		if($order['ShippingAddressLine2'       ]) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingAddressLine2'       ]); $shipping_join="
"; }
		if($order['ShippingCity'               ]) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingCity'               ]); $shipping_join="
"; }
		if($order['ShippingStateOrProvinceName']) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingStateOrProvinceName']); $shipping_join="
"; }
		if($order['ShippingPostalCode'         ]) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingPostalCode'         ]); $shipping_join="
"; }
		if($order['ShippingCountryName'        ]) { $shipping_blank_rows--; $shipping_address.=($shipping_join.$order['ShippingCountryName'        ]); $shipping_join="
"; }

		while($shipping_blank_rows>0)
		{
			$shipping_address.="
";
			$shipping_blank_rows--;
		}
		

		$this->AddPage();
		$this->SetFillColor(255,255,255);
		$this->SetTextColor(0,0,0);
		$this->SetFont('Times','',11);
		$this->MultiCell(190,7,'Invoice # '.$ID.'
Order # '.$SiteOrderID.'
Order Date: '.date("d M Y H:i:s",strtotime($PaymentDateUtc." UTC")).'',1,'L',true);


		switch($order['OrderTags'])
		{
			case 'AmazonBusiness'             					: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/amazon-business.jpg',       				125,55,30); break;
			case 'AmazonMerchantPrime'        					: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/amazon-merchant-prime.jpg', 				125,55,30); break;
			case 'eBayClickAndCollect'        					: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/click-and-collect.jpg',     				125,55,30); break;
			case 'eBayClickAndCollect,eBayWithdrawZeroSales'	: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/click-and-collect.jpg',     				125,55,30); break;
			case 'eBayPlusDelivery'           					: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/ebay-plus-delivery.jpg',    				125,55,30); break;
			case 'AmazonMerchantPrime,AmazonBusiness'			: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/amazon-merchant-prime_amazon-business.jpg',	125,55,30); break;
			case 'eBayClickAndCollect,eBayPlusDelivery'			: $this->Image('https://sewing-online.com/ca-rest-api/images/order-tags/click-and-collect_ebay-plus-delivery.jpg',	125,55,30); break;
		}

		switch($order['SiteName'])
		{
			case 'Amazon UK'                  : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/gb.jpg',177,49,20); break;
			case 'Amazon Seller Central - US' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/us.jpg',177,49,20); break;
			case 'Amazon Seller Central - AU' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/au.jpg',177,49,20); break;
			case 'Amazon Seller Central - CA' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/ca.jpg',177,49,20); break;
			case 'Amazon Seller Central - DE' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/de.jpg',177,49,20); break;
			case 'Amazon Seller Central - ES' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/es.jpg',177,49,20); break;
			case 'Amazon Seller Central - FR' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/fr.jpg',177,49,20); break;
			case 'Amazon Seller Central - IT' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/it.jpg',177,49,20); break;
			case 'Amazon Seller Central - NL' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/nl.jpg',177,49,20); break;
			case 'Amazon Seller Central - MX' : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/amazon.jpg', 157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/mx.jpg',177,49,20); break;
			case 'Catch AU'                   : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/catch.jpg',  97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/catch.jpg',  157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/au.jpg',177,49,20); break;
			case 'eBay Fixed Price UK'        : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/gb.jpg',177,49,20); break;
			case 'eBay Fixed Price US'        : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/us.jpg',177,49,20); break;
			case 'eBay Fixed Price AU'        : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/au.jpg',177,49,20); break;
			case 'eBay Fixed Price DE'        : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/de.jpg',177,49,20); break;
			case 'eBay Prix Fixe FR'          : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/ebay.jpg',   157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/fr.jpg',177,49,20); break;
			case 'TradeMe'                    : $this->Image('https://sewing-online.com/ca-rest-api/images/markets/trademe.jpg',97,1,45);  $this->Image('https://sewing-online.com/ca-rest-api/images/markets/trademe.jpg',157,49,20);  $this->Image('https://sewing-online.com/ca-rest-api/images/flags/nz.jpg',177,49,20); break;
		}

		$this->SetFillColor(220,220,230);
		$this->SetTextColor(0,0,0);
		$this->SetFont   ('Times','B',12);
		$this->Cell(95,10,'Sold to:',1,0,'L',true);
		$this->Cell(95,10,'Ship to:',1,1,'L',true);

		$this->SetFont   ('Times','',11);
		$this->MultiCell(95,6.5,$billing_address?$billing_address:$shipping_address,1,'L',false);

		$this->SetXY(105,79);

		$this->MultiCell(95,6.5,$shipping_address,1,'L',false);
		
		if($order['ShippingCountryFlag']!="gb.jpg") $this->Image('https://sewing-online.com/ca-rest-api/images/flags/'.$order['ShippingCountryFlag'],177,110,20);

		$this->Cell(95,8,'Billing Telephone:',1,0,'L',true);
		$this->Cell(95,8,'Shipping Telephone:',1,1,'L',true);
		$this->SetFillColor(255,255,255);
		$this->Cell(95,8,$billing_telephone,1,0,'L',true);
		$this->Cell(95,8,$shipping_telephone,1,1,'L',true);
		
		
		$order_fulfillments=@mysql_query("SELECT * FROM __ca_order_fulfillments WHERE OrderID=".$order['ID'],$db_link);
		$ship_class_join="";
		$ship_class="";
		while($order_fulfillment=@mysql_fetch_array($order_fulfillments,MYSQL_ASSOC))
		{
			$ship_class .= ($ship_class_join.$order_fulfillment['ShippingClass']);
			$ship_class_join=" / ";
		}

		$this->SetFillColor(220,220,230);
		$this->SetTextColor(0,0,0);
		$this->SetFont   ('Times','B',12);
		$this->Cell(125,10,'Payment Method: ',1,0,'L',true);
		$this->Cell(65,10,'Shipping Method:',1,1,'R',true);

		$this->SetFont   ('Times','',11);
		$this->MultiCell(125,6.5,$order['PaymentMethod']. ' ('.(($order['BuyerUserId']!=$order['BuyerEmailAddress'])?$order['BuyerUserId']:'').')
Payer Email: '.$order['BuyerEmailAddress'],1,'L',false);

		$this->SetXY(135,157);

		$this->SetFont   ('Times',(($ship_class!="Standard")?'B':''),15);
		$this->Cell(65,13,$ship_class,1,1,'R',false);

		$this->SetFillColor(220,220,230);
		$this->SetTextColor(0,0,0);

		$this->SetFont('Times','B',10);
		$this->Cell(85, 10, 'Product Title',    1, 0, 'L', true);
		$this->Cell(51, 10, 'Inventory Number', 1, 0, 'L', true);
		$this->Cell(14, 10, 'Price',            1, 0, 'R', true);
		$this->Cell(10, 10, 'Qty',              1, 0, 'R', true);
		$this->Cell(14, 10, 'Tax',              1, 0, 'R', true);
		$this->Cell(16, 10, 'Subtotal',         1, 1, 'R', true);

		$order_items=@mysql_query("SELECT * FROM `__ca_order_items` WHERE OrderID=".$order['ID']." ORDER BY `Sku` ASC",$db_link);
		$num_items = mysql_num_rows($order_items);

		$item_total = 0;
		$num_items--;
		
			
		$order_item_count=0;
		while($order_item=@mysql_fetch_array($order_items,MYSQL_ASSOC))
		{
			$order_item_count++;
			$ca_products=@mysql_query("SELECT * FROM __ca_products WHERE InventoryNumber=\"".$order_item['Sku']."\"",$db_link);
			$ca_product=@mysql_fetch_array($ca_products,MYSQL_ASSOC);

			$order_item_fulfillments=@mysql_query("
				SELECT  *
				FROM       __ca_order_item_fulfillmentitems AS OIFI
				LEFT JOIN  __ca_order_fulfillments     AS OF  ON OIFI.FulfillmentID=OF.ID
				WHERE OIFI.OrderID=".$order['ID']." AND OIFI.OrderItemID=".$order_item['ID']."
				ORDER BY OF.ShippingClass ASC",$db_link);

			switch($order['SiteName'])
			{
				case 'Amazon Seller Central - US': 
					$line_value = ($order_item ['Quantity'] * ($order_item['UnitPrice'])) + $order_item['TaxPrice'];
					break;
				default:
					$line_value = ($order_item ['Quantity'] * ($order_item['UnitPrice']));
					break;
			}

			$item_total += $line_value;


			$num_items--;
	
	
			$x=$this->GetX(); $y=$this->GetY(); 

			$this->SetTextColor(0,0,0);
			$this->SetFillColor(250,250,255);
			$this->SetFont   ('Times','',10);

			$this->MultiCell(85,5,$order_item ['Title'].' ['.$ca_product['ShippingType'.$site_country_code].":£".$ca_product['ShippingCost'.$site_country_code]."]",1,'L',true);

			$this->SetXY($x+85,$y);
			$this->SetFont   ('Times','',9);
			$this->Cell(51,15,$order_item ['Sku'],1,0,'L',true);
			$this->Cell(14,15,currency_format($order['SiteName'], ($order_item['UnitPrice'])), 1,0,'R',true);
			$this->Cell(10,15,$order_item ['Quantity'],1,0,'R',true);
			$this->Cell(14,15,currency_format($order['SiteName'], $order_item['TaxPrice']), 1,0,'R',true);
			$this->Cell(16,15,currency_format($order['SiteName'], $line_value), 1,1,'R',true);

			if($order_item_count==4 || $order_item_count==12)
			{
				$this->SetXY($x,$y+20);
				$this->SetTextColor(0,0,0);
				$this->SetFillColor(255,255,255);
				$this->SetFont   ('Times','',16);
				$this->Cell(190,15,"continued...", 0,0,'R',true);
				$this->AddPage();
			}
			else
			{
				$this->SetXY($x,$y+15);
			}
		}

		
		$this->SetFont   ('Times','',10);
		$this->SetFillColor(230,230,240); $this->Cell(174,6,'Sub-total: ',               1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $item_total                 ), 1,1,'R',true);
		$this->SetFillColor(230,230,220); $this->Cell(174,6,'Shipping & Handling: ',     1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $order['TotalShippingPrice']), 1,1,'R',true);
		switch($order['SiteName'])
		{
			case 'Amazon Seller Central - US': 
			case 'Amazon UK': 
			case 'eBay Fixed Price UK':
			case 'Shop.com Marketplace':
				$this->SetFillColor(230,230,240); $this->Cell(174,6,'Grand Total (Excl. Tax): ', 1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $order['TotalPrice']-$order['TotalTaxPrice']), 1,1,'R',true);
				$this->SetFillColor(230,230,220); $this->Cell(174,6,'Tax: ',                     1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $order['TotalTaxPrice']), 1,1,'R',true);
				$this->SetTextColor(255,255,255); $this->SetFont   ('Times','B',12); 
				$this->SetFillColor(  0,  0,  0); $this->Cell(174,10,'Grand Total (Incl. Tax): ', 1,0,'R',true);	$this->Cell(16,10,currency_format($order['SiteName'], $order['TotalPrice']), 1,1,'R',true);
				break;

			default: 
				$this->SetFillColor(230,230,240); $this->Cell(174,6,'Grand Total (Excl. Tax): ', 1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $order['TotalPrice']-$order['TotalTaxPrice']-$order['TotalTaxPrice']), 1,1,'R',true);
				$this->SetFillColor(230,230,220); $this->Cell(174,6,'Tax: ',                     1,0,'R',true);	$this->Cell(16,6,currency_format($order['SiteName'], $order['TotalTaxPrice']), 1,1,'R',true);
				$this->SetTextColor(255,255,255); $this->SetFont   ('Times','B',12); 
				$this->SetFillColor(  0,  0,  0); $this->Cell(174,10,'Grand Total (Incl. Tax): ', 1,0,'R',true);	$this->Cell(16,10,currency_format($order['SiteName'], $order['TotalPrice']-$order['TotalTaxPrice']), 1,1,'R',true);
				break;
		}

		$this->SetFillColor(  250,  250,  250);
		$this->Cell(190,8,'Grand Total (Incl. Tax): ', 1,1,'R',true);
		$this->Cell(190,8,'Grand Total (Incl. Tax): ', 1,1,'R',true);
		$this->Cell(190,8,'Grand Total (Incl. Tax): ', 1,1,'R',true);
		$this->Cell(190,8,'Grand Total (Incl. Tax): ', 1,1,'R',true);


		////////////////////////////////////////////////////////////////////////////////
		// PACKING SLIP
		////////////////////////////////////////////////////////////////////////////////

		$this->AddPage();

		$this->SetXY(10,49);

		$this->SetTextColor(0,0,0);
		$this->SetFillColor(255,255,255); 

		$this->SetXY(10,50);
		$this->SetFont   ('Courier','B',13); 
		$this->MultiCell(110,7,'
'.$shipping_address.'
','TR','L',true);



		if($order['SiteName']=='TradeMe' && time()>=1575115201)
		{
			$this->SetXY(10,114);
			$this->SetFont   ('Courier','B',9); 
			$this->MultiCell(110,6,'Trade Me Limited    GST number 072-491-386    GST PAID','TRL','C',true);
			$this->SetXY(70,90);
		}
		else
		{
			$this->SetXY(70,95);
		}

		$this->SetFont   ('Courier','',8); 
		$this->MultiCell(45,4,'SENDER:
GMI, Unit 9 Mallard Road
Victoria Business Park
Netherfield, Nottingham
NG4 2PE, United Kingdom',1,'L',true);

		$this->SetXY(120,50);
		$this->SetFont('Courier','B',11); 
		$this->MultiCell(80,6,'
'.$order['SiteName'].'

'.'Order Date'.'
'.$order['PaymentDateUtc'].'

'.'Order ID'.'
'.$SiteOrderID.'



','T','R',false);

		$this->ln(4.5);

		$this->SetFillColor(255,255,255); $this->SetTextColor(0,0,0); $this->SetFont('Courier','B',24);
		$this->Cell(0,15,'PACKING SLIP', 'T',1,'C',false);

		$this->ln(1);

		$this->SetFillColor(220,220,230); $this->SetTextColor(0,0,0); $this->SetFont('Courier','B',10);
		$this->Cell(80,10,'Inventory Number',1,0,'L',true); $this->Cell(10,10,'Qty',1,0,'C',true); $this->Cell(100,10,'Product Title',1,1,'L',true);


		$order_items=@mysql_query("SELECT * FROM `__ca_order_items` WHERE OrderID=".$order['ID']." ORDER BY `Sku` ASC",$db_link);
		$num_items = mysql_num_rows($order_items);

		$item_total = 0;
		$num_items--;
		
		while($order_item=@mysql_fetch_array($order_items,MYSQL_ASSOC))
		{
			$this->SetFillColor(250,250,255); $this->SetTextColor(0,0,0); $this->SetFont('Courier','', 10); 
			$this->Cell(80,10,$order_item['Sku'], 1,0,'L',true);
			$this->Cell(10,10,$order_item['Quantity'],1,0,'C',true);
			$this->MultiCell(100,5,$order_item['Title'],1,1,'L',true);
		}
		
		
	}

	// Page footer
	function Footer()
	{
		$this->SetY(-15);                      						// Position at 1.5 cm from bottom
		$this->SetTextColor(200,200,200);
		$this->SetFont('Helvetica','I',8);         					// Arial italic 8
#		$this->Cell(0,10,'Page '.$this->PageNo().'/{nb}',0,0,'C'); 	// Page number
		$this->Cell(0,10,$this->PageNo(),0,0,'C'); 					// Page number
	}
	
	function MultiCell ($w, $h, $txt, $border=0, $align='J', $fill=false                     ) { parent::MultiCell($w, $h,         $this->normalize($txt), $border,      $align, $fill       ); }
	function Cell      ($w, $h=0, $txt='', $border=0, $ln=0, $align='', $fill=false, $link='') { parent::Cell     ($w, $h,         $this->normalize($txt), $border, $ln, $align, $fill, $link); }
	function Write     ($h, $txt, $link=''                                                   ) { parent::Write    (    $h,         $this->normalize($txt),                              $link); }
	function Text      ($x, $y, $txt                                                         ) { parent::Text     (        $x, $y, $this->normalize($txt)                                    ); }
	protected function normalize($word)
	{
		$word = str_replace(Array("€",		"@",   "`",   "¢",   "£",   "¥",   "|",   "«",   "¬",   "¯",   "º",   "±",   "ª",   "µ",   "»",   "¼",   "½",   "¿",   "À",   "Á",   "Â",   "Ã",   "Ä",   "Å",   "Æ",   "Ç",   "È",   "É",   "Ê",   "Ë",   "Ì",   "Í",   "Î",   "Ï",   "Ð",   "Ñ",   "Ò",   "Ó",   "Ô",   "Õ",   "Ö",   "Ø",   "Ù",   "Ú",   "Û",   "Ü",   "Ý",   "Þ",   "ß",   "à",   "á",   "â",   "ã",   "ä",   "å",   "æ",   "ç",   "è",   "é",   "ê",   "ë",   "ì",   "í",   "î",   "ï",   "ð",   "ñ",   "ò",   "ó",   "ô",   "õ",   "ö",   "÷",   "ø",   "ù",   "ú",   "û",   "ü",   "ý",   "þ",   "ÿ"),
		                    Array(chr(128),	"%40", "%60", "%A2", "%A3", "%A5", "%A6", "%AB", "%AC", "%AD", "%B0", "%B1", "%B2", "%B5", "%BB", "%BC", "%BD", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF"),
							$word);
		return urldecode($word);
	}	
}


	define("DATABASE_NAME"  , "sewing_so");
	define("MYSQL_USERNAME" , "sewing_so");
	define("MYSQL_PASSWORD" , "YL1IC9bQEyN");
	if (!($db_link=mysql_connect('localhost',MYSQL_USERNAME,MYSQL_PASSWORD))) { die(MYSQL_USERNAME.'/'.MYSQL_PASSWORD.' Not connected : '.mysql_error()); }
	if (!(mysql_select_db(DATABASE_NAME,$db_link))) { die('Can\'t use '.DATABASE_NAME.' : '.mysql_error()); }

	$order_sql  = "SELECT CAO.*, CACC1.country_name AS ShippingCountryName, CACC1.flag AS ShippingCountryFlag, CACC2.country_name AS BillingCountryName, CACC2.flag AS BillingCountryFlag FROM __ca_orders AS CAO LEFT JOIN __ca_country_codes AS CACC1 ON CAO.ShippingCountry = CACC1.country_code LEFT JOIN __ca_country_codes AS CACC2 ON CAO.BillingCountry  = CACC2.country_code ";
	$order_sql .= "WHERE CAO.ID IN (".$_GET['orderIDs'].") ";
#	$order_sql .= "WHERE CAO.FlagDescription = \"\" AND DistributionCenterTypeRollup=\"SellerManaged\" ";
	$order_sql .= "ORDER BY CAO.ShippingCountry, CAO.SiteName, CAO.PaymentDateUtc ASC ";
#	$order_sql .= "LIMIT 0,200";
	

	$orders=@mysql_query($order_sql,$db_link);

	$pdf = new InvoicePDF(); // Instanciation of inherited class
	$pdf->AliasNbPages();

	while($order=@mysql_fetch_array($orders,MYSQL_ASSOC))
	{
		$pdf->Order($order,$db_link);
	}

	$pdf->Output();
?>
