Created
July 31, 2013 19:06
-
-
Save centerax/6125115 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Sage50 compatible Basket. | |
* | |
* @param Mage_Sales_Model_Quote $quote | |
* @return string Basket as string. | |
*/ | |
public function getSageBasket($quote) { | |
$basket = ''; | |
//$orderCurrencyCode = $this->getConfigCurrencyCode($quote); | |
//$_currency = Mage::getModel('directory/currency')->load($orderCurrencyCode); | |
$useBaseMoney = true; | |
$trnCurrency = (string)$this->getConfigData('trncurrency', $quote->getStoreId()); | |
if ($trnCurrency == 'store' or $trnCurrency == 'switcher') { | |
$useBaseMoney = false; | |
} | |
$itemsCollection = $quote->getItemsCollection(); | |
if ($itemsCollection->getSize() > 0) { | |
$numberOfdetailLines = $itemsCollection->getSize() + 1; | |
$todelete = 0; | |
foreach ($itemsCollection as $item) { | |
if ($item->getParentItem()) { # Configurable products | |
$numberOfdetailLines--; | |
} | |
} | |
$basket .= $numberOfdetailLines . self::BASKET_SEP; | |
foreach ($itemsCollection as $item) { | |
//Avoid duplicates SKUs on basket | |
if (strpos($basket, ($this->_cleanString($item->getSku()) . '|')) !== FALSE) { | |
continue; | |
} | |
if ($item->getParentItem()) { | |
continue; | |
} | |
$tax = ($item->getBaseTaxBeforeDiscount() ? $item->getBaseTaxBeforeDiscount() : ($item->getBaseTaxAmount() ? $item->getBaseTaxAmount() : 0)); | |
$calculationPrice = 0.00; | |
if($useBaseMoney) { | |
$calculationPrice = $item->getBaseCalculationPrice(); | |
} | |
else { | |
$calculationPrice = $item->getCalculationPrice(); | |
} | |
//Options | |
$options = $this->_getProductOptions($item); | |
$_options = ''; | |
if (count($options) > 0) { | |
foreach ($options as $opt) { | |
$_options .= $opt['label'] . '-' . $opt['value'] . '.'; | |
} | |
$_options = '_' . substr($_options, 0, -1) . '_'; | |
} | |
//[SKU]|Name | |
$line = str_replace(':', '-', '[' . $this->_cleanString($item->getSku()) . ']|' . $this->_cleanString($item->getName())) | |
. $this->_cleanString($_options) . self::BASKET_SEP; | |
//Quantity | |
$line .= ( $item->getQty() * 1) . self::BASKET_SEP; | |
//if ($this->getConfigData('sagefifty_basket')) { | |
$taxAmount = number_format(($item->getTaxAmount() / ($item->getQty() * 1)), 2); | |
//Item value | |
$line .= $calculationPrice . self::BASKET_SEP; | |
//Item tax | |
$line .= number_format($taxAmount, 2) . self::BASKET_SEP; | |
//Item total | |
$line .= number_format($calculationPrice + $taxAmount, 2) . self::BASKET_SEP; | |
if($useBaseMoney) { | |
$rowTotal = $item->getBaseRowTotal(); | |
} | |
else { | |
$rowTotal = $item->getRowTotal(); | |
} | |
//Line total | |
$line .= (($rowTotal + $tax) - $item->getDiscountAmount()) . self::BASKET_SEP; | |
/*} else { | |
//Item value | |
$line .= $_currency->formatPrecision($item->getCalculationPrice(), 2, array(), false) . self::BASKET_SEP; | |
//Item tax | |
$line .= $_currency->formatPrecision($item->getTaxAmount(), 2, array(), false) . self::BASKET_SEP; | |
//Item total | |
$line .= $_currency->formatPrecision($item->getTaxAmount() + $item->getCalculationPrice(), 2, array(), false) . self::BASKET_SEP; | |
//Line total | |
$line .= $_currency->formatPrecision((($item->getRowTotal() + $tax) - $item->getDiscountAmount()), 2, array(), false) . self::BASKET_SEP; | |
}*/ | |
if (strlen($basket . $line) < 7498) { | |
$basket .= $line; | |
} | |
else { | |
$todelete++; | |
} | |
} | |
} | |
//Delivery data | |
$shippingAddress = $quote->getShippingAddress(); | |
//if ($this->getConfigData('sagefifty_basket')) { | |
if($useBaseMoney) { | |
$deliveryValue = $shippingAddress->getBaseShippingAmount(); | |
$deliveryTax = $shippingAddress->getBaseShippingTaxAmount(); | |
$deliveryAmount = $shippingAddress->getBaseShippingInclTax(); | |
} | |
else { | |
$deliveryValue = $shippingAddress->getShippingAmount(); | |
$deliveryTax = $shippingAddress->getShippingTaxAmount(); | |
$deliveryAmount = $shippingAddress->getShippingInclTax(); | |
} | |
/*} else { | |
$deliveryValue = $_currency->formatPrecision($shippingAddress->getShippingAmount(), 2, array(), false); | |
$deliveryTax = $_currency->formatPrecision($shippingAddress->getShippingTaxAmount(), 2, array(), false); | |
$deliveryAmount = $_currency->formatPrecision($shippingAddress->getShippingInclTax(), 2, array(), false); | |
}*/ | |
$deliveryName = $shippingAddress->getShippingDescription() ? $shippingAddress->getShippingDescription() : 'Delivery'; | |
$delivery = $this->_cleanString($deliveryName) . self::BASKET_SEP . '1' . self::BASKET_SEP . $deliveryValue . self::BASKET_SEP | |
. $deliveryTax . self::BASKET_SEP . $deliveryAmount . self::BASKET_SEP . $deliveryAmount; | |
if (strlen($basket . $delivery) < 7498) { | |
$basket .= $delivery; | |
} | |
else { | |
$todelete++; | |
} | |
$numberOfLines = substr($basket, 0, strpos($basket, ':')); | |
if ($todelete > 0) { | |
$num = $numberOfLines - $todelete; | |
$basket = str_replace($numberOfLines, $num, $basket); | |
} | |
/** | |
* Verify that items count is correct | |
*/ | |
$items = explode(':', $basket); | |
//Remove line number from basket | |
array_shift($items); | |
//Split into rows | |
$rows = count(array_chunk($items, 6)); | |
if ($rows != $numberOfLines) { | |
$basket = str_replace($numberOfLines, $rows, $basket); | |
} | |
/** | |
* Verify that items count is correct | |
*/ | |
return $basket; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment