/** * get alias or generate a new one * * alias has length 16 and consists of quote creation date, a separator, * and the quote id to make sure we have the full quote id we shorten * the creation date accordingly * * @param Mage_Sales_Model_Quote $quote * * @return string */ public function getAlias($quote) { $alias = $quote->getPayment()->getAdditionalInformation('alias'); if (0 == strlen($alias)) { /* turn createdAt into format MMDDHHii */ $createdAt = substr(str_replace(array(':', '-', ' '), '', $quote->getCreatedAt()), 4, -2); $quoteId = $quote->getId(); /* shorten createdAt, if we would exceed maximum length */ $maxAliasLength = 16; $separator = '99'; $maxCreatedAtLength = $maxAliasLength - strlen($quoteId) - strlen($separator); $alias = substr($createdAt, 0, $maxCreatedAtLength) . $separator . $quoteId; } if ($this->isAdminSession() && !strpos($alias, 'BE')) { $alias = $alias . 'BE'; } return $alias; }
public function callGetTaxForQuote(Mage_Sales_Model_Quote $quote) { /** @var Aoe_AvaTax_Helper_Soap $helper */ $helper = Mage::helper('Aoe_AvaTax/Soap'); $address = $quote->getShippingAddress(); if ($address->validate() !== true) { $resultArray = array('ResultCode' => 'Skip', 'Messages' => array(), 'TaxLines' => array()); return $resultArray; } $store = $quote->getStore(); $hideDiscountAmount = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_APPLY_AFTER_DISCOUNT, $store); $timestamp = $quote->getCreatedAt() ? Varien_Date::toTimestamp($quote->getCreatedAt()) : now(); $date = new Zend_Date($timestamp); $request = new AvaTax\GetTaxRequest(); $request->setCompanyCode($this->limit($helper->getConfig('company_code', $store), 25)); $request->setDocType(AvaTax\DocumentType::$SalesOrder); $request->setCommit(false); $request->setDetailLevel(AvaTax\DetailLevel::$Tax); $request->setDocDate($date->toString('yyyy-MM-dd')); $request->setCustomerCode($helper->getCustomerDocCode($quote->getCustomer()) ?: $helper->getQuoteDocCode($quote)); $request->setCurrencyCode($this->limit($quote->getBaseCurrencyCode(), 3)); $request->setDiscount($hideDiscountAmount ? 0.0 : $store->roundPrice($address->getBaseDiscountAmount())); if ($quote->getCustomerTaxvat()) { $request->setBusinessIdentificationNo($this->limit($quote->getCustomerTaxvat(), 25)); } $request->setOriginAddress($this->getOriginAddress($store)); $request->setDestinationAddress($this->getAddress($address)); $taxLines = array(); $itemPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_PRICE_INCLUDES_TAX, $store); foreach ($this->getHelper()->getActionableQuoteAddressItems($address) as $k => $item) { /** @var Mage_Sales_Model_Quote_Item|Mage_Sales_Model_Quote_Address_Item $item */ $itemAmount = $store->roundPrice($itemPriceIncludesTax ? $item->getBaseRowTotalInclTax() : $item->getBaseRowTotal()); //$itemAmount = $store->roundPrice($item->getBaseRowTotal()); $itemAmount -= $store->roundPrice($item->getBaseDiscountAmount()); $taxLine = new AvaTax\Line(); $taxLine->setNo($this->limit($k, 50)); $taxLine->setItemCode($this->limit($item->getSku(), 50)); $taxLine->setQty(round($item->getQty(), 4)); $taxLine->setAmount($itemAmount); $taxLine->setDescription($this->limit($item->getName(), 255)); $taxLine->setTaxCode($this->limit($helper->getProductTaxCode($item->getProduct()), 25)); $taxLine->setDiscounted($item->getBaseDiscountAmount() > 0.0); $taxLine->setTaxIncluded($itemPriceIncludesTax); $taxLine->setRef1($this->limit($helper->getQuoteItemRef1($item, $store), 250)); $taxLine->setRef2($this->limit($helper->getQuoteItemRef2($item, $store), 250)); $taxLines[] = $taxLine; } $shippingPriceIncludesTax = Mage::getStoreConfigFlag(Mage_Tax_Model_Config::CONFIG_XML_PATH_SHIPPING_INCLUDES_TAX, $store); $shippingAmount = $store->roundPrice($shippingPriceIncludesTax ? $address->getBaseShippingInclTax() : $address->getBaseShippingAmount()); //$shippingAmount = $store->roundPrice($address->getBaseShippingAmount()); $shippingAmount -= $store->roundPrice($address->getBaseShippingDiscountAmount()); $taxLine = new AvaTax\Line(); $taxLine->setNo('SHIPPING'); $taxLine->setItemCode('SHIPPING'); $taxLine->setQty(1); $taxLine->setAmount($shippingAmount); $taxLine->setDescription($this->limit("Shipping: " . $address->getShippingMethod(), 255)); $taxLine->setTaxCode($this->limit($helper->getShippingTaxCode($store), 25)); $taxLine->setDiscounted($address->getBaseShippingDiscountAmount() > 0.0); $taxLine->setTaxIncluded($shippingPriceIncludesTax); $taxLine->setRef1($this->limit($address->getShippingMethod(), 25)); $taxLines[] = $taxLine; $request->setLines($taxLines); Mage::dispatchEvent('aoe_avatax_soapapi_get_tax_for_quote_before', array('request' => $request, 'quote' => $quote)); // TODO: Handle giftwrapping return $this->callGetTax($store, $request); }
/** * set the quote information * @param Mage_Sales_Model_Quote $quoteData */ public function __construct(Mage_Sales_Model_Quote $quoteData) { $customerModel = Mage::getModel('customer/customer'); $customerModel->load($quoteData->getCustomerId()); $this->id = $quoteData->getId(); $this->email = $quoteData->getCustomerEmail(); $this->store_name = $quoteData->getStore()->getName(); $created_at = new Zend_Date($quoteData->getCreatedAt(), Zend_Date::ISO_8601); $this->created_date = $created_at->toString(Zend_Date::ISO_8601); if ($quoteData->getShippingAddress()) { $this->delivery_method = $quoteData->getShippingAddress()->getShippingDescription(); $this->delivery_total = $quoteData->getShippingAddress()->getShippingAmount(); } $this->currency = $quoteData->getStoreCurrencyCode(); if ($payment = $quoteData->getPayment()) { $this->payment = $payment->getMethod(); } $this->couponCode = $quoteData->getCouponCode(); /** * custom quote attributes */ $helper = Mage::helper('ddg'); $website = Mage::app()->getStore($quoteData->getStore())->getWebsite(); $customAttributes = $helper->getConfigSelectedCustomQuoteAttributes($website); if ($customAttributes) { $fields = Mage::getResourceModel('ddg_automation/quote')->getQuoteTableDescription(); foreach ($customAttributes as $customAttribute) { if (isset($fields[$customAttribute])) { $field = $fields[$customAttribute]; $value = $this->_getCustomAttributeValue($field, $quoteData); if ($value) { $this->_assignCustom($field, $value); } } } } /** * Billing address. */ if ($quoteData->getBillingAddress()) { $billingData = $quoteData->getBillingAddress()->getData(); $this->billing_address = array('billing_address_1' => $this->_getStreet($billingData['street'], 1), 'billing_address_2' => $this->_getStreet($billingData['street'], 2), 'billing_city' => $billingData['city'], 'billing_region' => $billingData['region'], 'billing_country' => $billingData['country_id'], 'billing_postcode' => $billingData['postcode']); } /** * Shipping address. */ if ($quoteData->getShippingAddress()) { $shippingData = $quoteData->getShippingAddress()->getData(); $this->delivery_address = array('delivery_address_1' => $this->_getStreet($shippingData['street'], 1), 'delivery_address_2' => $this->_getStreet($shippingData['street'], 2), 'delivery_city' => $shippingData['city'], 'delivery_region' => $shippingData['region'], 'delivery_country' => $shippingData['country_id'], 'delivery_postcode' => $shippingData['postcode']); } /** * Quote items. * @var Mage_Sales_Model_Quote_Item $productItem */ foreach ($quoteData->getAllItems() as $productItem) { $product = $productItem->getProduct(); if ($product) { // category names $categoryCollection = $product->getCategoryCollection()->addAttributeToSelect('name'); foreach ($categoryCollection as $cat) { $categories = array(); $categories[] = $cat->getName(); $this->categories[]['Name'] = substr(implode(', ', $categories), 0, 244); } //get attribute set name $attributeSetName = $this->_getAttributeSetName($product); $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', ''), 'attribute-set' => $attributeSetName); } else { // when no product information is available limit to this data $this->products[] = array('name' => $productItem->getName(), 'sku' => $productItem->getSku(), 'qty' => (int) number_format($productItem->getData('qty'), 2), 'price' => (double) number_format($productItem->getPrice(), 2, '.', '')); } } $this->quote_subtotal = (double) number_format($quoteData->getData('subtotal'), 2, '.', ''); $discountAmount = $quoteData->getData('subtotal') - $quoteData->getData('subtotal_with_discount'); $this->discount_amount = (double) number_format($discountAmount, 2, '.', ''); $this->quote_total = (double) number_format($quoteData->getData('grand_total'), 2, '.', ''); return true; }
/** * calculate earning point for order quote * * @param Mage_Sales_Model_Quote $quote * @param int $customerGroupId * @param int $websiteId * @param string $date * @return int */ public function getShoppingCartPoints($quote, $customerGroupId = null, $websiteId = null, $date = null) { if ($quote->isVirtual()) { $address = $quote->getBillingAddress(); } else { $address = $quote->getShippingAddress(); } //webpos $customerId = Mage::getSingleton('checkout/session')->getData('webpos_customerid'); if ($customerId) { $customerGroupId = Mage::getModel('customer/customer')->load($customerId)->getGroupId(); } else { $customerGroupId = Mage_Customer_Model_Group::NOT_LOGGED_IN_ID; } if (is_null($websiteId)) { $websiteId = Mage::app()->getStore($quote->getStoreId())->getWebsiteId(); } if (is_null($date)) { $date = date('Y-m-d', strtotime($quote->getCreatedAt())); } $points = 0; $rules = Mage::getResourceModel('rewardpointsrule/earning_sales_collection')->setAvailableFilter($customerGroupId, $websiteId, $date); $items = $quote->getAllItems(); $this->setStoreId($quote->getStoreId()); foreach ($rules as $rule) { $rule->afterLoad(); if (!$rule->validate($address)) { continue; } $rowTotal = 0; $qtyTotal = 0; foreach ($items as $item) { if ($item->getParentItemId()) { continue; } if ($rule->getActions()->validate($item)) { $rowTotal += max(0, $item->getBaseRowTotal() - $item->getBaseDiscountAmount() - $item->getRewardpointsBaseDiscount()); $qtyTotal += $item->getQty(); } } if (!$qtyTotal) { continue; } if (Mage::getStoreConfigFlag(self::XML_PATH_EARNING_BY_SHIPPING, $quote->getStoreId())) { $rowTotal += $address->getBaseShippingAmount(); } $points += $this->calcSalesPoints($rule->getSimpleAction(), $rule->getPointsEarned(), $rule->getMoneyStep(), $rowTotal, $rule->getQtyStep(), $qtyTotal, $rule->getMaxPointsEarned()); if ($points && $rule->getStopRulesProcessing()) { break; } } return $points; }