Example #1
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $baseDiscount = 0;
     $discount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getAffiliateplusAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseAffiliateplusAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             $discount -= $orderItemDiscount * $item->getQty() / $orderItemQty;
             $baseDiscount -= $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
         }
     }
     if (!floatval($baseDiscount)) {
         $order = $creditmemo->getOrder();
         $baseDiscount = $order->getBaseAffiliateplusDiscount();
         $discount = $order->getAffiliateplusDiscount();
     }
     if (floatval($baseDiscount)) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $creditmemo->setBaseAffiliateplusDiscount($baseDiscount);
         $creditmemo->setAffiliateplusDiscount($discount);
         $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseDiscount);
         $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #2
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setDiscountAmount(0);
     $creditmemo->setBaseDiscountAmount(0);
     $totalDiscountAmount = 0;
     $baseTotalDiscountAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $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 #3
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $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;
         }
     }
     $creditmemo->setTaxAmount($totalTax);
     $creditmemo->setBaseTaxAmount($baseTotalTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #4
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setGomageGiftWrapAmount(0);
     $creditmemo->setBaseGomageGiftWrapAmount(0);
     $order = $creditmemo->getOrder();
     $totalGomageGiftWrapAmount = 0;
     $baseTotalGomageGiftWrapAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItemGomageGiftWrap = (double) $item->getOrderItem()->getGomageGiftWrapAmount();
         $baseOrderItemGomageGiftWrap = (double) $item->getOrderItem()->getBaseGomageGiftWrapAmount();
         $orderItemQty = $item->getOrderItem()->getQtyOrdered();
         if ($orderItemGomageGiftWrap && $orderItemQty) {
             $GomageGiftWrap = $orderItemGomageGiftWrap * $item->getQty() / $orderItemQty;
             $baseGomageGiftWrap = $baseOrderItemGomageGiftWrap * $item->getQty() / $orderItemQty;
             $GomageGiftWrap = $creditmemo->getStore()->roundPrice($GomageGiftWrap);
             $baseGomageGiftWrap = $creditmemo->getStore()->roundPrice($baseGomageGiftWrap);
             $item->setGomageGiftWrapAmount($GomageGiftWrap);
             $item->setBaseGomageGiftWrapAmount($baseGomageGiftWrap);
             $totalGomageGiftWrapAmount += $GomageGiftWrap;
             $baseTotalGomageGiftWrapAmount += $baseGomageGiftWrap;
         }
     }
     $creditmemo->setGomageGiftWrapAmount($totalGomageGiftWrapAmount);
     $creditmemo->setBaseGomageGiftWrapAmount($baseTotalGomageGiftWrapAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalGomageGiftWrapAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalGomageGiftWrapAmount);
     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 #6
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     $weeeTaxAmount = 0;
     $baseWeeeTaxAmount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = $item->getOrderItem();
         if ($orderItem->isDummy()) {
             continue;
         }
         $orderItemQty = $orderItem->getQtyOrdered();
         $weeeRowDiscountAmount = $orderItem->getDiscountAppliedForWeeeTax();
         $weeeDiscountAmount = $creditmemo->roundPrice($weeeRowDiscountAmount / $orderItemQty * $item->getQty(), 'regular', true);
         $baseWeeeRowDiscountAmount = $orderItem->getBaseDiscountAppliedForWeeeTax();
         $baseWeeeDiscountAmount = $creditmemo->roundPrice($baseWeeeRowDiscountAmount / $orderItemQty * $item->getQty(), 'base', true);
         $weeeAmountExclTax = (Mage::helper('weee')->getWeeeTaxInclTax($item) - Mage::helper('weee')->getTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $totalTax += $weeeAmountExclTax - $weeeDiscountAmount;
         $baseWeeeAmountExclTax = (Mage::helper('weee')->getBaseWeeeTaxInclTax($item) - Mage::helper('weee')->getBaseTotalTaxAppliedForWeeeTax($item)) * $item->getQty();
         $baseTotalTax += $baseWeeeAmountExclTax - $baseWeeeDiscountAmount;
         $item->setWeeeTaxAppliedRowAmount($weeeAmountExclTax);
         $item->setBaseWeeeTaxAppliedRowAmount($baseWeeeAmountExclTax);
         $weeeTaxAmount += Mage::helper('weee')->getWeeeTaxInclTax($item) * $item->getQty();
         $baseWeeeTaxAmount += Mage::helper('weee')->getBaseWeeeTaxInclTax($item) * $item->getQty();
         $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());
     }
     /*
      * please refer the description in weee - invoice section for reasoning
      */
     if (Mage::helper('weee')->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalTax);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalTax);
     } else {
         $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
         $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
     }
     //Increment the subtotal
     $creditmemo->setSubtotalInclTax($creditmemo->getSubtotalInclTax() + $weeeTaxAmount);
     $creditmemo->setBaseSubtotalInclTax($creditmemo->getBaseSubtotalInclTax() + $baseWeeeTaxAmount);
     //Increment the grand total
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
 /**
  * Collect gift wrapping tax totals
  *
  * @param   Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return  Enterprise_GiftWrapping_Model_Total_Creditmemo_Tax_Giftwrapping
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     /**
      * Wrapping for items
      */
     $refunded = 0;
     $baseRefunded = 0;
     foreach ($creditmemo->getAllItems() as $creditmemoItem) {
         if (!$creditmemoItem->getQty() || $creditmemoItem->getQty() == 0) {
             continue;
         }
         $orderItem = $creditmemoItem->getOrderItem();
         if ($orderItem->getGwId() && $orderItem->getGwBaseTaxAmountInvoiced() && $orderItem->getGwBaseTaxAmountInvoiced() != $orderItem->getGwBaseTaxAmountRefunded()) {
             $orderItem->setGwBaseTaxAmountRefunded($orderItem->getGwBaseTaxAmountInvoiced());
             $orderItem->setGwTaxAmountRefunded($orderItem->getGwTaxAmountInvoiced());
             $baseRefunded += $orderItem->getGwBaseTaxAmountInvoiced() * $creditmemoItem->getQty();
             $refunded += $orderItem->getGwTaxAmountInvoiced() * $creditmemoItem->getQty();
         }
     }
     if ($refunded > 0 || $baseRefunded > 0) {
         $order->setGwItemsBaseTaxRefunded($order->getGwItemsBaseTaxRefunded() + $baseRefunded);
         $order->setGwItemsTaxRefunded($order->getGwItemsTaxRefunded() + $refunded);
         $creditmemo->setGwItemsBaseTaxAmount($baseRefunded);
         $creditmemo->setGwItemsTaxAmount($refunded);
     }
     /**
      * Wrapping for order
      */
     if ($order->getGwId() && $order->getGwBaseTaxAmountInvoiced() && $order->getGwBaseTaxAmountInvoiced() != $order->getGwBaseTaxAmountRefunded()) {
         $order->setGwBaseTaxAmountRefunded($order->getGwBaseTaxAmountInvoiced());
         $order->setGwTaxAmountRefunded($order->getGwTaxAmountInvoiced());
         $creditmemo->setGwBaseTaxAmount($order->getGwBaseTaxAmountInvoiced());
         $creditmemo->setGwTaxAmount($order->getGwTaxAmountInvoiced());
     }
     /**
      * Printed card
      */
     if ($order->getGwAddCard() && $order->getGwCardBaseTaxInvoiced() && $order->getGwCardBaseTaxInvoiced() != $order->getGwCardBaseTaxRefunded()) {
         $order->setGwCardBaseTaxRefunded($order->getGwCardBaseTaxInvoiced());
         $order->setGwCardTaxRefunded($order->getGwCardTaxInvoiced());
         $creditmemo->setGwCardBaseTaxAmount($order->getGwCardBaseTaxInvoiced());
         $creditmemo->setGwCardTaxAmount($order->getGwCardTaxInvoiced());
     }
     $baseTaxAmount = $creditmemo->getGwItemsBaseTaxAmount() + $creditmemo->getGwBaseTaxAmount() + $creditmemo->getGwCardBaseTaxAmount();
     $taxAmount = $creditmemo->getGwItemsTaxAmount() + $creditmemo->getGwTaxAmount() + $creditmemo->getGwCardTaxAmount();
     $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTaxAmount);
     $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $taxAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTaxAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $taxAmount);
     $creditmemo->setBaseCustomerBalanceReturnMax($creditmemo->getBaseCustomerBalanceReturnMax() + $baseTaxAmount);
     $creditmemo->setCustomerBalanceReturnMax($creditmemo->getCustomerBalanceReturnMax() + $taxAmount);
     return $this;
 }
