2011-12-15 86 views
1

大圖片:我正在爲會計程序生成.txt文件。
我要總結獲取發票收集總計:總計含稅。 - 不含稅 - 按類型徵稅

  • 包括稅收總
  • 總含稅
  • 稅金總額按類型

詳細:我有發票的集合:

private function _getInvoiceCollectionPerStore($storeId) 
    { 
     $invoices = Mage::getResourceModel('sales/order_invoice_collection') 
       ->addFieldToFilter('main_table.store_id', $storeId); 
     //orde increment id 
     $invoices->addExpressionFieldToSelect(
       'order_increment_id', 
       'order.increment_id', 
       'main_table.order_id' 
     ); 
     //customer id 
     $invoices->getSelect()->join(
       array('order' => 'sales_flat_order'), 
       'main_table.order_id = order.entity_id', 
       array('order.customer_id')); 

     return $invoices; 
    } 

與該公司我可以計算總稅和總稅除外。

 $invoices->addExpressionFieldToSelect(
       'order_total', 
       'SUM({{main_table.grand_total}})', 
       'main_table.grand_total' 
     ); 
     $invoices->addExpressionFieldToSelect(
       'tax_total', 
       'SUM({{main_table.tax_amount}})', 
       'main_table.tax_amount' 
     ); 

現在我想計算髮票/訂單的不同稅額的總和。
對於這一點,我有表sales_order_tax在那裏我有不同的稅收爲每個訂單:

| tax_id | order_id | code | percent | amount | 
| 1 |  1 | 18 | 18.0000 | 25.0100| 
| 2 |  1 | 8 | 8.0000 | 1.1100| 
| 3 |  2 | 18 | 18.0000 | 3.2000| 
| 4 |  2 | 4 | 4.0000 | 0.6500| 

(我已經簡化表,因爲這是唯一的領域與此有關)。
正如你看到我的虛擬數據,我有2個訂單(ids 1和2)與3種稅種(4,8和18)。
我想要的結果是:

18 = 28.21 (=> 25.01+3.2) 
8 = 1.11 
4 = 0.65 

最後,我的問題是:我能得到這樣的結果與加入和組語句?或者我必須通過收集來總結它?

回答

1

如果我理解你正確地質疑,這會給這個SQL statment會給你每稅法總量

Select code, SUM(amount) 
    from sales_order_tax 
group by code 
+0

事實上,你正確地理解我,謝謝 – OSdave 2011-12-16 10:54:51