1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: class Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_PageBuilder
35: {
36: 37: 38:
39: const X_INDENT = 60;
40:
41: 42: 43:
44: const Y_INDENT = 15;
45:
46: 47: 48: 49: 50:
51: protected $_page;
52:
53: 54: 55:
56: public function __construct()
57: {
58: $this->_fontNormal = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA);
59: $this->_fontBold = Zend_Pdf_Font::fontWithName(Zend_Pdf_Font::FONT_HELVETICA_BOLD);
60: }
61:
62: 63: 64: 65: 66:
67: public function getPage()
68: {
69: return $this->_page;
70: }
71:
72: 73: 74: 75: 76: 77:
78: public function setPage(Zend_Pdf_Page $page)
79: {
80: $this->_page = $page;
81: return $this;
82: }
83:
84: 85: 86: 87: 88: 89:
90: protected function _x($pt)
91: {
92: return $pt + self::X_INDENT;
93: }
94:
95: 96: 97: 98: 99: 100:
101: protected function _y($pt)
102: {
103: return 595 - self::Y_INDENT - $pt;
104: }
105:
106: 107: 108: 109: 110: 111:
112: public function addBorder()
113: {
114: $x = $this->_x(0);
115: $y = $this->_y(0);
116:
117: $image = new Zend_Pdf_Resource_Image_Jpeg(Mage::getBaseDir('media') . DS . 'dhl' . DS . 'logo.jpg');
118: $this->_page->drawImage($image, $x + 191, $this->_y(27), $x + 287, $this->_y(1));
119:
120:
121: $this->_page->drawLine($x, $y, $x, $this->_y(568));
122: $this->_page->drawLine($x + 287.5, $y, $x + 287.5, $this->_y(568));
123: $this->_page->drawLine($x + 139.5, $y, $x + 139.5, $this->_y(28));
124: $this->_page->drawLine($x + 190.5, $y, $x + 190.5, $this->_y(28));
125:
126:
127: $this->_page->drawLine($x, $y, $x + 288, $y);
128: $this->_page->drawLine($x, $this->_y(28), $x + 288, $this->_y(28));
129: $this->_page->drawLine($x, $this->_y(80.5), $x + 288, $this->_y(80.5));
130: $this->_page->drawLine($x, $this->_y(164), $x + 288, $this->_y(164));
131: $this->_page->drawLine($x, $this->_y(194), $x + 288, $this->_y(194));
132: $this->_page->drawLine($x, $this->_y(217.5), $x + 288, $this->_y(217.5));
133: $this->_page->drawLine($x, $this->_y(245.5), $x + 288, $this->_y(245.5));
134: $this->_page->drawLine($x, $this->_y(568.5), $x + 288, $this->_y(568.5));
135:
136: $this->_page->setLineWidth(0.3);
137: $x = $this->_x(3);
138: $y = $this->_y(83);
139: $this->_page->drawLine($x, $y, $x + 10, $y);
140: $this->_page->drawLine($x, $y, $x, $y - 10);
141:
142: $x = $this->_x(3);
143: $y = $this->_y(161);
144: $this->_page->drawLine($x, $y, $x + 10, $y);
145: $this->_page->drawLine($x, $y, $x, $y + 10);
146:
147: $x = $this->_x(285);
148: $y = $this->_y(83);
149: $this->_page->drawLine($x, $y, $x - 10, $y);
150: $this->_page->drawLine($x, $y, $x, $y - 10);
151:
152: $x = $this->_x(285);
153: $y = $this->_y(161);
154: $this->_page->drawLine($x, $y, $x - 10, $y);
155: $this->_page->drawLine($x, $y, $x, $y + 10);
156:
157: return $this;
158: }
159:
160: 161: 162: 163: 164: 165: 166: 167:
168: public function addProductName($name)
169: {
170: $this->_page->saveGS();
171: $this->_page->setFont($this->_fontBold, 9);
172: if (!strlen($name)) {
173: throw new InvalidArgumentException(Mage::helper('usa')->__('Product name is missing'));
174: }
175: $this->_page->drawText($name, $this->_x(8), $this->_y(12));
176: $this->_page->restoreGS();
177: return $this;
178: }
179:
180: 181: 182: 183: 184: 185: 186: 187:
188: public function addProductContentCode($code)
189: {
190: $this->_page->saveGS();
191: $codes = array(
192: 'TDK' => 0, 'TDE' => 1, 'TDL' => 0, 'TDM' => 1, 'TDT' => 0,
193: 'TDY' => 1, 'XPD' => 0, 'DOX' => 0, 'WPX' => 1, 'ECX' => 0,
194: 'DOM' => 0
195: );
196: if (!key_exists($code, $codes)) {
197: throw new InvalidArgumentException(Mage::helper('usa')->__('Product content code is invalid'));
198: }
199: $font = null;
200: if ($codes[$code]) {
201: $this->_page->drawRectangle(
202: $this->_x(140),
203: $this->_y(0),
204: $this->_x(190),
205: $this->_y(28),
206: Zend_Pdf_Page::SHAPE_DRAW_FILL
207: );
208: $this->_page->setFillColor(new Zend_Pdf_Color_Html("#ffffff"));
209: $font = $this->_fontBold;
210: } else {
211: $font = $this->_fontNormal;
212: }
213: $this->_page->setFont($font, 17);
214: $this->_page->drawText($code, $this->_x(146), $this->_y(21));
215: $this->_page->restoreGS();
216: return $this;
217: }
218:
219: 220: 221: 222: 223: 224: 225:
226: public function addUnitId($id)
227: {
228: $this->_page->saveGS();
229: $this->_page->setFont($this->_fontNormal, 6);
230:
231: $this->_page->drawText('Unit ID', $this->_x(8), $this->_y(20));
232: $this->_page->restoreGS();
233: return $this;
234: }
235:
236: 237: 238: 239: 240: 241: 242:
243: public function addReferenceData($data)
244: {
245: $this->_page->saveGS();
246: $this->_page->setFont($this->_fontNormal, 6);
247: $this->_page->drawText('GREF', $this->_x(80), $this->_y(20));
248:
249: $this->_page->restoreGS();
250: return $this;
251: }
252:
253: 254: 255: 256: 257: 258: 259: 260:
261: public function addSenderInfo(SimpleXMLElement $sender)
262: {
263: $this->_page->saveGS();
264: $this->_page->setFont($this->_fontNormal, 6);
265: $this->_page->drawText('From:', $this->_x(8), $this->_y(36));
266: $contactName = implode(' ', array_filter(array((string)$sender->CompanyName,
267: (string)$sender->Contact->PersonName))
268: );
269: if (!$contactName) {
270: throw new InvalidArgumentException(Mage::helper('usa')->__('Sender contact name is missing'));
271: }
272: $this->_page->drawText($contactName, $this->_x(25), $this->_y(36));
273:
274: $phoneNumber = implode(' ', array_filter(array((string)$sender->Contact->PhoneNumber,
275: (string)$sender->Contact->PhoneExtension))
276: );
277: $phoneNumber = $phoneNumber ? "Phone: " . $phoneNumber : null;
278: $pageY = $this->_drawSenderAddress($sender->AddressLine, $phoneNumber);
279:
280: $divisionCode = (string)(strlen($sender->DivisionCode) ? $sender->DivisionCode . ' ' : null);
281: $cityInfo = implode(' ', array_filter(array($sender->City, $divisionCode, $sender->PostalCode)));
282: if (!strlen($cityInfo)) {
283: throw new InvalidArgumentException(Mage::helper('usa')->__('Sender city info is missing'));
284: }
285: $this->_page->drawText($cityInfo, $this->_x(25), $pageY);
286:
287: $this->_page->setFont($this->_fontBold, 6);
288: $countryInfo = (string)(($sender->CountryName) ? $sender->CountryName : $sender->CountryCode);
289: if (!strlen($countryInfo)) {
290: throw new InvalidArgumentException(Mage::helper('usa')->__('Sender country info is missing'));
291: }
292: $this->_page->drawText($countryInfo, $this->_x(25), $pageY - $this->_page->getFontSize());
293:
294: $this->_page->restoreGS();
295: return $this;
296: }
297:
298: 299: 300: 301: 302: 303: 304: 305:
306: protected function _drawSenderAddress(SimpleXMLElement $addressLines, $phoneNumber)
307: {
308: $lines = array();
309: foreach ($addressLines as $line) {
310: $lines [] = $line;
311: }
312:
313: $pageY = 0;
314: if (strlen($lines[0]) > 28) {
315: $firstLine = array_shift($lines);
316: $pageY = $this->_page->drawLines(array($firstLine), $this->_x(25), $this->_y(42), 28);
317: $this->_page->drawText($phoneNumber, $this->_x(103), $this->_y(42));
318: } else {
319: $pageY = $this->_y(42);
320: $lineLength = $this->_page->getTextWidth(
321: $lines[0] . ' ', $this->_page->getFont(), $this->_page->getFontSize()
322: );
323: $this->_page->drawText($phoneNumber, $this->_x(25 + $lineLength), $this->_y(42));
324: }
325:
326: return $this->_page->drawLines($lines, $this->_x(25), $pageY, 49);
327: }
328:
329: 330: 331: 332: 333: 334: 335: 336:
337: public function addOriginInfo($serviceAreaCode)
338: {
339: if (strlen(!$serviceAreaCode)) {
340: throw new InvalidArgumentException(Mage::helper('usa')->__('Origin serviceAreaCode is missing'));
341: }
342: $this->_page->saveGS();
343: $this->_page->setFont($this->_fontNormal, 6);
344: $this->_page->drawText("Origin:", $this->_x(260), $this->_y(36));
345: $this->_page->setFont($this->_fontBold, 9);
346: $this->_page->drawText($serviceAreaCode, $this->_x(260), $this->_y(45));
347:
348: $this->_page->restoreGS();
349: return $this;
350: }
351:
352: 353: 354: 355: 356: 357: 358:
359: public function addReceiveInfo(SimpleXMLElement $consignee)
360: {
361: $this->_page->saveGS();
362:
363: $this->_page->setFont($this->_fontNormal, 9);
364: $this->_page->drawText("To:", $this->_x(5), $this->_y(92));
365: $this->_page->drawText($consignee->CompanyName, $this->_x(20), $this->_y(90));
366: $y = $this->_page->drawLines($consignee->AddressLine, $this->_x(19), $this->_y(100), 50);
367:
368: $this->_page->setFont($this->_fontBold, 11);
369: $cityInfo = implode(' ', array_filter(array($consignee->PostalCode, $consignee->City,
370: $consignee->DivisionCode))
371: );
372: $y = min($y - 3, 460);
373: $this->_page->drawLines(array($cityInfo, $consignee->CountryName), $this->_x(20), $y, 44);
374:
375: $this->_page->setFont($this->_fontNormal, 6);
376: $this->_page->drawText('Contact:', $this->_x(260), $this->_y(90));
377:
378: $y = $this->_page->drawLines(array($consignee->Contact->PersonName), $this->_x(283), $this->_y(98), 25,
379: Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page::ALIGN_RIGHT
380: );
381: $phoneNumber = implode(' ', array_filter(array($consignee->Contact->PhoneNumber,
382: $consignee->Contact->PhoneExtension))
383: );
384: $this->_page->drawText($phoneNumber, $this->_x(283), $y, null,
385: Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page::ALIGN_RIGHT
386: );
387:
388: $this->_page->restoreGS();
389: return $this;
390: }
391:
392: 393: 394: 395: 396: 397: 398: 399: 400: 401:
402: public function addDestinationFacilityCode($countryCode, $serviceAreaCode, $facilityCode)
403: {
404: $this->_page->saveGS();
405: $this->_page->setFont($this->_fontNormal, 20);
406: $code = implode('-', array_filter(array($countryCode, $serviceAreaCode, $facilityCode)));
407:
408: if (!strlen($code)) {
409: throw new InvalidArgumentException(Mage::helper('usa')->__('Destination facility code is empty'));
410: }
411: $this->_page->drawText($code, $this->_x(144), $this->_y(186), null,
412: Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page::ALIGN_CENTER
413: );
414:
415: $this->_page->restoreGS();
416: return $this;
417: }
418:
419: 420: 421: 422: 423: 424:
425: public function addServiceFeaturesCodes()
426: {
427: $this->_page->saveGS();
428: $this->_page->drawRectangle($this->_x(0), $this->_y(195), $this->_x(218), $this->_y(217),
429: Zend_Pdf_Page::SHAPE_DRAW_FILL
430: );
431: $this->_page->restoreGS();
432: return $this;
433: }
434:
435: 436: 437: 438: 439: 440:
441: public function addDeliveryDateCode()
442: {
443: $this->_page->saveGS();
444:
445: $this->_page->setFont($this->_fontNormal, 6);
446: $this->_page->drawText('Day:', $this->_x(220), $this->_y(201));
447: $this->_page->drawText('Time:', $this->_x(250), $this->_y(201));
448:
449: $this->_page->setFont($this->_fontNormal, 20);
450:
451: $this->_page->restoreGS();
452: return $this;
453: }
454:
455: 456: 457: 458: 459: 460: 461:
462: public function addShipmentInformation($data)
463: {
464: $this->_page->saveGS();
465: $this->_page->setFont($this->_fontNormal, 6);
466:
467: $refCode = $data->getOrder()->getIncrementId();
468: if (!$refCode) {
469: throw new InvalidArgumentException(Mage::helper('usa')->__('Reference code is missing'));
470: }
471: $this->_page->drawText('Ref Code: ' . Mage::helper('usa')->__('Order #%s', $refCode), $this->_x(8),
472: $this->_y(224)
473: );
474: $this->_page->restoreGS();
475: return $this;
476: }
477:
478: 479: 480: 481: 482: 483:
484: public function addDateInfo($date)
485: {
486: $this->_page->saveGS();
487:
488: $this->_page->setFont($this->_fontNormal, 6);
489: $this->_page->drawText('Date:', $this->_x(160), $this->_y(224));
490: $this->_page->drawText($date, $this->_x(150), $this->_y(231));
491:
492: $this->_page->restoreGS();
493: return $this;
494: }
495:
496: 497: 498: 499: 500: 501: 502: 503: 504:
505: public function addWeightInfo($weight, $unit)
506: {
507: $this->_page->saveGS();
508:
509: $units = array("K" => 'kg', "L" => 'lb');
510: if (!isset($units[$unit])) {
511: throw new InvalidArgumentException(Mage::helper('usa')->__('Weight unit is invalid'));
512: }
513: $unit = $units[$unit];
514:
515: $this->_page->setFont($this->_fontNormal, 6);
516: $this->_page->drawText('Shpt Weight:', $this->_x(196), $this->_y(224));
517: $this->_page->setFont($this->_fontBold, 11);
518: $this->_page->drawText($weight . ' ' . $unit, $this->_x(195), $this->_y(234));
519:
520: $this->_page->restoreGS();
521: return $this;
522: }
523:
524: 525: 526: 527: 528: 529: 530:
531: public function addContentInfo($package)
532: {
533: $this->_page->saveGS();
534: $this->_page->setFont($this->_fontNormal, 6);
535: if (empty($package)) {
536: throw new InvalidArgumentException(Mage::helper('usa')->__('Package content is missing'));
537: }
538:
539: $x = 225;
540: $y = 300;
541: $this->_page->drawText('Content: ', $this->_x($x), $this->_y($y));
542: $i = 0;
543: foreach ($package['items'] as $item) {
544: $i++;
545: $this->_page->drawText(substr($item['name'], 0, 20), $this->_x($x), $this->_y($y += 6));
546: if ($i == 12) {
547: break;
548: }
549: }
550: $this->_page->restoreGS();
551: return $this;
552: }
553:
554: 555: 556: 557: 558: 559: 560: 561: 562:
563: public function addWaybillBarcode($number, $barCode)
564: {
565: $this->_page->saveGS();
566:
567: if(!strlen($number) || !strlen($barCode)) {
568: throw new InvalidArgumentException(Mage::helper('usa')->__('Waybill barcode information is missing'));
569: }
570: $image = new Zend_Pdf_Resource_Image_Png("data://image/png;base64," . $barCode);
571: $this->_page->drawImage($image, $this->_x(0), $this->_y(296), $this->_x(232), $this->_y(375));
572:
573: $this->_page->setFont($this->_fontNormal, 9);
574: $number = substr($number, 0, 2) . ' ' . substr($number, 2, 4) . ' ' . substr($number, 6, 4);
575: $this->_page->drawText("WAYBILL " . $number, $this->_x(13.5), $this->_y(382));
576:
577: $this->_page->restoreGS();
578: return $this;
579: }
580:
581: 582: 583: 584: 585: 586: 587: 588: 589: 590:
591: public function addRoutingBarcode($routingCode, $id, $barCode)
592: {
593: $this->_page->saveGS();
594:
595: if(!$barCode) {
596: throw new InvalidArgumentException(Mage::helper('usa')->__('Routing barcode is missing'));
597: }
598:
599: $image = new Zend_Pdf_Resource_Image_Png("data://image/png;base64," . $barCode);
600: $this->_page->drawImage($image, $this->_x(0), $this->_y(386), $this->_x(232), $this->_y(465));
601:
602: $this->_page->setFont($this->_fontNormal, 9);
603: $routingText = '(' . $id . ')' . $routingCode;
604: $this->_page->drawText($routingText, $this->_x(12), $this->_y(472));
605:
606: $this->_page->restoreGS();
607: return $this;
608: }
609:
610: 611: 612: 613: 614: 615: 616: 617: 618: 619:
620: public function addPieceIdBarcode($dataIdentifier, $licensePlate, $barCode)
621: {
622: $this->_page->saveGS();
623:
624: if (!strlen($barCode)) {
625: throw new InvalidArgumentException(Mage::helper('usa')->__('Piece Id barcode is missing'));
626: }
627:
628: $image = new Zend_Pdf_Resource_Image_Png('data://image/png;base64,' . $barCode);
629: $this->_page->drawImage($image, $this->_x(29), $this->_y(476), $this->_x(261), $this->_y(555));
630:
631: $this->_page->setFont($this->_fontNormal, 9);
632: $routingText = '(' . $dataIdentifier . ')' . $licensePlate;
633: $this->_page->drawText($routingText, $this->_x(144), $this->_y(563), '',
634: Mage_Usa_Model_Shipping_Carrier_Dhl_Label_Pdf_Page::ALIGN_CENTER
635: );
636:
637: $this->_page->restoreGS();
638: return $this;
639: }
640:
641: 642: 643: 644: 645: 646: 647: 648: 649:
650: public function addPieceNumber($pieceNumber, $piecesTotal)
651: {
652: $this->_page->saveGS();
653:
654: if (!$pieceNumber || !$piecesTotal) {
655: throw new InvalidArgumentException(Mage::helper('usa')->__('Piece number information is missing'));
656: }
657:
658: $this->_page->setFont($this->_fontNormal, 6);
659: $this->_page->drawText('Piece:', $this->_x(256), $this->_y(224));
660: $this->_page->setFont($this->_fontBold, 11);
661: $this->_page->drawText($pieceNumber . '/' . $piecesTotal, $this->_x(256), $this->_y(234));
662:
663: $this->_page->restoreGS();
664: return $this;
665: }
666: }
667: