/** * This method generates a set of line items, which represents the delta of the amount change. * * * @param Customweb_Payment_Authorization_IInvoiceItem[] $originalLineItems List of line items on which the delta based on. * @param float $amount The reduction amount. * @return Customweb_Payment_Authorization_IInvoiceItem[] The set of line items, which represents the delta. */ public static function getItemsByReductionAmount(array $originalLineItems, $amount, $currencyCode) { if (count($originalLineItems) <= 0) { throw new Exception("No line items provided."); } $total = self::getTotalAmountIncludingTax($originalLineItems); $factor = $amount / $total; $appliedTotal = 0; $newItems = array(); foreach ($originalLineItems as $item) { /* @var $item Customweb_Payment_Authorization_IInvoiceItem */ $newAmount = Customweb_Util_Currency::roundAmount($item->getAmountIncludingTax() * $factor, $currencyCode); $newItem = new Customweb_Payment_Authorization_DefaultInvoiceItem($item->getSku(), $item->getName(), $item->getTaxRate(), $newAmount, $item->getQuantity(), $item->getType(), $item->getOriginalSku()); $newItems[] = $newItem; if ($item->getType() == Customweb_Payment_Authorization_DefaultInvoiceItem::TYPE_DISCOUNT) { $appliedTotal -= $newAmount; } else { $appliedTotal += $newAmount; } } // Fix rounding error $roundingDifference = $amount - $appliedTotal; $item = $newItems[0]; $newAmount = $item->getAmountIncludingTax() + $roundingDifference; $newItems[0] = new Customweb_Payment_Authorization_DefaultInvoiceItem($item->getSku(), $item->getName(), $item->getTaxRate(), $newAmount, $item->getQuantity(), $item->getType(), $item->getOriginalSku()); return $newItems; }
public function getAdjustmentItem(array $items, $orderAmount, $currency) { $totalAmount = 0; foreach ($items as $item) { if ($item->getType() == Customweb_Payment_Authorization_IInvoiceItem::TYPE_DISCOUNT) { $totalAmount -= $item->getAmountIncludingTax(); } else { $totalAmount += $item->getAmountIncludingTax(); } } if (Customweb_Util_Currency::compareAmount($totalAmount, $orderAmount, $currency) > 0) { return new Customweb_Payment_Authorization_DefaultInvoiceItem('adjustment_disount', 'Adjustment Discount', 0, (double) ($totalAmount - $orderAmount), 1, Customweb_Payment_Authorization_IInvoiceItem::TYPE_DISCOUNT); } elseif (Customweb_Util_Currency::compareAmount($totalAmount, $orderAmount, $currency) < 0) { return new Customweb_Payment_Authorization_DefaultInvoiceItem('adjustment_fee', 'Adjustment Fee', 0, (double) ($orderAmount - $totalAmount), 1, Customweb_Payment_Authorization_IInvoiceItem::TYPE_FEE); } return null; }
public function refundByLineItems($items, $close = false, $additionalMessage = '') { $this->refundByLineItemsDry($items, $close, $additionalMessage); $amount = Customweb_Util_Invoice::getTotalAmountIncludingTax($items); $historyMessage = Customweb_I18n_Translation::__("A refund was added over !amount.", array('!amount' => Customweb_Util_Currency::formatAmount($amount, $this->getCurrencyCode()))); if (!empty($additionalMessage)) { $historyMessage .= ' (' . $additionalMessage . ')'; } $refundItem = $this->createRefundItem($amount); $refundItem->setRefundItems($items); $this->createHistoryItem($historyMessage, Customweb_Payment_Authorization_ITransactionHistoryItem::ACTION_REFUND); if (Customweb_Util_Currency::compareAmount($this->getRefundedTotalAmount(), $this->getCapturedAmount(), $this->getCurrencyCode()) >= 0) { $close = true; } $this->refundClosed = $close; return $refundItem; }