Example #1
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         $item->calcRowTotal();
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #2
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced();
     $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount() + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced();
     $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount() + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced();
     /**
      * Check if shipping tax calculation is included to current invoice.
      */
     $includeShippingTax = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
             $includeShippingTax = false;
             break;
         }
     }
     if ($includeShippingTax) {
         $allowedSubtotalInclTax -= $order->getShippingTaxAmount();
         $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
     } else {
         $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount();
         $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
         $subtotalInclTax = $allowedSubtotalInclTax;
         $baseSubtotalInclTax = $baseAllowedSubtotalInclTax;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
         $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
         $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #3
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     /**
      * Check order grand total and invoice amounts
      */
     if ($invoice->isLast()) {
         //
     }
     return $this;
 }
Example #4
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $invoice->setSveaPaymentFeeAmount(0);
     $invoice->setBaseSveaPaymentFeeAmount(0);
     $invoice->setSveaPaymentFeeTaxAmount(0);
     $invoice->setBaseSveaPaymentFeeTaxAmount(0);
     $invoice->setSveaPaymentFeeInclTax(0);
     $invoice->setBaseSveaPaymentFeeInclTax(0);
     $orderPaymentFeeAmount = $invoice->getOrder()->getSveaPaymentFeeAmount();
     $baseOrderPaymentFeeAmount = $invoice->getOrder()->getBaseSveaPaymentFeeAmount();
     $paymentFeeInclTax = $invoice->getOrder()->getSveaPaymentFeeInclTax();
     $basePaymentFeeInclTax = $invoice->getOrder()->getBaseSveaPaymentFeeInclTax();
     if ($orderPaymentFeeAmount) {
         foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
             if ($previousInvoice->getSveaPaymentFeeAmount() && !$previousInvoice->isCanceled()) {
                 // Payment fee has already been invoiced
                 return $this;
             }
         }
         $invoice->setSveaPaymentFeeAmount($orderPaymentFeeAmount);
         $invoice->setBaseSveaPaymentFeeAmount($baseOrderPaymentFeeAmount);
         $invoice->setSveaPaymentFeeTaxAmount($invoice->getOrder()->getSveaPaymentFeeTaxAmount());
         $invoice->setBaseSveaPaymentFeeTaxAmount($invoice->getOrder()->getBaseSveaPaymentFeeTaxAmount());
         $invoice->setSveaPaymentFeeInclTax($paymentFeeInclTax);
         $invoice->setBaseSveaPaymentFeeInclTax($basePaymentFeeInclTax);
         $subtotal = $invoice->getSubtotal();
         $baseSubtotal = $invoice->getBaseSubtotal();
         $subtotalInclTax = $invoice->getSubtotalInclTax();
         $baseSubtotalInclTax = $invoice->getBaseSubtotalInclTax();
         $grandTotal = $invoice->getGrandTotal() + $orderPaymentFeeAmount;
         $baseGrandTotal = $invoice->getBaseGrandTotal() + $baseOrderPaymentFeeAmount;
         $totalTax = $invoice->getTaxAmount();
         $baseTotalTax = $invoice->getBaseTaxAmount();
         if ($invoice->isLast()) {
             $subtotalInclTax -= $invoice->getOrder()->getSveaPaymentFeeTaxAmount();
             $baseSubtotalInclTax -= $invoice->getOrder()->getBaseSveaPaymentFeeTaxAmount();
         } else {
             $totalTax += $invoice->getOrder()->getSveaPaymentFeeTaxAmount();
             $baseTotalTax += $invoice->getOrder()->getBaseSveaPaymentFeeTaxAmount();
             $subtotalInclTax += $invoice->getOrder()->getSveaPaymentFeeTaxAmount();
             $baseSubtotalInclTax += $invoice->getOrder()->getBaseSveaPaymentFeeTaxAmount();
             $grandTotal += $invoice->getOrder()->getSveaPaymentFeeTaxAmount();
             $baseGrandTotal += $invoice->getOrder()->getBaseSveaPaymentFeeTaxAmount();
         }
         $invoice->setSubtotal($subtotal);
         $invoice->setBaseSubtotal($baseSubtotal);
         $invoice->setSubtotalInclTax($subtotalInclTax);
         $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
         $invoice->setTaxAmount($totalTax);
         $invoice->setBaseTaxAmount($baseTotalTax);
         $invoice->setGrandTotal($grandTotal);
         $invoice->setBaseGrandTotal($baseGrandTotal);
     }
     return $this;
 }
 /**
  * Collect gift wrapping tax totals
  *
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return Enterprise_GiftWrapping_Model_Total_Invoice_Tax_Giftwrapping
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * Wrapping for items
      */
     $invoiced = 0;
     $baseInvoiced = 0;
     foreach ($invoice->getAllItems() as $invoiceItem) {
         if (!$invoiceItem->getQty() || $invoiceItem->getQty() == 0) {
             continue;
         }
         $orderItem = $invoiceItem->getOrderItem();
         if ($orderItem->getGwId() && $orderItem->getGwBaseTaxAmount() && $orderItem->getGwBaseTaxAmount() != $orderItem->getGwBaseTaxAmountInvoiced()) {
             $orderItem->setGwBaseTaxAmountInvoiced($orderItem->getGwBaseTaxAmount());
             $orderItem->setGwTaxAmountInvoiced($orderItem->getGwTaxAmount());
             $baseInvoiced += $orderItem->getGwBaseTaxAmount();
             $invoiced += $orderItem->getGwTaxAmount();
         }
     }
     if ($invoiced > 0 || $baseInvoiced > 0) {
         $order->setGwItemsBaseTaxInvoiced($order->getGwItemsBaseTaxInvoiced() + $baseInvoiced);
         $order->setGwItemsTaxInvoiced($order->getGwItemsTaxInvoiced() + $invoiced);
         $invoice->setGwItemsBaseTaxAmount($baseInvoiced);
         $invoice->setGwItemsTaxAmount($invoiced);
     }
     /**
      * Wrapping for order
      */
     if ($order->getGwId() && $order->getGwBaseTaxAmount() && $order->getGwBaseTaxAmount() != $order->getGwBaseTaxAmountInvoiced()) {
         $order->setGwBaseTaxAmountInvoiced($order->getGwBaseTaxAmount());
         $order->setGwTaxAmountInvoiced($order->getGwTaxAmount());
         $invoice->setGwBaseTaxAmount($order->getGwBaseTaxAmount());
         $invoice->setGwTaxAmount($order->getGwTaxAmount());
     }
     /**
      * Printed card
      */
     if ($order->getGwAddCard() && $order->getGwCardBaseTaxAmount() && $order->getGwCardBaseTaxAmount() != $order->getGwCardBaseTaxInvoiced()) {
         $order->setGwCardBaseTaxInvoiced($order->getGwCardBaseTaxAmount());
         $order->setGwCardTaxInvoiced($order->getGwCardTaxAmount());
         $invoice->setGwCardBaseTaxAmount($order->getGwCardBaseTaxAmount());
         $invoice->setGwCardTaxAmount($order->getGwCardTaxAmount());
     }
     if (!$invoice->isLast()) {
         $baseTaxAmount = $invoice->getGwItemsBaseTaxAmount() + $invoice->getGwBaseTaxAmount() + $invoice->getGwCardBaseTaxAmount();
         $taxAmount = $invoice->getGwItemsTaxAmount() + $invoice->getGwTaxAmount() + $invoice->getGwCardTaxAmount();
         $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTaxAmount);
         $invoice->setTaxAmount($invoice->getTaxAmount() + $taxAmount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTaxAmount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $taxAmount);
     }
     return $this;
 }
