Giter VIP home page Giter VIP logo

Comments (8)

evs-xsarus avatar evs-xsarus commented on August 16, 2024 4

I've created a patch based on mathijsm@5dde194 . This way, the core files are used.

/patches/print-bold-order-comment-on-invoice-and-shipment-pdf.patch

--- module-sales/Model/Order/Pdf/AbstractPdf.php
+++ module-sales/Model/Order/Pdf/AbstractPdf.php
@@ -1124,4 +1124,78 @@
         }
         return $top;
     }
+
+    // START add boldcommerce/magento2-ordercomments to PDF's
+    protected function insertComment(\Zend_Pdf_Page $page, $comment)
+    {
+        if (!$comment) {
+            return $page;
+        }
+
+        if ($this->y < 50) {
+            $page = $this->newPage();
+        }
+
+        $this->drawCommentHeader($page);
+        $comment = $this->formatComment($comment);
+        $page = $this->drawCommentBody($page, $comment);
+
+        return $page;
+    }
+
+    protected function drawCommentHeader(\Zend_Pdf_Page $page)
+    {
+        $this->_setFontRegular($page, 10);
+        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0.93, 0.92, 0.92));
+        $page->setLineWidth(0.5);
+        $page->drawRectangle(25, $this->y, 570, $this->y - 15);
+        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
+        $page->drawText(__('Order Comment'), 35, $this->y -10);
+        $this->y -= 30;
+        return $page;
+    }
+
+    protected function drawCommentBody(\Zend_Pdf_Page $page, $commentLines)
+    {
+        $this->prepareCommentBodyBlock($page, count($commentLines));
+
+        foreach($commentLines as $index => $line) {
+            $page->drawText($line, 35, $this->y);
+            $this->y -= 10;
+
+            if ($this->y <= 15) {
+                $page = $this->newPage();
+                $remainingLines = count($commentLines) - ($index + 1);
+                $this->drawCommentHeader($page);
+                $this->prepareCommentBodyBlock($page, $remainingLines);
+            }
+        }
+        return $page;
+    }
+
+    protected function prepareCommentBodyBlock(\Zend_Pdf_Page $page, $numLines)
+    {
+        $page->setFillColor(new \Zend_Pdf_Color_GrayScale(1));
+        $page->drawRectangle(
+            25,
+            $this->y + 15,
+            570,
+            max($this->y - (10 * $numLines) + 10, 15)
+        );
+
+        $page->setFillColor(new \Zend_Pdf_Color_Rgb(0, 0, 0));
+    }
+
+    protected function formatComment($text)
+    {
+        $result = [];
+        foreach (explode("\n", $text) as $str) {
+            foreach ($this->string->split($str, 130, true, true) as $part) {
+                $result[] = $part;
+            }
+            $result[] = '';
+        }
+        return $result;
+    }
+    // END add boldcommerce/magento2-ordercomments to PDF's
 }

--- module-sales/Model/Order/Pdf/Invoice.php
+++ module-sales/Model/Order/Pdf/Invoice.php
@@ -148,6 +148,14 @@
             );
             /* Add document text and number */
             $this->insertDocumentNumber($page, __('Invoice # ') . $invoice->getIncrementId());
+
+            // START add boldcommerce/magento2-ordercomments to PDF's
+            $page = $this->insertComment(
+                $page,
+                $order->getBoldOrderComment()
+            );
+            // END add boldcommerce/magento2-ordercomments to PDF's
+
             /* Add table */
             $this->_drawHeader($page);
             /* Add body */

--- module-sales/Model/Order/Pdf/Shipment.php
+++ module-sales/Model/Order/Pdf/Shipment.php
@@ -139,6 +139,14 @@
             );
             /* Add document text and number */
             $this->insertDocumentNumber($page, __('Packing Slip # ') . $shipment->getIncrementId());
+
+            // START add boldcommerce/magento2-ordercomments to PDF's
+            $page = $this->insertComment(
+                $page,
+                $order->getBoldOrderComment()
+            );
+            // END add boldcommerce/magento2-ordercomments to PDF's
+
             /* Add table */
             $this->_drawHeader($page);
             /* Add body */

To be used with https://github.com/cweagans/composer-patches

composer.json

    "extra": {
        "patches": {
            "magento/module-sales": {
                "boldordercomments": "./patches/print-bold-order-comment-on-invoice-and-shipment-pdf.patch"
            }
        }
    }

from magento2-ordercomments.

duffner avatar duffner commented on August 16, 2024 1

@boldsidney, here is the business case: Many of my existing customers use this module to place "Shipping Comments" for their order, while this works great, we use the "Print Invoices" feature from the mass action drop down to give to our shipping department to process orders. Having the notes on the printed documented would prevent our team from having to review every order in the Admin panel. While the "Print Invoices" is our use case, I think many would make use of this in the "Print Packing Slips" PDF as well. As this feature will likely be specific per Magento installation, offering this as an admin configuration would be ideal.

screen shot 2018-07-09 at 7 25 41 am

from magento2-ordercomments.

robocrax avatar robocrax commented on August 16, 2024 1

No way @evs-xsarus just solved this 2 year open case just when I stumbled upon it. Legend and perfect timing!

from magento2-ordercomments.

boldsidney avatar boldsidney commented on August 16, 2024

hi @duffner ,
If you still want this functionality, I can look into this when i have some free time somewhere either this month or the next.

I didnt find the print order PDF button in Magento, but I did find the print packing slip. Was it just the packing slip that needed this addition?

from magento2-ordercomments.

khaltis avatar khaltis commented on August 16, 2024

Was there any update on this? I'm also interested in seeing the order comments in the pdf invoice printout. Common use case is that invoices will be printed in batch, and will be processed using the printouts

from magento2-ordercomments.

twoatechguy avatar twoatechguy commented on August 16, 2024

I don't use this module anymore, but it's better practice to override these files in your own module. Editing core files is a bad idea.

Create a custom module and include in /etc/admindhtml/di.xml lines like <preference for="Magento\Sales\Model\Order\Pdf\Shipment" type="VENDOR\MODULE\Model\Order\Pdf\Shipment" />

And put your new php files in the custom module.

from magento2-ordercomments.

evs-xsarus avatar evs-xsarus commented on August 16, 2024

@twoatechguy well, I agree partially. Doing it with a patch, gives me a warning should the core file change. This is a trigger for me to check if the functionality must be changed.

A preference on this PDF class also means overriding a long function. Should the function change a bit in the core, I'll never know.

Basically, when I can use a before or after plugin, I'll use a local module, otherwise, I'll patch.

from magento2-ordercomments.

twoatechguy avatar twoatechguy commented on August 16, 2024

@evs-xsarus agreed, but you will be called out by 3rd party auditors who don't understand the reasoning.

from magento2-ordercomments.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.