public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     $allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded();
     $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();
     /**
      * Check if shipping amount was specified (from invoice or another source).
      * Using has magic method to allow setting 0 as shipping amount.
      */
     if ($creditmemo->hasBaseShippingAmount()) {
         $baseShippingAmount = Mage::app()->getStore()->roundPrice($creditmemo->getBaseShippingAmount());
         if ($baseShippingAmount < $baseAllowedAmount) {
             $shippingAmount = $allowedAmount * $baseShippingAmount / $baseAllowedAmount;
             $shippingAmount = Mage::app()->getStore()->roundPrice($shippingAmount);
         } elseif ($baseShippingAmount == $baseAllowedAmount) {
             $shippingAmount = $allowedAmount;
         } else {
             $baseAllowedAmount = $order->formatBasePrice($baseAllowedAmount);
             Mage::throwException(Mage::helper('sales')->__('Maximum shipping amount allowed to refound is: %s', $baseAllowedAmount));
         }
     } else {
         $baseShippingAmount = $baseAllowedAmount;
         $shippingAmount = $allowedAmount;
     }
     $creditmemo->setShippingAmount($shippingAmount);
     $creditmemo->setBaseShippingAmount($baseShippingAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shippingAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShippingAmount);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $shippingTaxAmount = 0;
     $baseShippingTaxAmount = 0;
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItemTax = $item->getOrderItem()->getTaxAmount();
         $baseOrderItemTax = $item->getOrderItem()->getBaseTaxAmount();
         $orderItemQty = $item->getOrderItem()->getQtyOrdered();
         if ($orderItemTax && $orderItemQty) {
             $tax = $orderItemTax * $item->getQty() / $orderItemQty;
             $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty;
             $tax = $creditmemo->getStore()->roundPrice($tax);
             $baseTax = $creditmemo->getStore()->roundPrice($baseTax);
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
         }
     }
     if ($invoice = $creditmemo->getInvoice()) {
         $totalTax += $invoice->getShippingTaxAmount();
         $baseTotalTax += $invoice->getBaseShippingTaxAmount();
         $creditmemo->setShippingTaxAmount($invoice->getShippingTaxAmount());
         $creditmemo->setBaseShippingTaxAmount($invoice->getBaseShippingTaxAmount());
     } else {
         $shippingAmount = $creditmemo->getOrder()->getBaseShippingAmount();
         $shippingRefundedAmount = $creditmemo->getOrder()->getBaseShippingRefunded();
         $shippingTaxAmount = 0;
         $baseShippingTaxAmount = 0;
         if ($shippingAmount - $shippingRefundedAmount > $creditmemo->getShippingAmount()) {
             $shippingTaxAmount = $creditmemo->getShippingAmount() * ($creditmemo->getOrder()->getShippingTaxAmount() / $shippingAmount);
             $baseShippingTaxAmount = $creditmemo->getBaseShippingAmount() * ($creditmemo->getOrder()->getBaseShippingTaxAmount() / $shippingAmount);
             $shippingTaxAmount = $creditmemo->getStore()->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->getStore()->roundPrice($baseShippingTaxAmount);
         } elseif ($shippingAmount - $shippingRefundedAmount == $creditmemo->getShippingAmount()) {
             $shippingTaxAmount = $creditmemo->getOrder()->getShippingTaxAmount() - $creditmemo->getOrder()->getShippingTaxRefunded();
             $baseShippingTaxAmount = $creditmemo->getOrder()->getBaseShippingTaxAmount() - $creditmemo->getOrder()->getBaseShippingTaxRefunded();
         }
         $totalTax += $shippingTaxAmount;
         $baseTotalTax += $baseShippingTaxAmount;
     }
     $tmpBaseTotalTax = $baseTotalTax - ($creditmemo->getOrder()->getBaseTaxRefunded() - $creditmemo->getOrder()->getBaseShippingTaxRefunded());
     if ($tmpBaseTotalTax < 0) {
         $baseTotalTax = 0;
         $totalTax = 0;
     }
     $creditmemo->setTaxAmount($totalTax);
     $creditmemo->setBaseTaxAmount($baseTotalTax);
     $creditmemo->setShippingTaxAmount($shippingTaxAmount);
     $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #3
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     $allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded();
     $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();
     $shipping = $order->getShippingAmount();
     $baseShipping = $order->getBaseShippingAmount();
     $shippingInclTax = $order->getShippingInclTax();
     $baseShippingInclTax = $order->getBaseShippingInclTax();
     $isShippingInclTax = Mage::getSingleton('tax/config')->displaySalesShippingInclTax($order->getStoreId());
     /**
      * Check if shipping amount was specified (from invoice or another source).
      * Using has magic method to allow setting 0 as shipping amount.
      */
     if ($creditmemo->hasBaseShippingAmount()) {
         $baseShippingAmount = Mage::app()->getStore()->roundPrice($creditmemo->getBaseShippingAmount());
         if ($isShippingInclTax && $baseShippingInclTax != 0) {
             $part = $baseShippingAmount / $baseShippingInclTax;
             $shippingInclTax = Mage::app()->getStore()->roundPrice($shippingInclTax * $part);
             $baseShippingInclTax = $baseShippingAmount;
             $baseShippingAmount = Mage::app()->getStore()->roundPrice($baseShipping * $part);
         }
         if ($baseShippingAmount <= $baseAllowedAmount) {
             if ($baseShipping != 0) {
                 $shipping = $shipping * $baseShippingAmount / $baseShipping;
             }
             $shipping = Mage::app()->getStore()->roundPrice($shipping);
             $baseShipping = $baseShippingAmount;
         } else {
             $baseAllowedAmount = $order->formatBasePrice($baseAllowedAmount);
             Mage::throwException(Mage::helper('sales')->__('Maximum shipping amount allowed to refund is: %s', $baseAllowedAmount));
         }
     } else {
         if ($baseShipping != 0) {
             $shippingInclTax = Mage::app()->getStore()->roundPrice($shippingInclTax * $allowedAmount / $shipping);
             $baseShippingInclTax = Mage::app()->getStore()->roundPrice($baseShippingInclTax * $baseAllowedAmount / $baseShipping);
         }
         $shipping = $allowedAmount;
         $baseShipping = $baseAllowedAmount;
     }
     $creditmemo->setShippingAmount($shipping);
     $creditmemo->setBaseShippingAmount($baseShipping);
     $creditmemo->setShippingInclTax($shippingInclTax);
     $creditmemo->setBaseShippingInclTax($baseShippingInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shipping);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShipping);
     return $this;
 }
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $grandTotal = $creditmemo->getGrandTotal();
     $baseGrandTotal = $creditmemo->getBaseGrandTotal();
     $grandTotal += $creditmemo->getShippingAmount();
     $baseGrandTotal += $creditmemo->getBaseShippingAmount();
     $grandTotal += $creditmemo->getAdjustmentPositive();
     $baseGrandTotal += $creditmemo->getBaseAdjustmentPositive();
     $grandTotal -= $creditmemo->getAdjustmentNegative();
     $baseGrandTotal -= $creditmemo->getBaseAdjustmentNegative();
     $creditmemo->setGrandTotal($grandTotal);
     $creditmemo->setBaseGrandTotal($baseGrandTotal);
     $creditmemo->setAdjustment($creditmemo->getAdjustmentPositive() - $creditmemo->getAdjustmentNegative());
     $creditmemo->setBaseAdjustment($creditmemo->getBaseAdjustmentPositive() - $creditmemo->getBaseAdjustmentNegative());
     return $this;
 }
