/** * Set entity * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo $object * @return $this */ public function setEntity($object) { $this->setEntityId($object->getId()); $this->setEntityIncrementId($object->getIncrementId()); $this->setStoreId($object->getStoreId()); return $this; }
/** * Get gift wrapping tax class id * * @param Mage_Sales_Model_Order_Invoice|Mage_Sales_Model_Order_Creditmemo|Mage_Sales_Model_Quote_Address $object * @return int */ protected function _getGwTaxClassId($object) { if (!$object->getGwPrice() && !$object->getGwItemsPrice() && !$object->getGwPrintedCardPrice()) { return 0; } if ($object instanceof Mage_Sales_Model_Quote_Address) { $storeId = $object->getQuote()->getStoreId(); } else { $storeId = $object->getStoreId(); } return $this->_getWrappingTaxClass($storeId); }
/** * Generate and send a RefundInvoice * * @param Aoe_AvaTax_Model_Api $api * @param Mage_Sales_Model_Order_Creditmemo $creditmemo * * @return string * @throws Aoe_AvaTax_Exception * @throws Exception */ public function registerCreditmemo(Aoe_AvaTax_Model_Api $api, Mage_Sales_Model_Order_Creditmemo $creditmemo) { if (!$this->isActive($creditmemo->getStore())) { return null; } $incrementId = $creditmemo->getIncrementId(); if (empty($incrementId)) { /* @var $entityType Mage_Eav_Model_Entity_Type */ $entityType = Mage::getModel('eav/entity_type')->loadByCode('creditmemo'); $creditmemo->setIncrementId($entityType->fetchNewIncrementId($creditmemo->getStoreId())); } $result = $api->callGetTaxForCreditmemo($creditmemo, true); if ($result['ResultCode'] !== 'Success') { throw new Aoe_AvaTax_Exception($result['ResultCode'], $result['Messages']); } // NB: Ignoring the returned tax data on purpose $creditmemo->getResource()->beginTransaction(); try { $creditmemo->setAvataxDocument($result['DocCode']); $creditmemo->addComment(sprintf('RefundInvoice sent to AvaTax (%s)', $result['DocCode'])); $creditmemo->save(); $creditmemo->getOrder()->addStatusHistoryComment(sprintf('RefundInvoice sent to AvaTax (%s)', $result['DocCode'])); $creditmemo->getOrder()->save(); $creditmemo->getResource()->commit(); } catch (Exception $e) { $creditmemo->getResource()->rollBack(); throw $e; } return $result['DocCode']; }
/** * 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; }