Example #8
0
 /**
  * Collect total cost of refunded items
  *
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return Mage_Sales_Model_Order_Creditmemo_Total_Cost
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $baseRefundTotalCost = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if (!$item->getHasChildren()) {
             $baseRefundTotalCost += $item->getBaseCost() * $item->getQty();
         }
     }
     $creditmemo->setBaseCost($baseRefundTotalCost);
     return $this;
 }
Example #9
0
 /**
  * Collect Creditmemo subtotal
  *
  * @param   Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return  Mage_Sales_Model_Order_Creditmemo_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
     }
     $creditmemo->setSubtotal($subtotal);
     $creditmemo->setBaseSubtotal($baseSubtotal);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $subtotal);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #10
0
 /**
  * Convert a creditmemo to a LinkShare refund row.
  *
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return array
  */
 public function creditmemoToFeed(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $orderDate = new Zend_Date($creditmemo->getOrderDate(), Varien_Date::DATETIME_INTERNAL_FORMAT);
     $orderDate = $orderDate->toString('yyyy-MM-dd');
     $cmDate = new Zend_Date($creditmemo->getCreatedAt(), Varien_Date::DATETIME_INTERNAL_FORMAT);
     $cmDate = $cmDate->toString('yyyy-MM-dd');
     $rows = array();
     foreach ($creditmemo->getAllItems() as $cmItem) {
         /* @var $cmItem Mage_Sales_Model_Order_Creditmemo_Item */
         if ($cmItem->getBaseRowTotal()) {
             $rows[] = array('order_id' => $creditmemo->getOrderIncrementId(), 'site_id' => '', 'order_date' => $orderDate, 'transaction_date' => $cmDate, 'sku' => $cmItem->getSku(), 'quantity' => round($cmItem->getQty()), 'amount' => ($cmItem->getBaseRowTotal() - $cmItem->getBaseDiscountAmount()) * -100, 'currency' => $creditmemo->getBaseCurrencyCode(), 'blank' => '', 'blank' => '', 'blank' => '', 'product_name' => '');
         }
     }
     return $rows;
 }
Example #11
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 #12
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $order = $creditmemo->getOrder();
     //$affiliate = Mage::getModel('credit/creditorder')->load($order->getIncrementId())->getAffiliate();
     $affiliate = 0;
     //$credit = Mage::getModel('credit/creditorder')->load($order->getIncrementId())->getCredit();
     $credit = 0;
     $totalDiscountAmount = $affiliate + $credit;
     $baseTotalDiscountAmount = $affiliate + $credit;
     $items = $creditmemo->getAllItems();
     if (!count($items)) {
         return $this;
     }
     $creditmemo->setBaseDiscountAmount($creditmemo->getBaseDiscountAmount() - $baseTotalDiscountAmount);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() - $totalDiscountAmount);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() - $baseTotalDiscountAmount);
     return $this;
 }
Example #13
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;
 }
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     // Changed By Adam 22/09/2014
     if (!Mage::helper('affiliateplus')->isAffiliateModuleEnabled()) {
         return $this;
     }
     $baseDiscount = 0;
     $discount = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItem = $item->getOrderItem();
         $orderItemDiscount = (double) $orderItem->getAffiliateplusAmount();
         $baseOrderItemDiscount = (double) $orderItem->getBaseAffiliateplusAmount();
         $orderItemQty = $orderItem->getQtyOrdered();
         if ($orderItemDiscount && $orderItemQty) {
             $discount -= $orderItemDiscount * $item->getQty() / $orderItemQty;
             $baseDiscount -= $baseOrderItemDiscount * $item->getQty() / $orderItemQty;
         }
     }
     /* Changed By Adam 30/09/2014: to solve the problem: 
      * invoice san pham ko phai affiliate nhung van hien discount
      */
     //        if (!floatval($baseDiscount)){
     //            $order = $creditmemo->getOrder();
     //            $baseDiscount = $order->getBaseAffiliateplusDiscount();
     //            $discount = $order->getAffiliateplusDiscount();
     //        }
     if (floatval($baseDiscount)) {
         $baseDiscount = Mage::app()->getStore()->roundPrice($baseDiscount);
         $discount = Mage::app()->getStore()->roundPrice($discount);
         $creditmemo->setBaseAffiliateplusDiscount($baseDiscount);
         $creditmemo->setAffiliateplusDiscount($discount);
         $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseDiscount);
         $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $discount);
     }
     return $this;
 }
Example #15
0
 /**
  * Collect Creditmemo subtotal
  *
  * @param   Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return  Mage_Sales_Model_Order_Creditmemo_Total_Subtotal
  */
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $subtotal = 0;
     $baseSubtotal = 0;
     $subtotalInclTax = 0;
     $baseSubtotalInclTax = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $item->calcRowTotal();
         $subtotal += $item->getRowTotal();
         $baseSubtotal += $item->getBaseRowTotal();
         $subtotalInclTax += $item->getRowTotalInclTax();
         $baseSubtotalInclTax += $item->getBaseRowTotalInclTax();
     }
     $creditmemo->setSubtotal($subtotal);
     $creditmemo->setBaseSubtotal($baseSubtotal);
     $creditmemo->setSubtotalInclTax($subtotalInclTax);
     $creditmemo->setBaseSubtotalInclTax($baseSubtotalInclTax);
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $subtotal);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseSubtotal);
     return $this;
 }
Example #16
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $store = $creditmemo->getStore();
     $totalTax = 0;
     $baseTotalTax = 0;
     foreach ($creditmemo->getAllItems() as $item) {
         if ($item->getOrderItem()->isDummy()) {
             continue;
         }
         $orderItemQty = $item->getOrderItem()->getQtyOrdered();
         $totalTax += $item->getWeeeTaxAppliedAmount() * $item->getQty();
         $baseTotalTax += $item->getBaseWeeeTaxAppliedAmount() * $item->getQty();
         $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();
             $newApplied[] = $one;
         }
         Mage::helper('weee')->setApplied($item, $newApplied);
         $item->setWeeeTaxRowDisposition($item->getWeeeTaxDisposition() * $item->getQty());
         $item->setBaseWeeeTaxRowDisposition($item->getBaseWeeeTaxDisposition() * $item->getQty());
     }
     if (Mage::helper('weee')->includeInSubtotal($store)) {
         $creditmemo->setSubtotal($creditmemo->getSubtotal() + $totalTax);
         $creditmemo->setBaseSubtotal($creditmemo->getBaseSubtotal() + $baseTotalTax);
     } else {
         $creditmemo->setTaxAmount($creditmemo->getTaxAmount() + $totalTax);
         $creditmemo->setBaseTaxAmount($creditmemo->getBaseTaxAmount() + $baseTotalTax);
     }
     $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $totalTax);
     $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $baseTotalTax);
     return $this;
 }
Example #17
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 #18
0
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     $creditmemo->setGiftwrapAmount(0);
     $totalAmount = 0;
     $giftwrapPrice = 0;
     $giftcardPrice = 0;
     $totalQtyOfBox = array();
     $totalRefundedOfBox = array();
     $itemOfBox = array();
     $parentItemChildQty = array();
     $giftbox = array();
     $parentItemOrderedQty = array();
     $prepareQty = array();
     $order_id = Mage::app()->getRequest()->getParam('order_id');
     $order = $creditmemo->getOrder();
     $tax_info = $order->getFullTaxInfo();
     $rate = $tax_info[0]['percent'];
     //get all gitbox of order
     $giftboxes = Mage::getModel('giftwrap/selection')->getCollection()->addFieldToFilter('order_id', $order_id)->addFieldToFilter('is_invoiced', 1);
     foreach ($giftboxes as $gbox) {
         $totalQtyOfBox[$gbox->getId()] = 0;
         $totalRefundedOfBox[$gbox->getId()] = 0;
         $giftboxItemCollection = Mage::getModel('giftwrap/selectionitem')->getCollection()->addFieldToFilter('selection_id', $gbox->getId());
         foreach ($giftboxItemCollection as $giftboxItem) {
             $totalQtyOfBox[$gbox->getId()] += (int) $giftboxItem->getQty();
             $orderItem = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('quote_item_id', $giftboxItem->getItemId())->getFirstItem();
             $itemOfBox[$orderItem->getItemId()] = $gbox->getId();
             $totalRefundedOfBox[$gbox->getId()] += (int) $orderItem->getQtyRefunded();
         }
     }
     foreach ($creditmemo->getAllItems() as $item) {
         $orderItem = Mage::getModel('sales/order_item')->getCollection()->addFieldToFilter('item_id', $item->getOrderItemId())->getFirstItem();
         if (!$orderItem->getParentItemId() && $item->getQty() > 0) {
             if (isset($itemOfBox[$item->getOrderItemId()])) {
                 $prepareQty[$itemOfBox[$item->getOrderItemId()]] += (int) $totalRefundedOfBox[$itemOfBox[$item->getOrderItemId()]] + (int) $item->getQty();
                 if ($prepareQty[$itemOfBox[$item->getOrderItemId()]] == (int) $totalQtyOfBox[$itemOfBox[$item->getOrderItemId()]]) {
                     $giftwrapPrice = 0;
                     $giftcardPrice = 0;
                     $totalQtyInBox = 0;
                     $selection = Mage::getModel('giftwrap/selection')->load($itemOfBox[$item->getOrderItemId()]);
                     $giftbox[] = $selection->getId();
                     $giftwrapPrice += (double) Mage::getModel('giftwrap/giftwrap')->load($selection->getStyleId())->getPrice();
                     if ($selection->getGiftcardId()) {
                         $giftcardPrice += (double) Mage::getModel('giftwrap/giftcard')->load($selection->getGiftcardId())->getPrice();
                     }
                     if ($selection->getCalculateByItem()) {
                         $totalQtyInBox = $totalQtyOfBox[$selection->getId()];
                         $totalAmount += $totalQtyInBox * ($giftwrapPrice + $giftcardPrice);
                     } else {
                         $totalAmount += $giftwrapPrice + $giftcardPrice;
                     }
                 }
             }
         } else {
             $parentItemChildQty[$orderItem->getParentItemId()] += (int) $orderItem->getQtyRefunded() + (int) $item->getQty();
             $parentItemOrderedQty[$orderItem->getParentItemId()] += (int) $orderItem->getQtyOrdered();
         }
     }
     if (count($parentItemChildQty) > 0) {
         foreach ($parentItemChildQty as $orderItemId => $qty) {
             $orderItem = Mage::getModel('sales/order_item')->load($orderItemId);
             $quoteItemId = $orderItem->getQuoteItemId();
             $selectionItem = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('item_id', $quoteItemId)->getFirstItem();
             $selection = Mage::getModel('giftwrap/selection')->load($selectionItem->getSelectionId());
             $giftwrapPrice = 0;
             $giftcardPrice = 0;
             $totalQtyInBox = 0;
             $giftwrapPrice += (double) Mage::getModel('giftwrap/giftwrap')->load($selection->getStyleId())->getPrice();
             if ($selection->getGiftcardId()) {
                 $giftcardPrice += (double) Mage::getModel('giftwrap/giftcard')->load($selection->getGiftcardId())->getPrice();
             }
             $key = array_search($selection->getId(), $giftbox);
             if ($key !== false) {
                 if ($qty != $parentItemOrderedQty[$orderItemId]) {
                     if ($selection->getCalculateByItem()) {
                         $selectionItems = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('selection_id', $selection->getId());
                         foreach ($selectionItems as $sItem) {
                             $totalQtyInBox += $sItem->getQty();
                         }
                         $totalAmount -= $totalQtyInBox * ($giftwrapPrice + $giftcardPrice);
                     } else {
                         $totalAmount -= $giftwrapPrice + $giftcardPrice;
                     }
                     unset($giftbox[$key]);
                 }
             } else {
                 $qtyToRefund = (int) $qty;
                 if ($qtyToRefund == (int) $parentItemOrderedQty[$orderItemId]) {
                     $giftbox[] = $selection->getId();
                     if ($selection->getCalculateByItem()) {
                         $selectionItems = Mage::getResourceModel('giftwrap/selectionitem_collection')->addFieldToFilter('selection_id', $selection->getId());
                         foreach ($selectionItems as $sItem) {
                             $totalQtyInBox += $sItem->getQty();
                         }
                         $totalAmount += $totalQtyInBox * ($giftwrapPrice + $giftcardPrice);
                     } else {
                         $totalAmount += $giftwrapPrice + $giftcardPrice;
                     }
                 }
             }
         }
     }
     $giftbox = array_filter($giftbox);
     Mage::getSingleton('adminhtml/session')->setSubtotalGiftwrap($totalAmount);
     Mage::getSingleton('adminhtml/session')->setGiftboxRefund($giftbox);
     $lastId = $creditmemo->getItemsCollection()->getLastItem()->getId();
     Mage::getSingleton('adminhtml/session')->setLastId($lastId);
     $orderGiftwrapAmount = $totalAmount;
     if (Mage::getStoreConfig('giftwrap/calculation/tax', Mage::app()->getStore(true)->getId())) {
         if ($rate) {
             $orderGiftwrapTax = $totalAmount * $rate;
         }
     }
     if ($orderGiftwrapAmount || $orderGiftwrapAmount == 0) {
         $creditmemo->setGiftwrapAmount($orderGiftwrapAmount);
         $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $orderGiftwrapAmount);
         $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $orderGiftwrapAmount);
         if ($orderGiftwrapTax) {
             $creditmemo->setGiftwrapTax($orderGiftwrapTax);
             $creditmemo->setGrandTotal($creditmemo->getGrandTotal() + $orderGiftwrapTax);
             $creditmemo->setBaseGrandTotal($creditmemo->getBaseGrandTotal() + $orderGiftwrapTax);
         }
     }
     return $this;
 }