Example #6
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if ($order->getCustomercreditDiscount() < 0.0001) {
         return;
     }
     if ($invoice->isLast()) {
         $baseDiscount = $order->getBaseCustomercreditDiscount();
         $discount = $order->getCustomercreditDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($baseDiscount > 0.0001) {
                 $baseDiscount -= $existedInvoice->getBaseCustomercreditDiscount();
                 $discount -= $existedInvoice->getCustomercreditDiscount();
             }
         }
     } else {
         $orderData = $order->getData();
         $invoiceData = $invoice->getData();
         if ($invoiceData['shipping_incl_tax']) {
             $ratio = ($invoiceData['subtotal_incl_tax'] + $invoiceData['shipping_incl_tax']) / ($orderData['subtotal_incl_tax'] + $orderData['shipping_incl_tax']);
         } else {
             $ratio = $invoiceData['subtotal_incl_tax'] / ($orderData['subtotal_incl_tax'] + $orderData['shipping_incl_tax']);
         }
         $baseDiscount = $order->getBaseCustomercreditDiscount() * $ratio;
         $discount = $order->getCustomercreditDiscount() * $ratio;
         $maxBaseDiscount = $order->getBaseCustomercreditDiscount();
         $maxDiscount = $order->getCustomercreditDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($maxBaseDiscount > 0.0001) {
                 $maxBaseDiscount -= $existedInvoice->getBaseCustomercreditDiscount();
                 $maxDiscount -= $existedInvoice->getCustomercreditDiscount();
             }
         }
         if ($baseDiscount > $maxBaseDiscount) {
             $baseDiscount = $maxBaseDiscount;
             $discount = $maxDiscount;
         }
     }
     if ($baseDiscount > 0.0001) {
         if ($invoice->getBaseGrandTotal() <= $baseDiscount) {
             $invoice->setBaseCustomercreditDiscount($invoice->getBaseGrandTotal());
             $invoice->setCustomercreditDiscount($invoice->getGrandTotal());
             $invoice->setBaseGrandTotal(0.0);
             $invoice->setGrandTotal(0.0);
         } else {
             $invoice->setBaseCustomercreditDiscount($baseDiscount);
             $invoice->setCustomercreditDiscount($discount);
             $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseDiscount);
             $invoice->setGrandTotal($invoice->getGrandTotal() - $discount);
         }
     }
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     // Changed By Adam 22/09/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return $this;
     }
     $baseDiscount = 0;
     $discount = 0;
     $order = $invoice->getOrder();
     $baseOrderDiscount = $order->getBaseAffiliateCredit();
     $orderDiscount = $order->getAffiliateCredit();
     if ($invoice->getBaseGrandTotal() < 0.0001 || $baseOrderDiscount >= 0) {
         return $this;
     }
     $baseInvoicedDiscount = 0;
     $invoicedDiscount = 0;
     foreach ($order->getInvoiceCollection() as $_invoice) {
         $baseInvoicedDiscount += $_invoice->getBaseAffiliateCredit();
         $invoicedDiscount += $_invoice->getAffiliateCredit();
     }
     if ($invoice->isLast()) {
         $baseDiscount = $baseOrderDiscount - $baseInvoicedDiscount;
         $discount = $orderDiscount - $invoicedDiscount;
     } else {
         //            edit by viet
         $baseOrderTotal = $order->getBaseSubtotalInclTax();
         // - $baseOrderDiscount;
         $baseDiscount = $baseOrderDiscount * $invoice->getBaseSubtotalInclTax() / $baseOrderTotal;
         $discount = $orderDiscount * $invoice->getBaseSubtotalInclTax() / $baseOrderTotal;
         //            end by viet
         if ($baseDiscount < $baseOrderDiscount) {
             $baseDiscount = $baseOrderDiscount;
             $discount = $orderDiscount;
         }
     }
     if ($baseDiscount) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $invoice->setBaseAffiliateCredit($baseDiscount);
         $invoice->setAffiliateCredit($discount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseDiscount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #8
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if ($order->getWebposCash() < 0.0001 || $order->getGrandTotal() < 0.0001) {
         return;
     }
     if ($invoice->isLast()) {
         $cash = $order->getWebposCash();
         $baseCash = $order->getWebposBaseCash();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($cash > 0.0001) {
                 $cash -= $existedInvoice->getWebposCash();
                 $baseCash -= $existedInvoice->getWebposBaseCash();
             }
         }
     } else {
         $ratio = $invoice->getGrandTotal() / $order->getGrandTotal();
         $cash = $order->getWebposCash() * $ratio;
         $baseCash = $order->getWebposBaseCash() * $ratio;
         $maxcash = $order->getWebposCash();
         $maxbaseCash = $order->getWebposBaseCash();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($maxcash > 0.0001) {
                 $maxcash -= $existedInvoice->getWebposCash();
                 $maxbaseCash -= $existedInvoice->getWebposBaseCash();
             }
         }
         if ($cash > $maxcash) {
             $cash = $maxcash;
             $baseCash = $maxbaseCash;
         }
     }
     if ($cash > 0.0001) {
         $invoice->setWebposCash($cash)->setWebposBaseCash($baseCash);
         /*
                     if ($invoice->getGrandTotal() <= $cash) {
                         $invoice->setBaseGrandTotal(0.0)
                             ->setGrandTotal(0.0);
                     } else {
                         $invoice->setGrandTotal($invoice->getGrandTotal() - $cash)
                             ->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseCash);
                     }
         */
     }
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $codTax = 0;
     $baseCodTax = 0;
     $order = $invoice->getOrder();
     $includeCodTax = true;
     if ($order->getPayment()->getMethodInstance()->getCode() != 'phoenix_cashondelivery') {
         return $this;
     }
     /**
      * Check Cod amount in previus invoices
      */
     foreach ($order->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getCodFee() && !$previousInvoice->isCanceled()) {
             $includeCodTax = false;
         }
     }
     if ($includeCodTax) {
         $codTax = $order->getCodTaxAmount();
         $baseCodTax = $order->getBaseCodTaxAmount();
         $invoice->setCodTaxAmount($order->getCodTaxAmount());
         $invoice->setBaseCodTaxAmount($order->getBaseCodTaxAmount());
         $invoice->getOrder()->setCodTaxAmountInvoiced($codTax);
         $invoice->getOrder()->setBaseCodTaxAmountInvoiced($baseCodTax);
     }
     /**
      * Not isLast() invoice case handling
      * totalTax adjustment
      * check Mage_Sales_Model_Order_Invoice_Total_Tax::collect()
      */
     $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();
     $totalTax = $invoice->getTaxAmount();
     $baseTotalTax = $invoice->getBaseTaxAmount();
     if (!$invoice->isLast() && $allowedTax > $totalTax) {
         $newTotalTax = min($allowedTax, $totalTax + $codTax);
         $newBaseTotalTax = min($allowedBaseTax, $baseTotalTax + $baseCodTax);
         $invoice->setTaxAmount($newTotalTax);
         $invoice->setBaseTaxAmount($newBaseTotalTax);
         $invoice->setGrandTotal($invoice->getGrandTotal() - $totalTax + $newTotalTax);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalTax + $newBaseTotalTax);
     }
     return $this;
 }
