A QCodo powered CMS
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.

306 lines
11 KiB

  1. <?php
  2. if(!defined('QUINTACMS') ) die('No Quinta.');
  3. if (!defined("ORDERPRINTER.CLASS.PHP")){
  4. define("ORDERPRINTER.CLASS.PHP",1);
  5. define('FPDF_FONTPATH', __QUINTA_CONTRIB__ . '/assets/php/fpdf/font/');
  6. require_once(__QUINTA_CONTRIB__ . '/assets/php/fpdf/fpdf.php');
  7. /**
  8. * Class OrderPrinter - prints out label images, invoices and packing slips for orders
  9. *
  10. *@author Erik Winn <sidewalksoftware@gmail.com>
  11. *
  12. *@version 0.3
  13. *
  14. *@package Quinta
  15. * @subpackage CMS
  16. */
  17. class OrderPrinter{
  18. /**
  19. *@var Order member object
  20. */
  21. protected $objOrder;
  22. /**
  23. *@var string Errors
  24. */
  25. protected $strErrors;
  26. /**
  27. *@var string strTempDirectory
  28. */
  29. protected $strTempDirectory = '/tmp' ;
  30. /**
  31. *@var string strPrinterName
  32. */
  33. protected $strPrinterName = '';
  34. /**
  35. *@var string strPrinterOptions
  36. */
  37. protected $strPrinterOptions = '';
  38. /**
  39. *@var string strFileNamePrefix
  40. */
  41. protected $strFileNamePrefix = STORE_NAME;
  42. /**
  43. * OrderPrinter Constructor
  44. *
  45. * @param Order objOrder
  46. */
  47. public function __construct($objOrder){
  48. $this->objOrder = $objOrder;
  49. }
  50. public function PrintInvoice(){
  51. $strFileName = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_invoice_' . $this->objOrder->Id . '.pdf';
  52. $objPdf = new FPDF();
  53. $objPdf->AddPage();
  54. //Logo
  55. $objPdf->Image(__QUINTA_CORE__ . '/assets/images/header_logo.jpg',10, 8, 33);
  56. //Move to the right
  57. $objPdf->Cell(80);
  58. $objPdf->SetFont('Arial','B',16);
  59. $objPdf->Cell(30, 10, STORE_NAME . ' Order Invoice', 'B', 1, 'C');
  60. $objPdf->Ln(3);
  61. $objPdf->SetFont('Arial','I',11);
  62. $objPdf->Cell(0, 5, STORE_NAME . ' - ' . STORE_ADDRESS1 . ', ' . STORE_ADDRESS2 . ', ' . STORE_CITY . ', ' . STORE_STATE . ' ' . STORE_POSTAL_CODE, 0, 1, 'C');
  63. $objPdf->Ln(5);
  64. $objPdf->SetFont('Times','B',15);
  65. $objPdf->Cell(20, 10, 'Order #' . $this->objOrder->Id , 0,0);
  66. $objPdf->Cell(60);
  67. $objPdf->Cell(0, 10, 'Order Date: ' . $this->objOrder->CreationDate , 0,0);
  68. $objPdf->Ln(10);
  69. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  70. $objPdf->SetFont('Times','',14);
  71. $objPdf->Ln(5);
  72. $objPdf->Cell(0, 10, 'Shipping Method: ' . $this->objOrder->ShippingMethod->Title . ' ' . $this->objOrder->ShippingMethod->ServiceType, 0,1);
  73. $objPdf->Cell(0, 10, 'Payment Method: ' . $this->objOrder->PaymentMethod->Title . ' via ' . $this->objOrder->PaymentMethod->ServiceProvider , 0,1);
  74. $objPdf->Ln(5);
  75. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  76. $objPdf->SetFont('Times','B',14);
  77. $objPdf->Cell(20, 10, 'Ship to: ' , 0,0);
  78. $objPdf->Cell(75);
  79. $objPdf->Cell(0, 10, 'Bill to: ' , 0,1);
  80. $objPdf->SetFont('Times','',12);
  81. $objPdf->Cell(20, 5, $this->objOrder->FullShippingName, 0,0);
  82. $objPdf->Cell(75);
  83. $objPdf->Cell(0, 5, $this->objOrder->FullBillingName, 0,1);
  84. if($this->objOrder->ShippingStreet1){
  85. $objPdf->Cell(20, 5, $this->objOrder->ShippingStreet1, 0,0);
  86. $objPdf->Cell(75);
  87. $objPdf->Cell(0, 5, $this->objOrder->BillingStreet1, 0,1);
  88. }
  89. if($this->objOrder->ShippingStreet2){
  90. $objPdf->Cell(20, 5, $this->objOrder->ShippingStreet2, 0,0);
  91. $objPdf->Cell(75);
  92. $objPdf->Cell(0, 5, $this->objOrder->BillingStreet2, 0,1);
  93. }
  94. if($this->objOrder->ShippingSuburb){
  95. $objPdf->Cell(20, 5, $this->objOrder->ShippingSuburb, 0,0);
  96. $objPdf->Cell(75);
  97. $objPdf->Cell(0, 5, $this->objOrder->BillingSuburb, 0,1);
  98. }
  99. $objPdf->Cell(20, 5, $this->objOrder->ShippingCity, 0,0);
  100. $objPdf->Cell(75);
  101. $objPdf->Cell(0, 5, $this->objOrder->BillingCity, 0,1);
  102. if($this->objOrder->ShippingCounty){
  103. $objPdf->Cell(20, 5, $this->objOrder->ShippingCounty, 0,0);
  104. $objPdf->Cell(75);
  105. $objPdf->Cell(0, 5, $this->objOrder->BillingCounty, 0,1);
  106. }
  107. if(ZoneType::NoZone != $this->objOrder->ShippingZoneId ){
  108. $objPdf->Cell(20, 5, ZoneType::ToString($this->objOrder->ShippingZoneId) . ' - ' . $this->objOrder->ShippingPostalCode, 0,0);
  109. $objPdf->Cell(75);
  110. $objPdf->Cell(0, 5, ZoneType::ToString($this->objOrder->BillingZoneId) . ' - ' . $this->objOrder->BillingPostalCode, 0,1);
  111. }
  112. $objPdf->Cell(20, 5, CountryType::ToString($this->objOrder->ShippingCountryId), 0,0);
  113. $objPdf->Cell(75);
  114. $objPdf->Cell(0, 5, CountryType::ToString($this->objOrder->BillingCountryId), 0,1);
  115. $objPdf->Ln(5);
  116. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  117. $objPdf->SetFont('Times','B',14);
  118. $objPdf->Cell(130, 10, 'Product Name' , 0,0);
  119. $objPdf->Cell(15, 10, 'Qty' , 0,0);
  120. $objPdf->Cell(15, 10, 'Price' , 0,0);
  121. $objPdf->Cell(15, 10, 'Total' , 0,1);
  122. $objPdf->SetFont('Times','',12);
  123. foreach($this->objOrder->GetOrderItemArray() as $objOrderItem ){
  124. $objPdf->Cell(130, 5, $objOrderItem->Product->Model, 0,0);
  125. $objPdf->Cell(15, 5, $objOrderItem->Quantity, 0,0);
  126. $objPdf->Cell(18, 5, money_format('%n', $objOrderItem->Product->RetailPrice), 0,0);
  127. $objPdf->Cell(18, 5, money_format('%n', ($objOrderItem->Quantity * $objOrderItem->Product->RetailPrice)) , 0,1);
  128. }
  129. $objPdf->Ln(3);
  130. $objPdf->Line(5,$objPdf->GetY(),200,$objPdf->GetY());
  131. $objPdf->Ln(3);
  132. $objPdf->Cell(150, 5, 'Sub-Total: ' , 0, 0, 'R');
  133. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ProductTotalCharged) , 0, 1, 'R');
  134. $objPdf->Cell(150, 5, 'Shipping and Handling: ' , 0, 0, 'R');
  135. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ShippingCharged + $this->objOrder->HandlingCharged) , 'B', 1, 'R' );
  136. $objPdf->Ln(3);
  137. $objPdf->Cell(150, 5, 'Grand Total: ' , 0, 0, 'R');
  138. $objPdf->Cell(27, 5, money_format('%n', $this->objOrder->ProductTotalCharged + $this->objOrder->ShippingCharged + $this->objOrder->HandlingCharged) , 0, 1, 'R');
  139. $objPdf->Ln(10);
  140. $objPdf->SetFont('Arial','I',16);
  141. $objPdf->Cell(0, 5, 'Thanks for using ' . STORE_NAME . '!', 0, 1, 'C');
  142. $objPdf->Output($strFileName);
  143. $this->printFile($strFileName);
  144. }
  145. public function PrintShippingLabels(){
  146. $strShipLabelFilename = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_shipping_label_' . $this->objOrder->Id;
  147. $strCustomsFormFilename = $this->strTempDirectory . '/' . $this->strFileNamePrefix . '_customs_form_' . $this->objOrder->Id;
  148. //use old images if possible - this is for reprints on errors ..
  149. //Note that it is assumed that if the shipping label images are there then the customs images
  150. //are also there - if this is not true there will be problems ..
  151. if(file_exists($strShipLabelFilename))
  152. return $this->reprintLabels( $strShipLabelFilename, $strCustomsFormFilename);
  153. $objImage = $this->objOrder->CreateShippingLabel();
  154. //local pickup does not need labels ..
  155. if(!$objImage)
  156. return;
  157. if(is_string($objImage)){
  158. $fp = fopen( $strShipLabelFilename, 'w+' );
  159. fwrite( $fp, $objImage );
  160. fclose($fp);
  161. $this->printFile($strShipLabelFilename);
  162. if($this->objOrder->ExtraDocumentImages){
  163. foreach($this->objOrder->ExtraDocumentImages as $objDocumentImage){
  164. $strFilename = $strShipLabelFilename . '_' . $objDocumentImage->Type . '-' . $objDocumentImage->Copies;
  165. $fp = fopen( $strFilename, 'w+' );
  166. fwrite( $fp, $objDocumentImage->Image );
  167. fclose($fp);
  168. $this->printFile($strFilename, $objDocumentImage->Copies);
  169. }
  170. }
  171. if($this->objOrder->CustomsFormImages){
  172. foreach($this->objOrder->CustomsFormImages as $intIdx => $strDocumentImage){
  173. $fp = fopen( $strCustomsFormFilename . '-' . $intIdx, 'w+' );
  174. fwrite( $fp, $strDocumentImage );
  175. fclose($fp);
  176. $this->printFile( $strCustomsFormFilename . '-' . $intIdx);
  177. }
  178. }
  179. }else{
  180. imagepng($objImage, $strShipLabelFilename);
  181. $this->printFile($strShipLabelFilename);
  182. if($this->objOrder->ExtraDocumentImages){
  183. foreach($this->objOrder->ExtraDocumentImages as $objDocumentImage){
  184. $strFilename = $strShipLabelFilename . '_' . $objDocumentImage->Type . '-' . $objDocumentImage->Copies;
  185. imagepng($objDocumentImage->Image, $strFileName );
  186. $this->printFile($strFilename, $objDocumentImage->Copies);
  187. }
  188. }
  189. if($this->objOrder->CustomsFormImages){
  190. foreach($this->objOrder->CustomsFormImages as $intIdx => $strDocumentImage){
  191. imagepng($strDocumentImage,$strCustomsFormFilename . '-' . $intIdx );
  192. $this->printFile( $strCustomsFormFilename . '-' . $intIdx);
  193. }
  194. }
  195. }
  196. }
  197. public function reprintLabels($strLabelFileName, $strCustomsFormFileName){
  198. $aryDirList = scandir($this->strTempDirectory);
  199. foreach($aryDirList as $strFileName){
  200. if('.' == $strFileName || '..' == $strFileName )
  201. continue;
  202. if(false !== strpos( $strLabelFileName, $strFileName )
  203. || false !== strpos( $strCustomsFormFileName, $strFileName ))
  204. $this->printFile($this->strTempDirectory . '/' . $strFileName);
  205. }
  206. }
  207. public function printFile($strFileName, $intQuantity=1){
  208. $strCommand = sprintf("lpr -P%s %s '-#%s' %s",
  209. $this->strPrinterName,
  210. $this->strPrinterOptions,
  211. $intQuantity,
  212. $strFileName);
  213. exec($strCommand);
  214. }
  215. public function __get($strName){
  216. switch ($strName){
  217. case 'TempDirectory':
  218. return $this->strTempDirectory ;
  219. case 'FileNamePrefix':
  220. return $this->strFileNamePrefix ;
  221. case 'PrinterName':
  222. return $this->strPrinterName ;
  223. case 'PrinterOptions':
  224. return $this->strPrinterOptions ;
  225. case 'Order':
  226. return $this->objOrder ;
  227. case 'Errors':
  228. return $this->objErrors ;
  229. default:
  230. throw new QCallerException('OrderPrinter::__get() Unknown property: ' . $strName);
  231. }
  232. }
  233. public function __set($strName, $mixValue){
  234. switch ($strName){
  235. case 'Order':
  236. try {
  237. return ($this->objOrder = QType::Cast($mixValue, 'Order' ));
  238. } catch (QInvalidCastException $objExc) {
  239. $objExc->IncrementOffset();
  240. throw $objExc;
  241. }
  242. case 'PrinterOptions':
  243. try {
  244. return ($this->strPrinterOptions = QType::Cast($mixValue, QType::String ));
  245. } catch (QInvalidCastException $objExc) {
  246. $objExc->IncrementOffset();
  247. throw $objExc;
  248. }
  249. case 'PrinterName':
  250. try {
  251. return ($this->strPrinterName = QType::Cast($mixValue, QType::String ));
  252. } catch (QInvalidCastException $objExc) {
  253. $objExc->IncrementOffset();
  254. throw $objExc;
  255. }
  256. case 'FileNamePrefix':
  257. try {
  258. return ($this->strFileNamePrefix = QType::Cast($mixValue, QType::String ));
  259. } catch (QInvalidCastException $objExc) {
  260. $objExc->IncrementOffset();
  261. throw $objExc;
  262. }
  263. case 'TempDirectory':
  264. try {
  265. return ($this->strTempDirectory = QType::Cast($mixValue, QType::String ));
  266. } catch (QInvalidCastException $objExc) {
  267. $objExc->IncrementOffset();
  268. throw $objExc;
  269. }
  270. case 'Errors':
  271. try {
  272. return ($this->strErrors = QType::Cast($mixValue, QType::String ));
  273. } catch (QInvalidCastException $objExc) {
  274. $objExc->IncrementOffset();
  275. throw $objExc;
  276. }
  277. default:
  278. throw new QCallerException('OrderPrinter::__set() Unknown property: ' . $strName);
  279. }
  280. }
  281. }//end class
  282. }//end define
  283. ?>