Example #19
0
 /**
  * Article preparations for PAYMENT_REQUEST, PAYMENT_CHANGE, CONFIRMATION_DELIVER
  *
  * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order|Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object
  * @return array
  */
 public function getArticles($object)
 {
     $articles = array();
     $articleDiscountAmount = 0;
     $objectItems = $object->getAllItems();
     foreach ($objectItems as $item) {
         if ($item instanceof Mage_Sales_Model_Order_Item || $item instanceof Mage_Sales_Model_Quote_Item) {
             $orderItem = $item;
         } else {
             $orderItem = Mage::getModel('sales/order_item')->load($item->getOrderItemId());
         }
         $shopProduct = Mage::getModel('catalog/product')->load($orderItem->getProductId());
         if (($orderItem->getProductType() !== 'bundle' || $orderItem->getProductType() === 'bundle' && $shopProduct->getPrice() > 0) && $orderItem->getRowTotal() > 0) {
             $article = array();
             $article['articleNumber'] = $item->getSku();
             $article['articleName'] = $item->getName();
             $article['quantity'] = $object instanceof Mage_Sales_Model_Order ? $item->getQtyOrdered() : $item->getQty();
             $article['unitPriceGross'] = $item->getPriceInclTax();
             $article['tax'] = $item->getTaxAmount();
             $article['taxPercent'] = $orderItem->getTaxPercent();
             if ($object instanceof Mage_Sales_Model_Quote) {
                 $article['unitPriceNett'] = $item->getCalculationPrice();
             } else {
                 $article['unitPriceNett'] = $item->getPrice();
                 // netto
             }
             $article['discountId'] = '';
             if ($item->getDiscountAmount() > 0) {
                 $discount = array();
                 $discount['articleNumber'] = 'DISCOUNT-' . $item->getSku();
                 $discount['articleName'] = 'DISCOUNT - ' . $item->getName();
                 $discount['quantity'] = $article['quantity'];
                 $article['tax'] = $item->getRowTotalInclTax() - $item->getRowTotal();
                 $discount['tax'] = -1 * ($article['tax'] - $item->getTaxAmount());
                 $tax = 0;
                 $taxConfig = Mage::getSingleton('tax/config');
                 if ($taxConfig->priceIncludesTax($object->getStoreId())) {
                     $tax = $discount['tax'];
                 }
                 if (round($discount['tax'], 2) < 0) {
                     $discount['taxPercent'] = $article['taxPercent'];
                 }
                 $discount['unitPriceGross'] = -1 * $item->getDiscountAmount() / $article['quantity'];
                 $discount['discountId'] = $item->getSku();
                 $articleDiscountAmount = $articleDiscountAmount + $item->getDiscountAmount();
             }
             $articles[] = $article;
             if ($item->getDiscountAmount() > 0) {
                 // only for sort reason
                 $articles[] = $discount;
             }
         }
     }
     if ($object instanceof Mage_Sales_Model_Order || $object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Creditmemo) {
         $shippingObject = $object;
         if ($object instanceof Mage_Sales_Model_Order_Creditmemo) {
             $articles = $this->addAdjustments($object, $articles);
         }
     } else {
         $shippingObject = $object->getShippingAddress();
     }
     if ($shippingObject->getShippingAmount() > 0) {
         if ($object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Shipment || $object instanceof Mage_Sales_Model_Order_Creditmemo) {
             $shippingDiscountAmount = $shippingObject->getDiscountAmount() - $articleDiscountAmount;
             $shippingDescription = $object->getOrder()->getShippingDescription();
         } else {
             $shippingDiscountAmount = $shippingObject->getShippingDiscountAmount();
             $shippingDescription = $shippingObject->getShippingDescription();
         }
         $article = array();
         $article['articleNumber'] = 'SHIPPING';
         $article['articleName'] = $shippingDescription;
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $shippingObject->getShippingInclTax();
         $article['tax'] = $shippingObject->getShippingTaxAmount();
         $shippingTaxPercent = 0;
         if ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount() > 0) {
             $shippingTaxPercent = ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount()) * 100 / $shippingObject->getShippingAmount();
         }
         $article['taxPercent'] = $shippingTaxPercent;
         $article['discountId'] = '';
         if ($shippingDiscountAmount > 0) {
             $discount = array();
             $discount['articleNumber'] = 'SHIPPINGDISCOUNT';
             $discount['articleName'] = 'Shipping - Discount';
             $discount['quantity'] = 1;
             $discount['unitPriceGross'] = -1 * $shippingObject->getShippingDiscountAmount();
             $article['tax'] = $shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount();
             $discount['tax'] = -1 * ($article['tax'] - $shippingObject->getShippingTaxAmount());
             $tax = 0;
             if (Mage::getSingleton('tax/config')->shippingPriceIncludesTax($object->getStoreId())) {
                 $tax = $discount['tax'];
             }
             $discount['unitPrice'] = -1 * $shippingObject->getShippingDiscountAmount() - $tax;
             $discount['totalPrice'] = -1 * $shippingObject->getShippingDiscountAmount() - $tax;
             $discount['taxPercent'] = 0;
             if (round($discount['tax'], 2) < 0) {
                 $discount['taxPercent'] = $article['taxPercent'];
             }
             $discount['discountId'] = 'SHIPPING';
         }
         $articles[] = $article;
         if ($shippingDiscountAmount > 0) {
             // only for sort reason
             $articles[] = $discount;
         }
     }
     return $articles;
 }