Example #5
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setDiscountAmount(0);
     $creditmemo->setBaseDiscountAmount(0);
     $order = $creditmemo->getOrder();
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Calculate how much shipping discount should be applied
      * basing on how much shipping should be refunded.
      */
     $baseShippingAmount = $creditmemo->getBaseShippingAmount();
     if ($baseShippingAmount) {
         $baseShippingDiscount = $baseShippingAmount * $order->getBaseShippingDiscountAmount() / $order->getBaseShippingAmount();
         $shippingDiscount = $order->getShippingAmount() * $baseShippingDiscount / $order->getBaseShippingAmount();
         $totalDiscountAmount = $totalDiscountAmount + $shippingDiscount;
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $baseShippingDiscount;
     }
     /** @var $item Mage_Sales_Model_Order_Invoice_Item */
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemDiscount = (double) $orderItem->getDiscountInvoiced();
         $baseOrderItemDiscount = (double) $orderItem->getBaseDiscountInvoiced();
         $orderItemQty = $orderItem->getQtyInvoiced();
         if ($orderItemDiscount && $orderItemQty) {
             $discount = $orderItemDiscount - $orderItem->getDiscountRefunded();
             $baseDiscount = $baseOrderItemDiscount - $orderItem->getBaseDiscountRefunded();
             if (!$item->isLast()) {
                 $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
                 $discount = $creditmemo->roundPrice($discount / $availableQty * $item->getQty(), 'regular', true);
                 $baseDiscount = $creditmemo->roundPrice($baseDiscount / $availableQty * $item->getQty(), 'base', true);
             }
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
         }
     }
     $creditmemo->setDiscountAmount(-$totalDiscountAmount);
     $creditmemo->setBaseDiscountAmount(-$baseTotalDiscountAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalDiscountAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #6
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setDiscountAmount(0);
     $creditmemo->setBaseDiscountAmount(0);
     $order = $creditmemo->getOrder();
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     /**
      * Calculate how much shipping discount should be applied
      * basing on how much shipping should be refunded.
      */
     $baseShippingAmount = $creditmemo->getBaseShippingAmount();
     if ($baseShippingAmount) {
         $baseShippingDiscount = $baseShippingAmount * $order->getBaseShippingDiscountAmount() / $order->getBaseShippingAmount();
         $shippingDiscount = $order->getShippingAmount() * $baseShippingDiscount / $order->getBaseShippingAmount();
         $totalDiscountAmount = $totalDiscountAmount + $shippingDiscount;
         $baseTotalDiscountAmount = $baseTotalDiscountAmount + $baseShippingDiscount;
     }
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItemDiscount = (double) $item->getOrderItem()->getDiscountAmount();
         $baseOrderItemDiscount = (double) $item->getOrderItem()->getBaseDiscountAmount();
         $orderItemQty = $item->getOrderItem()->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             $discount = $orderItemDiscount * $item->getQty() / $orderItemQty;
             $baseDiscount = $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
             $discount = $creditmemo->getStore()->roundPrice($discount);
             $baseDiscount = $creditmemo->getStore()->roundPrice($baseDiscount);
             $item->setDiscountAmount($discount);
             $item->setBaseDiscountAmount($baseDiscount);
             $totalDiscountAmount += $discount;
             $baseTotalDiscountAmount += $baseDiscount;
         }
     }
     $creditmemo->setDiscountAmount($totalDiscountAmount);
     $creditmemo->setBaseDiscountAmount($baseTotalDiscountAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalDiscountAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #7
0
 /**
  * Collects the total tax for the credit memo
  *
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return Mage_Sales_Model_Order_Creditmemo_Total_Tax
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $shippingTaxAmount = 0;
     $baseShippingTaxAmount = 0;
     $totalTax = 0;
     $baseTotalTax = 0;
     $totalHiddenTax = 0;
     $baseTotalHiddenTax = 0;
     $weeeTaxAmount = 0;
     $baseWeeeTaxAmount = 0;
     $order = $creditmemo->getOrder();
     /** @var $item Mage_Sales_Model_Order_Creditmemo_Item */
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemTax = $orderItem->getTaxInvoiced();
         $baseOrderItemTax = $orderItem->getBaseTaxInvoiced();
         $orderItemHiddenTax = $orderItem->getHiddenTaxInvoiced();
         $baseOrderItemHiddenTax = $orderItem->getBaseHiddenTaxInvoiced();
         $orderItemQty = $orderItem->getQtyInvoiced();
         if (($orderItemTax || $orderItemHiddenTax) && $orderItemQty) {
             /**
              * Check item tax amount
              */
             $tax = $orderItemTax - $orderItem->getTaxRefunded();
             $baseTax = $baseOrderItemTax - $orderItem->getTaxRefunded();
             $hiddenTax = $orderItemHiddenTax - $orderItem->getHiddenTaxRefunded();
             $baseHiddenTax = $baseOrderItemHiddenTax - $orderItem->getBaseHiddenTaxRefunded();
             if (!$item->isLast()) {
                 $availableQty = $orderItemQty - $orderItem->getQtyRefunded();
                 $tax = $creditmemo->roundPrice($tax / $availableQty * $item->getQty());
                 $baseTax = $creditmemo->roundPrice($baseTax / $availableQty * $item->getQty(), 'base');
                 $hiddenTax = $creditmemo->roundPrice($hiddenTax / $availableQty * $item->getQty());
                 $baseHiddenTax = $creditmemo->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;
         }
     }
     $invoice = $creditmemo->getInvoice();
     if ($invoice) {
         //recalculate tax amounts in case if refund shipping value was changed
         if ($order->getBaseShippingAmount() && $creditmemo->getBaseShippingAmount()) {
             $taxFactor = $creditmemo->getBaseShippingAmount() / $order->getBaseShippingAmount();
             $shippingTaxAmount = $invoice->getShippingTaxAmount() * $taxFactor;
             $baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount() * $taxFactor;
             $totalHiddenTax += $invoice->getShippingHiddenTaxAmount() * $taxFactor;
             $baseTotalHiddenTax += $invoice->getBaseShippingHiddenTaxAmount() * $taxFactor;
             $shippingHiddenTaxAmount = $invoice->getShippingHiddenTaxAmount() * $taxFactor;
             $baseShippingHiddenTaxAmount = $invoice->getBaseShippingHiddenTaxAmount() * $taxFactor;
             $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
             $totalHiddenTax = $creditmemo->roundPrice($totalHiddenTax);
             $baseTotalHiddenTax = $creditmemo->roundPrice($baseTotalHiddenTax, 'base');
             $shippingHiddenTaxAmount = $creditmemo->roundPrice($shippingHiddenTaxAmount);
             $baseShippingHiddenTaxAmount = $creditmemo->roundPrice($baseShippingHiddenTaxAmount, 'base');
             $totalTax += $shippingTaxAmount;
             $baseTotalTax += $baseShippingTaxAmount;
         }
     } else {
         $orderShippingAmount = $order->getShippingAmount();
         $baseOrderShippingAmount = $order->getBaseShippingAmount();
         $orderShippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount();
         $baseOrderShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount();
         $baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
         $baseOrderShippingHiddenTaxRefunded = $order->getBaseShippingHiddenTaxRefunded();
         $shippingTaxAmount = 0;
         $baseShippingTaxAmount = 0;
         $shippingHiddenTaxAmount = 0;
         $baseShippingHiddenTaxAmount = 0;
         $shippingDelta = $baseOrderShippingAmount - $baseOrderShippingRefundedAmount;
         if ($shippingDelta > $creditmemo->getBaseShippingAmount()) {
             $part = $creditmemo->getShippingAmount() / $orderShippingAmount;
             $basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
             $shippingTaxAmount = $order->getShippingTaxAmount() * $part;
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() * $basePart;
             $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount() * $part;
             $baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount() * $basePart;
             $shippingTaxAmount = $creditmemo->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->roundPrice($baseShippingTaxAmount, 'base');
             $shippingHiddenTaxAmount = $creditmemo->roundPrice($shippingHiddenTaxAmount);
             $baseShippingHiddenTaxAmount = $creditmemo->roundPrice($baseShippingHiddenTaxAmount, 'base');
         } elseif ($shippingDelta == $creditmemo->getBaseShippingAmount()) {
             $shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
             $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount() - $order->getShippingHiddenTaxRefunded();
             $baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount() - $order->getBaseShippingHiddenTaxRefunded();
         }
         $totalTax += $shippingTaxAmount;
         $baseTotalTax += $baseShippingTaxAmount;
         $totalHiddenTax += $shippingHiddenTaxAmount;
         $baseTotalHiddenTax += $baseShippingHiddenTaxAmount;
     }
     $allowedTax = $order->getTaxInvoiced() - $order->getTaxRefunded() - $creditmemo->getTaxAmount();
     $allowedBaseTax = $order->getBaseTaxInvoiced() - $order->getBaseTaxRefunded() - $creditmemo->getBaseTaxAmount();
     $allowedHiddenTax = $order->getHiddenTaxInvoiced() + $order->getShippingHiddenTaxAmount() - $order->getHiddenTaxRefunded() - $order->getShippingHiddenTaxRefunded();
     $allowedBaseHiddenTax = $order->getBaseHiddenTaxInvoiced() + $order->getBaseShippingHiddenTaxAmount() - $order->getBaseHiddenTaxRefunded() - $order->getBaseShippingHiddenTaxRefunded();
     $totalTax = min($allowedTax, $totalTax);
     $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
     $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
     $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
     $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
     $creditmemo->setHiddenTaxAmount($totalHiddenTax);
     $creditmemo->setBaseHiddenTaxAmount($baseTotalHiddenTax);
     $creditmemo->setShippingTaxAmount($shippingTaxAmount);
     $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalHiddenTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
     return $this;
 }
