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 +