0

I want to show total number of quantity in invoice PDF even though I have managed to get quantity name on PDF

here is code for this

config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Inchoo_Invoice>
        <version>0.1.0</version>
    </Inchoo_Invoice>
</modules>
<global>
    <models>
        <sales>
            <rewrite>
                <order_pdf_invoice>Inchoo_Invoice_Model_Order_Pdf_Invoice</order_pdf_invoice>
                <order_pdf_items_invoice_default>Inchoo_Invoice_Model_Order_Pdf_Items_Invoice_Default</order_pdf_items_invoice_default>
            </rewrite>
        </sales>
    </models>
    <pdf>
        <totals>
            <qty translate="title">
                <title>Quantity</title>
                <source_field>qty</source_field>
                <font_size>7</font_size>
                <display_zero>1</display_zero>
                <sort_order>50</sort_order>
            </qty>
        </totals>
    </pdf>
</global>

Invoice.php

<?php


class Inchoo_Invoice_Model_Order_Pdf_Invoice extends Mage_Sales_Model_Order_Pdf_Invoice
{
protected function insertImage($image, $x1, $y1, $x2, $y2, $width, $height, &$page)
{
    if (!is_null($image)) {
        try{
            $width = (int) $width;
            $height = (int) $height;

            //Get product image and resize it
            $imagePath = Mage::helper('catalog/image')->init($image, 'image')
                ->keepAspectRatio(true)
                ->keepFrame(false)
                ->resize($width, $height)
                ->__toString();

            //$imageLocation = substr($imagePath,strlen(Mage::getBaseUrl()));
            $imageLocation = substr($imagePath,strlen(Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB)));
            $image = Zend_Pdf_Image::imageWithPath($imageLocation);
            //Draw image to PDF
            $page->drawImage($image, $x1, $y1, $x2, $y2);
        }
        catch (Exception $e) {
            return false;
        }
    }
}

public function getPdf($invoices = array())
{
    $width = 1000;
    $height = 1000;
    $this->_beforeGetPdf();
    $this->_initRenderer('invoice');

    $pdf = new Zend_Pdf();
    $this->_setPdf($pdf);
    $style = new Zend_Pdf_Style();
    $this->_setFontBold($style, 10);

    foreach ($invoices as $invoice) {
        if ($invoice->getStoreId()) {
            Mage::app()->getLocale()->emulate($invoice->getStoreId());
        }
        $page = $pdf->newPage(Zend_Pdf_Page::SIZE_A4);
        $pdf->pages[] = $page;

        $order = $invoice->getOrder();

        /* Add image */
        $this->insertLogo($page, $invoice->getStore());

        /* Add address */
        $this->insertAddress($page, $invoice->getStore());

        /* Add head */
        $this->insertOrder($page, $order, Mage::getStoreConfigFlag(self::XML_PATH_SALES_PDF_INVOICE_PUT_ORDER_ID, $order->getStoreId()));

        $page->setFillColor(new Zend_Pdf_Color_GrayScale(1));
        $this->_setFontRegular($page);
        $page->drawText(Mage::helper('sales')->__('Invoice # ') . $invoice->getIncrementId(), 35, 780, 'UTF-8');

        /* Add table */
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
        $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);

        $page->drawRectangle(25, $this->y, 570, $this->y -15);
        $this->y -=10;

        /* Add table head */
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
        $page->drawText(Mage::helper('sales')->__('Products'), 35, $this->y, 'UTF-8');
        //Added for product image
        //$page->drawText(Mage::helper('sales')->__('Product Image'), 245, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Stock Location'), 245, $this->y, 'UTF-8');

        $page->drawText(Mage::helper('sales')->__('SKU'), 325, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Qty'), 430, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

        $this->y -=15;

        $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));

        /* Add body */
        foreach ($invoice->getAllItems() as $item){
                         if ($item->getOrderItem()->getParentItem()) {
                continue;
            }

            if ($this->y < 15) {
                $page = $this->newPage(array('table_header' => true));
            }
                            /* Draw item */
            $page = $this->_drawItem($item, $page, $order);

            /* Draw product image */
                            /*
            $productId = $item->getOrderItem()->getProductId();
            $image = Mage::getModel('catalog/product')->load($productId);
            $this->insertImage($image, 245, (int)($this->y + 15), 310, (int)($this->y+65), $width, $height, $page);
                            */
        }

        /* Add totals */
        $page = $this->insertTotals($page, $invoice);

        if ($invoice->getStoreId()) {
            Mage::app()->getLocale()->revert();
        }
    }
    $this->_afterGetPdf();

    return $pdf;
}