Example #20
0
 /**
  * Article preparations for PAYMENT_REQUEST, PAYMENT_CHANGE, CONFIRMATION_DELIVER
  *
  * @param Mage_Sales_Model_Quote|Mage_Sales_Model_Order|Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object
  * @return array
  */
 public function getArticles($object)
 {
     $articles = array();
     $articleDiscountAmount = 0;
     $objectItems = $object->getAllItems();
     foreach ($objectItems as $item) {
         if ($item instanceof Mage_Sales_Model_Order_Item || $item instanceof Mage_Sales_Model_Quote_Item) {
             $orderItem = $item;
         } else {
             $orderItem = Mage::getModel('sales/order_item')->load($item->getOrderItemId());
         }
         $shopProduct = Mage::getModel('catalog/product')->load($orderItem->getProductId());
         if (($orderItem->getProductType() !== 'bundle' || $orderItem->getProductType() === 'bundle' && $shopProduct->getPrice() > 0) && $orderItem->getRowTotal() > 0) {
             $article = array();
             $article['articleNumber'] = $item->getSku();
             $article['articleName'] = $item->getName();
             $article['quantity'] = $object instanceof Mage_Sales_Model_Order ? $item->getQtyOrdered() : $item->getQty();
             $article['unitPriceGross'] = $item->getPriceInclTax();
             $article['taxPercent'] = $orderItem->getTaxPercent();
             $article['discountId'] = '';
             if ($item->getDiscountAmount() > 0) {
                 $discount = array();
                 $discount['articleNumber'] = 'DISCOUNT-' . $item->getSku();
                 $discount['articleName'] = 'DISCOUNT - ' . $item->getName();
                 $discount['quantity'] = $article['quantity'];
                 $discount['unitPriceGross'] = -1 * $item->getDiscountAmount() / $article['quantity'];
                 $discount['discountId'] = $item->getSku();
                 $articleDiscountAmount = $articleDiscountAmount + $item->getDiscountAmount();
             }
             $articles[] = $article;
             if ($item->getDiscountAmount() > 0) {
                 // only for sort reason
                 $articles[] = $discount;
             }
         }
     }
     if ($object->getGwPrice() > 0) {
         $article = array();
         $article['articleNumber'] = 'orderwrapping';
         $article['articleName'] = 'Wrapping Cost Order';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwPrice();
         $article['taxPercent'] = 100 / $object->getGwPrice() * $object->getGwTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if ($object->getGwItemsPrice() > 0) {
         $article = array();
         $article['articleNumber'] = 'itemswrapping';
         $article['articleName'] = 'Wrapping Cost Items';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwItemsPrice();
         $article['taxPercent'] = 100 / $object->getGwItemsPrice() * $object->getGwItemsTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if ($object->getGwAddCard() > 0) {
         $article = array();
         $article['articleNumber'] = 'printed_card';
         $article['articleName'] = 'Printed Card';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $object->getGwCardPrice();
         $article['taxPercent'] = 100 / $object->getGwCardPrice() * $object->getGwCardTaxAmount();
         $article['discountId'] = '';
         $articles[] = $article;
     }
     if (Mage::getEdition() == 'Enterprise') {
         $_cards = Mage::getBlockSingleton('enterprise_giftcardaccount/checkout_cart_total')->getQuoteGiftCards();
         if ($_cards) {
             foreach ($_cards as $card) {
                 $article = array();
                 $article['articleNumber'] = 'gift_card';
                 $article['articleName'] = $card['c'];
                 $article['quantity'] = '1';
                 $article['unitPriceGross'] = -round($card['ba'], 2);
                 $article['taxPercent'] = 0;
                 $article['discountId'] = '';
                 $articles[] = $article;
             }
         }
     }
     if ($object instanceof Mage_Sales_Model_Order || $object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Creditmemo) {
         $shippingObject = $object;
         if ($object instanceof Mage_Sales_Model_Order_Creditmemo) {
             $articles = $this->addAdjustments($object, $articles);
         }
     } else {
         $shippingObject = $object->getShippingAddress();
     }
     if ($shippingObject->getShippingAmount() > 0) {
         if ($object instanceof Mage_Sales_Model_Order_Invoice || $object instanceof Mage_Sales_Model_Order_Shipment || $object instanceof Mage_Sales_Model_Order_Creditmemo) {
             $shippingDiscountAmount = $shippingObject->getDiscountAmount() - $articleDiscountAmount;
             $shippingDescription = $object->getOrder()->getShippingDescription();
         } else {
             $shippingDiscountAmount = $shippingObject->getShippingDiscountAmount();
             $shippingDescription = $shippingObject->getShippingDescription();
         }
         $article = array();
         $article['articleNumber'] = 'SHIPPING';
         $article['articleName'] = $shippingDescription;
         $article['quantity'] = '1';
         $article['unitPriceGross'] = $shippingObject->getShippingInclTax();
         $shippingTaxPercent = 0;
         if ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount() > 0) {
             $shippingTaxPercent = ($shippingObject->getShippingInclTax() - $shippingObject->getShippingAmount()) * 100 / $shippingObject->getShippingAmount();
         }
         $article['taxPercent'] = $shippingTaxPercent;
         $article['discountId'] = '';
         if ($shippingDiscountAmount > 0) {
             $discount = array();
             $discount['articleNumber'] = 'SHIPPINGDISCOUNT';
             $discount['articleName'] = 'Shipping - Discount';
             $discount['quantity'] = 1;
             $discount['unitPriceGross'] = -1 * $shippingObject->getShippingDiscountAmount();
             $discount['taxPercent'] = 0;
             $discount['discountId'] = 'SHIPPING';
         }
         $articles[] = $article;
         if ($shippingDiscountAmount > 0) {
             // only for sort reason
             $articles[] = $discount;
         }
     }
     if ($object->getRewardCurrencyAmount() > 0) {
         $article = array();
         $article['articleNumber'] = 'REWARDPOINTS';
         $article['articleName'] = 'Reward points';
         $article['quantity'] = '1';
         $article['unitPriceGross'] = -1 * $object->getRewardCurrencyAmount();
         $article['taxPercent'] = 0;
         $article['discountId'] = '';
         $articles[] = $article;
     }
     return $articles;
 }
Example #21
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 #22
0
 /**
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  *
  * @return Bronto_Common_Model_Email_Template_Filter
  */
 protected function _filterCreditmemo(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     if (!in_array('creditmemo', $this->_filteredObjects)) {
         $index = 1;
         foreach ($creditmemo->getAllItems() as $item) {
             $_item = $item->getOrderItem();
             if (!$_item->getParentItem()) {
                 $this->_filterOrderItem($_item, $index);
                 $index++;
             }
         }
         $createdAt = $creditmemo->getCreatedAtStoreDate();
         if (empty($createdAt)) {
             // unset the blank string to force current timestamp
             $createdAt = null;
         }
         // Add Related Content
         $this->_items = $creditmemo->getAllItems();
         $this->setField('creditmemoIncrementId', $creditmemo->getIncrementId());
         $this->setField('creditmemoCreatedAt', Mage::helper('core')->formatDate($createdAt, 'long', true));
         // TODO: needed?
         $this->setField('creditmemoItems', $this->_filterCreditmemoItems($creditmemo));
         $this->_filteredObjects[] = 'creditmemo';
     }
     return $this;
 }
Example #23
0
 /**
  * check credit memo is last or not
  * 
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  * @return boolean
  */
 public function isLast($creditmemo)
 {
     foreach ($creditmemo->getAllItems() as $item) {
         if (!$item->isLast()) {
             return false;
         }
     }
     return true;
 }
Example #24
0
 /**
  * @param Mage_Sales_Model_Order_Creditmemo $creditmemo
  *
  * @return Mage_Sales_Model_Order_Creditmemo_Item[]
  */
 public function getActionableCreditmemoItems(Mage_Sales_Model_Order_Creditmemo $creditmemo)
 {
     /** @var Mage_Sales_Model_Order_Creditmemo_Item[] $items */
     $items = $creditmemo->getAllItems();
     foreach ($items as $k => $item) {
         if ($item->getOrderItem()->isDummy()) {
             unset($items[$k]);
         }
     }
     return $items;
 }
 public function collect(Mage_Sales_Model_Order_Creditmemo $creditMemo)
 {
     $currencyFrom = Mage::getModel('directory/currency')->load($creditMemo->getOrderCurrencyCode());
     $currencyTo = $creditMemo->getStore()->getBaseCurrency();
     /* @var $helper Dutycalculator_Charge_Helper_Data */
     $helper = Mage::helper('dccharge');
     $creditMemo->setImportDutyTax(0);
     $creditMemo->setBaseImportDutyTax(0);
     $creditMemo->setImportDuty(0);
     $creditMemo->setBaseImportDuty(0);
     $creditMemo->setSalesTax(0);
     $creditMemo->setBaseSalesTax(0);
     $creditMemo->setDeliveryDutyType($creditMemo->getOrder()->getDeliveryDutyType());
     $creditMemo->setFailedCalculation($creditMemo->getOrder()->getFailedCalculation());
     $creditMemo->setDcOrderId(0);
     foreach ($creditMemo->getAllItems() as $creditMemoItem) {
         $creditMemoItem->setImportDutyTax(0);
         $creditMemoItem->setBaseImportDutyTax(0);
         $creditMemoItem->setImportDuty(0);
         $creditMemoItem->setBaseImportDuty(0);
         $creditMemoItem->setSalesTax(0);
         $creditMemoItem->setBaseSalesTax(0);
     }
     if ($creditMemo->getOrder()->getDcOrderId()) {
         $result = Dutycalculator_Charge_Model_Importdutytaxes::creditMemoCalculation($creditMemo);
         if ($result) {
             $amountToRefund = $result['total'];
             $baseAmountToRefund = $helper->convertPrice($currencyFrom, $currencyTo, $result['total']);
             $creditMemo->setDeliveryDutyType($creditMemo->getOrder()->getDeliveryDutyType());
             $creditMemo->setFailedCalculation($creditMemo->getOrder()->getFailedCalculation());
             $creditMemo->setImportDutyTax($result['total']);
             $creditMemo->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['total']));
             $creditMemo->setImportDuty($result['duty']);
             $creditMemo->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['duty']));
             $creditMemo->setSalesTax($result['sales_tax']);
             $creditMemo->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['sales_tax']));
             $creditMemo->setDcOrderId($result['dc_order_id']);
             if ($creditMemo->getOrder()->getDeliveryDutyType() == Dutycalculator_Charge_Helper_Data::DC_DELIVERY_TYPE_DDP) {
                 $creditMemo->setGrandTotal($creditMemo->getGrandTotal() + $amountToRefund);
                 $creditMemo->setBaseGrandTotal($creditMemo->getBaseGrandTotal() + $baseAmountToRefund);
                 $aggregatedItemsValues = array();
                 foreach ($creditMemo->getAllItems() as $creditMemoItem) {
                     if ($creditMemoItem->getOrderItem()->getParentItemId()) {
                         continue;
                     }
                     $id = $creditMemoItem->getOrderItem()->getQuoteItemId();
                     if (isset($result['items'][$id])) {
                         $creditMemoItem->setImportDutyTax($result['items'][$id]['total']);
                         $creditMemoItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['total']));
                         $creditMemoItem->setImportDuty($result['items'][$id]['duty']);
                         $creditMemoItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['duty']));
                         $creditMemoItem->setSalesTax($result['items'][$id]['sales_tax']);
                         $creditMemoItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $result['items'][$id]['sales_tax']));
                     } else {
                         foreach ($result['aggregated_items'] as $key => $_items) {
                             if (in_array($id, $_items['items'])) {
                                 $aggregatedItemsValues[$key][$id] = $creditMemoItem->getRowTotal();
                             }
                         }
                     }
                 }
                 $totals = array();
                 $totalDuty = array();
                 $totalSalesTaxes = array();
                 foreach ($aggregatedItemsValues as $key => $aggregatedItemsValue) {
                     $aggregatedTotal = $result['aggregated_items'][$key]['aggregated_total'];
                     $aggregatedDuty = $result['aggregated_items'][$key]['aggregated_duty'];
                     $aggregatedSalesTax = $result['aggregated_items'][$key]['aggregated_sales_tax'];
                     $totalAggregatedItemsValue = array_sum($aggregatedItemsValue);
                     foreach ($aggregatedItemsValue as $itemId => $value) {
                         $totals[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedTotal, 2);
                         $totalDuty[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedDuty, 2);
                         $totalSalesTaxes[$itemId] = round($value / $totalAggregatedItemsValue * $aggregatedSalesTax, 2);
                     }
                 }
                 foreach ($creditMemo->getAllItems() as $creditMemoItem) {
                     if ($creditMemoItem->getOrderItem()->getParentItemId()) {
                         continue;
                     }
                     $id = $creditMemoItem->getOrderItem()->getQuoteItemId();
                     if (isset($taxes[$id])) {
                         $creditMemoItem->setImportDutyTax($totals[$id]);
                         $creditMemoItem->setBaseImportDutyTax($helper->convertPrice($currencyFrom, $currencyTo, $totals[$id]));
                         $creditMemoItem->setImportDuty($totalDuty[$id]);
                         $creditMemoItem->setBaseImportDuty($helper->convertPrice($currencyFrom, $currencyTo, $totalDuty[$id]));
                         $creditMemoItem->setSalesTax($totalSalesTaxes[$id]);
                         $creditMemoItem->setBaseSalesTax($helper->convertPrice($currencyFrom, $currencyTo, $totalSalesTaxes[$id]));
                     }
                     if ($creditMemoItem->getQty() == 0) {
                         $creditMemoItem->setImportDutyTax(0);
                         $creditMemoItem->setBaseImportDutyTax(0);
                         $creditMemoItem->setImportDuty(0);
                         $creditMemoItem->setBaseImportDuty(0);
                         $creditMemoItem->setSalesTax(0);
                         $creditMemoItem->setBaseSalesTax(0);
                     }
                 }
             }
         }
     }
     return $this;
 }
 public static function creditMemoCalculation(Mage_Sales_Model_Order_Creditmemo $creditMemo)
 {
     $params = array();
     /* @var $helper Dutycalculator_Charge_Helper_Data */
     $helper = Mage::helper('dccharge');
     $creditMemoItems = $creditMemo->getAllItems();
     $params['calculation_id'] = $creditMemo->getOrder()->getDcOrderId();
     $params['shipping'] = (double) $creditMemo->getShippingAmount();
     $params['output_currency'] = $creditMemo->getOrderCurrencyCode();
     $params['cat'] = array();
     $params['qty'] = array();
     $params['reference'] = array();
     $idx = 0;
     $itemsToSend = 0;
     foreach ($creditMemoItems as $creditMemoItem) {
         $orderItem = $creditMemoItem->getOrderItem();
         $product = Mage::getModel('catalog/product')->load($orderItem->getProductId());
         $qty = $creditMemoItem->getQty();
         if ($orderItem->getParentItemId() || !$orderItem->getQuoteItemId() || $product->isVirtual() || $qty <= 0) {
             continue;
         }
         $itemsToSend++;
         /* @var $creditMemoItem Mage_Sales_Model_Order_Creditmemo_Item */
         /* @var $product Mage_Catalog_Model_Product */
         $params['qty'][$idx] = (double) $qty;
         $params['reference'][$idx] = $orderItem->getQuoteItemId();
         if ($product->getDcProductId()) {
             $params['cat'][$idx] = $product->getDcProductId();
         } else {
             $params['cat'][$idx] = '';
         }
         $idx++;
     }
     if ($itemsToSend > 0) {
         $rawXml = $helper->sendRequest('credit_note_calculation', $params);
         try {
             if (stripos($rawXml, '<?xml') === false) {
                 throw new Exception($rawXml);
             }
             $answer = new SimpleXMLElement($rawXml);
             $answerAttributes = $answer->attributes();
             $dcOrderId = (int) $answerAttributes['id'];
             $totals = current($answer->xpath('total-charges'));
             $items = $answer->xpath('item');
             $result = array();
             $result['dc_order_id'] = $dcOrderId;
             $result['total'] = (double) $totals->total->amount;
             $result['duty'] = (double) $totals->duty->amount;
             $additionalTaxes = $totals->xpath('additional-import-taxes');
             if ($additionalTaxes) {
                 $additionalTaxes = current($additionalTaxes);
                 foreach ($additionalTaxes->tax as $additionalTax) {
                     $result['duty'] += (double) $additionalTax->amount;
                 }
             }
             $result['sales_tax'] = (double) current($totals->xpath('sales-tax'))->amount;
             $result['items'] = array();
             $result['aggregated_items'] = array();
             foreach ($items as $item) {
                 $attributes = $item->attributes();
                 $references = explode(',', (string) $attributes->reference);
                 if (count($references) > 1) {
                     $total = (double) $item->total->amount;
                     $duty = (double) $item->duty->amount;
                     $additionalTaxes = $item->xpath('additional-import-taxes');
                     if ($additionalTaxes) {
                         $additionalTaxes = current($additionalTaxes);
                         foreach ($additionalTaxes->tax as $additionalTax) {
                             $duty += (double) $additionalTax->amount;
                         }
                     }
                     $salesTax = (double) current($item->xpath('sales-tax'))->amount;
                     $result['aggregated_items'][(string) $attributes->reference] = array('items' => $references, 'aggregated_total' => $total, 'aggregated_duty' => $duty, 'aggregated_sales_tax' => $salesTax);
                 } else {
                     $total = (double) $item->total->amount;
                     $duty = (double) $item->duty->amount;
                     $additionalTaxes = $item->xpath('additional-import-taxes');
                     if ($additionalTaxes) {
                         $additionalTaxes = current($additionalTaxes);
                         foreach ($additionalTaxes->tax as $additionalTax) {
                             $duty += (double) $additionalTax->amount;
                         }
                     }
                     $salesTax = (double) current($item->xpath('sales-tax'))->amount;
                     $result['items'][(string) $attributes->reference] = array('total' => $total, 'duty' => $duty, 'sales_tax' => $salesTax);
                 }
             }
             return $result;
             //(float)$totals->total->amount;
         } catch (Exception $ex) {
             $result = array();
             $result['failed_calculation'] = 1;
             $result['dc_order_id'] = 0;
             $result['total'] = 0;
             $result['duty'] = 0;
             $result['sales_tax'] = 0;
             $result['items'] = array();
             $result['aggregated_items'] = array();
             return $result;
         }
     } else {
         return false;
     }
 }