A Qcodo based CMS/ecommerce framework
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

354 lines
15 KiB

12 years ago
  1. <?php
  2. /**
  3. * This file is a part of Quasi CMS
  4. *@package Quasi
  5. */
  6. if(!defined('QUASICMS') ) die('No Quasi.');
  7. if (!defined("ORDERPRINTER.CLASS.PHP")){
  8. define("ORDERPRINTER.CLASS.PHP",1);
  9. define('FPDF_FONTPATH', __QUASI_CONTRIB__ . '/assets/php/fpdf/font/');
  10. require_once(__QUASI_CONTRIB__ . '/assets/php/fpdf/fpdf.php');
  11. /**
  12. * Class OrderPrinter - prints out label images, invoices and packing slips for orders
  13. *
  14. *@author Erik Winn <erikwinnmail@yahoo.com>
  15. *
  16. * $Id: OrderPrinter.class.php 517 2009-03-24 17:59:23Z erikwinn $
  17. *@version 0.1
  18. *
  19. *@copyright (C) 2008 by Erik Winn
  20. *@license GPL v.2
  21. This program is free software; you can redistribute it and/or modify
  22. it under the terms of the GNU General Public License as published by
  23. the Free Software Foundation; either version 2 of the License, or
  24. (at your option) any later version.
  25. This program is distributed in the hope that it will be useful,
  26. but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. GNU General Public License for more details.
  29. You should have received a copy of the GNU General Public License
  30. along with this program; if not, write to the Free Software
  31. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA
  32. *
  33. *@package Quasi
  34. * @subpackage CMS
  35. */
  36. class OrderPrinter
  37. {
  38. /**
  39. *@var Order member object
  40. */
  41. protected $objOrder;
  42. /**
  43. *@var string Errors
  44. */
  45. protected $strErrors;
  46. /**
  47. *@var string strTempDirectory
  48. */
  49. protected $strTempDirectory = '/tmp' ;
  50. /**
  51. *@var string strPrinterName
  52. */
  53. protected $strPrinterName = '';
  54. /**
  55. *@var string strPrinterOptions
  56. */
  57. protected $strPrinterOptions = '';
  58. /**
  59. *@var string strFileNamePrefix
  60. */
  61. protected $strFileNamePrefix = STORE_NAME;
  62. /**
  63. * OrderPrinter Constructor
  64. *
  65. * @param Order objOrder
  66. */
  67. public function __construct($objOrder)
  68. {
  69. $this->objOrder = $objOrder;
  70. }
  71. public function PrintInvoice()
  72. {
  73. $strFileName = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_invoice_' . $this->objOrder->Id . '.pdf';
  74. $objPdf = new FPDF();
  75. $objPdf->AddPage();
  76. //Logo
  77. $objPdf->Image(__QUASI_CORE__ . '/assets/images/header_logo.jpg',10, 8, 33);
  78. //Move to the right
  79. $objPdf->Cell(80);
  80. $objPdf->SetFont('Arial','B',16);
  81. $objPdf->Cell(30, 10, STORE_NAME . ' Order Invoice', 'B', 1, 'C');
  82. $objPdf->Ln(3);
  83. $objPdf->SetFont('Arial','I',11);
  84. $objPdf->Cell(0, 5, STORE_NAME . ' - ' . STORE_ADDRESS1 . ', ' . STORE_ADDRESS2 . ', ' . STORE_CITY . ', ' . STORE_STATE . ' ' . STORE_POSTAL_CODE, 0, 1, 'C');
  85. $objPdf->Ln(5);
  86. $objPdf->SetFont('Times','B',15);
  87. $objPdf->Cell(20, 10, 'Order #' . $this->objOrder->Id , 0,0);
  88. $objPdf->Cell(60);
  89. $objPdf->Cell(0, 10, 'Order Date: ' . $this->objOrder->CreationDate , 0,0);
  90. $objPdf->Ln(10);
  91. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  92. $objPdf->SetFont('Times','',14);
  93. $objPdf->Ln(5);
  94. $objPdf->Cell(0, 10, 'Shipping Method: ' . $this->objOrder->ShippingMethod->Title . ' ' . $this->objOrder->ShippingMethod->ServiceType, 0,1);
  95. $objPdf->Cell(0, 10, 'Payment Method: ' . $this->objOrder->PaymentMethod->Title . ' via ' . $this->objOrder->PaymentMethod->ServiceProvider , 0,1);
  96. $objPdf->Ln(5);
  97. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  98. $objPdf->SetFont('Times','B',14);
  99. $objPdf->Cell(20, 10, 'Ship to: ' , 0,0);
  100. $objPdf->Cell(75);
  101. $objPdf->Cell(0, 10, 'Bill to: ' , 0,1);
  102. $objPdf->SetFont('Times','',12);
  103. $objPdf->Cell(20, 5, $this->objOrder->FullShippingName, 0,0);
  104. $objPdf->Cell(75);
  105. $objPdf->Cell(0, 5, $this->objOrder->FullBillingName, 0,1);
  106. if($this->objOrder->ShippingStreet1)
  107. {
  108. $objPdf->Cell(20, 5, $this->objOrder->ShippingStreet1, 0,0);
  109. $objPdf->Cell(75);
  110. $objPdf->Cell(0, 5, $this->objOrder->BillingStreet1, 0,1);
  111. }
  112. if($this->objOrder->ShippingStreet2)
  113. {
  114. $objPdf->Cell(20, 5, $this->objOrder->ShippingStreet2, 0,0);
  115. $objPdf->Cell(75);
  116. $objPdf->Cell(0, 5, $this->objOrder->BillingStreet2, 0,1);
  117. }
  118. if($this->objOrder->ShippingSuburb)
  119. {
  120. $objPdf->Cell(20, 5, $this->objOrder->ShippingSuburb, 0,0);
  121. $objPdf->Cell(75);
  122. $objPdf->Cell(0, 5, $this->objOrder->BillingSuburb, 0,1);
  123. }
  124. $objPdf->Cell(20, 5, $this->objOrder->ShippingCity, 0,0);
  125. $objPdf->Cell(75);
  126. $objPdf->Cell(0, 5, $this->objOrder->BillingCity, 0,1);
  127. if($this->objOrder->ShippingCounty)
  128. {
  129. $objPdf->Cell(20, 5, $this->objOrder->ShippingCounty, 0,0);
  130. $objPdf->Cell(75);
  131. $objPdf->Cell(0, 5, $this->objOrder->BillingCounty, 0,1);
  132. }
  133. if(ZoneType::NoZone != $this->objOrder->ShippingZoneId )
  134. {
  135. $objPdf->Cell(20, 5, ZoneType::ToString($this->objOrder->ShippingZoneId) . ' - ' . $this->objOrder->ShippingPostalCode, 0,0);
  136. $objPdf->Cell(75);
  137. $objPdf->Cell(0, 5, ZoneType::ToString($this->objOrder->BillingZoneId) . ' - ' . $this->objOrder->BillingPostalCode, 0,1);
  138. }
  139. $objPdf->Cell(20, 5, CountryType::ToString($this->objOrder->ShippingCountryId), 0,0);
  140. $objPdf->Cell(75);
  141. $objPdf->Cell(0, 5, CountryType::ToString($this->objOrder->BillingCountryId), 0,1);
  142. $objPdf->Ln(5);
  143. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  144. $objPdf->SetFont('Times','B',14);
  145. $objPdf->Cell(130, 10, 'Product Name' , 0,0);
  146. $objPdf->Cell(15, 10, 'Qty' , 0,0);
  147. $objPdf->Cell(15, 10, 'Price' , 0,0);
  148. $objPdf->Cell(15, 10, 'Total' , 0,1);
  149. $objPdf->SetFont('Times','',12);
  150. foreach($this->objOrder->GetOrderItemArray() as $objOrderItem )
  151. {
  152. $objPdf->Cell(130, 5, $objOrderItem->Product->Model, 0,0);
  153. $objPdf->Cell(15, 5, $objOrderItem->Quantity, 0,0);
  154. $objPdf->Cell(18, 5, money_format('%n', $objOrderItem->Product->RetailPrice), 0,0);
  155. $objPdf->Cell(18, 5, money_format('%n', ($objOrderItem->Quantity * $objOrderItem->Product->RetailPrice)) , 0,1);
  156. }
  157. $objPdf->Ln(3);
  158. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  159. $objPdf->Ln(3);
  160. $objPdf->Cell(150, 5, 'Sub-Total: ' , 0, 0, 'R');
  161. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ProductTotalCharged) , 0, 1, 'R');
  162. $objPdf->Cell(150, 5, 'Shipping and Handling: ' , 0, 0, 'R');
  163. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ShippingCharged + $this->objOrder->HandlingCharged) , 'B', 1, 'R' );
  164. $objPdf->Ln(3);
  165. $objPdf->Cell(150, 5, 'Grand Total: ' , 0, 0, 'R');
  166. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ProductTotalCharged + $this->objOrder->ShippingCharged + $this->objOrder->HandlingCharged) , 0, 1, 'R');
  167. $objPdf->Ln(10);
  168. $objPdf->SetFont('Arial','I',16);
  169. $objPdf->Cell(0, 5, 'Thanks for using ' . STORE_NAME . '!', 0, 1, 'C');
  170. $objPdf->Output($strFileName);
  171. $this->printFile($strFileName);
  172. }
  173. public function PrintShippingLabels()
  174. {
  175. $strShipLabelFilename = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_shipping_label_' . $this->objOrder->Id;
  176. $strCustomsFormFilename = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_customs_form_' . $this->objOrder->Id;
  177. //use old images if possible - this is for reprints on errors ..
  178. //Note that it is assumed that if the shipping label images are there then the customs images
  179. //are also there - if this is not true there will be problems ..
  180. if(file_exists($strShipLabelFilename))
  181. return $this->reprintLabels( $strShipLabelFilename, $strCustomsFormFilename);
  182. $objImage = $this->objOrder->CreateShippingLabel();
  183. //local pickup does not need labels ..
  184. if(!$objImage)
  185. return;
  186. if(is_string($objImage))
  187. {
  188. $fp = fopen( $strShipLabelFilename, 'w+' );
  189. fwrite( $fp, $objImage );
  190. fclose($fp);
  191. $this->printFile($strShipLabelFilename);
  192. if($this->objOrder->ExtraDocumentImages)
  193. {
  194. foreach($this->objOrder->ExtraDocumentImages as $objDocumentImage)
  195. {
  196. $strFilename = $strShipLabelFilename . '_' . $objDocumentImage->Type . '-' . $objDocumentImage->Copies;
  197. $fp = fopen( $strFilename, 'w+' );
  198. fwrite( $fp, $objDocumentImage->Image );
  199. fclose($fp);
  200. $this->printFile($strFilename, $objDocumentImage->Copies);
  201. }
  202. }
  203. if($this->objOrder->CustomsFormImages)
  204. {
  205. foreach($this->objOrder->CustomsFormImages as $intIdx => $strDocumentImage)
  206. {
  207. $fp = fopen( $strCustomsFormFilename . '-' . $intIdx, 'w+' );
  208. fwrite( $fp, $strDocumentImage );
  209. fclose($fp);
  210. $this->printFile( $strCustomsFormFilename . '-' . $intIdx);
  211. }
  212. }
  213. }
  214. else
  215. {
  216. imagepng($objImage, $strShipLabelFilename);
  217. $this->printFile($strShipLabelFilename);
  218. if($this->objOrder->ExtraDocumentImages)
  219. {
  220. foreach($this->objOrder->ExtraDocumentImages as $objDocumentImage)
  221. {
  222. $strFilename = $strShipLabelFilename . '_' . $objDocumentImage->Type . '-' . $objDocumentImage->Copies;
  223. imagepng($objDocumentImage->Image, $strFileName );
  224. $this->printFile($strFilename, $objDocumentImage->Copies);
  225. }
  226. }
  227. if($this->objOrder->CustomsFormImages)
  228. {
  229. foreach($this->objOrder->CustomsFormImages as $intIdx => $strDocumentImage)
  230. imagepng($strDocumentImage,$strCustomsFormFilename . '-' . $intIdx );
  231. $this->printFile( $strCustomsFormFilename . '-' . $intIdx);
  232. }
  233. }
  234. }
  235. public function reprintLabels($strLabelFileName, $strCustomsFormFileName)
  236. {
  237. $aryDirList = scandir($this->strTempDirectory);
  238. foreach($aryDirList as $strFileName)
  239. {
  240. if('.' == $strFileName || '..' == $strFileName )
  241. continue;
  242. if(false !== strpos( $strLabelFileName, $strFileName )
  243. || false !== strpos( $strCustomsFormFileName, $strFileName ))
  244. $this->printFile($this->strTempDirectory . '/' . $strFileName);
  245. }
  246. }
  247. public function printFile($strFileName, $intQuantity=1)
  248. {
  249. $strCommand = sprintf("lpr -P%s %s '-#%s' %s",
  250. $this->strPrinterName,
  251. $this->strPrinterOptions,
  252. $intQuantity,
  253. $strFileName);
  254. exec($strCommand);
  255. }
  256. public function __get($strName)
  257. {
  258. switch ($strName)
  259. {
  260. case 'TempDirectory':
  261. return $this->strTempDirectory ;
  262. case 'FileNamePrefix':
  263. return $this->strFileNamePrefix ;
  264. case 'PrinterName':
  265. return $this->strPrinterName ;
  266. case 'PrinterOptions':
  267. return $this->strPrinterOptions ;
  268. case 'Order':
  269. return $this->objOrder ;
  270. case 'Errors':
  271. return $this->objErrors ;
  272. default:
  273. throw new QCallerException('OrderPrinter::__get() Unknown property: ' . $strName);
  274. }
  275. }
  276. public function __set($strName, $mixValue)
  277. {
  278. switch ($strName)
  279. {
  280. case 'Order':
  281. try {
  282. return ($this->objOrder = QType::Cast($mixValue, 'Order' ));
  283. } catch (QInvalidCastException $objExc) {
  284. $objExc->IncrementOffset();
  285. throw $objExc;
  286. }
  287. case 'PrinterOptions':
  288. try {
  289. return ($this->strPrinterOptions = QType::Cast($mixValue, QType::String ));
  290. } catch (QInvalidCastException $objExc) {
  291. $objExc->IncrementOffset();
  292. throw $objExc;
  293. }
  294. case 'PrinterName':
  295. try {
  296. return ($this->strPrinterName = QType::Cast($mixValue, QType::String ));
  297. } catch (QInvalidCastException $objExc) {
  298. $objExc->IncrementOffset();
  299. throw $objExc;
  300. }
  301. case 'FileNamePrefix':
  302. try {
  303. return ($this->strFileNamePrefix = QType::Cast($mixValue, QType::String ));
  304. } catch (QInvalidCastException $objExc) {
  305. $objExc->IncrementOffset();
  306. throw $objExc;
  307. }
  308. case 'TempDirectory':
  309. try {
  310. return ($this->strTempDirectory = QType::Cast($mixValue, QType::String ));
  311. } catch (QInvalidCastException $objExc) {
  312. $objExc->IncrementOffset();
  313. throw $objExc;
  314. }
  315. case 'Errors':
  316. try {
  317. return ($this->strErrors = QType::Cast($mixValue, QType::String ));
  318. } catch (QInvalidCastException $objExc) {
  319. $objExc->IncrementOffset();
  320. throw $objExc;
  321. }
  322. default:
  323. throw new QCallerException('OrderPrinter::__set() Unknown property: ' . $strName);
  324. }
  325. }
  326. }//end class
  327. }//end define
  328. ?>