public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $invoice->setDiscountAmount(0); $invoice->setBaseDiscountAmount(0); $totalDiscountAmount = 0; $baseTotalDiscountAmount = 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 ($invoice->getOrder()->getInvoiceCollection() as $previusInvoice) { if ($previusInvoice->getDiscountAmount()) { $addShippingDicount = false; } } if ($addShippingDicount) { $totalDiscountAmount = $totalDiscountAmount + $invoice->getOrder()->getShippingDiscountAmount(); $baseTotalDiscountAmount = $baseTotalDiscountAmount + $invoice->getOrder()->getBaseShippingDiscountAmount(); } /** @var $item Mage_Sales_Model_Order_Invoice_Item */ foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); if ($orderItem->isDummy()) { continue; } $orderItemDiscount = (double) $orderItem->getDiscountAmount(); $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountAmount(); $orderItemQty = $orderItem->getQtyOrdered(); if ($orderItemDiscount && $orderItemQty) { /** * Resolve rounding problems * * We dont want to include the weee discount amount as the right amount * is added when calculating the taxes. * * Also the subtotal is without weee */ $discount = $orderItemDiscount - $orderItem->getDiscountInvoiced(); $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountInvoiced(); if (!$item->isLast()) { $activeQty = $orderItemQty - $orderItem->getQtyInvoiced(); $discount = $invoice->roundPrice($discount / $activeQty * $item->getQty(), 'regular', true); $baseDiscount = $invoice->roundPrice($baseDiscount / $activeQty * $item->getQty(), 'base', true); } $item->setDiscountAmount($discount); $item->setBaseDiscountAmount($baseDiscount); $totalDiscountAmount += $discount; $baseTotalDiscountAmount += $baseDiscount; } } $invoice->setDiscountAmount(-$totalDiscountAmount); $invoice->setBaseDiscountAmount(-$baseTotalDiscountAmount); $invoice->setGrandTotal($invoice->getGrandTotal() - $totalDiscountAmount); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalDiscountAmount); return $this; }
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; }
/** * Collect invoice tax amount * * @param Mage_Sales_Model_Order_Invoice $invoice * @return Mage_Sales_Model_Order_Invoice_Total_Tax */ public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $totalTax = 0; $baseTotalTax = 0; $totalHiddenTax = 0; $baseTotalHiddenTax = 0; $order = $invoice->getOrder(); /** @var $item Mage_Sales_Model_Order_Invoice_Item */ foreach ($invoice->getAllItems() as $item) { $orderItem = $item->getOrderItem(); $orderItemQty = $orderItem->getQtyOrdered(); if (($orderItem->getTaxAmount() || $orderItem->getHiddenTaxAmount()) && $orderItemQty) { if ($item->getOrderItem()->isDummy()) { continue; } /** * Resolve rounding problems */ $tax = $orderItem->getTaxAmount() - $orderItem->getTaxInvoiced(); $baseTax = $orderItem->getBaseTaxAmount() - $orderItem->getBaseTaxInvoiced(); $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxInvoiced(); $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxInvoiced(); if (!$item->isLast()) { $availableQty = $orderItemQty - $orderItem->getQtyInvoiced(); $tax = $invoice->roundPrice($tax / $availableQty * $item->getQty()); $baseTax = $invoice->roundPrice($baseTax / $availableQty * $item->getQty(), 'base'); $hiddenTax = $invoice->roundPrice($hiddenTax / $availableQty * $item->getQty()); $baseHiddenTax = $invoice->roundPrice($baseHiddenTax / $availableQty * $item->getQty(), 'base'); } $item->setTaxAmount($tax); $item->setBaseTaxAmount($baseTax); $item->setHiddenTaxAmount($hiddenTax); $item->setBaseHiddenTaxAmount($baseHiddenTax); $totalTax += $tax; $baseTotalTax += $baseTax; $totalHiddenTax += $hiddenTax; $baseTotalHiddenTax += $baseHiddenTax; } } if ($this->_canIncludeShipping($invoice)) { $totalTax += $order->getShippingTaxAmount(); $baseTotalTax += $order->getBaseShippingTaxAmount(); $totalHiddenTax += $order->getShippingHiddenTaxAmount(); $baseTotalHiddenTax += $order->getBaseShippingHiddenTaxAmount(); $invoice->setShippingTaxAmount($order->getShippingTaxAmount()); $invoice->setBaseShippingTaxAmount($order->getBaseShippingTaxAmount()); $invoice->setShippingHiddenTaxAmount($order->getShippingHiddenTaxAmount()); $invoice->setBaseShippingHiddenTaxAmount($order->getBaseShippingHiddenTaxAmount()); } $allowedTax = $order->getTaxAmount() - $order->getTaxInvoiced(); $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxInvoiced(); $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount() - $order->getHiddenTaxInvoiced() - $order->getShippingHiddenTaxInvoiced(); $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount() - $order->getBaseHiddenTaxInvoiced() - $order->getBaseShippingHiddenTaxInvoiced(); if ($invoice->isLast()) { $totalTax = $allowedTax; $baseTotalTax = $allowedBaseTax; $totalHiddenTax = $allowedHiddenTax; $baseTotalHiddenTax = $allowedBaseHiddenTax; } else { $totalTax = min($allowedTax, $totalTax); $baseTotalTax = min($allowedBaseTax, $baseTotalTax); $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax); $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax); } $invoice->setTaxAmount($totalTax); $invoice->setBaseTaxAmount($baseTotalTax); $invoice->setHiddenTaxAmount($totalHiddenTax); $invoice->setBaseHiddenTaxAmount($baseTotalHiddenTax); $invoice->setGrandTotal($invoice->getGrandTotal() + $totalTax + $totalHiddenTax); $invoice->setBaseGrandTotal($invoice->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax); 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; } $weeeRowDiscountAmount = $orderItem->getDiscountAppliedForWeeeTax(); $weeeDiscountAmount = $invoice->roundPrice($weeeRowDiscountAmount / $orderItemQty * $item->getQty(), 'regular', true); $baseWeeeRowDiscountAmount = $orderItem->getBaseDiscountAppliedForWeeeTax(); $baseWeeeDiscountAmount = $invoice->roundPrice($baseWeeeRowDiscountAmount / $orderItemQty * $item->getQty(), 'base', true); $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(); $one['weee_discount'] = $weeeDiscountAmount; $one['base_weee_discount'] = $baseWeeeDiscountAmount; $newApplied[] = $one; } Mage::helper('weee')->setApplied($item, $newApplied); $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty()); $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty()); $totalTax += $weeeTaxAmount - $weeeDiscountAmount; $baseTotalTax += $baseWeeeTaxAmount - $baseWeeeDiscountAmount; $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; }
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); }
/** * 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; }