Example #8
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     $allowedAmount = $order->getShippingAmount() - $order->getShippingRefunded();
     $baseAllowedAmount = $order->getBaseShippingAmount() - $order->getBaseShippingRefunded();
     $shipping = $order->getShippingAmount();
     $baseShipping = $order->getBaseShippingAmount();
     $shippingInclTax = $order->getShippingInclTax();
     $baseShippingInclTax = $order->getBaseShippingInclTax();
     $isShippingInclTax = Mage::getSingleton('tax/config')->displaySalesShippingInclTax($order->getStoreId());
     /**
      * Check if shipping amount was specified (from invoice or another source).
      * Using has magic method to allow setting 0 as shipping amount.
      */
     if ($creditmemo->hasBaseShippingAmount()) {
         $baseShippingAmount = Mage::app()->getStore()->roundPrice($creditmemo->getBaseShippingAmount());
         if ($isShippingInclTax && $baseShippingInclTax != 0) {
             $part = $baseShippingAmount / $baseShippingInclTax;
             $shippingInclTax = Mage::app()->getStore()->roundPrice($shippingInclTax * $part);
             $baseShippingInclTax = $baseShippingAmount;
             $baseShippingAmount = Mage::app()->getStore()->roundPrice($baseShipping * $part);
         }
         /*
          * Rounded allowed shipping refund amount is the highest acceptable shipping refund amount.
          * Shipping refund amount shouldn't cause errors, if it doesn't exceed that limit.
          * Note: ($x < $y + 0.0001) means ($x <= $y) for floats
          */
         if ($baseShippingAmount < Mage::app()->getStore()->roundPrice($baseAllowedAmount) + 0.0001) {
             /*
              * Shipping refund amount should be equated to allowed refund amount,
              * if it exceeds that limit.
              * Note: ($x > $y - 0.0001) means ($x >= $y) for floats
              */
             if ($baseShippingAmount > $baseAllowedAmount - 0.0001) {
                 $shipping = $allowedAmount;
                 $baseShipping = $baseAllowedAmount;
             } else {
                 if ($baseShipping != 0) {
                     $shipping = $shipping * $baseShippingAmount / $baseShipping;
                 }
                 $shipping = Mage::app()->getStore()->roundPrice($shipping);
                 $baseShipping = $baseShippingAmount;
             }
         } else {
             $baseAllowedAmount = $order->getBaseCurrency()->format($baseAllowedAmount, null, false);
             Mage::throwException(Mage::helper('sales')->__('Maximum shipping amount allowed to refund is: %s', $baseAllowedAmount));
         }
     } else {
         if ($baseShipping != 0) {
             $shippingInclTax = Mage::app()->getStore()->roundPrice($shippingInclTax * $allowedAmount / $shipping);
             $baseShippingInclTax = Mage::app()->getStore()->roundPrice($baseShippingInclTax * $baseAllowedAmount / $baseShipping);
         }
         $shipping = $allowedAmount;
         $baseShipping = $baseAllowedAmount;
     }
     $creditmemo->setShippingAmount($shipping);
     $creditmemo->setBaseShippingAmount($baseShipping);
     $creditmemo->setShippingInclTax($shippingInclTax);
     $creditmemo->setBaseShippingInclTax($baseShippingInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $shipping);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseShipping);
     return $this;
 }
