Example #1
0
 /**
  * This method produces a list of line item which are cleaned up. This means the amounts of the line items are rounded 
  * correctly. The line items only contains items with unique SKUs. If there is a difference between the expected sum and 
  * the actual sum an adjustment line item will be added. 
  * 
  * @param array $originalLineItems The line items to clean.
  * @param float $expectedSum The sum which should be met with this line items.
  * @param string $currencyCode The currency to use when the amounts are rounded.
  */
 public static function cleanupLineItems(array $originalLineItems, $expectedSum, $currencyCode)
 {
     $expectedSum = Customweb_Util_Currency::roundAmount($expectedSum, $currencyCode);
     $result = array();
     foreach ($originalLineItems as $lineItem) {
         $type = $lineItem->getType();
         $amount = $lineItem->getAmountIncludingTax();
         if ($type == Customweb_Payment_Authorization_IInvoiceItem::TYPE_DISCOUNT && $amount < 0) {
             $type = Customweb_Payment_Authorization_IInvoiceItem::TYPE_FEE;
             $amount = $amount * -1;
         }
         $result[] = new Customweb_Payment_Authorization_DefaultInvoiceItem($lineItem->getSku(), $lineItem->getName(), $lineItem->getTaxRate(), Customweb_Util_Currency::roundAmount($amount, $currencyCode), $lineItem->getQuantity(), $type, $lineItem->getOriginalSku());
     }
     $realSum = self::getTotalAmountIncludingTax($result);
     $diff = Customweb_Util_Currency::compareAmount($realSum, $expectedSum, $currencyCode);
     if ($diff > 0) {
         $amountDifferenceWithTax = $realSum - $expectedSum;
         $result[] = new Customweb_Payment_Authorization_DefaultInvoiceItem('rounding-adjustment', Customweb_I18n_Translation::__("Rounding Adjustment")->toString(), 0, Customweb_Util_Currency::roundAmount($amountDifferenceWithTax, $currencyCode), 1, Customweb_Payment_Authorization_IInvoiceItem::TYPE_DISCOUNT);
     } else {
         if ($diff < 0) {
             $amountDifferenceWithTax = $expectedSum - $realSum;
             $result[] = new Customweb_Payment_Authorization_DefaultInvoiceItem('rounding-adjustment', Customweb_I18n_Translation::__("Rounding Adjustment")->toString(), 0, Customweb_Util_Currency::roundAmount($amountDifferenceWithTax, $currencyCode), 1, Customweb_Payment_Authorization_IInvoiceItem::TYPE_FEE);
         }
     }
     return self::ensureUniqueSku($result);
 }
 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;
 }