Example #10
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if (!$order->getRewardpointsInvitedBaseDiscount()) {
         return $this;
     }
     $invoice->setRewardpointsInvitedDiscount(0);
     $invoice->setRewardpointsInvitedBaseDiscount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     if ($invoice->isLast()) {
         $baseTotalDiscountAmount = $order->getRewardpointsInvitedBaseDiscount();
         $totalDiscountAmount = $order->getRewardpointsInvitedDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             $baseTotalDiscountAmount -= $existedInvoice->getRewardpointsInvitedBaseDiscount();
             $totalDiscountAmount -= $existedInvoice->getRewardpointsInvitedDiscount();
         }
     } else {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemDiscount = (double) $orderItem->getRewardpointsInvitedDiscount();
             $baseOrderItemDiscount = (double) $orderItem->getRewardpointsInvitedBaseDiscount();
             $orderItemQty = $orderItem->getQtyOrdered();
             if ($orderItemDiscount && $orderItemQty) {
                 $discount = $invoice->roundPrice($orderItemDiscount / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $invoice->roundPrice($baseOrderItemDiscount / $orderItemQty * $item->getQty(), 'base', true);
                 $item->setRewardpointsInvitedDiscount($discount);
                 $item->setRewardpointsInvitedBaseDiscount($baseDiscount);
                 $totalDiscountAmount += $discount;
                 $baseTotalDiscountAmount += $baseDiscount;
             }
         }
     }
     $invoice->setRewardpointsInvitedDiscount($totalDiscountAmount);
     $invoice->setRewardpointsInvitedBaseDiscount($baseTotalDiscountAmount);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #11