Example #9
0
 /**
  * Adds shipping cost to request as item
  *
  * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object
  * @param bool $credit
  * @return int|bool
  */
 protected function _addShipping($object, $credit = false)
 {
     if ($object->getBaseShippingAmount() == 0) {
         return false;
     }
     $order = $object->getOrder();
     $lineNumber = $this->_getNewLineCode();
     $storeId = $object->getStore()->getId();
     $taxClass = Mage::helper('tax')->getShippingTaxClass($storeId);
     $line = $this->_getNewDocumentRequestLineObject();
     $amount = $object->getBaseShippingAmount();
     if ($this->_getTaxDataHelper()->priceIncludesTax($storeId)) {
         $amount = (double) $object->getBaseShippingInclTax();
         $line->setTaxIncluded('true');
     }
     if ($this->_getTaxDataHelper()->applyTaxAfterDiscount($storeId)) {
         $amount -= (double) $order->getBaseShippingDiscountAmount();
     }
     //@startSkipCommitHooks
     $amount = $credit ? -1 * $amount : $amount;
     //@finishSkipCommitHooks
     $line->setLineCode($lineNumber);
     $shippingSku = $this->_getConfigHelper()->getShippingSku($storeId);
     $line->setItemCode($shippingSku ? $shippingSku : self::DEFAULT_SHIPPING_ITEMS_SKU);
     $line->setItemDescription(self::DEFAULT_SHIPPING_ITEMS_DESCRIPTION);
     $line->setAvalaraGoodsAndServicesType($taxClass);
     $line->setNumberOfItems(1);
     $line->setlineAmount($amount);
     $discounted = (double) $order->getBaseShippingDiscountAmount() && $this->_getTaxDataHelper()->applyTaxAfterDiscount($storeId) ? 'true' : 'false';
     $line->setDiscounted($discounted);
     $this->_lineToItemId[$lineNumber] = $shippingSku;
     $this->_lines[$lineNumber] = $line;
     $this->_setLinesToRequest();
     return $lineNumber;
 }
