From ee90b20a65fa649fa6e9c7e790d50fde4b8cc180 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Thu, 10 May 2018 14:46:20 +0300 Subject: [PATCH 1/2] Multisite, validation of settings before saving, ICML catalog generation changed --- .../System/Config/Backend/Api/Key.php | 37 ++++++ .../System/Config/Backend/Api/Url.php | 42 ++++++ .../System/Config/Form/Fieldset/Base.php | 27 +--- .../System/Config/Form/Fieldset/Site.php | 85 ++++++++++++ .../System/Config/Form/Fieldset/Sites.php | 89 +++++++++++++ .../Retailcrm/Retailcrm/Helper/Data.php | 70 +++++++++- .../Retailcrm/Retailcrm/Model/Customer.php | 27 ++-- .../Retailcrm/Retailcrm/Model/Exchange.php | 50 +++++-- .../Retailcrm/Retailcrm/Model/Icml.php | 7 +- .../Retailcrm/Retailcrm/Model/Observer.php | 4 +- .../Retailcrm/Retailcrm/Model/Order.php | 123 +++++++++--------- .../Retailcrm/Retailcrm/etc/system.xml | 23 +++- 12 files changed, 469 insertions(+), 115 deletions(-) create mode 100644 app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Key.php create mode 100644 app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Url.php create mode 100644 app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Site.php create mode 100644 app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php diff --git a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Key.php b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Key.php new file mode 100644 index 0000000..e509f40 --- /dev/null +++ b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Key.php @@ -0,0 +1,37 @@ +isValueChanged()) { + $api_url = $this->getFieldsetDataValue('api_url'); + $api_key = $this->getValue(); + + if (!$api_key) { + Mage::throwException(Mage::helper('retailcrm')->__('Field API KEY could not be empty')); + } + + $api_client = new Retailcrm_Retailcrm_Model_ApiClient( + $api_url, + $api_key + ); + + try { + $response = $api_client->sitesList(); + } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $curlException) { + Mage::throwException(Mage::helper('retailcrm')->__($curlException->getMessage())); + } catch (Retailcrm_Retailcrm_Model_Exception_InvalidJsonException $invalidJsonException) { + Mage::throwException(Mage::helper('retailcrm')->__($invalidJsonException->getMessage())); + } catch (\InvalidArgumentException $invalidArgumentException) { + Mage::throwException(Mage::helper('retailcrm')->__($invalidArgumentException->getMessage())); + } + + if (isset($response['errorMsg'])) { + Mage::throwException(Mage::helper('retailcrm')->__($response['errorMsg'])); + } + } + + return $this; + } +} diff --git a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Url.php b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Url.php new file mode 100644 index 0000000..e626a7c --- /dev/null +++ b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Backend/Api/Url.php @@ -0,0 +1,42 @@ +isValueChanged()) { + $api_url = $this->getValue(); + $api_key = $this->getFieldsetDataValue('api_key'); + + if (!$api_url) { + Mage::throwException(Mage::helper('retailcrm')->__('Field API URL could not be empty')); + } + + if (false === stripos($api_url, 'https://')) { + $api_url = str_replace("http://", "https://", $api_url); + $this->setValue($api_url); + } + + $api_client = new Retailcrm_Retailcrm_Model_ApiClient( + $api_url, + $api_key + ); + + try { + $response = $api_client->sitesList(); + } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $curlException) { + Mage::throwException(Mage::helper('retailcrm')->__($curlException->getMessage())); + } catch (Retailcrm_Retailcrm_Model_Exception_InvalidJsonException $invalidJsonException) { + Mage::throwException(Mage::helper('retailcrm')->__($invalidJsonException->getMessage())); + } catch (\InvalidArgumentException $invalidArgumentException) { + Mage::throwException(Mage::helper('retailcrm')->__($invalidArgumentException->getMessage())); + } + + if (isset($response['errorMsg'])) { + Mage::throwException(Mage::helper('retailcrm')->__($response['errorMsg'])); + } + } + + return $this; + } +} diff --git a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Base.php b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Base.php index 7540e40..3308797 100644 --- a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Base.php +++ b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Base.php @@ -6,7 +6,7 @@ class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base exten protected $_apiKey; protected $_apiUrl; protected $_isCredentialCorrect; - + public function __construct() { parent::__construct(); @@ -14,30 +14,9 @@ class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base exten $this->_apiUrl = Mage::getStoreConfig('retailcrm/general/api_url'); $this->_apiKey = Mage::getStoreConfig('retailcrm/general/api_key'); $this->_isCredentialCorrect = false; + if (!empty($this->_apiUrl) && !empty($this->_apiKey)) { - if (false === stripos($this->_apiUrl, 'https://')) { - $this->_apiUrl = str_replace("http://", "https://", $this->_apiUrl); - Mage::getModel('core/config')->saveConfig('retailcrm/general/api_url', $this->_apiUrl); - } - - $client = new Retailcrm_Retailcrm_Model_ApiClient( - $this->_apiUrl, - $this->_apiKey - ); - - try { - $response = $client->sitesList(); - } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { - Mage::log($e->getMessage()); - } - - if ($response->isSuccessful()) { - $this->_isCredentialCorrect = true; - - if($response['success'] != 1) { - Mage::getModel('core/config')->saveConfig('retailcrm/general/api_url', ''); - } - } + $this->_isCredentialCorrect = true; } } } diff --git a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Site.php b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Site.php new file mode 100644 index 0000000..e9c3da8 --- /dev/null +++ b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Site.php @@ -0,0 +1,85 @@ +_getHeaderHtml($element); + + if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) { + $html .= $this->_getFieldHtml($element); + } else { + $html .= '
Please check your API Url & API Key
'; + } + + $html .= $this->_getFooterHtml($element); + return $html; + } + + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field'); + } + + return $this->_fieldRenderer; + } + + /** + * @return array + */ + protected function _getValues() + { + if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) { + $client = new Retailcrm_Retailcrm_Model_ApiClient( + $this->_apiUrl, + $this->_apiKey + ); + + try { + $sites = $client->sitesList(); + } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { + Mage::log($e->getMessage()); + } + + if ($sites->isSuccessful()) { + if (empty($this->_values)) { + foreach ($sites['sites'] as $site) { + $this->_values[] = array('label'=>Mage::helper('adminhtml')->__($site['name']), 'value'=>$site['code']); + } + } + } + } + + return $this->_values; + } + + protected function _getFieldHtml($fieldset) + { + $configData = $this->getConfigData(); + + $path = 'retailcrm/site/default'; + if (isset($configData[$path])) { + $data = $configData[$path]; + $inherit = false; + } else { + $data = (int)(string)$this->getForm()->getConfigRoot()->descend($path); + $inherit = true; + } + + $field = $fieldset->addField( + 'site_default', 'select', + array( + 'name' => 'groups[site][fields][default][value]', + 'label' => 'Default', + 'value' => $data, + 'values' => $this->_getValues(), + 'inherit' => $inherit, + 'can_use_default_value' => 1, + 'can_use_website_value' => 1 + ) + )->setRenderer($this->_getFieldRenderer()); + + return $field->toHtml(); + } +} \ No newline at end of file diff --git a/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php new file mode 100644 index 0000000..a5ae50d --- /dev/null +++ b/app/code/community/Retailcrm/Retailcrm/Block/Adminhtml/System/Config/Form/Fieldset/Sites.php @@ -0,0 +1,89 @@ +_getHeaderHtml($element); + + if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) { + $websiteCode = Mage::app()->getRequest()->getParam('website'); + $website = Mage::app()->getWebsite()->load($websiteCode); + + foreach ($website->getStoreCollection() as $store) { + $html .= $this->_getFieldHtml($element, $store); + } + } else { + $html .= '
Please check your API Url & API Key
'; + } + + $html .= $this->_getFooterHtml($element); + return $html; + } + + protected function _getFieldRenderer() + { + if (empty($this->_fieldRenderer)) { + $this->_fieldRenderer = Mage::getBlockSingleton('adminhtml/system_config_form_field'); + } + + return $this->_fieldRenderer; + } + + /** + * @return array + */ + protected function _getValues() + { + if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) { + $client = new Retailcrm_Retailcrm_Model_ApiClient( + $this->_apiUrl, + $this->_apiKey + ); + + try { + $sites = $client->sitesList(); + } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { + Mage::log($e->getMessage()); + } + + if ($sites->isSuccessful()) { + if (empty($this->_values)) { + foreach ($sites['sites'] as $site) { + $this->_values[] = array('label'=>Mage::helper('adminhtml')->__($site['name']), 'value'=>$site['code']); + } + } + } + } + + return $this->_values; + } + + protected function _getFieldHtml($fieldset, $group) + { + $configData = $this->getConfigData(); + + $path = 'retailcrm/sites/' . $group->getCode(); + if (isset($configData[$path])) { + $data = $configData[$path]; + $inherit = false; + } else { + $data = (int)(string)$this->getForm()->getConfigRoot()->descend($path); + $inherit = true; + } + + $field = $fieldset->addField( + 'sites_' . $group->getCode(), 'select', + array( + 'name' => 'groups[sites][fields][' . $group->getCode() . '][value]', + 'label' => $group->getName(), + 'value' => $data, + 'values' => $this->_getValues(), + 'inherit' => $inherit, + 'can_use_default_value' => 1, + 'can_use_website_value' => 1 + ) + )->setRenderer($this->_getFieldRenderer()); + + return $field->toHtml(); + } +} diff --git a/app/code/community/Retailcrm/Retailcrm/Helper/Data.php b/app/code/community/Retailcrm/Retailcrm/Helper/Data.php index b22b438..10402ca 100644 --- a/app/code/community/Retailcrm/Retailcrm/Helper/Data.php +++ b/app/code/community/Retailcrm/Retailcrm/Helper/Data.php @@ -37,7 +37,75 @@ class Retailcrm_Retailcrm_Helper_Data extends Mage_Core_Helper_Abstract */ const XML_API_KEY = 'retailcrm/general/api_key'; - /** + /** + * @var string + */ + const XML_SITES = 'retailcrm/sites/'; + + /** + * @var string + */ + const XML_SITE = 'retailcrm/site/default'; + + + /** + * Get site code + * + * @param Mage_Core_Model_Store $store + * + * @return mixed|null + */ + public function getSite($store) + { + if (!$store instanceof Mage_Core_Model_Store + && is_int($store) + ) { + $store = Mage::app()->getStore($store); + } + + $website = $store->getWebsite(); + $site = $website->getConfig(self::XML_SITES . $store->getCode()); + + if ($site) { + return $site; + } else { + $site = Mage::getStoreConfig(self::XML_SITE); + + if ($site) { + return $site; + } + } + + return null; + } + + /** + * @return array + */ + public function getMappingSites() { + $sites = array(); + $webSites = Mage::app()->getWebsites(); + + foreach ($webSites as $webSite) { + $storesFromSite = $webSite->getStores(); + + foreach ($storesFromSite as $store) { + $config = $webSite->getConfig(self::XML_SITES . $store->getCode()); + + if ($config) { + $sites[$config] = $store->getCode(); + } + } + + unset($storesFromSite); + } + + unset($webSites); + + return $sites; + } + + /** * Get api url * * @param Mage_Core_Model_Store $store store instance diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Customer.php b/app/code/community/Retailcrm/Retailcrm/Model/Customer.php index 13716df..11636fe 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Customer.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Customer.php @@ -31,6 +31,7 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha 'lastName' => $data->getLastname(), 'createdAt' => Mage::getSingleton('core/date')->date() ); + $this->_api->setSite(Mage::helper('retailcrm')->getSite($data->getStore())); $this->_api->customersEdit($customer); } @@ -44,7 +45,7 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha */ public function customersExport() { - $customers = array(); + $customersSites = array(); $customerCollection = Mage::getModel('customer/customer') ->getCollection() ->addAttributeToSelect('email') @@ -57,19 +58,23 @@ class Retailcrm_Retailcrm_Model_Customer extends Retailcrm_Retailcrm_Model_Excha 'firstName' => $customerData->getData('firstname'), 'lastName' => $customerData->getData('lastname') ); - $customers[] = $customer; + + $customersSites[$customerData->getStore()->getId()][] = $customer; } - + unset($customerCollection); - $chunked = array_chunk($customers, 50); - unset($customers); - foreach ($chunked as $chunk) { - $this->_api->customersUpload($chunk); - time_nanosleep(0, 250000000); + + foreach ($customersSites as $storeId => $customers) { + $chunked = array_chunk($customers, 50); + unset($customers); + foreach ($chunked as $chunk) { + $this->_api->customersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId)); + time_nanosleep(0, 250000000); + } + + unset($chunked); } - - unset($chunked); - + return true; } } diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php b/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php index 21bbc99..fa855a5 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php @@ -6,9 +6,11 @@ class Retailcrm_Retailcrm_Model_Exchange protected $_apiUrl; protected $_config; protected $_api; - + private $_helper; + public function __construct() { + $this->_helper = Mage::helper('retailcrm'); $this->_apiUrl = Mage::getStoreConfig('retailcrm/general/api_url'); $this->_apiKey = Mage::getStoreConfig('retailcrm/general/api_key'); @@ -97,17 +99,17 @@ class Retailcrm_Retailcrm_Model_Exchange die(); } } - + /** * @param array $order */ private function doCreate($order) { Mage::log($order, null, 'retailcrmHistoriCreate.log', true); - + try { $response = $this->_api->ordersGet($order['id'], $by = 'id'); - + if ($response->isSuccessful() && 200 === $response->getStatusCode()) { $order = $response->order; } else { @@ -126,22 +128,32 @@ class Retailcrm_Retailcrm_Model_Exchange } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); } - + // get references $this->_config = Mage::getStoreConfig('retailcrm'); $payments = array_flip(array_filter($this->_config['payment'])); $shippings = array_flip(array_filter($this->_config['shipping'])); - + // get store $_sendConfirmation = '0'; - $storeId = Mage::app()->getStore()->getId(); + $sitesConfig = $this->_helper->getMappingSites(); + + if (!$sitesConfig) { + $storeId = Mage::app() + ->getWebsite(true) + ->getDefaultGroup() + ->getDefaultStoreId(); + } else { + $storeId = Mage::app()->getStore()->load($sitesConfig[$order['site']])->getId(); + } + $siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId(); - + // search or create customer $customer = Mage::getSingleton('customer/customer'); $customer->setWebsiteId($siteid); $customer->loadByEmail($order['email']); - + if (!is_numeric($customer->getId())) { $customer ->setGroupId(1) @@ -152,7 +164,7 @@ class Retailcrm_Retailcrm_Model_Exchange ->setLastname($order['lastName']) ->setMiddleName($order['patronymic']) ->setPassword(uniqid()); - + try { $customer->save(); $customer->setConfirmation(null); @@ -331,7 +343,7 @@ class Retailcrm_Retailcrm_Model_Exchange $response = $this->_api->ordersGet($order['id'], $by = 'id'); if ($response->isSuccessful() && 200 === $response->getStatusCode()) { - $order = $response->order; + $order = $response->order; } else { Mage::log( sprintf( @@ -348,7 +360,7 @@ class Retailcrm_Retailcrm_Model_Exchange } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); } - + // get references $this->_config = Mage::getStoreConfig('retailcrm'); $payments = array_flip(array_filter($this->_config['payment'])); @@ -356,7 +368,17 @@ class Retailcrm_Retailcrm_Model_Exchange // get store $_sendConfirmation = '0'; - $storeId = Mage::app()->getStore()->getId(); + $sitesConfig = $this->_helper->getMappingSites(); + + if (!$sitesConfig) { + $storeId = Mage::app() + ->getWebsite(true) + ->getDefaultGroup() + ->getDefaultStoreId(); + } else { + $storeId = Mage::app()->getStore()->load($sitesConfig[$order['site']])->getId(); + } + $siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId(); // search or create customer @@ -504,7 +526,7 @@ class Retailcrm_Retailcrm_Model_Exchange $oldOrder = Mage::getModel('sales/order')->loadByIncrementId($originalId); $oldOrderArr = $oldOrder->getData(); - if(!empty($oldOrderArr['original_increment_id'])) { + if (!empty($oldOrderArr['original_increment_id'])) { $originalId = $oldOrderArr['original_increment_id']; } diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Icml.php b/app/code/community/Retailcrm/Retailcrm/Model/Icml.php index c2f1e3a..9fbb637 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Icml.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Icml.php @@ -14,7 +14,7 @@ class Retailcrm_Retailcrm_Model_Icml $string = ' - '.Mage::app()->getStore($shop)->getName().' + '.$shop->getName().' @@ -41,9 +41,8 @@ class Retailcrm_Retailcrm_Model_Icml $this->_dd->saveXML(); $baseDir = Mage::getBaseDir(); - $shopCode = Mage::app()->getStore($shop)->getCode(); + $shopCode = $shop->getCode(); $this->_dd->save($baseDir . DS . 'retailcrm_' . $shopCode . '.xml'); - } private function addCategories() @@ -102,7 +101,6 @@ class Retailcrm_Retailcrm_Model_Icml $collection->addFieldToFilter('status', Mage_Catalog_Model_Product_Status::STATUS_ENABLED); $collection->addFieldToFilter('visibility', Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH); - $collection->addAttributeToFilter('type_id', array('eq' => 'simple')); foreach ($collection as $product) { /** @var Mage_Catalog_Model_Product $product */ @@ -174,6 +172,7 @@ class Retailcrm_Retailcrm_Model_Icml $offers[] = $offer; if($product->getTypeId() == 'configurable') { + /** @var Mage_Catalog_Model_Product_Type_Configurable $product */ $associatedProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null, $product); diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Observer.php b/app/code/community/Retailcrm/Retailcrm/Model/Observer.php index 332499c..3497f3f 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Observer.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Observer.php @@ -59,8 +59,8 @@ class Retailcrm_Retailcrm_Model_Observer public function exportCatalog() { foreach (Mage::app()->getWebsites() as $website) { - foreach ($website->getGroups() as $group) { - Mage::getModel('retailcrm/icml')->generate((int)$group->getId()); + foreach ($website->getStores() as $store) { + Mage::getModel('retailcrm/icml')->generate($store); } } } diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Order.php b/app/code/community/Retailcrm/Retailcrm/Model/Order.php index 5706ca9..7524f0b 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Order.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Order.php @@ -25,17 +25,20 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange public function orderPay($orderId) { $order = Mage::getModel('sales/order')->load($orderId); - - if((string)$order->getBaseGrandTotal() == (string)$order->getTotalPaid()){ + + if ((string)$order->getBaseGrandTotal() == (string)$order->getTotalPaid()) { $preparedOrder = array( 'externalId' => $order->getRealOrderId(),//getId(), 'paymentStatus' => 'paid', ); - $preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder); + + $helper = Mage::helper('retailcrm'); + $preparedOrder = $helper->filterRecursive($preparedOrder); + $this->_api->setSite($helper->getSite($order->getStore())); $this->_api->ordersEdit($preparedOrder); } } - + public function orderStatusHistoryCheck($order) { $config = Mage::getModel( @@ -48,19 +51,19 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange 'externalId' => $order->getRealOrderId(),//getId(), 'status' => $config->getMapping($order->getStatus(), 'status'), ); - - $comment = $order->getStatusHistoryCollection()->getData(); - - if(!empty($comment[0]['comment'])) { - $preparedOrder['managerComment'] = $comment[0]['comment']; - } - - $preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder); - $this->_api->ordersEdit($preparedOrder); - + + $comment = $order->getStatusHistoryCollection()->getData(); + + if(!empty($comment[0]['comment'])) { + $preparedOrder['managerComment'] = $comment[0]['comment']; + } + + $helper = Mage::helper('retailcrm'); + $preparedOrder = $helper->filterRecursive($preparedOrder); + $this->_api->setSite($helper->getSite($order->getStore())); + $this->_api->ordersEdit($preparedOrder); } - - + public function orderUpdate($order) { $config = Mage::getModel( @@ -73,16 +76,18 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange 'externalId' => $order->getRealOrderId(),//getId(), 'status' => $config->getMapping($order->getStatus(), 'status'), ); - if((float)$order->getBaseGrandTotal() == (float)$order->getTotalPaid()) { + if ((float)$order->getBaseGrandTotal() == (float)$order->getTotalPaid()) { $preparedOrder['paymentStatus'] = 'paid'; - $preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder); + $helper = Mage::helper('retailcrm'); + $preparedOrder = $helper->filterRecursive($preparedOrder); + $this->_api->setSite($helper->getSite($order->getStore())); $this->_api->ordersEdit($preparedOrder); } - } - + public function orderCreate($order) { + $helper = Mage::helper('retailcrm'); $config = Mage::getModel('retailcrm/settings', ['storeId' => $order->getStoreId()]); $address = $order->getShippingAddress()->getData(); $orderItems = $order->getItemsCollection() @@ -127,7 +132,6 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange $shipping = $this->getShippingCode($order->getShippingMethod()); $preparedOrder = array( - 'site' => $order->getStore()->getCode(), 'externalId' => $order->getRealOrderId(), 'number' => $order->getRealOrderId(), 'createdAt' => Mage::getModel('core/date')->date(), @@ -164,17 +168,16 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange ), ) ); - - - if(trim($preparedOrder['delivery']['code']) == ''){ + + if (trim($preparedOrder['delivery']['code']) == ''){ unset($preparedOrder['delivery']['code']); } - if(trim($preparedOrder['paymentType']) == ''){ + if (trim($preparedOrder['paymentType']) == ''){ unset($preparedOrder['paymentType']); } - if(trim($preparedOrder['status']) == ''){ + if (trim($preparedOrder['status']) == ''){ unset($preparedOrder['status']); } @@ -182,18 +185,18 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange $preparedCustomer = array( 'externalId' => $order->getCustomerId() ); - + if ($this->_api->customersCreate($preparedCustomer)) { $preparedOrder['customer']['externalId'] = $order->getCustomerId(); } } - - $preparedOrder = Mage::helper('retailcrm')->filterRecursive($preparedOrder); - + + $preparedOrder = $helper->filterRecursive($preparedOrder); + Mage::log($preparedOrder, null, 'retailcrmCreatePreparedOrder.log', true); - + try { - $response = $this->_api->ordersCreate($preparedOrder); + $response = $this->_api->ordersCreate($preparedOrder, $helper->getSite($order->getStore())); if ($response->isSuccessful() && 201 === $response->getStatusCode()) { Mage::log($response->id); } else { @@ -218,8 +221,8 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange { $config = Mage::getStoreConfig('retailcrm'); $ordersId = explode(",", $config['load_order']['numberOrder']); - $orders = array(); - + $ordersSites = array(); + $ordersList = Mage::getResourceModel('sales/order_collection') ->addAttributeToSelect('*') ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left') @@ -247,20 +250,22 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange ->setCurPage(1) ->addAttributeToFilter('increment_id', $ordersId) ->load(); - + foreach ($ordersList as $order) { - $orders[] = $this->prepareOrder($order); + $ordersSites[$order->getStore()->getId()][] = $this->prepareOrder($order); } - - $chunked = array_chunk($orders, 50); - unset($orders); - foreach ($chunked as $chunk) { - $this->_api->ordersUpload($chunk); - time_nanosleep(0, 250000000); + + foreach ($ordersSites as $storeId => $orders) { + $chunked = array_chunk($orders, 50); + unset($orders); + foreach ($chunked as $chunk) { + $this->_api->ordersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId)); + time_nanosleep(0, 250000000); + } + + unset($chunked); } - - unset($chunked); - + return true; } @@ -275,7 +280,7 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange */ public function ordersExport() { - $orders = array(); + $ordersSites = array(); $ordersList = Mage::getResourceModel('sales/order_collection') ->addAttributeToSelect('*') ->joinAttribute('billing_firstname', 'order_address/firstname', 'billing_address_id', null, 'left') @@ -302,20 +307,22 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange ->setPageSize(1000) ->setCurPage(1) ->load(); - + foreach ($ordersList as $order) { - $orders[] = $this->prepareOrder($order); + $ordersSites[$order->getStore()->getId()][] = $this->prepareOrder($order); } - - $chunked = array_chunk($orders, 50); - unset($orders); - foreach ($chunked as $chunk) { - $this->_api->ordersUpload($chunk); - time_nanosleep(0, 250000000); + + foreach ($ordersSites as $storeId => $orders) { + $chunked = array_chunk($orders, 50); + unset($orders); + foreach ($chunked as $chunk) { + $this->_api->ordersUpload($chunk, Mage::helper('retailcrm')->getSite($storeId)); + time_nanosleep(0, 250000000); + } + + unset($chunked); } - - unset($chunked); - + return true; } @@ -323,7 +330,7 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange { $config = Mage::getModel('retailcrm/settings', ['storeId' => $order->getStoreId()]); $address = $order->getShippingAddress(); - + $orderItems = $order->getItemsCollection() ->addAttributeToSelect('*') ->addAttributeToFilter('product_type', array('eq'=>'simple')) diff --git a/app/code/community/Retailcrm/Retailcrm/etc/system.xml b/app/code/community/Retailcrm/Retailcrm/etc/system.xml index 292274a..de86b48 100644 --- a/app/code/community/Retailcrm/Retailcrm/etc/system.xml +++ b/app/code/community/Retailcrm/Retailcrm/etc/system.xml @@ -57,6 +57,7 @@ SOFTWARE. 1 1 YourCrmName.retailcrm.ru]]> + retailcrm_retailcrm_block_adminhtml_system_config_backend_api_url @@ -66,6 +67,7 @@ SOFTWARE. 1 1 Integration > API Keys]]> + retailcrm_retailcrm_block_adminhtml_system_config_backend_api_key @@ -153,7 +155,26 @@ SOFTWARE. - + + 1 + + text + 1 + 0 + 0 + 16 + retailcrm/adminhtml_system_config_form_fieldset_site + + + 1 + + text + 0 + 1 + 1 + 17 + retailcrm/adminhtml_system_config_form_fieldset_sites + From c04286f62bc7d22899682c3191caa1eb0288f148 Mon Sep 17 00:00:00 2001 From: Akolzin Dmitry Date: Mon, 3 Sep 2018 17:25:35 +0300 Subject: [PATCH 2/2] Update items array in prepare method. Fix setStore() for customer in history --- .../Retailcrm/Retailcrm/Model/Exchange.php | 224 +++++++++--------- .../Retailcrm/Retailcrm/Model/Order.php | 42 ++-- 2 files changed, 133 insertions(+), 133 deletions(-) diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php b/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php index fa855a5..0764567 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Exchange.php @@ -21,7 +21,7 @@ class Retailcrm_Retailcrm_Model_Exchange ); } } - + /** * Get orders history & modify orders into shop * @@ -31,12 +31,12 @@ class Retailcrm_Retailcrm_Model_Exchange $runTime = $this->getExchangeTime($this->_config['general']['history']); $historyFilter = array(); $historiOrder = array(); - + $historyStart = Mage::getStoreConfig('retailcrm/general/fhistory'); if($historyStart && $historyStart > 0) { $historyFilter['sinceId'] = $historyStart; } - + while(true) { try { $response = $this->_api->ordersHistory($historyFilter); @@ -46,38 +46,38 @@ class Retailcrm_Retailcrm_Model_Exchange Mage::log( sprintf("Orders history error: [HTTP status %s] %s", $response->getStatusCode(), $response->getErrorMsg()) ); - + if (isset($response['errors'])) { Mage::log(implode(' :: ', $response['errors'])); } - + return false; } } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); - + return false; } - + $orderH = isset($response['history']) ? $response['history'] : array(); - if(count($orderH) == 0) { + if (count($orderH) == 0) { return true; } - + $historiOrder = array_merge($historiOrder, $orderH); $end = array_pop($response->history); $historyFilter['sinceId'] = $end['id']; - + if($response['pagination']['totalPageCount'] == 1) { Mage::getModel('core/config')->saveConfig('retailcrm/general/fhistory', $historyFilter['sinceId']); $orders = self::assemblyOrder($historiOrder); $this->processOrders($orders, $nowTime); - + return true; } }//endwhile } - + /** * @param array $orders */ @@ -89,13 +89,13 @@ class Retailcrm_Retailcrm_Model_Exchange ); foreach ($orders as $order) { - if(!empty($order['externalId'])) { + if(!empty($order['externalId'])) { $this->doUpdate($order); } else { $this->doCreate($order); } } - + die(); } } @@ -154,11 +154,11 @@ class Retailcrm_Retailcrm_Model_Exchange $customer->setWebsiteId($siteid); $customer->loadByEmail($order['email']); - if (!is_numeric($customer->getId())) { + if (!is_numeric($customer->getId())) { $customer ->setGroupId(1) ->setWebsiteId($siteid) - ->setStore($storeId) + ->setStoreId($storeId) ->setEmail($order['email']) ->setFirstname($order['firstName']) ->setLastname($order['lastName']) @@ -224,7 +224,7 @@ class Retailcrm_Retailcrm_Model_Exchange foreach ($order['items'] as $item) { $products[$item['offer']['externalId']] = array('qty' => $item['quantity']); } - + $orderData = array( 'session' => array( 'customer_id' => $customer->getId(), @@ -275,45 +275,45 @@ class Retailcrm_Retailcrm_Model_Exchange $quote = Mage::getModel('sales/quote')->setStoreId($storeId); $quote->assignCustomer($customer); $quote->setSendCconfirmation($_sendConfirmation); - + foreach($products as $idx => $val) { $product = Mage::getModel('catalog/product')->load($idx); $quote->addProduct($product, new Varien_Object($val)); } - + $shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']); $billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']); $shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']); - + $shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod($shipping_method) ->setPaymentMethod($orderData['payment']['method']); - + $quote->getPayment()->importData($orderData['payment']); $quote->collectTotals(); $quote->reserveOrderId(); $quote->save(); - + $service = Mage::getModel('sales/service_quote', $quote); - + try{ $service->submitAll(); } catch (Exception $e) { Mage::log($e->getMessage()); } - + try { $response = $this->_api->ordersFixExternalIds( array( array( - 'id' => $order['id'], + 'id' => $order['id'], 'externalId' =>$service->getOrder()->getRealOrderId() ) ) ); - + if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) { Mage::log( sprintf( @@ -329,7 +329,7 @@ class Retailcrm_Retailcrm_Model_Exchange } } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); - } + } } /** @@ -338,10 +338,10 @@ class Retailcrm_Retailcrm_Model_Exchange private function doCreateUp($order) { Mage::log($order, null, 'retailcrmHistoriCreateUp.log', true); - + try { $response = $this->_api->ordersGet($order['id'], $by = 'id'); - + if ($response->isSuccessful() && 200 === $response->getStatusCode()) { $order = $response->order; } else { @@ -352,7 +352,7 @@ class Retailcrm_Retailcrm_Model_Exchange $response->getErrorMsg() ) ); - + if (isset($response['errors'])) { Mage::log(implode(' :: ', $response['errors'])); } @@ -365,7 +365,7 @@ class Retailcrm_Retailcrm_Model_Exchange $this->_config = Mage::getStoreConfig('retailcrm'); $payments = array_flip(array_filter($this->_config['payment'])); $shippings = array_flip(array_filter($this->_config['shipping'])); - + // get store $_sendConfirmation = '0'; $sitesConfig = $this->_helper->getMappingSites(); @@ -380,23 +380,23 @@ class Retailcrm_Retailcrm_Model_Exchange } $siteid = Mage::getModel('core/store')->load($storeId)->getWebsiteId(); - + // search or create customer $customer = Mage::getSingleton('customer/customer'); $customer->setWebsiteId($siteid); $customer->loadByEmail($order['email']); - - if (!is_numeric($customer->getId())) { + + if (!is_numeric($customer->getId())) { $customer ->setGropuId(1) ->setWebsiteId($siteid) - ->setStore($storeId) + ->setStoreId($storeId) ->setEmail($order['email']) ->setFirstname($order['firstName']) ->setLastname($order['lastName']) ->setMiddleName($order['patronymic']) ->setPassword(uniqid()); - + try { $customer->save(); $customer->setConfirmation(null); @@ -404,7 +404,7 @@ class Retailcrm_Retailcrm_Model_Exchange } catch (Exception $e) { Mage::log($e->getMessage()); } - + $address = Mage::getModel("customer/address"); $address->setCustomerId($customer->getId()) ->setFirstname($customer->getFirstname()) @@ -419,14 +419,14 @@ class Retailcrm_Retailcrm_Model_Exchange ->setIsDefaultBilling('1') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); - + try{ $address->save(); } catch (Exception $e) { Mage::log($e->getMessage()); } - + try { $response = $this->_api->customersFixExternalIds( array( @@ -442,7 +442,7 @@ class Retailcrm_Retailcrm_Model_Exchange $response->getErrorMsg() ) ); - + if (isset($response['errors'])) { Mage::log(implode(' :: ', $response['errors'])); } @@ -451,12 +451,12 @@ class Retailcrm_Retailcrm_Model_Exchange Mage::log($e->getMessage()); } } - + $products = array(); foreach ($order['items'] as $item) { $products[$item['offer']['externalId']] = array('qty' => $item['quantity']); } - + $orderData = array( 'session' => array( 'customer_id' => $customer->getId(), @@ -500,11 +500,11 @@ class Retailcrm_Retailcrm_Model_Exchange 'send_confirmation' => $_sendConfirmation ) ); - + $quote = Mage::getModel('sales/quote')->setStoreId($storeId); $quote->assignCustomer($customer); $quote->setSendCconfirmation($_sendConfirmation); - + foreach($products as $idx => $val) { $product = Mage::getModel('catalog/product')->load($idx); $quote->addProduct($product, new Varien_Object($val)); @@ -513,23 +513,23 @@ class Retailcrm_Retailcrm_Model_Exchange $shipping_method = self::getAllShippingMethodsCode($orderData['order']['shipping_method']); $billingAddress = $quote->getBillingAddress()->addData($orderData['order']['billing_address']); $shippingAddress = $quote->getShippingAddress()->addData($orderData['order']['shipping_address']); - + $shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod($shipping_method) ->setPaymentMethod($orderData['payment']['method']); - + $quote->getPayment()->importData($orderData['payment']); $quote->collectTotals(); - + $originalId = $order['externalId']; $oldOrder = Mage::getModel('sales/order')->loadByIncrementId($originalId); $oldOrderArr = $oldOrder->getData(); - + if (!empty($oldOrderArr['original_increment_id'])) { $originalId = $oldOrderArr['original_increment_id']; } - + $orderDataUp = array( 'original_increment_id' => $originalId, 'relation_parent_id' => $oldOrder->getId(), @@ -537,23 +537,23 @@ class Retailcrm_Retailcrm_Model_Exchange 'edit_increment' => $oldOrder->getEditIncrement()+1, 'increment_id' => $originalId.'-'.($oldOrder->getEditIncrement()+1) ); - + $quote->setReservedOrderId($orderDataUp['increment_id']); $quote->save(); - + $service = Mage::getModel('sales/service_quote', $quote); $service->setOrderData($orderDataUp); - + try{ $service->submitAll(); } catch (Exception $e) { Mage::log($e->getMessage()); } - + $magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($orderDataUp['relation_parent_real_id']); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save(); - + try { $response = $this->_api->ordersFixExternalIds( array( @@ -563,7 +563,7 @@ class Retailcrm_Retailcrm_Model_Exchange ) ) ); - + if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) { Mage::log( sprintf( @@ -572,7 +572,7 @@ class Retailcrm_Retailcrm_Model_Exchange $response->getErrorMsg() ) ); - + if (isset($response['errors'])) { Mage::log(implode(' :: ', $response['errors'])); } @@ -580,58 +580,58 @@ class Retailcrm_Retailcrm_Model_Exchange } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); } - + } - + /** * @param array $order */ private function doUpdate($order) - { + { $magentoOrder = Mage::getModel('sales/order')->loadByIncrementId($order['externalId']); $magentoOrderArr = $magentoOrder->getData(); $config = Mage::getStoreConfig('retailcrm'); - + Mage::log($order, null, 'retailcrmHistoriUpdate.log', true); - + if((!empty($order['order_edit']))&&($order['order_edit'] == 1)) { $this->doCreateUp($order); } - + if (!empty($order['status'])) { try { $response = $this->_api->statusesList(); - + if ($response->isSuccessful() && 200 === $response->getStatusCode()) { - $code = $order['status']; + $code = $order['status']; $group = $response->statuses[$code]['group']; - + if ($magentoOrder->hasInvoices()) { $invIncrementIDs = array(); foreach ($magentoOrder->getInvoiceCollection() as $inv) { $invIncrementIDs[] = $inv->getIncrementId(); } } - + if (in_array($group, array('approval', 'assembling', 'delivery'))) { if(empty($invIncrementIDs)) { $magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true); $magentoOrder->save(); - + $invoice = $magentoOrder->prepareInvoice() ->setTransactionId($magentoOrder->getRealOrderId()) ->addComment("Add status on CRM") ->register() ->pay(); - + $transaction_save = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); - + $transaction_save->save(); } } - + if (in_array($group, array('complete'))) { if(empty($invIncrementIDs)){ $invoice = $magentoOrder->prepareInvoice() @@ -639,15 +639,15 @@ class Retailcrm_Retailcrm_Model_Exchange ->addComment("Add status on CRM") ->register() ->pay(); - + $transaction_save = Mage::getModel('core/resource_transaction') ->addObject($invoice) ->addObject($invoice->getOrder()); - + $transaction_save->save(); $magentoOrder->setState(Mage_Sales_Model_Order::STATE_PROCESSING, true)->save(); } - + if($magentoOrder->canShip()) { $itemQty = $magentoOrder->getItemsCollection()->count(); $shipment = Mage::getModel('sales/service_order', $magentoOrder)->prepareShipment($itemQty); @@ -656,36 +656,36 @@ class Retailcrm_Retailcrm_Model_Exchange } } - if($code == $config['status']['canceled']) { + if($code == $config['status']['canceled']) { $magentoOrder->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save(); } - + if($code == $config['status']['holded']) { if($magentoOrder->canHold()){ $magentoOrder->hold()->save(); } } - + if($code == $config['status']['unhold']) { if($magentoOrder->canUnhold()) { $magentoOrder->unhold()->save(); } } - + if($code == $config['status']['closed']) { if($magentoOrder->canCreditmemo()) { $orderItem = $magentoOrder->getItemsCollection(); foreach ($orderItem as $item) { $data['qtys'][$item->getid()] = $item->getQtyOrdered(); - } - + } + $service = Mage::getModel('sales/service_order', $magentoOrder); $creditMemo = $service->prepareCreditmemo($data)->register()->save(); $magentoOrder->addStatusToHistory(Mage_Sales_Model_Order::STATE_CLOSED, 'Add status on CRM', false); $magentoOrder->save(); } } - + Mage::log("Update: " . $order['externalId'], null, 'history.log'); } else { Mage::log( @@ -702,14 +702,14 @@ class Retailcrm_Retailcrm_Model_Exchange } } catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) { Mage::log($e->getMessage()); - } + } } - + if(!empty($order['manager_comment'])) { $magentoOrder->addStatusHistoryComment($order['manager_comment']); $magentoOrder->save(); } - + } /** @@ -842,8 +842,8 @@ class Retailcrm_Retailcrm_Model_Exchange return (string) $country; } - - + + public static function assemblyOrder($orderHistory) { $orders = array(); @@ -855,32 +855,32 @@ class Retailcrm_Retailcrm_Model_Exchange if(isset($change['created'])) { $item['create'] = 1; } - + $items[$item['id']] = $item; } - + $change['order']['items'] = $items; } - + Mage::log($change, null, 'retailcrmHistoryAssemblyOrder.log', true); - + if($change['order']['contragent']['contragentType']) { $change['order']['contragentType'] = self::newValue($change['order']['contragent']['contragentType']); unset($change['order']['contragent']); } - + if($orders[$change['order']['id']]) { $orders[$change['order']['id']] = array_merge($orders[$change['order']['id']], $change['order']); } - + else { $orders[$change['order']['id']] = $change['order']; } - + if($change['field'] == 'manager_comment'){ $orders[$change['order']['id']][$change['field']] = $change['newValue']; } - + if(($change['field'] != 'status')&& ($change['field'] != 'country')&& ($change['field'] != 'manager_comment')&& @@ -890,31 +890,31 @@ class Retailcrm_Retailcrm_Model_Exchange ) { $orders[$change['order']['id']]['order_edit'] = 1; } - - if($change['item']) { + + if($change['item']) { if($orders[$change['order']['id']]['items'][$change['item']['id']]) { $orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge($orders[$change['order']['id']]['items'][$change['item']['id']], $change['item']); } - + else{ $orders[$change['order']['id']]['items'][$change['item']['id']] = $change['item']; } - + if(empty($change['oldValue']) && $change['field'] == 'order_product') { $orders[$change['order']['id']]['items'][$change['item']['id']]['create'] = 1; $orders[$change['order']['id']]['order_edit'] = 1; unset($orders[$change['order']['id']]['items'][$change['item']['id']]['delete']); } - + if(empty($change['newValue']) && $change['field'] == 'order_product') { $orders[$change['order']['id']]['items'][$change['item']['id']]['delete'] = 1; - $orders[$change['order']['id']]['order_edit'] = 1; + $orders[$change['order']['id']]['order_edit'] = 1; } - + if(!empty($change['newValue']) && $change['field'] == 'order_product.quantity') { $orders[$change['order']['id']]['order_edit'] = 1; } - + if(!$orders[$change['order']['id']]['items'][$change['item']['id']]['create'] && $fields['item'][$change['field']]) { $orders[$change['order']['id']]['items'][$change['item']['id']][$fields['item'][$change['field']]] = $change['newValue']; } @@ -941,11 +941,11 @@ class Retailcrm_Retailcrm_Model_Exchange elseif($fields['order'][$change['field']]) { $orders[$change['order']['id']][$fields['order'][$change['field']]] = self::newValue($change['newValue']); } - + if(isset($change['created'])) { $orders[$change['order']['id']]['create'] = 1; } - + if(isset($change['deleted'])) { $orders[$change['order']['id']]['deleted'] = 1; } @@ -954,7 +954,7 @@ class Retailcrm_Retailcrm_Model_Exchange return $orders; } - + public static function removeEmpty($inputArray) { $outputArray = array(); @@ -964,15 +964,15 @@ class Retailcrm_Retailcrm_Model_Exchange if (is_array($element)) { $element = self::removeEmpty($element); } - + $outputArray[$key] = $element; } } } - + return $outputArray; } - + public static function newValue($value) { if(isset($value['code'])) { @@ -981,7 +981,7 @@ class Retailcrm_Retailcrm_Model_Exchange return $value; } } - + public static function getAllShippingMethodsCode($code) { $methods = Mage::getSingleton('shipping/config')->getActiveCarriers(); @@ -992,12 +992,12 @@ class Retailcrm_Retailcrm_Model_Exchange $_code = $_ccode . '_' . $_mcode; $options[$_ccode] = $_code; } - } + } } - + return $options[$code]; } - - + + } diff --git a/app/code/community/Retailcrm/Retailcrm/Model/Order.php b/app/code/community/Retailcrm/Retailcrm/Model/Order.php index 7524f0b..0ee8f70 100644 --- a/app/code/community/Retailcrm/Retailcrm/Model/Order.php +++ b/app/code/community/Retailcrm/Retailcrm/Model/Order.php @@ -103,25 +103,23 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange } $product = array( - 'productId' => $item->getProductId(), 'productName' => !isset($parent) ? $item->getName() : $parent->getName(), 'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()), 'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice(), - 'offer'=>array( - 'externalId'=>$item->getProductId() + 'offer' => array( + 'externalId' => $item->getProductId() ) ); unset($parent); $items[] = $product; - } elseif($item->getProductType() == "grouped") { + } elseif ($item->getProductType() == "grouped") { $product = array( - 'productId' => $item->getProductId(), 'productName' => $item->getName(), 'quantity' => $item->getQtyOrdered(), 'initialPrice' => $item->getPrice(), - 'offer'=>array( - 'externalId'=>$item->getProductId() + 'offer' => array( + 'externalId '=> $item->getProductId() ) ); @@ -172,15 +170,15 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange if (trim($preparedOrder['delivery']['code']) == ''){ unset($preparedOrder['delivery']['code']); } - + if (trim($preparedOrder['paymentType']) == ''){ unset($preparedOrder['paymentType']); } - + if (trim($preparedOrder['status']) == ''){ unset($preparedOrder['status']); } - + if ($order->getCustomerIsGuest() == 0) { $preparedCustomer = array( 'externalId' => $order->getCustomerId() @@ -267,9 +265,9 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange } return true; - + } - + /** * Orders export * @@ -341,18 +339,20 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange if ($item->getParentItemId()) { $parent = Mage::getModel('sales/order_item')->load($item->getParentItemId()); } - + $product = array( - 'productId' => $item->getProductId(), 'productName' => !isset($parent) ? $item->getName() : $parent->getName(), 'quantity' => !isset($parent) ? intval($item->getQtyOrdered()) : intval($parent->getQtyOrdered()), - 'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice() + 'initialPrice' => !isset($parent) ? $item->getPrice() : $parent->getPrice(), + 'offer' => array( + 'externalId' => $item->getProductId() + ) ); unset($parent); $items[] = $product; } } - + $shipping = $this->getShippingCode($order->getShippingMethod()); $preparedOrder = array( 'externalId' => $order->getRealOrderId(), @@ -391,23 +391,23 @@ class Retailcrm_Retailcrm_Model_Order extends Retailcrm_Retailcrm_Model_Exchange ), ) ); - + if(trim($preparedOrder['delivery']['code']) == ''){ unset($preparedOrder['delivery']['code']); } - + if(trim($preparedOrder['paymentType']) == ''){ unset($preparedOrder['paymentType']); } - + if(trim($preparedOrder['status']) == ''){ unset($preparedOrder['status']); } - + if ($order->getCustomerIsGuest() != 0) { $preparedOrder['customer']['externalId'] = $order->getCustomerId(); } - + return Mage::helper('retailcrm')->filterRecursive($preparedOrder); }