public function collect(Mage_Sales_Model_Order_Invoice $invoice) { $_result = parent::collect($invoice); $baseTotal = $invoice->getBaseGrandTotal(); $total = $invoice->getGrandTotal(); $baseTotalGiftcardAmount = 0; $totalGiftcardAmount = 0; $invoiceGiftCards = array(); if (null === $invoice->getId()) { $quoteGiftcardsItems = Mage::helper('aw_giftcard/totals')->getQuoteGiftCards($invoice->getOrder()->getQuoteId()); foreach ($quoteGiftcardsItems as $quoteCard) { $_baseGiftcardAmount = $quoteCard->getBaseGiftcardAmount(); $_giftcardAmount = $quoteCard->getGiftcardAmount(); $invoices = Mage::helper('aw_giftcard/totals')->getAllInvoicesForGiftCard($invoice->getOrder()->getId(), $quoteCard->getGiftcardId()); if (count($invoices) > 0) { foreach ($invoices as $giftcardInvoice) { $_baseGiftcardAmount -= $giftcardInvoice->getBaseGiftcardAmount(); $_giftcardAmount -= $giftcardInvoice->getGiftcardAmount(); } } $baseCardUsedAmount = $_baseGiftcardAmount; if ($_baseGiftcardAmount >= $baseTotal) { $baseCardUsedAmount = $baseTotal; } $baseTotal -= $baseCardUsedAmount; $cardUsedAmount = $_giftcardAmount; if ($_giftcardAmount >= $total) { $cardUsedAmount = $total; } $total -= $cardUsedAmount; $_baseGiftcardAmount = round($baseCardUsedAmount, 4); $_giftcardAmount = round($cardUsedAmount, 4); $baseTotalGiftcardAmount += $_baseGiftcardAmount; $totalGiftcardAmount += $_giftcardAmount; $_invoiceCard = new Varien_Object($quoteCard->getData()); $_invoiceCard->setBaseGiftcardAmount($_baseGiftcardAmount)->setGiftcardAmount($_giftcardAmount); array_push($invoiceGiftCards, $_invoiceCard); } } if (null !== $invoice->getId() && $invoice->getAwGiftCards()) { $invoiceGiftCards = $invoice->getAwGiftCards(); foreach ($invoiceGiftCards as $invoiceCard) { $baseTotalGiftcardAmount += $invoiceCard->getBaseGiftcardAmount(); $totalGiftcardAmount += $invoiceCard->getGiftcardAmount(); } } $invoice->setAwGiftCards($invoiceGiftCards)->setBaseAwGiftCardsAmount($baseTotalGiftcardAmount)->setAwGiftCardsAmount($totalGiftcardAmount)->setBaseGrandTotal($invoice->getBaseGrandTotal() - $baseTotalGiftcardAmount)->setGrandTotal($invoice->getGrandTotal() - $totalGiftcardAmount); return $_result; }
protected function _attachTotals(Mage_Sales_Model_Order_Creditmemo $creditMemo, $mode) { $total = $creditMemo->getGrandTotal(); $baseTotal = $creditMemo->getBaseGrandTotal(); $order = $creditMemo->getOrder(); $order->setForcedCanCreditmemo(false); if ($mode == AW_Giftcard_Model_Source_Giftcard_Config_Refund::ALLOW_AFTER_REAL_MONEY_VALUE || $mode == AW_Giftcard_Model_Source_Giftcard_Config_Refund::NOT_ALLOW_VALUE) { $_needBaseMoneyToRefund = abs($order->getBaseTotalPaid() - $order->getBaseTotalRefunded()); $_needMoneyToRefund = abs($order->getTotalPaid() - $order->getTotalRefunded()); $total = $creditMemo->getGrandTotal() - $_needMoneyToRefund; $baseTotal = $creditMemo->getBaseGrandTotal() - $_needBaseMoneyToRefund; } if ($total <= 0 || $baseTotal <= 0) { return $this; } $baseTotalGiftcardAmount = 0; $totalGiftcardAmount = 0; $creditmemoGiftCards = array(); if (null === $creditMemo->getId()) { $invoiceGiftCards = Mage::helper('aw_giftcard/totals')->getInvoicedGiftCardsByOrderId($creditMemo->getOrder()->getId()); foreach ($invoiceGiftCards as $invoiceCard) { $_baseGiftcardAmount = $invoiceCard->getBaseGiftcardAmount(); $_giftcardAmount = $invoiceCard->getGiftcardAmount(); $creditmemoItems = Mage::helper('aw_giftcard/totals')->getAllCreditmemoForGiftCard($creditMemo->getOrder()->getId(), $invoiceCard->getGiftcardId()); if (count($creditmemoItems) > 0) { foreach ($creditmemoItems as $creditmemoGiftcard) { $_baseGiftcardAmount -= $creditmemoGiftcard->getBaseGiftcardAmount(); $_giftcardAmount -= $creditmemoGiftcard->getGiftcardAmount(); } } $baseCardUsedAmount = $_baseGiftcardAmount; if ($_baseGiftcardAmount >= $baseTotal) { $baseCardUsedAmount = $baseTotal; } $baseTotal -= $baseCardUsedAmount; $cardUsedAmount = $_giftcardAmount; if ($_giftcardAmount >= $total) { $cardUsedAmount = $total; } $total -= $cardUsedAmount; $_baseGiftcardAmount = round($baseCardUsedAmount, 4); $_giftcardAmount = round($cardUsedAmount, 4); $baseTotalGiftcardAmount += $_baseGiftcardAmount; $totalGiftcardAmount += $_giftcardAmount; $_creditmemoCard = new Varien_Object($invoiceCard->getData()); $_creditmemoCard->setBaseGiftcardAmount($_baseGiftcardAmount)->setGiftcardAmount($_giftcardAmount); array_push($creditmemoGiftCards, $_creditmemoCard); } } if (null !== $creditMemo->getId() && $creditMemo->getAwGiftCards()) { $creditmemoGiftCards = $creditMemo->getAwGiftCards(); foreach ($creditmemoGiftCards as $creditmemoCard) { $baseTotalGiftcardAmount += $creditmemoCard->getBaseGiftcardAmount(); $totalGiftcardAmount += $creditmemoCard->getGiftcardAmount(); } } if (count($creditmemoGiftCards) > 0) { $creditMemo->setAllowZeroGrandTotal(true); } $creditMemo->setAwGiftCards($creditmemoGiftCards)->setBaseAwGiftCardsAmount($baseTotalGiftcardAmount)->setAwGiftCardsAmount($totalGiftcardAmount)->setBaseGrandTotal($creditMemo->getBaseGrandTotal() - $baseTotalGiftcardAmount)->setGrandTotal($creditMemo->getGrandTotal() - $totalGiftcardAmount); return $this; }