0
 /**
  * Collect the order total
  *
  * @param object $invoice The invoice instance to collect from
  *
  * @return Mage_Sales_Model_Order_Invoice_Total_Abstract
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     $method = $order->getPayment()->getMethodInstance();
     if (substr($method->getCode(), 0, 5) != 'sisow') {
         return $this;
     }
     // Only collect the invoice fee if we do not have any recent invoices
     if ($invoice->getOrder()->hasInvoices() != 0) {
         return $this;
     }
     $info = $method->getInfoInstance();
     if (!$info) {
         return $this;
     }
     $invoiceFee = $info->getAdditionalInformation('invoice_fee');
     $baseInvoiceFee = $info->getAdditionalInformation('base_invoice_fee');
     $invoiceFeeExludingVat = $info->getAdditionalInformation('invoice_fee_exluding_vat');
     $baseInvoiceFeeExludingVat = $info->getAdditionalInformation('base_invoice_fee_exluding_vat');
     if (!$invoiceFee) {
         return $this;
     }
     if ($invoice->isLast()) {
         //The tax for our invoice fee is already applied to the grand total
         //at this point, so we only need to add the remaining  amount
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseInvoiceFeeExludingVat);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $invoiceFeeExludingVat);
     } else {
         //Our tax doesn't get picked up by the parent function so we need
         //to add our complete invoice fee
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseInvoiceFee);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $invoiceFee);
     }
     $invoice->setBaseInvoiceFee($baseInvoiceFee);
     $invoice->setInvoiceFee($invoiceFee);
     $order->setBaseInvoiceFeeInvoiced($invoiceFeeExludingVat);
     $order->setInvoiceFeeInvoiced($invoiceFee);
     return $this;
 }
Example #12
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $baseDiscount = 0;
     $discount = 0;
     $order = $invoice->getOrder();
     $baseOrderDiscount = $order->getBaseAffiliateCredit();
     $orderDiscount = $order->getAffiliateCredit();
     if ($invoice->getBaseGrandTotal() < 0.0001 || $baseOrderDiscount >= 0) {
         return $this;
     }
     $baseInvoicedDiscount = 0;
     $invoicedDiscount = 0;
     foreach ($order->getInvoiceCollection() as $_invoice) {
         $baseInvoicedDiscount += $_invoice->getBaseAffiliateCredit();
         $invoicedDiscount += $_invoice->getAffiliateCredit();
     }
     if ($invoice->isLast()) {
         $baseDiscount = $baseOrderDiscount - $baseInvoicedDiscount;
         $discount = $orderDiscount - $invoicedDiscount;
     } else {
         $baseOrderTotal = $order->getBaseGrandTotal() - $baseOrderDiscount;
         $baseDiscount = $baseOrderDiscount * $invoice->getBaseGrandTotal() / $baseOrderTotal;
         $discount = $orderDiscount * $invoice->getBaseGrandTotal() / $baseOrderTotal;
         if ($baseDiscount < $baseOrderDiscount) {
             $baseDiscount = $baseOrderDiscount;
             $discount = $orderDiscount;
         }
     }
     if ($baseDiscount) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $invoice->setBaseAffiliateCredit($baseDiscount);
         $invoice->setAffiliateCredit($discount);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseDiscount);
         $invoice->setGrandTotal($invoice->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #13
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $feeTax = 0;
     $baseFeeTax = 0;
     $order = $invoice->getOrder();
     $includeFeeTax = TRUE;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $prevInvoice) {
         if ($prevInvoice->getCapayableFee() && !$prevInvoice->isCanceled()) {
             $includeFeeTax = FALSE;
         }
     }
     if ($includeFeeTax) {
         $feeTax += $invoice->getOrder()->getCapayableFeeTaxAmount();
         $baseFeeTax += $invoice->getOrder()->getBaseCapayableFeeTaxAmount();
         $invoice->setCapayableFeeTaxAmount($invoice->getOrder()->getCapayableFeeTaxAmount());
         $invoice->setBaseCapayableFeeTaxAmount($invoice->getOrder()->getBaseCapayableFeeTaxAmount());
         $invoice->getOrder()->setCapayableFeeTaxAmountInvoiced($feeTax);
         $invoice->getOrder()->setBaseCapayableFeeTaxAmountInvoiced($baseFeeTax);
     }
     /**
      * Not isLast() invoice case handling
      * totalTax adjustment
      * check Mage_Sales_Model_Order_Invoice_Total_Tax::collect()
      */
     $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();
     $totalTax = $invoice->getTaxAmount();
     $baseTotalTax = $invoice->getBaseTaxAmount();
     if (!$invoice->isLast() && $allowedTax > $totalTax) {
         $newTotalTax = min($allowedTax, $totalTax + $feeTax);
         $newBaseTotalTax = min($allowedBaseTax, $baseTotalTax + $baseFeeTax);
         $invoice->setTaxAmount($newTotalTax);
         $invoice->setBaseTaxAmount($newBaseTotalTax);
         $invoice->setGrandTotal($invoice->getGrandTotal() - $totalTax + $newTotalTax);
         $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalTax + $newBaseTotalTax);
     }
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $totalTax = 0;
     $baseTotalTax = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemTax = $orderItem->getTaxAmount();
         $baseOrderItemTax = $orderItem->getBaseTaxAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemTax && $orderItemQty) {
             if ($item->getOrderItem()->isDummy()) {
                 continue;
             }
             /**
              * Resolve rounding problems
              */
             if ($item->isLast()) {
                 $tax = $orderItemTax - $orderItem->getTaxInvoiced();
                 $baseTax = $baseOrderItemTax - $orderItem->getBaseTaxInvoiced();
             } else {
                 $tax = $orderItemTax * $item->getQty() / $orderItemQty;
                 $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty;
                 $tax = $invoice->getStore()->roundPrice($tax);
                 $baseTax = $invoice->getStore()->roundPrice($baseTax);
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
         }
     }
     $includeShippingTax = true;
     /**
      * Check shipping amount in previus invoices
      */
     foreach ($order->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getShippingAmount() && !$previusInvoice->isCanceled()) {
             $includeShippingTax = false;
         }
     }
     if ($includeShippingTax) {
         $totalTax += $order->getShippingTaxAmount();
         $baseTotalTax += $order->getBaseShippingTaxAmount();
         $invoice->setShippingTaxAmount($order->getShippingTaxAmount());
         $invoice->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount());
     }
     $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced();
     if ($invoice->isLast()) {
         $totalTax = $allowedTax;
         $baseTotalTax = $allowedBaseTax;
     } else {
         $totalTax = min($allowedTax, $totalTax);
         $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
     }
     $invoice->setTaxAmount($totalTax);
     $invoice->setBaseTaxAmount($baseTotalTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #15
0
 /**
  * Collect total when create Invoice
  * 
  * @param Mage_Sales_Model_Order_Invoice $invoice
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * update 2.0
      */
     $earnPoint = 0;
     $maxEarn = $order->getRewardpointsEarn();
     $maxEarn -= (int) Mage::getResourceModel('rewardpoints/transaction_collection')->addFieldToFilter('action', 'earning_invoice')->addFieldToFilter('order_id', $order->getId())->getFieldTotal();
     if ($maxEarn >= 0) {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $itemPoint = (int) $orderItem->getRewardpointsEarn();
             $itemPoint = $itemPoint * $item->getQty() / $orderItem->getQtyOrdered();
             $earnPoint += floor($itemPoint);
         }
         if ($invoice->isLast() || $earnPoint >= $maxEarn) {
             $earnPoint = $maxEarn;
         }
         $invoice->setRewardpointsEarn($earnPoint);
     }
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     if ($invoice->isLast()) {
         $baseDiscount = $order->getRewardpointsBaseDiscount();
         $discount = $order->getRewardpointsDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($baseDiscount > 0.0001) {
                 $baseDiscount -= $existedInvoice->getRewardpointsBaseDiscount();
                 $discount -= $existedInvoice->getRewardpointsDiscount();
             }
         }
     } else {
         $orderTotal = $order->getGrandTotal() + $order->getRewardpointsDiscount();
         $ratio = $invoice->getGrandTotal() / $orderTotal;
         $baseDiscount = $order->getRewardpointsBaseDiscount() * $ratio;
         $discount = $order->getRewardpointsDiscount() * $ratio;
         $maxBaseDiscount = $order->getRewardpointsBaseDiscount();
         $maxDiscount = $order->getRewardpointsDiscount();
         foreach ($order->getInvoiceCollection() as $existedInvoice) {
             if ($maxBaseDiscount > 0.0001) {
                 $maxBaseDiscount -= $existedInvoice->getRewardpointsBaseDiscount();
                 $maxDiscount -= $existedInvoice->getRewardpointsDiscount();
             }
         }
         if ($baseDiscount > $maxBaseDiscount) {
             $baseDiscount = $maxBaseDiscount;
             $discount = $maxDiscount;
         }
     }
     if ($baseDiscount > 0.0001) {
         if ($invoice->getBaseGrandTotal() <= $baseDiscount) {
             $invoice->setRewardpointsBaseDiscount($invoice->getBaseGrandTotal());
             $invoice->setRewardpointsDiscount($invoice->getGrandTotal());
             $invoice->setBaseGrandTotal(0.0);
             $invoice->setGrandTotal(0.0);
         } else {
             $invoice->setRewardpointsBaseDiscount($baseDiscount);
             $invoice->setRewardpointsDiscount($discount);
             $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseDiscount);
             $invoice->setGrandTotal($invoice->getGrandTotal() - $discount);
         }
     }
 }