Example #10
0
 /**
  * Adds shipping cost to request as item
  *
  * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object
  * @param bool $credit
  * @return int|bool
  */
 protected function _addShipping($object, $credit = false)
 {
     if ($object->getBaseShippingAmount() == 0) {
         return false;
     }
     $order = $object->getOrder();
     $lineNumber = count($this->_lines);
     $storeId = $object->getStore()->getId();
     $taxClass = Mage::helper('tax')->getShippingTaxClass($storeId);
     $line = new Line();
     $amount = (double) $object->getBaseShippingAmount();
     if ($this->_getTaxDataHelper()->priceIncludesTax($storeId)) {
         $amount = (double) $object->getBaseShippingInclTax();
         $line->setTaxIncluded(true);
     }
     if ($this->_getTaxDataHelper()->applyTaxAfterDiscount()) {
         $amount -= (double) $order->getBaseShippingDiscountAmount();
     }
     if ($credit) {
         //@startSkipCommitHooks
         $amount *= -1;
         //@finishSkipCommitHooks
     }
     $line->setNo($lineNumber);
     $line->setItemCode($this->_getConfigHelper()->getShippingSku($storeId));
     $line->setDescription('Shipping costs');
     $line->setTaxCode($taxClass);
     $line->setQty(1);
     $line->setAmount($amount);
     $line->setDiscounted((double) $order->getBaseShippingDiscountAmount() && $this->_getTaxDataHelper()->applyTaxAfterDiscount($storeId));
     $this->_lineToItemId[$lineNumber] = 'shipping';
     $this->_lines[$lineNumber] = $line;
     $this->_request->setLines($this->_lines);
     return $lineNumber;
 }
 /**
  * Cancel a creditmemo: substract its totals from the payment
  *
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return Mage_Sales_Model_Order_Payment
  */
 public function cancelCreditmemo($creditmemo)
 {
     $this->_updateTotals(array('amount_refunded' => -1 * $creditmemo->getGrandTotal(), 'base_amount_refunded' => -1 * $creditmemo->getBaseGrandTotal(), 'shipping_refunded' => -1 * $creditmemo->getShippingAmount(), 'base_shipping_refunded' => -1 * $creditmemo->getBaseShippingAmount()));
     Mage::dispatchEvent('sales_order_payment_cancel_creditmemo', array('payment' => $this, 'creditmemo' => $creditmemo));
     return $this;
 }
