Magento模块在pdf发票中向总计添加行

时间:2013-07-05 22:13:01

标签: magento

我正在尝试编写模块以将项目添加到pdf发票中的行总计。这是我的模块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>

这是我的模特课:

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;



}

}

pdf发票上没有任何内容。有人可以建议,这里有什么不妥。感谢。

3 个答案:

答案 0 :(得分:2)

我解决了我的问题,在看了我的代码后,出现了一些错误。但我的问题与你的不一样。

我注意到你的config.xml和模型类中的代码都不正确。试试这个。

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>

模型类,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;
   }

}

答案 1 :(得分:1)

我已经尝试过上面的代码,它没有得到$item的值。我从$ order对象获得了小计值和折扣值,但它确实有效。

    $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;

答案 2 :(得分:0)

我按照@Wakanina回答并更改config.xml - 路径到pdf模式:

<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>

和totalpdf Model类:

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;
    }  
}

这有助于解决我的问题。