Example #16
0
 /**
  * Collect total when create Invoice
  * 
  * @param Mage_Sales_Model_Order_Invoice $invoice
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     /**
      * update 2.0
      */
     $earnPoint = 0;
     $maxEarn = $order->getRewardpointsEarn();
     $maxEarn -= (int) Mage::getResourceModel('rewardpoints/transaction_collection')->addFieldToFilter('action', 'earning_invoice')->addFieldToFilter('order_id', $order->getId())->getFieldTotal();
     if ($maxEarn >= 0) {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $itemPoint = (int) $orderItem->getRewardpointsEarn();
             $itemPoint = $itemPoint * $item->getQty() / $orderItem->getQtyOrdered();
             $earnPoint += floor($itemPoint);
         }
         if ($invoice->isLast() || $earnPoint >= $maxEarn) {
             $earnPoint = $maxEarn;
         }
         $invoice->setRewardpointsEarn($earnPoint);
     }
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     $invoice->setRewardpointsDiscount(0);
     $invoice->setRewardpointsBaseDiscount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     $totalDiscountInvoiced = 0;
     $baseTotalDiscountInvoiced = 0;
     $hiddenTaxInvoiced = 0;
     $baseHiddenTaxInvoiced = 0;
     $totalHiddenTax = 0;
     $baseTotalHiddenTax = 0;
     /**
      * Checking if shipping discount was added in previous invoices.
      * So basically if we have invoice with positive discount and it
      * was not canceled we don't add shipping discount to this one.
      */
     $addShippingDicount = true;
     foreach ($order->getInvoiceCollection() as $previusInvoice) {
         if ($previusInvoice->getRewardpointsDiscount()) {
             $addShippingDicount = false;
             $totalDiscountInvoiced += $previusInvoice->getRewardpointsDiscount();
             $baseTotalDiscountInvoiced += $previusInvoice->getRewardpointsBaseDiscount();
             $hiddenTaxInvoiced += $previusInvoice->getRewardpointsHiddenTaxAmount();
             $baseHiddenTaxInvoiced += $previusInvoice->getRewardpointsBaseHiddenTaxAmount();
         }
     }
     if ($addShippingDicount) {
         $totalDiscountAmount += $order->getRewardpointsAmount();
         $baseTotalDiscountAmount += $order->getRewardpointsBaseAmount();
         $totalHiddenTax += $order->getRewardpointsShippingHiddenTaxAmount();
         $baseTotalHiddenTax += $order->getRewardpointsBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $totalDiscountAmount = $order->getRewardpointsDiscount() - $totalDiscountInvoiced;
         $baseTotalDiscountAmount = $order->getRewardpointsBaseDiscount() - $baseTotalDiscountInvoiced;
         $totalHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxInvoiced;
         $baseTotalHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxInvoiced;
     } else {
         /** @var $item Mage_Sales_Model_Order_Invoice_Item */
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemDiscount = (double) $orderItem->getRewardpointsDiscount();
             $baseOrderItemDiscount = (double) $orderItem->getRewardpointsBaseDiscount();
             $orderItemHiddenTax = (double) $orderItem->getRewardpointsHiddenTaxAmount();
             $baseOrderItemHiddenTax = (double) $orderItem->getRewardpointsBaseHiddenTaxAmount();
             $orderItemQty = $orderItem->getQtyOrdered();
             if ($orderItemDiscount && $orderItemQty) {
                 $totalDiscountAmount += $invoice->roundPrice($orderItemDiscount / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalDiscountAmount += $invoice->roundPrice($baseOrderItemDiscount / $orderItemQty * $item->getQty(), 'base', true);
                 $totalHiddenTax += $invoice->roundPrice($orderItemHiddenTax / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalHiddenTax += $invoice->roundPrice($baseOrderItemHiddenTax / $orderItemQty * $item->getQty(), 'base', true);
             }
         }
         $allowedBaseHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxInvoiced;
         $allowedHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxInvoiced;
         $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
         $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     }
     $invoice->setRewardpointsDiscount($totalDiscountAmount);
     $invoice->setRewardpointsBaseDiscount($baseTotalDiscountAmount);
     $invoice->setRewardpointsHiddenTaxAmount($totalHiddenTax);
     $invoice->setRewardpointsBaseHiddenTaxAmount($baseTotalHiddenTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount + $totalHiddenTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount + $baseTotalHiddenTax);
     return $this;
 }