public function newPage(array $settings = array())
{
    /* Add new table head */
    $page = $this->_getPdf()->newPage(Zend_Pdf_Page::SIZE_A4);
    $this->_getPdf()->pages[] = $page;
    $this->y = 800;

    if (!empty($settings['table_header'])) {
        $this->_setFontRegular($page);
        $page->setFillColor(new Zend_Pdf_Color_RGB(0.93, 0.92, 0.92));
        $page->setLineColor(new Zend_Pdf_Color_GrayScale(0.5));
        $page->setLineWidth(0.5);
        $page->drawRectangle(25, $this->y, 570, $this->y-15);
        $this->y -=10;

        $page->setFillColor(new Zend_Pdf_Color_RGB(0.4, 0.4, 0.4));
        $page->drawText(Mage::helper('sales')->__('Product'), 35, $this->y, 'UTF-8');
        //Added for product image
        //$page->drawText(Mage::helper('sales')->__('Product Image'), 245, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Stock Location'), 245, $this->y, 'UTF-8');

        $page->drawText(Mage::helper('sales')->__('SKU'), 325, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Price'), 380, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Qty'), 430, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Tax'), 480, $this->y, 'UTF-8');
        $page->drawText(Mage::helper('sales')->__('Subtotal'), 535, $this->y, 'UTF-8');

        $page->setFillColor(new Zend_Pdf_Color_GrayScale(0));
        $this->y -=20;
    }

    return $page;
}

}

Default.php

<?php

class Inchoo_Invoice_Model_Order_Pdf_Items_Invoice_Default extends Mage_Sales_Model_Order_Pdf_Items_Invoice_Default
{
/**
 * Draw item line
 **/
public function draw()
{
    $order  = $this->getOrder();
    $item   = $this->getItem();
    $pdf    = $this->getPdf();
    $page   = $this->getPage();
    $lines  = array();
    $stock_location = $this->getStockLocation($item);

    // draw Product name
    $lines[0] = array(array(
        'text' => Mage::helper('core/string')->str_split($item->getName(), 60, true, true),
        'feed' => 35,
    ));

    //Inchoo - Added custom attribute
    //draw Stock Location
    $lines[0][] = array(
        'text'  => Mage::helper('core/string')->str_split($stock_location, 25),
        'feed'  => 245
    );
    // draw SKU
    $lines[0][] = array(
        'text'  => Mage::helper('core/string')->str_split($this->getSku($item), 25),
        'feed'  => 325
    );

    // draw QTY
    $lines[0][] = array(
        'text'  => $item->getQty()*1,
        'feed'  => 435
    );

    // draw Price
    $lines[0][] = array(
        'text'  => $order->formatPriceTxt($item->getPrice()),
        'feed'  => 395,
        'font'  => 'bold',
        'align' => 'right'
    );

    // draw Tax
    $lines[0][] = array(
        'text'  => $order->formatPriceTxt($item->getTaxAmount()),
        'feed'  => 495,
        'font'  => 'bold',
        'align' => 'right'
    );

    // draw Subtotal
    $lines[0][] = array(
        'text'  => $order->formatPriceTxt($item->getRowTotal()),
        'feed'  => 565,
        'font'  => 'bold',
        'align' => 'right'
    );

    // custom options

    $options = $this->getItemOptions();
    if ($options) {
        foreach ($options as $option) {
            // draw options label
            $lines[][] = array(
                'text' => Mage::helper('core/string')->str_split(strip_tags($option['label']), 70, true, true),
                'font' => 'italic',
                'feed' => 35
            );

            if ($option['value']) {
                $_printValue = isset($option['print_value']) ? $option['print_value'] : strip_tags($option['value']);
                $values = explode(', ', $_printValue);
                foreach ($values as $value) {
                    $lines[][] = array(
                        'text' => Mage::helper('core/string')->str_split($value, 50, true, true),
                        'feed' => 40
                    );
                }
            }
        }
    }

    //Edited default line height
    $lineBlock = array(
        'lines'  => $lines,
        'height' => 40
    );

    $page = $pdf->drawLineBlocks($page, array($lineBlock), array('table_header' => true));
    $this->setPage($page);

}

private function getStockLocation($item)
{
        $prod = Mage::getModel('catalog/product')->load($item->getProductId());

        if(!($return_location = $prod->getStockLocation()))
        {
            return 'N/A';
        }
        else
        {
            return $return_location;
        }
}
}

