srdan
srdan

Reputation: 211

Magento module add rows to totals in pdf invoice

I'm trying to write module for add item to row totals in pdf invoice. This is my modules config.xml:

<?xml version="1.0"?>
<config>
<modules>
<Devpassion_Rowtotal>
    <version>0.0.1</version>
</Devpassion_Rowtotal>
</modules>
<global>
 <pdf>
     <totals>
         <rowtotal translate="title">
            <title>Subtotal less discount</title>
            <source_field>rowtotal</source_field>
            <model>rowtotal_pdf_model_totalpdf</model>
            <font_size>7</font_size>
            <display_zero>1</display_zero>
            <sort_order>200</sort_order>
         </rowtotal>
     </totals>
  </pdf>

And this is my model class:

class Devpassion_Rowtotal_Pdf_Model_Totalpdf extends   Mage_Sales_Model_Order_Pdf_Total_Default {

public function getTotalsForDisplay () {

$order = $this->getOrder();
$item = $this->getItem();

$subtotaldisc = $item->getRowTotal() + $item->getTaxAmount() + $item->getHiddenTaxAmount() ; - $item->getDiscountAmount();  
$result = $order->formatPriceTxt($subtotaldisc) ;

$totals = array(array(
    'label' => 'Cijena sa popustom',
    'amount' => $result,
    'font_size' => $fontSize,
    )
    );

return $totals;



}

}

And nothing shows up on pdf invoice. Can anybody advice please what can be wrong here. Thanks.

Upvotes: 0

Views: 2409

Answers (3)

Dalbin Shimy
Dalbin Shimy

Reputation: 85

I have tried the above code, it doesn't get the value of $item. I have get the subtotal value and discount value from $order object and it worked.

    $order = $this->getOrder ();
    $subtotaldisc = $order ['subtotal'] + $order ['discount_amount']; //discount amount returns negative value, so add
    $result = $order->formatPriceTxt ( $subtotaldisc );
    $fontSize = $this->getFontSize () ? $this->getFontSize () : 10;
    $totals = array (
            array (
                    'label' => 'Subtotal with Discount',
                    'amount' => $result,
                    'font_size' => $fontSize 
            ) 
    );
    return $totals;

Upvotes: 1

srdan
srdan

Reputation: 211

I follow @Wakanina answer and change config.xml - path to pdf mode:

<pdf>
            <totals>
              <rowtotalbezpdv translate="title">
                <title>Ukupno s popustom</title>
                <source_field>rowtot_alamount</source_field>
                <model>Devpassion_Rowtotalbezpdv_Model_Pdf_Total_Totalbezpdf</model>
                <font_size>7</font_size>
                <display_zero>0</display_zero>
                <sort_order>300</sort_order>
              </rowtotalbezpdv>
            </totals>
        </pdf>

And totalpdf Model class:

class Devpassion_Rowtotalbezpdv_Model_Pdf_Total_Totalbezpdf extends Mage_Sales_Model_Order_Pdf_Total_Default
{

    public function getTotalsForDisplay(){
        $amount = $this->getAmount();
        $fontSize = $this->getFontSize() ? $this->getFontSize() : 7;
        if(floatval($amount)){
            $amount = $this->getOrder()->formatPriceTxt($amount);

            $totals = array(
                array(
                    'label' => 'Cijena knjige/a s popustom bez PDV-a',
                    'amount' => $amount,
                    'font_size' => $fontSize,
                )
            );

            return $totals;
        }
    }

    public function getAmount(){
        $order = $this->getOrder();
        $subtotaldiscnopdv = 0;
        foreach ($order->getAllItems() as $item) {
            $subtotaldiscnopdv += $item->getRowTotal() - $item->getDiscountAmount();
        }
        return $subtotaldiscnopdv;
    }  
}

This helps me out and solve my issue.

Upvotes: 0

Wakanina
Wakanina

Reputation: 602

I solved my problem, after I looked at my code, there is some mistakes. But my problem is not like yours.

I noticed that you have incorrect code in your config.xml and also in your model class. Try this.

config.xml

<global>
   <pdf>
     <totals>
         <rowtotal translate="title">
            <title>Subtotal less discount</title>
            <source_field>rowtotal</source_field>
            <model>Devpassion_Rowtotal_Pdf_Model_Totalpdf</model>
            <font_size>7</font_size>
            <display_zero>1</display_zero>
            <sort_order>200</sort_order>
         </rowtotal>
     </totals>
  </pdf>
</global>

Model class, Totalpdf.php

class Devpassion_Rowtotal_Pdf_Model_Totalpdf extends Mage_Sales_Model_Order_Pdf_Total_Default {

   public function getTotalsForDisplay() {

      $order = $this->getOrder();
      $item = $this->getItem();

      $subtotaldisc = $item->getRowTotal() + $item->getTaxAmount() + $item->getHiddenTaxAmount() - $item->getDiscountAmount();  
      $result = $order->formatPriceTxt($subtotaldisc);

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

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

      $totals = array(array(
                'label' => 'Cijena sa popustom:',
                'amount' => $result,
                'font_size' => $fontSize,
             )
         );

      return $totals;
   }

}

Upvotes: 2

Related Questions