Example #17
0
 /**
  * Collect invoice subtotal
  *
  * @param   Mage_Sales_Model_Order_Invoice $invoice
  * @return  Mage_Sales_Model_Order_Invoice_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     /// Below is the original magento code instead of marked lines
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     $totalWeeeDiscount = 0;
     $totalBaseWeeeDiscount = 0;
     $order = $invoice->getOrder();
     foreach ($invoice->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
         $totalWeeeDiscount += $item->getOrderItem()->getDiscountAppliedForWeeeTax();
         $totalBaseWeeeDiscount += $item->getOrderItem()->getBaseDiscountAppliedForWeeeTax();
     }
     ///// Changes!!! Deducted refunded amount from allowed subtotal
     $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() + $order->getSubtotalRefunded();
     $baseAllowedSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() + $order->getBaseSubtotalRefunded();
     $allowedSubtotalInclTax = $allowedSubtotal + $order->getHiddenTaxAmount() + $totalWeeeDiscount + $order->getTaxAmount() - $order->getTaxInvoiced() - $order->getHiddenTaxInvoiced() + $order->getTaxRefunded() + $order->getHiddenTaxRefunded();
     $baseAllowedSubtotalInclTax = $baseAllowedSubtotal + $order->getBaseHiddenTaxAmount() + $totalBaseWeeeDiscount + $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $order->getBaseHiddenTaxInvoiced() + $order->getBaseTaxRefunded() + $order->getBaseHiddenTaxRefunded();
     /**
      * Check if shipping tax calculation is included to current invoice.
      */
     $includeShippingTax = true;
     foreach ($invoice->getOrder()->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getShippingAmount() && !$previousInvoice->isCanceled()) {
             $includeShippingTax = false;
             break;
         }
     }
     if ($includeShippingTax) {
         $allowedSubtotalInclTax -= $order->getShippingTaxAmount();
         $baseAllowedSubtotalInclTax -= $order->getBaseShippingTaxAmount();
     } else {
         $allowedSubtotalInclTax += $order->getShippingHiddenTaxAmount();
         $baseAllowedSubtotalInclTax += $order->getBaseShippingHiddenTaxAmount();
     }
     if ($invoice->isLast()) {
         $subtotal = $allowedSubtotal;
         $baseSubtotal = $baseAllowedSubtotal;
         $subtotalInclTax = $allowedSubtotalInclTax;
         $baseSubtotalInclTax = $baseAllowedSubtotalInclTax;
     } else {
         $subtotal = min($allowedSubtotal, $subtotal);
         $baseSubtotal = min($baseAllowedSubtotal, $baseSubtotal);
         $subtotalInclTax = min($allowedSubtotalInclTax, $subtotalInclTax);
         $baseSubtotalInclTax = min($baseAllowedSubtotalInclTax, $baseSubtotalInclTax);
     }
     $invoice->setSubtotal($subtotal);
     $invoice->setBaseSubtotal($baseSubtotal);
     $invoice->setSubtotalInclTax($subtotalInclTax);
     $invoice->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() + $subtotal);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
 /**
  * Weee tax collector
  *
  * @param Mage_Sales_Model_Order_Invoice $invoice
  * @return Mage_Weee_Model_Total_Invoice_Weee
  */
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $store = $invoice->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     $weeeInclTax = 0;
     $baseWeeeInclTax = 0;
     foreach ($invoice->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         $orderItemQty = $orderItem->getQtyOrdered();
         if (!$orderItemQty || $orderItem->isDummy()) {
             continue;
         }
         $weeeTaxAmount = $item->getWeeeTaxAppliedAmount() * $item->getQty();
         $baseWeeeTaxAmount = $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
         $weeeTaxAmountInclTax = Mage::helper('weee')->getWeeeTaxInclTax($item) * $item->getQty();
         $baseWeeeTaxAmountInclTax = Mage::helper('weee')->getBaseWeeeTaxInclTax($item) * $item->getQty();
         $item->setWeeeTaxAppliedRowAmount($weeeTaxAmount);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeTaxAmount);
         $newApplied = array();
         $applied = Mage::helper('weee')->getApplied($item);
         foreach ($applied as $one) {
             $one['base_row_amount'] = $one['base_amount'] * $item->getQty();
             $one['row_amount'] = $one['amount'] * $item->getQty();
             $one['base_row_amount_incl_tax'] = $one['base_amount_incl_tax'] * $item->getQty();
             $one['row_amount_incl_tax'] = $one['amount_incl_tax'] * $item->getQty();
             $newApplied[] = $one;
         }
         Mage::helper('weee')->setApplied($item, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
         $totalTax += $weeeTaxAmount;
         $baseTotalTax += $baseWeeeTaxAmount;
         $weeeInclTax += $weeeTaxAmountInclTax;
         $baseWeeeInclTax += $baseWeeeTaxAmountInclTax;
     }
     /*
      * Add FPT to totals
      * Notice that we check restriction on allowed tax, because
      * a) for last invoice we don't need to collect FPT - it is automatically collected by subtotal/tax collector,
      * that adds whole remaining (not invoiced) subtotal/tax value, so fpt is automatically included into it
      * b) FPT tax is included into order subtotal/tax value, so after multiple invoices with partial item quantities
      * it can happen that other collector will take some FPT value from shared subtotal/tax order value
      */
     $order = $invoice->getOrder();
     if (Mage::helper('weee')->includeInSubtotal($store)) {
         $allowedSubtotal = $order->getSubtotal() - $order->getSubtotalInvoiced() - $invoice->getSubtotal();
         $allowedBaseSubtotal = $order->getBaseSubtotal() - $order->getBaseSubtotalInvoiced() - $invoice->getBaseSubtotal();
         $totalTax = min($allowedSubtotal, $totalTax);
         $baseTotalTax = min($allowedBaseSubtotal, $baseTotalTax);
         $invoice->setSubtotal($invoice->getSubtotal() + $totalTax);
         $invoice->setBaseSubtotal($invoice->getBaseSubtotal() + $baseTotalTax);
     } else {
         $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced() - $invoice->getTaxAmount();
         $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced() - $invoice->getBaseTaxAmount();
         $totalTax = min($allowedTax, $totalTax);
         $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
         $invoice->setTaxAmount($invoice->getTaxAmount() + $totalTax);
         $invoice->setBaseTaxAmount($invoice->getBaseTaxAmount() + $baseTotalTax);
     }
     if (!$invoice->isLast()) {
         $invoice->setSubtotalInclTax($invoice->getSubtotalInclTax() + $weeeInclTax);
         $invoice->setBaseSubtotalInclTax($invoice->getBaseSubtotalInclTax() + $baseWeeeInclTax);
     }
     $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #19
