Compare commits
12 commits
Author | SHA1 | Date | |
---|---|---|---|
|
8def052f47 | ||
|
48fe118be9 | ||
|
c04286f62b | ||
|
bcefce1c4d | ||
|
d2d4866c13 | ||
|
d45ddf3fbf | ||
|
92dd5cedee | ||
|
ee90b20a65 | ||
|
ecd0dc03c0 | ||
|
086862ea87 | ||
|
aad19442a4 | ||
|
e43221c76d |
13 changed files with 610 additions and 255 deletions
12
README.md
12
README.md
|
@ -1,14 +1,14 @@
|
|||
Magento module
|
||||
==============
|
||||
Magento module (THIS VERSION IS DEPRECATED!)
|
||||
============================================
|
||||
|
||||
Magento module for interaction with [RetailCRM](http://www.retailcrm.ru) through [REST API](http://retailcrm.ru/docs/Разработчики).
|
||||
Magento 1.9.x module for interaction with [retailCRM](https://www.retailcrm.pro) through [REST API](https://www.retailcrm.pro/docs/Developers/Index).
|
||||
|
||||
Module allows:
|
||||
|
||||
* Exchange the orders with retailCRM
|
||||
* Configure relations between dictionaries of RetailCRM and Magento (statuses, payments, delivery types and etc)
|
||||
* Generate [ICML](http://docs.retailcrm.ru/index.php?n=Разработчики.ФорматICML) (Intaro Markup Language) for catalog loading by RetailCRM
|
||||
* Configure relations between dictionaries of retailCRM and Magento (statuses, payments, delivery types and etc)
|
||||
* Generate [ICML](https://www.retailcrm.pro/docs/Developers/ICML) (Intaro Markup Language) for catalog loading by retailCRM
|
||||
|
||||
ICML
|
||||
|
||||
By default ICML file is being generated by module every 4 hours. You can find file in the web root folder with name "retailcrm_{{shop_code}}.xml". For example, http://retailcrm.ru/retailcrm_default.xml
|
||||
By default ICML file is being generated by module every 4 hours. You can find file in the web root folder with name "retailcrm_{{shop_code}}.xml". For example, http://example.com/retailcrm_default.xml
|
||||
|
|
|
@ -0,0 +1,37 @@
|
|||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Backend_Api_Key extends Mage_Core_Model_Config_Data
|
||||
{
|
||||
protected function _beforeSave()
|
||||
{
|
||||
if ($this->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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Backend_Api_Url extends Mage_Core_Model_Config_Data
|
||||
{
|
||||
protected function _beforeSave()
|
||||
{
|
||||
if ($this->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;
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Site extends Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base
|
||||
{
|
||||
public function render(Varien_Data_Form_Element_Abstract $element)
|
||||
{
|
||||
$html = $this->_getHeaderHtml($element);
|
||||
|
||||
if (!empty($this->_apiUrl) && !empty($this->_apiKey) && $this->_isCredentialCorrect) {
|
||||
$html .= $this->_getFieldHtml($element);
|
||||
} else {
|
||||
$html .= '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
|
@ -0,0 +1,89 @@
|
|||
<?php
|
||||
class Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Sites extends Retailcrm_Retailcrm_Block_Adminhtml_System_Config_Form_Fieldset_Base
|
||||
{
|
||||
public function render(Varien_Data_Form_Element_Abstract $element)
|
||||
{
|
||||
$html = $this->_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 .= '<div style="margin-left: 15px;"><b><i>Please check your API Url & API Key</i></b></div>';
|
||||
}
|
||||
|
||||
$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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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');
|
||||
|
||||
|
@ -19,7 +21,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get orders history & modify orders into shop
|
||||
*
|
||||
|
@ -29,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);
|
||||
|
@ -44,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
|
||||
*/
|
||||
|
@ -87,27 +89,27 @@ 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();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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,33 +128,44 @@ 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())) {
|
||||
|
||||
|
||||
if (!is_numeric($customer->getId())) {
|
||||
$customer
|
||||
->setGropuId(1)
|
||||
->setGroupId(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);
|
||||
|
@ -212,7 +225,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(),
|
||||
|
@ -263,45 +276,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(
|
||||
|
@ -317,7 +330,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
}
|
||||
} catch (Retailcrm_Retailcrm_Model_Exception_CurlException $e) {
|
||||
Mage::log($e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -326,12 +339,12 @@ 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;
|
||||
$order = $response->order;
|
||||
} else {
|
||||
Mage::log(
|
||||
sprintf(
|
||||
|
@ -340,7 +353,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
$response->getErrorMsg()
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if (isset($response['errors'])) {
|
||||
Mage::log(implode(' :: ', $response['errors']));
|
||||
}
|
||||
|
@ -348,33 +361,43 @@ 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())) {
|
||||
|
||||
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);
|
||||
|
@ -382,7 +405,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())
|
||||
|
@ -397,14 +420,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(
|
||||
|
@ -420,7 +443,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
$response->getErrorMsg()
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if (isset($response['errors'])) {
|
||||
Mage::log(implode(' :: ', $response['errors']));
|
||||
}
|
||||
|
@ -429,12 +452,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(),
|
||||
|
@ -478,11 +501,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));
|
||||
|
@ -491,23 +514,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'])) {
|
||||
|
||||
if (!empty($oldOrderArr['original_increment_id'])) {
|
||||
$originalId = $oldOrderArr['original_increment_id'];
|
||||
}
|
||||
|
||||
|
||||
$orderDataUp = array(
|
||||
'original_increment_id' => $originalId,
|
||||
'relation_parent_id' => $oldOrder->getId(),
|
||||
|
@ -515,23 +538,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(
|
||||
|
@ -541,7 +564,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if (!$response->isSuccessful() || 200 !== $response->getStatusCode()) {
|
||||
Mage::log(
|
||||
sprintf(
|
||||
|
@ -550,7 +573,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
$response->getErrorMsg()
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
if (isset($response['errors'])) {
|
||||
Mage::log(implode(' :: ', $response['errors']));
|
||||
}
|
||||
|
@ -558,58 +581,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()
|
||||
|
@ -617,15 +640,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);
|
||||
|
@ -634,36 +657,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(
|
||||
|
@ -680,14 +703,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();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -820,8 +843,8 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
|
||||
return (string) $country;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
public static function assemblyOrder($orderHistory)
|
||||
{
|
||||
$orders = array();
|
||||
|
@ -833,32 +856,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')&&
|
||||
|
@ -868,31 +891,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'];
|
||||
}
|
||||
|
@ -919,11 +942,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;
|
||||
}
|
||||
|
@ -932,7 +955,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
|
||||
return $orders;
|
||||
}
|
||||
|
||||
|
||||
public static function removeEmpty($inputArray)
|
||||
{
|
||||
$outputArray = array();
|
||||
|
@ -942,15 +965,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'])) {
|
||||
|
@ -959,7 +982,7 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
return $value;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static function getAllShippingMethodsCode($code)
|
||||
{
|
||||
$methods = Mage::getSingleton('shipping/config')->getActiveCarriers();
|
||||
|
@ -970,12 +993,12 @@ class Retailcrm_Retailcrm_Model_Exchange
|
|||
$_code = $_ccode . '_' . $_mcode;
|
||||
$options[$_ccode] = $_code;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return $options[$code];
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ class Retailcrm_Retailcrm_Model_Icml
|
|||
$string = '<?xml version="1.0" encoding="UTF-8"?>
|
||||
<yml_catalog date="'.date('Y-m-d H:i:s').'">
|
||||
<shop>
|
||||
<name>'.Mage::app()->getStore($shop)->getName().'</name>
|
||||
<name>'.$shop->getName().'</name>
|
||||
<categories/>
|
||||
<offers/>
|
||||
</shop>
|
||||
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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()
|
||||
|
@ -98,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()
|
||||
)
|
||||
);
|
||||
|
||||
|
@ -127,7 +130,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,36 +166,35 @@ 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']);
|
||||
}
|
||||
|
||||
|
||||
if ($order->getCustomerIsGuest() == 0) {
|
||||
$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 +219,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,24 +248,26 @@ 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;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Orders export
|
||||
*
|
||||
|
@ -275,7 +278,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 +305,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 +328,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'))
|
||||
|
@ -334,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(),
|
||||
|
@ -384,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);
|
||||
}
|
||||
|
|
@ -57,6 +57,7 @@ SOFTWARE.
|
|||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<tooltip><![CDATA[https://<i><b>YourCrmName</b></i>.retailcrm.ru]]></tooltip>
|
||||
<backend_model>retailcrm_retailcrm_block_adminhtml_system_config_backend_api_url</backend_model>
|
||||
</api_url>
|
||||
<api_key translate="label comment">
|
||||
<label>API Key</label>
|
||||
|
@ -66,6 +67,7 @@ SOFTWARE.
|
|||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<tooltip><![CDATA[To generate an API Key, log in to RetailCRM then select Admin > Integration > API Keys]]></tooltip>
|
||||
<backend_model>retailcrm_retailcrm_block_adminhtml_system_config_backend_api_key</backend_model>
|
||||
</api_key>
|
||||
</fields>
|
||||
</general>
|
||||
|
@ -153,7 +155,26 @@ SOFTWARE.
|
|||
|
||||
</fields>
|
||||
</load_order>
|
||||
|
||||
<site translate="label comment" module="retailcrm">
|
||||
<expanded>1</expanded>
|
||||
<label>Site</label>
|
||||
<frontend_type>text</frontend_type>
|
||||
<show_in_default>1</show_in_default>
|
||||
<show_in_website>0</show_in_website>
|
||||
<show_in_store>0</show_in_store>
|
||||
<sort_order>16</sort_order>
|
||||
<frontend_model>retailcrm/adminhtml_system_config_form_fieldset_site</frontend_model>
|
||||
</site>
|
||||
<sites translate="label comment" module="retailcrm">
|
||||
<expanded>1</expanded>
|
||||
<label>Sites</label>
|
||||
<frontend_type>text</frontend_type>
|
||||
<show_in_default>0</show_in_default>
|
||||
<show_in_website>1</show_in_website>
|
||||
<show_in_store>1</show_in_store>
|
||||
<sort_order>17</sort_order>
|
||||
<frontend_model>retailcrm/adminhtml_system_config_form_fieldset_sites</frontend_model>
|
||||
</sites>
|
||||
</groups>
|
||||
</retailcrm>
|
||||
</sections>
|
||||
|
|
Loading…
Add table
Reference in a new issue