Example #12
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $shippingTaxAmount = 0;
     $baseShippingTaxAmount = 0;
     $totalTax = 0;
     $baseTotalTax = 0;
     $totalHiddenTax = 0;
     $baseTotalHiddenTax = 0;
     $order = $creditmemo->getOrder();
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemTax = $item->getOrderItem()->getTaxAmount();
         $baseOrderItemTax = $item->getOrderItem()->getBaseTaxAmount();
         $orderItemQty = $item->getOrderItem()->getQtyOrdered();
         if ($orderItemTax && $orderItemQty) {
             /**
              * Check item tax amount
              */
             if ($item->isLast()) {
                 $tax = $orderItemTax - $item->getOrderItem()->getTaxRefunded() - $item->getOrderItem()->getTaxCanceled();
                 $baseTax = $baseOrderItemTax - $item->getOrderItem()->getTaxRefunded() - $item->getOrderItem()->getTaxCanceled();
                 $hiddenTax = $orderItem->getHiddenTaxAmount() - $orderItem->getHiddenTaxRefunded() - $item->getOrderItem()->getHiddenTaxCanceled();
                 $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() - $orderItem->getBaseHiddenTaxRefunded() - $item->getOrderItem()->getHiddenTaxCanceled();
             } else {
                 $tax = $orderItemTax * $item->getQty() / $orderItemQty;
                 $baseTax = $baseOrderItemTax * $item->getQty() / $orderItemQty;
                 $hiddenTax = $orderItem->getHiddenTaxAmount() * $item->getQty() / $orderItemQty;
                 $baseHiddenTax = $orderItem->getBaseHiddenTaxAmount() * $item->getQty() / $orderItemQty;
                 $tax = $creditmemo->getStore()->roundPrice($tax);
                 $baseTax = $creditmemo->getStore()->roundPrice($baseTax);
                 $hiddenTax = $creditmemo->getStore()->roundPrice($hiddenTax);
                 $baseHiddenTax = $creditmemo->getStore()->roundPrice($baseHiddenTax);
             }
             $item->setTaxAmount($tax);
             $item->setBaseTaxAmount($baseTax);
             $item->setHiddenTaxAmount($hiddenTax);
             $item->setBaseHiddenTaxAmount($baseHiddenTax);
             $totalTax += $tax;
             $baseTotalTax += $baseTax;
             $totalHiddenTax += $hiddenTax;
             $baseTotalHiddenTax += $baseHiddenTax;
         }
     }
     if ($invoice = $creditmemo->getInvoice()) {
         $totalTax += $invoice->getShippingTaxAmount();
         $baseTotalTax += $invoice->getBaseShippingTaxAmount();
         $totalHiddenTax += $invoice->getShippingHiddenTaxAmount();
         $baseTotalHiddenTax += $invoice->getBaseShippingHiddenTaxAmount();
         $shippingTaxAmount = $invoice->getShippingTaxAmount();
         $baseShippingTaxAmount = $invoice->getBaseShippingTaxAmount();
         $shippingHiddenTaxAmount = $invoice->getShippingHiddenTaxAmount();
         $baseShippingHiddenTaxAmount = $invoice->getBaseShippingHiddenTaxAmount();
     } else {
         $orderShippingAmount = $order->getShippingAmount();
         $baseOrderShippingAmount = $order->getBaseShippingAmount();
         $orderShippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount();
         $baseOrderShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount();
         $baseOrderShippingRefundedAmount = $order->getBaseShippingRefunded();
         $baseOrderShippingHiddenTaxRefunded = $order->getBaseShippingHiddenTaxRefunded();
         $shippingTaxAmount = 0;
         $baseShippingTaxAmount = 0;
         $shippingHiddenTaxAmount = 0;
         $baseShippingHiddenTaxAmount = 0;
         if ($baseOrderShippingAmount - $baseOrderShippingRefundedAmount > $creditmemo->getBaseShippingAmount()) {
             $part = $creditmemo->getShippingAmount() / $orderShippingAmount;
             $basePart = $creditmemo->getBaseShippingAmount() / $baseOrderShippingAmount;
             $shippingTaxAmount = $order->getShippingTaxAmount() * $part;
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() * $basePart;
             $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount() * $part;
             $baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount() * $basePart;
             $shippingTaxAmount = $creditmemo->getStore()->roundPrice($shippingTaxAmount);
             $baseShippingTaxAmount = $creditmemo->getStore()->roundPrice($baseShippingTaxAmount);
             $shippingHiddenTaxAmount = $creditmemo->getStore()->roundPrice($shippingHiddenTaxAmount);
             $baseShippingHiddenTaxAmount = $creditmemo->getStore()->roundPrice($baseShippingHiddenTaxAmount);
         } elseif ($baseOrderShippingAmount - $baseOrderShippingRefundedAmount == $creditmemo->getBaseShippingAmount()) {
             $shippingTaxAmount = $order->getShippingTaxAmount() - $order->getShippingTaxRefunded();
             $baseShippingTaxAmount = $order->getBaseShippingTaxAmount() - $order->getBaseShippingTaxRefunded();
             $shippingHiddenTaxAmount = $order->getShippingHiddenTaxAmount() - $order->getShippingHiddenTaxRefunded();
             $baseShippingHiddenTaxAmount = $order->getBaseShippingHiddenTaxAmount() - $order->getBaseShippingHiddenTaxRefunded();
         }
         $totalTax += $shippingTaxAmount;
         $baseTotalTax += $baseShippingTaxAmount;
         $totalHiddenTax += $shippingHiddenTaxAmount;
         $baseTotalHiddenTax += $baseShippingHiddenTaxAmount;
     }
     $allowedTax = $order->getTaxAmount() - $order->getTaxRefunded();
     $allowedBaseTax = $order->getBaseTaxAmount() - $order->getBaseTaxRefunded();
     $allowedHiddenTax = $order->getHiddenTaxAmount() + $order->getShippingHiddenTaxAmount() - $order->getHiddenTaxRefunded() - $order->getShippingHiddenTaxRefunded();
     $allowedBaseHiddenTax = $order->getBaseHiddenTaxAmount() + $order->getBaseShippingHiddenTaxAmount() - $order->getBaseHiddenTaxRefunded() - $order->getBaseShippingHiddenTaxRefunded();
     $totalTax = min($allowedTax, $totalTax);
     $baseTotalTax = min($allowedBaseTax, $baseTotalTax);
     $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
     $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     $creditmemo->setTaxAmount($totalTax);
     $creditmemo->setBaseTaxAmount($baseTotalTax);
     $creditmemo->setHiddenTaxAmount($totalHiddenTax);
     $creditmemo->setBaseHiddenTaxAmount($baseTotalHiddenTax);
     $creditmemo->setShippingTaxAmount($shippingTaxAmount);
     $creditmemo->setBaseShippingTaxAmount($baseShippingTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax + $totalHiddenTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax + $baseTotalHiddenTax);
     return $this;
 }