enter image description here

PDF is showing like that i don't know from where S$0.0 is coming from. How can I show total number of quantity please help.

1

Try to add below code in your .phtml file.

And You have to find code in this app\design\adminhtml\default\default\template\sales\order\invoice folder path for invoice.

$this->getOrder()->getData('total_qty_ordered');

You will get total numbers of quatity in invoice.

  • have you get solution ? – Hardik Visa Jan 10 '17 at 9:48
  • I think there is no phtml for this – Ramkishan Suthar Jan 10 '17 at 9:50
  • Do you know the exact location ? – Ramkishan Suthar Jan 10 '17 at 9:50
  • I edited my answer. – Hardik Visa Jan 10 '17 at 9:56
  • Thank you for your effort @New Bee but i found my solution in other way – Ramkishan Suthar Jan 10 '17 at 9:59
0

I found my solution myself here are the additional changes

config.xml

<?xml version="1.0"?>
<config>
<modules>
    <Inchoo_Invoice>
        <version>0.1.0</version>
    </Inchoo_Invoice>
</modules>
<global>
    <models>
        <sales>
            <rewrite>
                <order_pdf_invoice>Inchoo_Invoice_Model_Order_Pdf_Invoice</order_pdf_invoice>
                <order_pdf_items_invoice_default>Inchoo_Invoice_Model_Order_Pdf_Items_Invoice_Default</order_pdf_items_invoice_default>
                 <order_pdf_totalpdf>Inchoo_Invoice_Model_Order_Pdf_Totalpdf</order_pdf_totalpdf>               
            </rewrite>
        </sales>
    </models>
    <pdf>
        <totals>
            <qty translate="title">
                <title>Quantity</title>
                <source_field>qty</source_field>
                <font_size>7</font_size>
                <model>Inchoo_Invoice_Model_Order_Pdf_Totalpdf</model>
                <display_zero>1</display_zero>
                <sort_order>50</sort_order>
            </qty>
        </totals>
    </pdf>
</global>

Totalpdf.php

<?php
class Inchoo_Invoice_Model_Order_Pdf_Totalpdf extends Mage_Sales_Model_Order_Pdf_Total_Default {

public function getTotalsForDisplay() {

  $order = $this->getOrder();
  $item = $this->getItem();
  $result = $order->getData('total_qty_ordered');

  if($this->getAmountPrefix()){
     $result= $this->getAmountPrefix().$result;
  }

  $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;

  $totals = array(array(
            'label' => 'Quantity',
            'amount' => $result,
            'font_size' => $fontSize,
         )
     );

  return $totals;
}

}

thanx to @Wakanina https://stackoverflow.com/questions/17497485/magento-module-add-rows-to-totals-in-pdf-invoice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.