0
 public function collect(Mage_Sales_Model_Order_Invoice $invoice)
 {
     $order = $invoice->getOrder();
     if ($order->getCustomercreditDiscount() < 0.0001) {
         return;
     }
     $invoice->setBaseCustomercreditDiscount(0);
     $invoice->setCustomercreditDiscount(0);
     $totalDiscountInvoiced = 0;
     $totalBaseDiscountInvoiced = 0;
     $totalDiscountAmount = 0;
     $totalBaseDiscountAmount = 0;
     $totalHiddenTax = 0;
     $totalBaseHiddenTax = 0;
     $hiddenTaxInvoiced = 0;
     $baseHiddenTaxInvoiced = 0;
     $checkAddShipping = true;
     foreach ($order->getInvoiceCollection() as $previousInvoice) {
         if ($previousInvoice->getCustomercreditDiscount()) {
             $checkAddShipping = false;
             $totalBaseDiscountInvoiced += $previousInvoice->getBaseCustomercreditDiscount();
             $totalDiscountInvoiced += $previousInvoice->getCustomercreditDiscount();
             $hiddenTaxInvoiced += $previousInvoice->getCustomercreditHiddenTax();
             $baseHiddenTaxInvoiced += $previousInvoice->getBaseCustomercreditHiddenTax();
         }
     }
     if ($checkAddShipping) {
         $totalBaseDiscountAmount += $order->getBaseCustomercreditDiscountForShipping();
         $totalDiscountAmount += $order->getCustomercreditDiscountForShipping();
         $totalBaseHiddenTax += $order->getBaseCustomercreditShippingHiddenTax();
         $totalHiddenTax += $order->getCustomercreditShippingHiddenTax();
     }
     if ($invoice->isLast()) {
         $totalBaseDiscountAmount = $order->getBaseCustomercreditDiscount() - $totalBaseDiscountInvoiced;
         $totalDiscountAmount = $order->getCustomercreditDiscount() - $totalDiscountInvoiced;
         $totalHiddenTax = $order->getCustomercreditHiddenTax() - $hiddenTaxInvoiced;
         $totalBaseHiddenTax = $order->getBaseCustomercreditHiddenTax() - $baseHiddenTaxInvoiced;
     } else {
         foreach ($invoice->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $baseOrderItemCustomercreditDiscount = (double) $orderItem->getBaseCustomercreditDiscount();
             $orderItemCustomercreditDiscount = (double) $orderItem->getCustomercreditDiscount();
             $baseOrderItemHiddenTax = (double) $orderItem->getBaseCustomercreditHiddenTax();
             $orderItemHiddenTax = (double) $orderItem->getCustomercreditHiddenTax();
             $orderItemQty = $orderItem->getQtyOrdered();
             $invoiceItemQty = $item->getQty();
             if ($baseOrderItemCustomercreditDiscount && $orderItemQty) {
                 if (version_compare(Mage::getVersion(), '1.7.0.0', '>=')) {
                     $totalBaseDiscountAmount += $invoice->roundPrice($baseOrderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty, 'base', true);
                     $totalDiscountAmount += $invoice->roundPrice($orderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty, 'regular', true);
                     $totalHiddenTax += $invoice->roundPrice($orderItemHiddenTax / $orderItemQty * $invoiceItemQty, 'regular', true);
                     $totalBaseHiddenTax += $invoice->roundPrice($baseOrderItemHiddenTax / $orderItemQty * $invoiceItemQty, 'base', true);
                 } else {
                     $totalBaseDiscountAmount += $baseOrderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty;
                     $totalDiscountAmount += $orderItemCustomercreditDiscount / $orderItemQty * $invoiceItemQty;
                     $totalHiddenTax += $orderItemHiddenTax / $orderItemQty * $invoiceItemQty;
                     $totalBaseHiddenTax += $baseOrderItemHiddenTax / $orderItemQty * $invoiceItemQty;
                 }
             }
         }
     }
     $invoice->setBaseCustomercreditDiscount($totalBaseDiscountAmount);
     $invoice->setCustomercreditDiscount($totalDiscountAmount);
     $invoice->setBaseCustomercreditHiddenTax($totalBaseHiddenTax);
     $invoice->setCustomercreditHiddenTax($totalHiddenTax);
     $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $totalBaseDiscountAmount + $totalBaseHiddenTax);
     $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount + $totalHiddenTax);
 }