Example #13
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     if ($order->getCustomercreditDiscount() < 0.0001) {
         return;
     }
     //        echo $creditmemo->getBaseShippingAmount();
     $creditmemo->setBaseCustomercreditDiscount(0);
     $creditmemo->setCustomercreditDiscount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     $hiddenTaxDiscount = 0;
     $baseHiddenTaxDiscount = 0;
     $totalDiscountRefunded = 0;
     $baseTotalDiscountRefunded = 0;
     $hiddenTaxRefunded = 0;
     $baseHiddenTaxRefunded = 0;
     foreach ($order->getCreditmemosCollection() as $existedCreditmemo) {
         if ($existedCreditmemo->getCustomercreditDiscount()) {
             $baseTotalDiscountRefunded += $existedCreditmemo->getBaseCustomercreditDiscount();
             $totalDiscountRefunded += $existedCreditmemo->getCustomercreditDiscount();
             $baseHiddenTaxRefunded += $existedCreditmemo->getBaseCustomercreditHiddenTax();
             $hiddenTaxRefunded += $existedCreditmemo->getCustomercreditHiddenTax();
         }
     }
     $baseShippingAmount = $creditmemo->getBaseShippingAmount();
     if ($baseShippingAmount) {
         $baseTotalDiscountAmount += $baseShippingAmount * $order->getBaseCustomercreditDiscountForShipping() / $order->getBaseShippingAmount();
         $totalDiscountAmount += $order->getShippingAmount() * $baseTotalDiscountAmount / $order->getBaseShippingAmount();
         $baseHiddenTaxDiscount += $baseShippingAmount * $order->getBaseCustomercreditShippingHiddenTax() / $order->getBaseShippingAmount();
         $hiddenTaxDiscount += $order->getShippingAmount() * $baseHiddenTaxDiscount / $order->getBaseShippingAmount();
     }
     if ($this->isLast($creditmemo)) {
         $baseTotalDiscountAmount = $order->getBaseCustomercreditDiscount() - $baseTotalDiscountRefunded;
         //            echo $baseTotalDiscountRefunded;
         $totalDiscountAmount = $order->getCustomercreditDiscount() - $totalDiscountRefunded;
         $baseHiddenTaxDiscount = $order->getBaseCustomercreditHiddenTax() - $baseHiddenTaxRefunded;
         $hiddenTaxDiscount = $order->getCustomercreditHiddenTax() - $hiddenTaxRefunded;
     } else {
         foreach ($creditmemo->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemBaseDiscount = (double) $orderItem->getBaseCustomercreditDiscount();
             $orderItemDiscount = (double) $orderItem->getCustomercreditDiscount();
             $orderItemBaseHiddenTax = (double) $orderItem->getBaseCustomercreditHiddenTax();
             $orderItemHiddenTax = (double) $orderItem->getCustomercreditHiddenTax();
             $orderItemQty = $orderItem->getQtyOrdered();
             $refundItemQty = $item->getQty();
             if ($orderItemDiscount && $orderItemQty) {
                 if (version_compare(Mage::getVersion(), '1.7.0.0', '>=')) {
                     $totalDiscountAmount += $creditmemo->roundPrice($refundItemQty * $orderItemBaseDiscount / $orderItemQty, 'regular', true);
                     $baseTotalDiscountAmount += $creditmemo->roundPrice($refundItemQty * $orderItemDiscount / $orderItemQty, 'base', true);
                     $hiddenTaxDiscount += $creditmemo->roundPrice($refundItemQty * $orderItemHiddenTax / $orderItemQty, 'regular', true);
                     $baseHiddenTaxDiscount += $creditmemo->roundPrice($refundItemQty * $orderItemBaseHiddenTax / $orderItemQty, 'base', true);
                 } else {
                     $totalDiscountAmount += $refundItemQty * $orderItemBaseDiscount / $orderItemQty;
                     $baseTotalDiscountAmount += $refundItemQty * $orderItemDiscount / $orderItemQty;
                     $hiddenTaxDiscount += $refundItemQty * $orderItemHiddenTax / $orderItemQty;
                     $baseHiddenTaxDiscount += $refundItemQty * $orderItemBaseHiddenTax / $orderItemQty;
                 }
             }
         }
     }
     $creditmemo->setBaseCustomercreditDiscount($baseTotalDiscountAmount);
     $creditmemo->setCustomercreditDiscount($totalDiscountAmount);
     $creditmemo->setBaseCustomercreditHiddenTax($baseHiddenTaxDiscount);
     $creditmemo->setCustomercreditHiddenTax($hiddenTaxDiscount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $hiddenTaxDiscount - $totalDiscountAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseHiddenTaxDiscount - $baseTotalDiscountAmount);
     $creditmemo->setAllowZeroGrandTotal(true);
 }
Example #14
0
 /**
  * Adds shipping cost to request as item
  *
  * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object
  * @param bool $credit
  * @return int|bool
  */
 protected function _addShipping($object, $credit = false)
 {
     if ($object->getBaseShippingAmount() == 0) {
         return false;
     }
     $lineNumber = count($this->_lines);
     $storeId = $object->getStore()->getId();
     $taxClass = Mage::helper('tax')->getShippingTaxClass($storeId);
     $amount = $object->getBaseShippingAmount();
     if ($credit) {
         //@startSkipCommitHooks
         $amount *= -1;
         //@finishSkipCommitHooks
     }
     $line = new Line();
     $line->setNo($lineNumber);
     $line->setItemCode(Mage::helper('avatax')->getShippingSku($storeId));
     $line->setDescription('Shipping costs');
     $line->setTaxCode($taxClass);
     $line->setQty(1);
     $line->setAmount($amount);
     $line->setDiscounted(false);
     $this->_lineToItemId[$lineNumber] = 'shipping';
     $this->_lines[$lineNumber] = $line;
     $this->_request->setLines($this->_lines);
     return $lineNumber;
 }
