Allright, this is what I have got
I have modified FlyWebdesign_PaypalFee_Model_Sales_Quote_Address_Total_Paymentcharge like this (see the four comments marked clime):
<?php
/**
* @category FlyWebdesign
* @package FlyWebdesign_PaypalFee
*/
class FlyWebdesign_PaypalFee_Model_Sales_Quote_Address_Total_Paymentcharge extends Mage_Sales_Model_Quote_Address_Total_Abstract
{
protected $_calculator = null;
public function __construct()
{
$this->setCode('payment_charge');
$this->_calculator = Mage::getSingleton('tax/calculation');
}
public function collect(Mage_Sales_Model_Quote_Address $address)
{
$address->setPaymentCharge(0);
$address->setBasePaymentCharge(0);
$items = $address->getAllItems();
if (!count($items)) {
return $this;
}
$paymentMethod = $address->getQuote()->getPayment()->getMethod();
if ($paymentMethod) {
$amount = Mage::helper('paymentcharge')->getPaymentCharge($paymentMethod, $address->getQuote());
$address->setPaymentCharge($amount);
$address->setBasePaymentCharge($amount);
/* clime: tax calculation start */
$calc = $this->_calculator;
$store = $address->getQuote()->getStore();
$addressTaxRequest = $calc->getRateRequest(
$address->getQuote()->getShippingAddress(),
$address->getQuote()->getBillingAddress(),
$address->getQuote()->getCustomerTaxClassId(),
$store
);
$paymentTaxClass = Mage::getStoreConfig('payment/'.$paymentMethod.'/payment_tax_class');
$addressTaxRequest->setProductClassId($paymentTaxClass);
$rate = $calc->getRate($addressTaxRequest);
$taxAmount = $calc->calcTaxAmount($amount, $rate, false, true);
$baseTaxAmount = $calc->calcTaxAmount($amount, $rate, false, true);
$address->setPaymentTaxAmount($taxAmount);
$address->setBasePaymentTaxAmount($baseTaxAmount);
$address->setTaxAmount($address->getTaxAmount() + $taxAmount);
$address->setBaseTaxAmount($address->getBaseTaxAmount() + $baseTaxAmount);
/* clime: tax calculation end */
}
//clime: tax added
$address->setGrandTotal($address->getGrandTotal() + $address->getPaymentCharge() + $address->getPaymentTaxAmount());
//clime: tax added
$address->setBaseGrandTotal($address->getBaseGrandTotal() + $address->getBasePaymentCharge() + $address->getBasePaymentTaxAmount());
return $this;
}
public function fetch(Mage_Sales_Model_Quote_Address $address)
{
$amount = $address->getPaymentCharge();
if (($amount!=0)) {
$address->addTotal(array(
'code' => $this->getCode(),
'title' => Mage::helper('sales')->__('Payment Charge'),
'full_info' => array(),
'value' => $amount
));
}
return $this;
}
}
I have modified plugin's system.xml to add admin field Tax class for the payment fee. Next, I haven't actually changed the plugin's classes Creditmemo_Total_Paymentcharge and Invoice_Total_Paymentcharge as I had in mind previously, but instead I have add the following rewrites in plugin's config.xml (in models section):
<sales>
<rewrite>
<order_invoice_total_tax>FlyWebdesign_PaypalFee_Model_Sales_Order_Invoice_Total_Tax</order_invoice_total_tax>
<order_creditmemo_total_tax>FlyWebdesign_PaypalFee_Model_Sales_Order_Creditmemo_Total_Tax</order_creditmemo_total_tax>
</rewrite>
</sales>
In these two new files (copies of the original Magento source files - one of them being: /app/code/core/Mage/Sales/Model/Order/Invoice/Total/Tax.php), I have added the following into collect method of order_invoice_total_tax:
if ($this->_canIncludePaymentTax($invoice)) {
$totalTax += $order->getPaymentTaxAmount();
$baseTotalTax += $order->getBasePaymentTaxAmount();
$invoice->setPaymentTaxAmount($order->getPaymentTaxAmount());
$invoice->setBasePaymentTaxAmount($order->getBasePaymentTaxAmount());
}
And I also added this method to the class.
protected function _canIncludePaymentTax($invoice)
{
$includePaymentTax = true;
/**
* Check payment amount in previous invoices
*/
foreach ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) {
if ($previusInvoice->getPaymentAmount() && !$previusInvoice->isCanceled()) {
$includePaymentTax = false;
}
}
return $includePaymentTax;
}
I have added this into collect method of the other class order_creditmemo_total_tax:
if ($invoice = $creditmemo->getInvoice()) {
$paymentTaxAmount = $invoice->getPaymentTaxAmount();
$basePaymentTaxAmount = $invoice->getBasePaymentTaxAmount();
$totalTax += $paymentTaxAmount;
$baseTotalTax += $basePaymentTaxAmount;
} else {
$paymentTaxAmount = $order->getPaymentTaxAmount();
$basePaymentTaxAmount = $order->getBasePaymentTaxAmount();
$totalTax += $paymentTaxAmount;
$baseTotalTax += $basePaymentTaxAmount;
}
$creditmemo->setPaymentTaxAmount($paymentTaxAmount);
$creditmemo->setBasePaymentTaxAmount($basePaymentTaxAmount);
I also needed to make the following sql upgrade script for the plugin
<?php
/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
$installer->addAttribute('quote_address', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('quote_address', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('order', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('order', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('invoice', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('invoice', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('creditmemo', 'payment_tax_amount', array('type' => 'decimal'));
$installer->addAttribute('creditmemo', 'base_payment_tax_amount', array('type' => 'decimal'));
$installer->endSetup();
And I added this into plugin's config.xml - section <fieldsets><sales_convert_quote_address>
<payment_tax_amount><to_order>*</to_order></payment_tax_amount>
<base_payment_tax_amount><to_order>*</to_order></base_payment_tax_amount>
As far as my testing goes, this works for me. However there are still some unsolved (potential) troubles that I would like to solve.
potential problem with specified payment amount if base currency is not store currency (not sure how magento will behave in such a case)
amount specified in admin is always considered to exclude tax (there is no option to change it)
tax for payment fee does not occur in full tax summary (if enabled) as a row on its own
Btw my Magento version is 1.7.0.2