/** * @return array */ protected function _prepareOrderData() { // magento 1.5 compat $shipping_method_c = $this->_order->getShippingMethod(true); $shipping_method = $this->_order->getData('shipping_method'); $shipping_method_code = $shipping_method_c ? $shipping_method_c->getData('carrier_code') : $shipping_method; $data = array_filter(array('currency_code' => $this->_order->getOrderCurrencyCode(), 'shipping_method_code' => $shipping_method_code, 'shipping_method_title' => $this->_order->getShippingDescription(), 'created_on' => $this->_order->getCreatedAt(), 'updated_on' => $this->_order->getUpdatedAt(), 'state' => $this->_order->getState(), 'status' => $this->_order->getStatus(), 'is_gift' => $this->_order->getGiftMessageId() != null, 'ref_quote_id' => $this->_order->getQuoteId(), 'order_subtotal_with_tax' => $this->_order->getSubtotalInclTax(), 'order_subtotal' => $this->_order->getSubtotal(), 'order_tax' => $this->_order->getTaxAmount(), 'order_hidden_tax' => $this->_order->getHiddenTaxAmount(), 'order_shipping_with_tax' => $this->_order->getShippingInclTax(), 'order_shipping' => $this->_order->getShippingAmount(), 'order_discount' => $this->_order->getDiscountAmount(), 'order_shipping_discount' => $this->_order->getShippingDiscountAmount(), 'order_total' => $this->_order->getGrandTotal(), 'order_total_items' => $this->_order->getTotalItemCount())); return $data; }
public static function createFromOrder(Mage_Sales_Model_Order $order) { $address = $order->getShippingAddress(); $shippingInfo = new self(); $shippingInfo->first_name = $address->getFirstname(); $shippingInfo->last_name = $address->getLastname(); $shippingInfo->street = $address->getStreet(1); $shippingInfo->city = $address->getCity(); $shippingInfo->state = $address->getRegion(); $shippingInfo->country = $address->getCountryModel()->getIso2Code(); $shippingInfo->postcode = $address->getPostcode(); $shippingInfo->name = $order->getShippingMethod(); $shippingInfo->price = Aplazame_Sdk_Serializer_Decimal::fromFloat($order->getShippingAmount()); $shippingInfo->phone = $address->getTelephone(); $shippingInfo->alt_phone = $address->getAltTelephone(); $shippingInfo->address_addition = $address->getStreet(2); if ($order->getShippingAmount() > 0) { $shippingInfo->tax_rate = Aplazame_Sdk_Serializer_Decimal::fromFloat(100 * $order->getShippingTaxAmount() / $order->getShippingAmount()); } $shippingInfo->discount = Aplazame_Sdk_Serializer_Decimal::fromFloat($order->getShippingDiscountAmount()); return $shippingInfo; }
/** * Add PayEx Single Order Line * @param string $orderRef * @param Mage_Sales_Model_Order $order * @return bool */ public function addOrderLine($orderRef, $order) { // add Order Items $items = $order->getAllVisibleItems(); $i = 1; /** @var $item Mage_Sales_Model_Order_Item */ foreach ($items as $item) { // @todo Calculate prices using Discount Rules // @todo Get children products from bundle //if (!$item->getNoDiscount()) { // Mage::helper('payexautopay/tools')->addToDebug('Warning: The product has a discount. There might be problems.', $order->getIncrementId()); //} $itemQty = (int) $item->getQtyOrdered(); //$taxPrice = $item->getTaxAmount(); $taxPrice = $itemQty * $item->getPriceInclTax() - $itemQty * $item->getPrice(); $taxPercent = $item->getTaxPercent(); $priceWithTax = $itemQty * $item->getPriceInclTax(); // Calculate tax percent for Bundle products if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $taxPercent = $taxPrice > 0 ? round(100 / (($priceWithTax - $taxPrice) / $taxPrice)) : 0; } $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $item->getName(), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => $itemQty, 'amount' => (int) (100 * $priceWithTax), 'vatPrice' => (int) (100 * $taxPrice), 'vatPercent' => (int) (100 * $taxPercent)); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // add Shipping if (!$order->getIsVirtual()) { $shipping = $order->getShippingAmount(); $shippingIncTax = $order->getShippingInclTax(); $shippingTax = $order->getShippingTaxAmount(); $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $order->getShippingDescription(), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => (int) (100 * $shippingIncTax), 'vatPrice' => (int) (100 * $shippingTax), 'vatPercent' => $shipping != 0 ? round(100 * 100 * $shippingTax / $shipping) : 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // add Discount $discount = $order->getDiscountAmount() + $order->getShippingDiscountAmount(); if (abs($discount) > 0) { $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => $order->getDiscountDescription() !== null ? Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription()) : Mage::helper('sales')->__('Discount'), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => (int) (100 * $discount), 'vatPrice' => 0, 'vatPercent' => 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } // Add reward points if ((double) $order->getBaseRewardCurrencyAmount() > 0) { $params = array('accountNumber' => '', 'orderRef' => $orderRef, 'itemNumber' => $i, 'itemDescription1' => Mage::helper('payexautopay')->__('Reward points'), 'itemDescription2' => '', 'itemDescription3' => '', 'itemDescription4' => '', 'itemDescription5' => '', 'quantity' => 1, 'amount' => -1 * (int) (100 * $order->getBaseRewardCurrencyAmount()), 'vatPrice' => 0, 'vatPercent' => 0); $result = Mage::helper('payexautopay/api')->getPx()->AddSingleOrderLine2($params); Mage::helper('payexautopay/tools')->debugApi($result, 'PxOrder.AddSingleOrderLine2'); $i++; } return true; }
/** * Function to persist orders to be flushed to Bronto * * @param Mage_Sales_Model_Order $order * @param Bronto_Api_Order $brontoOrder * @param Bronto_Order_Model_Queue $orderRow * @param array $context */ protected function _persistOrder($order, $brontoOrder, $orderRow, $context) { extract($context); // Get visible items from order $items = $order->getAllVisibleItems(); // Keep product order by using a new array $fullItems = array(); $brontoOrderItems = array(); // loop through the items. if it's a bundled item, // replace the parent item with the child items. foreach ($items as $item) { $itemProduct = Mage::getModel('catalog/product')->load($item->getProductId()); // Handle product based on product type switch ($itemProduct->getTypeId()) { // Bundled products need child items case Mage_Catalog_Model_Product_Type::TYPE_BUNDLE: if (count($item->getChildrenItems()) > 0) { foreach ($item->getChildrenItems() as $childItem) { if ($childItem->getPrice() != 0) { $item->setPrice(0); } $fullItems[] = $childItem; } } $fullItems[] = $item; break; // Configurable products just need simple config item // Configurable products just need simple config item case Mage_Catalog_Model_Product_Type::TYPE_CONFIGURABLE: $childItems = $item->getChildrenItems(); if (1 === count($childItems)) { $childItem = $childItems[0]; // Collect options applicable to the configurable product $productAttributeOptions = $itemProduct->getTypeInstance(true)->getConfigurableAttributesAsArray($itemProduct); // Build Selected Options Name $nameWithOptions = array(); foreach ($productAttributeOptions as $productAttribute) { $itemValue = $productHelper->getProductAttribute($childItem->getProductId(), $productAttribute['attribute_code'], $storeId); $nameWithOptions[] = $productAttribute['label'] . ': ' . $itemValue; } // Set parent product name to include selected options $parentName = $item->getName() . ' [' . implode(', ', $nameWithOptions) . ']'; $item->setName($parentName); } $fullItems[] = $item; break; // Grouped products need parent and child items // Grouped products need parent and child items case Mage_Catalog_Model_Product_Type::TYPE_GROUPED: // This condition probably never gets hit, parent grouped items don't show in order $fullItems[] = $item; foreach ($item->getChildrenItems() as $child_item) { $fullItems[] = $child_item; } break; // Anything else (namely simples) just get added to array // Anything else (namely simples) just get added to array default: $fullItems[] = $item; break; } } // Cycle through newly created array of products foreach ($fullItems as $item) { // If product has a parent, get that parent product $parent = false; if ($item->getParentItem()) { $parent = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getParentItem()->getProductId()); } /* @var $product Mage_Catalog_Model_Product */ $product = Mage::getModel('catalog/product')->setStoreId($storeId)->load($item->getProductId()); // If there is a parent product, use that to get category ids if ($parent) { $categoryIds = $parent->getCategoryIds(); } else { $categoryIds = $product->getCategoryIds(); } // If the product type is simple and the description // is empty, then attempt to find a parent product // to backfill the description. $parentProduct = $productHelper->getConfigurableProduct($product); if (!$product->getData($descriptionAttr)) { $product->setData($descriptionAttr, $parentProduct->getData($descriptionAttr)); } if (empty($categoryIds)) { $categoryIds = $parentProduct->getCategoryIds(); } // Cycle through category ids to pull category details $categories = array(); foreach ($categoryIds as $categoryId) { /* @var $category Mage_Catalog_Model_Category */ $category = Mage::getModel('catalog/category')->load($categoryId); $parent = $category->getParentCategory(); $categories[] = $parent->getUrlKey() ? $parent->getUrlKey() : $parent->formatUrlKey($parent->getName()); $categories[] = $category->getUrlKey() ? $category->getUrlKey() : $category->formatUrlKey($category->getName()); } // Check to ensure there are no duplicate categories $categories = array_unique($categories); // Write orderItem $brontoOrderItems[] = array('id' => $item->getId(), 'sku' => $item->getSku(), 'name' => $item->getName(), 'description' => $product->getData($descriptionAttr), 'category' => implode(' ', $categories), 'image' => $this->_helper->getItemImg($item, $product, $storeId), 'url' => $this->_helper->getItemUrl($item, $product, $storeId), 'quantity' => (int) $item->getQtyOrdered(), 'price' => $this->_helper->getItemPrice($item, $basePrefix, $inclTaxes, $inclDiscounts)); } if ($inclShipping && $order->getState() == Mage_Sales_Model_Order::STATE_COMPLETE && $order->hasShipments()) { $shippingObject = new Varien_Object(array('qty_ordered' => 1, 'base_row_total' => $order->getBaseShippingAmount(), 'row_total' => $order->getShippingAmount(), 'base_tax_amount' => $order->getBaseShippingTaxAmount(), 'tax_amount' => $order->getShippingTaxAmount(), 'base_discount_amount' => $order->getBaseShippingDiscountAmount(), 'discount_amount' => $order->getShippingDiscountAmount())); $descriptions = array(); foreach ($order->getTracksCollection() as $track) { if ($track->hasTrackNumber() && $track->hasTitle()) { $descriptions[] = "{$track->getTitle()} - {$track->getTrackNumber()}"; } } $shipmentItem = array('sku' => 'SHIPPING', 'name' => $order->getShippingDescription(), 'description' => implode("<br/>", $descriptions), 'quantity' => 1, 'price' => $this->_helper->getItemPrice($shippingObject, $basePrefix, $inclTaxes, $inclDiscounts)); $brontoOrderItems[] = $shipmentItem; } $brontoOrder->products = $brontoOrderItems; $brontoOrder->persist(); }
/** * Gets the total discount from $order * inkl. and excl. tax * Data is returned as a Varien_Object with these data-keys set: * - discount_incl_tax * - discount_excl_tax * * @param Mage_Sales_Model_Order $order * * @return Varien_Object */ public function getOrderDiscountData(Mage_Sales_Model_Order $order) { // if catalog-prices includes tax $CatPriceIncl = Mage::getStoreConfig(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $order->getStore()); $discountIncl = 0; $discountExcl = 0; // find discount on the items foreach ($order->getItemsCollection() as $item) { /** @var Mage_Sales_Model_Quote_Item $item */ if (!$CatPriceIncl) { $discountExcl += $item->getDiscountAmount(); $discountIncl += $item->getDiscountAmount() * ($item->getTaxPercent() / 100 + 1); } else { $discountExcl += $item->getDiscountAmount() / ($item->getTaxPercent() / 100 + 1); $discountIncl += $item->getDiscountAmount(); } } // find out tax-rate for the shipping if ((double) $order->getShippingInclTax() && (double) $order->getShippingAmount()) { $shippingTaxRate = $order->getShippingInclTax() / $order->getShippingAmount(); } else { $shippingTaxRate = 1; } // get discount amount for shipping $shippingDiscount = (double) $order->getShippingDiscountAmount(); // apply/remove tax to shipping-discount if (!$CatPriceIncl) { $discountIncl += $shippingDiscount * $shippingTaxRate; $discountExcl += $shippingDiscount; } else { $discountIncl += $shippingDiscount; $discountExcl += $shippingDiscount / $shippingTaxRate; } $return = new Varien_Object(); return $return->setDiscountInclTax($discountIncl)->setDiscountExclTax($discountExcl); }
/** * Generate Invoice Print XML * @param Mage_Sales_Model_Order $order * @return mixed */ public function getInvoiceExtraPrintBlocksXML($order) { $dom = new DOMDocument('1.0', 'utf-8'); $OnlineInvoice = $dom->createElement('OnlineInvoice'); $dom->appendChild($OnlineInvoice); $OnlineInvoice->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); $OnlineInvoice->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xsd', 'http://www.w3.org/2001/XMLSchema'); $OrderLines = $dom->createElement('OrderLines'); $OnlineInvoice->appendChild($OrderLines); // Add Order Lines $items = $order->getAllVisibleItems(); /** @var $item Mage_Sales_Model_Order_Item */ foreach ($items as $item) { // @todo Calculate prices using Discount Rules // @todo Get children products from bundle //if (!$item->getNoDiscount()) { // Mage::helper('partpayment/tools')->addToDebug('Warning: The product has a discount. There might be problems.', $order->getIncrementId()); //} $itemQty = (int) $item->getQtyOrdered(); //$taxPrice = $item->getTaxAmount(); $taxPrice = $itemQty * $item->getPriceInclTax() - $itemQty * $item->getPrice(); $taxPercent = $item->getTaxPercent(); $priceWithTax = $itemQty * $item->getPriceInclTax(); // Calculate tax percent for Bundle products if ($item->getProductType() === Mage_Catalog_Model_Product_Type::TYPE_BUNDLE) { $taxPercent = $taxPrice > 0 ? round(100 / (($priceWithTax - $taxPrice) / $taxPrice)) : 0; } $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $item->getName())); $OrderLine->appendChild($dom->createElement('Qty', $itemQty)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $item->getPrice()))); $OrderLine->appendChild($dom->createElement('VatRate', sprintf("%.2f", $taxPercent))); $OrderLine->appendChild($dom->createElement('VatAmount', sprintf("%.2f", $taxPrice))); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $priceWithTax))); $OrderLines->appendChild($OrderLine); } // Add Shipping Line if (!$order->getIsVirtual()) { $shipping = $order->getShippingAmount(); //$shippingIncTax = $order->getShippingInclTax(); $shippingTax = $order->getShippingTaxAmount(); $shippingTaxPercent = $shipping != 0 ? (int) (100 * $shippingTax / $shipping) : 0; $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $order->getShippingDescription())); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $shipping))); $OrderLine->appendChild($dom->createElement('VatRate', sprintf("%.2f", $shippingTaxPercent))); $OrderLine->appendChild($dom->createElement('VatAmount', sprintf("%.2f", $shippingTax))); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $shipping + $shippingTax))); $OrderLines->appendChild($OrderLine); } // add Payment Fee $fee = $order->getPartpaymentPaymentFee(); if ($fee > 0) { $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', Mage::helper('partpayment')->__('Payment fee'))); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $fee))); $OrderLine->appendChild($dom->createElement('VatRate', 0)); $OrderLine->appendChild($dom->createElement('VatAmount', 0)); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $fee))); $OrderLines->appendChild($OrderLine); } // add Discount $discount = $order->getDiscountAmount() + $order->getShippingDiscountAmount(); if (abs($discount) > 0) { $discount_description = $order->getDiscountDescription() !== null ? Mage::helper('sales')->__('Discount (%s)', $order->getDiscountDescription()) : Mage::helper('sales')->__('Discount'); $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', $discount_description)); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', sprintf("%.2f", $discount))); $OrderLine->appendChild($dom->createElement('VatRate', 0)); $OrderLine->appendChild($dom->createElement('VatAmount', 0)); $OrderLine->appendChild($dom->createElement('Amount', sprintf("%.2f", $discount))); $OrderLines->appendChild($OrderLine); } // Add reward points if ((double) $order->getBaseRewardCurrencyAmount() > 0) { $OrderLine = $dom->createElement('OrderLine'); $OrderLine->appendChild($dom->createElement('Product', Mage::helper('partpayment')->__('Reward points'))); $OrderLine->appendChild($dom->createElement('Qty', 1)); $OrderLine->appendChild($dom->createElement('UnitPrice', -1 * $order->getBaseRewardCurrencyAmount())); $OrderLine->appendChild($dom->createElement('VatRate', 0)); $OrderLine->appendChild($dom->createElement('VatAmount', 0)); $OrderLine->appendChild($dom->createElement('Amount', -1 * $order->getBaseRewardCurrencyAmount())); $OrderLines->appendChild($OrderLine); } return str_replace("\n", '', $dom->saveXML()); }