Example #15
0
 /**
  * Collect total when create Creditmemo
  * 
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setRewardpointsDiscount(0);
     $creditmemo->setRewardpointsBaseDiscount(0);
     $order = $creditmemo->getOrder();
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     foreach ($order->getCreditmemosCollection() as $existedCreditmemo) {
         if ($existedCreditmemo->getRewardpointsDiscount()) {
             $totalDiscountRefunded = $existedCreditmemo->getRewardpointsDiscount();
             $baseTotalDiscountRefunded = $existedCreditmemo->getRewardpointsBaseDiscount();
             $hiddenTaxRefunded = $existedCreditmemo->getRewardpointsHiddenTaxAmount();
             $baseHiddenTaxRefunded = $existedCreditmemo->getRewardpointsBaseHiddenTaxAmount();
         }
     }
     /**
      * Calculate how much shipping discount should be applied
      * basing on how much shipping should be refunded.
      */
     $baseShippingAmount = $creditmemo->getBaseShippingAmount();
     if ($baseShippingAmount) {
         $baseTotalDiscountAmount = $baseShippingAmount * $order->getRewardpointsBaseAmount() / $order->getBaseShippingAmount();
         $totalDiscountAmount = $order->getRewardpointsAmount() * $baseTotalDiscountAmount / $order->getBaseShippingAmount();
         $baseTotalHiddenTax = $baseShippingAmount * $order->getRewardpointsBaseShippingHiddenTaxAmount() / $order->getBaseShippingAmount();
         $totalHiddenTax = $order->getRewardpointsShippingHiddenTaxAmount() * $baseTotalHiddenTax / $order->getBaseShippingAmount();
     }
     if ($this->isLast($creditmemo)) {
         $baseTotalDiscountAmount = $order->getRewardpointsBaseDiscount() - $baseTotalDiscountRefunded;
         $totalDiscountAmount = $order->getRewardpointsDiscount() - $totalDiscountRefunded;
         $totalHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxRefunded;
         $baseTotalHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxRefunded;
     } else {
         /** @var $item Mage_Sales_Model_Order_Invoice_Item */
         foreach ($creditmemo->getAllItems() as $item) {
             $orderItem = $item->getOrderItem();
             if ($orderItem->isDummy()) {
                 continue;
             }
             $orderItemDiscount = (double) $orderItem->getRewardpointsDiscount() * $orderItem->getQtyInvoiced() / $orderItem->getQtyOrdered();
             $baseOrderItemDiscount = (double) $orderItem->getRewardpointsBaseDiscount() * $orderItem->getQtyInvoiced() / $orderItem->getQtyOrdered();
             $orderItemHiddenTax = (double) $orderItem->getRewardpointsHiddenTaxAmount();
             $baseOrderItemHiddenTax = (double) $orderItem->getRewardpointsBaseHiddenTaxAmount();
             $orderItemQty = $orderItem->getQtyInvoiced();
             if ($orderItemDiscount && $orderItemQty) {
                 $totalDiscountAmount += $creditmemo->roundPrice($orderItemDiscount / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalDiscountAmount += $creditmemo->roundPrice($baseOrderItemDiscount / $orderItemQty * $item->getQty(), 'base', true);
                 $totalHiddenTax += $creditmemo->roundPrice($orderItemHiddenTax / $orderItemQty * $item->getQty(), 'regular', true);
                 $baseTotalHiddenTax += $creditmemo->roundPrice($baseOrderItemHiddenTax / $orderItemQty * $item->getQty(), 'base', true);
             }
         }
         $allowedBaseHiddenTax = $order->getRewardpointsBaseHiddenTaxAmount() - $baseHiddenTaxRefunded;
         $allowedHiddenTax = $order->getRewardpointsHiddenTaxAmount() - $hiddenTaxRefunded;
         $totalHiddenTax = min($allowedHiddenTax, $totalHiddenTax);
         $baseTotalHiddenTax = min($allowedBaseHiddenTax, $baseTotalHiddenTax);
     }
     $creditmemo->setRewardpointsDiscount($totalDiscountAmount);
     $creditmemo->setRewardpointsBaseDiscount($baseTotalDiscountAmount);
     $creditmemo->setRewardpointsHiddenTaxAmount($totalHiddenTax);
     $creditmemo->setRewardpointsBaseHiddenTaxAmount($baseTotalHiddenTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalDiscountAmount + $totalHiddenTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount + $baseTotalHiddenTax);
     return $this;
     $order = $creditmemo->getOrder();
     if ($order->getRewardpointsDiscount() < 0.0001) {
         return;
     }
     if ($this->isLast($creditmemo)) {
         $baseDiscount = $order->getRewardpointsBaseDiscount();
         $discount = $order->getRewardpointsDiscount();
         foreach ($order->getCreditmemosCollection() as $existedCreditmemo) {
             if ($baseDiscount > 0.0001) {
                 $baseDiscount -= $existedCreditmemo->getRewardpointsBaseDiscount();
                 $discount -= $existedCreditmemo->getRewardpointsDiscount();
             }
         }
     } else {
         $orderTotal = $order->getGrandTotal() + $order->getRewardpointsDiscount();
         $ratio = $creditmemo->getGrandTotal() / $orderTotal;
         $baseDiscount = $order->getRewardpointsBaseDiscount() * $ratio;
         $discount = $order->getRewardpointsDiscount() * $ratio;
         $maxBaseDiscount = $order->getRewardpointsBaseDiscount();
         $maxDiscount = $order->getRewardpointsDiscount();
         foreach ($order->getCreditmemosCollection() as $existedCreditmemo) {
             if ($maxBaseDiscount > 0.0001) {
                 $maxBaseDiscount -= $existedCreditmemo->getRewardpointsBaseDiscount();
                 $maxDiscount -= $existedCreditmemo->getRewardpointsDiscount();
             }
         }
         if ($baseDiscount > $maxBaseDiscount) {
             $baseDiscount = $maxBaseDiscount;
             $discount = $maxDiscount;
         }
     }
     if ($baseDiscount > 0.0001) {
         if ($creditmemo->getBaseGrandTotal() <= $baseDiscount) {
             $creditmemo->setRewardpointsBaseDiscount($creditmemo->getBaseGrandTotal());
             $creditmemo->setRewardpointsDiscount($creditmemo->getGrandTotal());
             $creditmemo->setBaseGrandTotal(0.0);
             $creditmemo->setGrandTotal(0.0);
         } else {
             $creditmemo->setRewardpointsBaseDiscount($baseDiscount);
             $creditmemo->setRewardpointsDiscount($discount);
             $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseDiscount);
             $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $discount);
         }
         $creditmemo->setAllowZeroGrandTotal(true);
     }
 }