Guest User

Untitled

a guest
Feb 21st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.34 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
  3. <preference for="MagentoSalesModelOrderPdfInvoice" type="VendornameModulenameModelOrderPdfInvoice"/>
  4. </config>
  5.  
  6. <?php
  7.  
  8. namespace ModulenameModulenameModelOrderPdf;
  9.  
  10. use MagentoSalesModelOrderPdfConfig;
  11.  
  12. class Invoice extends MagentoSalesModelOrderPdfInvoice
  13. {
  14. /**
  15. * @var MagentoStoreModelStoreManagerInterface
  16. */
  17. protected $_storeManager;
  18.  
  19. /**
  20. * @var MagentoFrameworkLocaleResolverInterface
  21. */
  22. protected $_localeResolver;
  23.  
  24. /**
  25. * @param MagentoPaymentHelperData $paymentData
  26. * @param MagentoFrameworkStdlibStringUtils $string
  27. * @param MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig
  28. * @param MagentoFrameworkFilesystem $filesystem
  29. * @param Config $pdfConfig
  30. * @param MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory
  31. * @param MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory
  32. * @param MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate
  33. * @param MagentoFrameworkTranslateInlineStateInterface $inlineTranslation
  34. * @param MagentoSalesModelOrderAddressRenderer $addressRenderer
  35. * @param MagentoStoreModelStoreManagerInterface $storeManager
  36. * @param MagentoFrameworkLocaleResolverInterface $localeResolver
  37. * @param array $data
  38. *
  39. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  40. */
  41. public function __construct(
  42. MagentoPaymentHelperData $paymentData,
  43. MagentoFrameworkStdlibStringUtils $string,
  44. MagentoFrameworkAppConfigScopeConfigInterface $scopeConfig,
  45. MagentoFrameworkFilesystem $filesystem,
  46. Config $pdfConfig,
  47. MagentoSalesModelOrderPdfTotalFactory $pdfTotalFactory,
  48. MagentoSalesModelOrderPdfItemsFactory $pdfItemsFactory,
  49. MagentoFrameworkStdlibDateTimeTimezoneInterface $localeDate,
  50. MagentoFrameworkTranslateInlineStateInterface $inlineTranslation,
  51. MagentoSalesModelOrderAddressRenderer $addressRenderer,
  52. MagentoStoreModelStoreManagerInterface $storeManager,
  53. MagentoFrameworkLocaleResolverInterface $localeResolver,
  54. array $data = []
  55. )
  56. {
  57. parent::__construct(
  58. $paymentData,
  59. $string,
  60. $scopeConfig,
  61. $filesystem,
  62. $pdfConfig,
  63. $pdfTotalFactory,
  64. $pdfItemsFactory,
  65. $localeDate,
  66. $inlineTranslation,
  67. $addressRenderer,
  68. $storeManager,
  69. $localeResolver,
  70. $data
  71. );
  72. }
  73.  
  74. /**
  75. * Draw header for item table
  76. *
  77. * @param Zend_Pdf_Page $page
  78. * @return void
  79. */
  80. protected function _drawHeader(Zend_Pdf_Page $page)
  81. {
  82. /* Add table head */
  83. $this->_setFontRegular($page, 10);
  84. $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
  85. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  86. $page->setLineWidth(0.5);
  87. $page->drawRectangle(25, $this->y, 570, $this->y - 15);
  88. $this->y -= 10;
  89. $page->setFillColor(new Zend_Pdf_Color_RGB(0, 0, 0));
  90.  
  91. //columns headers
  92. $lines[0][] = ['text' => __('Products'), 'feed' => 35];
  93.  
  94. $lines[0][] = ['text' => __('SKU'), 'feed' => 290, 'align' => 'right'];
  95.  
  96. $lines[0][] = ['text' => __('Qty'), 'feed' => 435, 'align' => 'right'];
  97.  
  98. $lines[0][] = ['text' => __('Price'), 'feed' => 360, 'align' => 'right'];
  99.  
  100. $lines[0][] = ['text' => __('Tax'), 'feed' => 495, 'align' => 'right'];
  101.  
  102. $lines[0][] = ['text' => __('Subtotal'), 'feed' => 565, 'align' => 'right'];
  103.  
  104. $lineBlock = ['lines' => $lines, 'height' => 5];
  105.  
  106. $this->drawLineBlocks($page, [$lineBlock], ['table_header' => true]);
  107. $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
  108. $this->y -= 20;
  109. }
  110.  
  111. /**
  112. * Return PDF document
  113. *
  114. * @param array|Collection $invoices
  115. * @return Zend_Pdf
  116. */
  117. public function getPdf($invoices = [])
  118. {
  119. $this->_beforeGetPdf();
  120. $this->_initRenderer('invoice');
  121.  
  122. $pdf = new Zend_Pdf();
  123. $this->_setPdf($pdf);
  124. $style = new Zend_Pdf_Style();
  125. $this->_setFontBold($style, 10);
  126.  
  127. foreach ($invoices as $invoice) {
  128. if ($invoice->getStoreId()) {
  129. $this->_localeResolver->emulate($invoice->getStoreId());
  130. $this->_storeManager->setCurrentStore($invoice->getStoreId());
  131. }
  132. $page = $this->newPage();
  133. $order = $invoice->getOrder();
  134. /* Add image */
  135. $this->insertLogo($page, $invoice->getStore());
  136. /* Add address */
  137. $this->insertAddress($page, $invoice->getStore());
  138. /* Add head */
  139. $this->insertOrder(
  140. $page,
  141. $order,
  142. $this->_scopeConfig->isSetFlag(
  143. self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID,
  144. MagentoStoreModelScopeInterface::SCOPE_STORE,
  145. $order->getStoreId()
  146. )
  147. );
  148. /* Add document text and number */
  149. $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
  150. /* Add table */
  151. $this->_drawHeader($page);
  152. /* Add body */
  153. foreach ($invoice->getAllItems() as $item) {
  154. if ($item->getOrderItem()->getParentItem()) {
  155. continue;
  156. }
  157. /* Draw item */
  158. $this->_drawItem($item, $page, $order);
  159. $page = end($pdf->pages);
  160. }
  161. /* Add totals */
  162. $this->insertTotals($page, $invoice);
  163. if ($invoice->getStoreId()) {
  164. $this->_localeResolver->revert();
  165. }
  166. }
  167. $this->_drawFooter($page);
  168. $this->_afterGetPdf();
  169. return $pdf;
  170. }
  171.  
  172.  
  173.  
  174. /**
  175. * Set font as regular
  176. *
  177. * @param Zend_Pdf_Page $object
  178. * @param int $size
  179. * @return Zend_Pdf_Resource_Font
  180. */
  181. protected function _setFontRegular($object, $size = 7)
  182. {
  183. $font = Zend_Pdf_Font::fontWithPath(
  184. $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Regular.ttf')
  185. );
  186. $object->setFont($font, $size);
  187. return $font;
  188. }
  189.  
  190. /**
  191. * Set font as bold
  192. *
  193. * @param Zend_Pdf_Page $object
  194. * @param int $size
  195. * @return Zend_Pdf_Resource_Font
  196. */
  197. protected function _setFontBold($object, $size = 7)
  198. {
  199. $font = Zend_Pdf_Font::fontWithPath(
  200. $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Bold.ttf')
  201. );
  202. $object->setFont($font, $size);
  203. return $font;
  204. }
  205.  
  206. /**
  207. * Set font as italic
  208. *
  209. * @param Zend_Pdf_Page $object
  210. * @param int $size
  211. * @return Zend_Pdf_Resource_Font
  212. */
  213. protected function _setFontItalic($object, $size = 7)
  214. {
  215. $font = Zend_Pdf_Font::fontWithPath(
  216. $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Italic.ttf')
  217. );
  218. $object->setFont($font, $size);
  219. return $font;
  220. }
  221.  
  222.  
  223.  
  224. protected function insertLogo(&$page, $store = null)
  225. {
  226. $this->y = $this->y ? $this->y : 815;
  227. $image = $this->_scopeConfig->getValue(
  228. 'sales/identity/logo',
  229. MagentoStoreModelScopeInterface::SCOPE_STORE,
  230. $store
  231. );
  232. if ($image) {
  233. $imagePath = '/sales/store/logo/' . $image;
  234. if ($this->_mediaDirectory->isFile($imagePath)) {
  235. $image = Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
  236. $top = 830;
  237. //top border of the page
  238. $widthLimit = 270;
  239. //half of the page width
  240. $heightLimit = 270;
  241. //assuming the image is not a "skyscraper"
  242. $width = $image->getPixelWidth();
  243. $height = $image->getPixelHeight();
  244.  
  245. //preserving aspect ratio (proportions)
  246. $ratio = $width / $height;
  247. if ($ratio > 1 && $width > $widthLimit) {
  248. $width = $widthLimit;
  249. $height = $width / $ratio;
  250. } elseif ($ratio < 1 && $height > $heightLimit) {
  251. $height = $heightLimit;
  252. $width = $height * $ratio;
  253. } elseif ($ratio == 1 && $height > $heightLimit) {
  254. $height = $heightLimit;
  255. $width = $widthLimit;
  256. }
  257.  
  258. $y1 = $top - $height;
  259. $y2 = $top;
  260. $x1 = 158;
  261. $x2 = $x1 + $width;
  262.  
  263. //coordinates after transformation are rounded by Zend
  264. $page->drawImage($image, $x1, $y1, $x2, $y2);
  265.  
  266. $this->y = $y1 - 10;
  267. }
  268. }
  269. }
  270.  
  271.  
  272.  
  273. /**
  274. * Create new page and assign to PDF object
  275. *
  276. * @param array $settings
  277. * @return Zend_Pdf_Page
  278. */
  279. public function newPage(array $settings = [])
  280. {
  281. /* Add new table head */
  282. $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
  283. $this->_getPdf()->pages[] = $page;
  284. $this->y = 800;
  285. if (!empty($settings['table_header'])) {
  286. $this->_drawHeader($page);
  287. }
  288. return $page;
  289. }
  290.  
  291.  
  292.  
  293. protected function _drawFooter(Zend_Pdf_Page $page)
  294. {
  295. $this->y =40;
  296. $page->setFillColor(new Zend_Pdf_Color_RGB(1, 1, 1));
  297. $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
  298. $page->setLineWidth(0.5);
  299. $page->drawRectangle(60, $this->y, 510, $this->y -30);
  300.  
  301. $page->setFillColor(new Zend_Pdf_Color_RGB(0.1, 0.1, 0.1));
  302. $font = Zend_Pdf_Font::fontWithPath(
  303. $this->_rootDirectory->getAbsolutePath('lib/internal/open-sans/OpenSans-Regular.ttf')
  304. );
  305. $page->setFont($font, 8);
  306.  
  307. $this->y -=15;
  308.  
  309. $page->drawText("Mailing Address: ABC Road sector-1 Noida", 285, $this->y, 'UTF-8');
  310. $this->y -=10;
  311. $page->drawText("help@abc.com", 285, $this->y, 'UTF-8');
  312.  
  313.  
  314. $store = null;
  315. $this->y = $this->y ? $this->y : 815;
  316. $image = $this->_scopeConfig->getValue(
  317. 'sales/identity/logo',
  318. MagentoStoreModelScopeInterface::SCOPE_STORE,
  319. $store
  320. );
  321. if ($image) {
  322. $imagePath = '/sales/store/logo/' . $image;
  323. if ($this->_mediaDirectory->isFile($imagePath)) {
  324. $image = Zend_Pdf_Image::imageWithPath($this->_mediaDirectory->getAbsolutePath($imagePath));
  325. $top = 830;
  326. //top border of the page
  327. $widthLimit = 270;
  328. //half of the page width
  329. $heightLimit = 270;
  330. //assuming the image is not a "skyscraper"
  331. $width = $image->getPixelWidth();
  332. $height = $image->getPixelHeight();
  333.  
  334. //preserving aspect ratio (proportions)
  335. $ratio = $width / $height;
  336. if ($ratio > 1 && $width > $widthLimit) {
  337. $width = $widthLimit;
  338. $height = $width / $ratio;
  339. } elseif ($ratio < 1 && $height > $heightLimit) {
  340. $height = $heightLimit;
  341. $width = $height * $ratio;
  342. } elseif ($ratio == 1 && $height > $heightLimit) {
  343. $height = $heightLimit;
  344. $width = $widthLimit;
  345. }
  346.  
  347. $y1 = $top - $height;
  348. $y2 = $top;
  349. $x1 = 455;
  350. $x2 = $x1 + $width;
  351.  
  352. //coordinates after transformation are rounded by Zend
  353. $width = 260;
  354. $height = 40;
  355. $y = $height /2.5;
  356. $page->drawImage($image, 80, $y, 35+ $width / 1.5, $y + $height/2);
  357. }
  358. }
  359.  
  360. }
  361.  
  362.  
  363. }
Add Comment
Please, Sign In to add comment