Skip to content

Commit

Permalink
6.4.6 RELEASE FINAL
Browse files Browse the repository at this point in the history
  • Loading branch information
d-belousov committed Nov 22, 2016
1 parent e554f8d commit dba2737
Show file tree
Hide file tree
Showing 30 changed files with 611 additions and 298 deletions.
10 changes: 10 additions & 0 deletions app/code/community/Ess/M2ePro/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
* 6.4.6 (r10944) (22/11/2016)

* Improvement: Orders could not be dowloaded if there were a lot of Orders updates in one second on channel
* Improvement: There was Orders downloading delay if many List/Relist/Revise/Stop actions are processed at that time
* Improvement: [eBay] Now you cannot List the Variational Product if their Attributes/Options have space at start or end
* Improvement: [Amazon] There was Orders statuses updating delay if many List/Relist/Revise/Stop actions are processed at that time
* Fix: Magento order was not created by "Out of Stock" reason if this Product Quantity was reserved earlier
* Fix: Missed records in Listings Log during Magento Orders creation for products with negative QTY
* Fix: [eBay] "Undefined index: reference_id" error during Magento Order creation

* 6.4.5 (r10812) (02/10/2016)

* Added: Compatibility with PHP version < 7.0.11 (6.x.x - 7.0.11 added) [only as beta mode]
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Ess/M2ePro/Helper/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function getVersion()

public function getRevision()
{
$revision = '10812';
$revision = '10944';

if ($revision == str_replace('|','#','|REVISION|')) {
$revision = (int)exec('svnversion');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,39 +47,45 @@ protected function performActions()
$iteration = 0;
$percentsForOneAccount = $this->getPercentsInterval() / count($permittedAccounts);

foreach ($permittedAccounts as $account) {
foreach ($permittedAccounts as $merchantId => $accounts) {

/** @var $account Ess_M2ePro_Model_Account **/

$accountsIds = array();
$accountsTitles = array();
foreach ($accounts as $account) {
$accountsIds[] = $account->getId();
$accountsTitles[] = $account->getTitle();
}
$accountsIds = implode(', ',$accountsIds);
$accountsTitles = implode(', ',$accountsTitles);

// ---------------------------------------
$this->getActualOperationHistory()->addText('Starting Account "'.$account->getTitle().'"');
$this->getActualOperationHistory()->addText('Starting Accounts "'.$accountsTitles.'"');
// M2ePro_TRANSLATIONS
// The "Receive" Action for Amazon Account: "%account_title%" is started. Please wait...
$status = 'The "Receive" Action for Amazon Account: "%account_title%" is started. Please wait...';
$this->getActualLockItem()->setStatus(Mage::helper('M2ePro')->__($status, $account->getTitle()));
// The "Receive" Action for Amazon Accounts: "%account_title%" is started. Please wait...
$status = 'The "Receive" Action for Amazon Accounts: "%account_title%" is started. Please wait...';
$this->getActualLockItem()->setStatus(Mage::helper('M2ePro')->__($status, $accountsTitles));
// ---------------------------------------

if (!$this->isLockedAccount($account)) {

// ---------------------------------------
$this->getActualOperationHistory()->addTimePoint(
__METHOD__.'process'.$account->getId(),
'Process Account '.$account->getTitle()
);
// ---------------------------------------
// ---------------------------------------
$this->getActualOperationHistory()->addTimePoint(
__METHOD__.'process'.$accountsIds,
'Process Accounts '.$accountsTitles
);
// ---------------------------------------

$this->processAccount($account);
$this->processAccounts($merchantId, $accounts);

// ---------------------------------------
$this->getActualOperationHistory()->saveTimePoint(__METHOD__.'process'.$account->getId());
// ---------------------------------------
}
// ---------------------------------------
$this->getActualOperationHistory()->saveTimePoint(__METHOD__.'process'.$accountsIds);
// ---------------------------------------

// ---------------------------------------
// M2ePro_TRANSLATIONS
// The "Receive" Action for Amazon Account: "%account_title%" is finished. Please wait...
$status = 'The "Receive" Action for Amazon Account: "%account_title%" is finished. Please wait...';
$this->getActualLockItem()->setStatus(Mage::helper('M2ePro')->__($status, $account->getTitle()));
// The "Receive" Action for Amazon Accounts: "%account_title%" is finished. Please wait...
$status = 'The "Receive" Action for Amazon Accounts: "%account_title%" is finished. Please wait...';
$this->getActualLockItem()->setStatus(Mage::helper('M2ePro')->__($status, $accountsTitles));
$this->getActualLockItem()->setPercents($this->getPercentsStart() + $iteration * $percentsForOneAccount);
$this->getActualLockItem()->activate();
// ---------------------------------------
Expand All @@ -94,47 +100,67 @@ private function getPermittedAccounts()
{
/** @var $accountsCollection Mage_Core_Model_Mysql4_Collection_Abstract */
$accountsCollection = Mage::helper('M2ePro/Component_Amazon')->getCollection('Account');
return $accountsCollection->getItems();

$accounts = array();
foreach ($accountsCollection->getItems() as $accountItem) {
/** @var $accountItem Ess_M2ePro_Model_Account */

$merchantId = $accountItem->getChildObject()->getMerchantId();
if (!isset($accounts[$merchantId])) {
$accounts[$merchantId] = array();
}

$accounts[$merchantId][] = $accountItem;
}

return $accounts;
}

// ---------------------------------------

private function isLockedAccount(Ess_M2ePro_Model_Account $account)
private function processAccounts($merchantId, array $accounts)
{
/** @var $lockItem Ess_M2ePro_Model_LockItem */
$lockItem = Mage::getModel('M2ePro/LockItem');
$lockItem->setNick(self::LOCK_ITEM_PREFIX.'_'.$account->getId());
$lockItem->setMaxInactiveTime(Ess_M2ePro_Model_Processing_Request::MAX_LIFE_TIME_INTERVAL);
return $lockItem->isExist();
}
$updateSinceTime = Mage::getSingleton('M2ePro/Config_Synchronization')->getGroupValue(
"/amazon/orders/receive/{$merchantId}/", "from_update_date"
);

$fromDate = $this->prepareFromDate($updateSinceTime);
$toDate = $this->prepareToDate();

if (strtotime($fromDate) >= strtotime($toDate)) {
$fromDate = new DateTime($toDate, new DateTimeZone('UTC'));
$fromDate->modify('- 5 minutes');

$fromDate = $fromDate->format('Y-m-d H:i:s');
}

private function processAccount(Ess_M2ePro_Model_Account $account)
{
$fromDate = $this->prepareFromDate($account->getData('orders_last_synchronization'));
$params = array(
'from_date' => $fromDate
'accounts' => $accounts,
'from_update_date' => $fromDate,
'to_update_date'=> $toDate
);

/** @var Ess_M2ePro_Model_Amazon_Account $amazonAccount */
$amazonAccount = $account->getChildObject();
$jobToken = Mage::getSingleton('M2ePro/Config_Synchronization')->getGroupValue(
"/amazon/orders/receive/{$merchantId}/", "job_token"
);

if (is_null($amazonAccount->getData('orders_last_synchronization'))) {
$amazonAccount->setData('orders_last_synchronization', $fromDate)->save();
if (!empty($jobToken)) {
$params['job_token'] = $jobToken;
}

$dispatcherObject = Mage::getModel('M2ePro/Connector_Amazon_Dispatcher');
$connectorObj = $dispatcherObject->getConnector('orders', 'receive', 'requester',
$params, $account, 'Ess_M2ePro_Model_Amazon_Synchronization');
$connectorObj = $dispatcherObject->getConnector(
'orders', 'receive', 'requester', $params, NULL,
'Ess_M2ePro_Model_Amazon_Synchronization'
);
$dispatcherObject->process($connectorObj);
}

//########################################

private function prepareFromDate($lastFromDate)
{
// Get last from date
// ---------------------------------------
if (is_null($lastFromDate)) {
if (empty($lastFromDate)) {
$lastFromDate = new DateTime('now', new DateTimeZone('UTC'));
} else {
$lastFromDate = new DateTime($lastFromDate, new DateTimeZone('UTC'));
Expand All @@ -144,7 +170,7 @@ private function prepareFromDate($lastFromDate)
// Get min date for synch
// ---------------------------------------
$minDate = new DateTime('now',new DateTimeZone('UTC'));
$minDate->modify('-7 days');
$minDate->modify('-30 days');
// ---------------------------------------

// Prepare last date
Expand All @@ -157,5 +183,18 @@ private function prepareFromDate($lastFromDate)
return $lastFromDate->format('Y-m-d H:i:s');
}

private function prepareToDate()
{
$operationHistory = $this->getActualOperationHistory()->getParentObject('synchronization');
if (!is_null($operationHistory)) {
$toDate = $operationHistory->getData('start_date');
} else {
$toDate = new DateTime('now', new DateTimeZone('UTC'));
$toDate = $toDate->format('Y-m-d H:i:s');
}

return $toDate;
}

//########################################
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,6 @@ class Ess_M2ePro_Model_Amazon_Synchronization_Orders_Receive_Requester

//########################################

/**
* @param Ess_M2ePro_Model_Processing_Request $processingRequest
* @throws Ess_M2ePro_Model_Exception_Logic
*/
public function setProcessingLocks(Ess_M2ePro_Model_Processing_Request $processingRequest)
{
parent::setProcessingLocks($processingRequest);

/** @var $lockItem Ess_M2ePro_Model_LockItem */
$lockItem = Mage::getModel('M2ePro/LockItem');

$tempNick = Ess_M2ePro_Model_Amazon_Synchronization_Orders_Receive::LOCK_ITEM_PREFIX
.'_'.$this->account->getId();

$lockItem->setNick($tempNick);
$lockItem->setMaxInactiveTime(Ess_M2ePro_Model_Processing_Request::MAX_LIFE_TIME_INTERVAL);
$lockItem->create();

$this->account->addObjectLock(NULL, $processingRequest->getHash());
$this->account->addObjectLock('synchronization', $processingRequest->getHash());
$this->account->addObjectLock('synchronization_amazon', $processingRequest->getHash());
$this->account->addObjectLock(
Ess_M2ePro_Model_Amazon_Synchronization_Orders_Receive::LOCK_ITEM_PREFIX, $processingRequest->getHash()
);
}

//########################################

public function process()
{
$cacheConfig = Mage::helper('M2ePro/Module')->getCacheConfig();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function isNeedToParseResponseData($responseBody)
return false;
}

if ($this->hasErrorMessages()) {
if ($this->hasErrorMessages() && !isset($response['items'])) {
return false;
}

Expand All @@ -49,31 +49,6 @@ protected function isNeedToParseResponseData($responseBody)

//########################################

/**
* @param Ess_M2ePro_Model_Processing_Request $processingRequest
* @throws Ess_M2ePro_Model_Exception_Logic
*/
public function unsetProcessingLocks(Ess_M2ePro_Model_Processing_Request $processingRequest)
{
parent::unsetProcessingLocks($processingRequest);

/** @var $lockItem Ess_M2ePro_Model_LockItem */
$lockItem = Mage::getModel('M2ePro/LockItem');

$lockItem->setNick(
Ess_M2ePro_Model_Amazon_Synchronization_Orders_Receive::LOCK_ITEM_PREFIX.'_'.$this->params['account_id']
);
$lockItem->setMaxInactiveTime(Ess_M2ePro_Model_Processing_Request::MAX_LIFE_TIME_INTERVAL);
$lockItem->remove();

$this->getAccount()->deleteObjectLocks(NULL, $processingRequest->getHash());
$this->getAccount()->deleteObjectLocks('synchronization', $processingRequest->getHash());
$this->getAccount()->deleteObjectLocks('synchronization_amazon', $processingRequest->getHash());
$this->getAccount()->deleteObjectLocks(
Ess_M2ePro_Model_Amazon_Synchronization_Orders_Receive::LOCK_ITEM_PREFIX, $processingRequest->getHash()
);
}

public function eventFailedExecuting($message)
{
parent::eventFailedExecuting($message);
Expand All @@ -89,44 +64,61 @@ public function eventFailedExecuting($message)

protected function processResponseData($response)
{
try {
$accounts = $this->getAccountsByAccessTokens();

$processedAmazonOrders = array();
foreach ($response['orders'] as $accountAccessToken => $ordersData) {

$amazonOrders = $this->processAmazonOrders($ordersData, $accounts[$accountAccessToken]);

$amazonOrders = $this->processAmazonOrders($response, $this->getAccount());
if (empty($amazonOrders)) {
return;
continue;
}

$this->createMagentoOrders($amazonOrders);
$processedAmazonOrders[] = $amazonOrders;
}

} catch (Exception $exception) {
$merchantId = current($accounts)->getChildObject()->getMerchantId();

$this->getSynchronizationLog()->addMessage(
Mage::helper('M2ePro')->__($exception->getMessage()),
Ess_M2ePro_Model_Log_Abstract::TYPE_ERROR,
Ess_M2ePro_Model_Log_Abstract::PRIORITY_HIGH
if (!empty($response['job_token'])) {
Mage::getSingleton('M2ePro/Config_Synchronization')->setGroupValue(
"/amazon/orders/receive/{$merchantId}/", "job_token", $response['job_token']
);
} else {
Mage::getSingleton('M2ePro/Config_Synchronization')->deleteGroupValue(
"/amazon/orders/receive/{$merchantId}/", "job_token"
);
}

Mage::getSingleton('M2ePro/Config_Synchronization')->setGroupValue(
"/amazon/orders/receive/{$merchantId}/", "from_update_date", $response['to_update_date']
);

foreach ($processedAmazonOrders as $amazonOrders) {
try {

Mage::helper('M2ePro/Module_Exception')->process($exception);
$this->createMagentoOrders($amazonOrders);

} catch (Exception $exception) {

$this->getSynchronizationLog()->addMessage(
Mage::helper('M2ePro')->__($exception->getMessage()),
Ess_M2ePro_Model_Log_Abstract::TYPE_ERROR,
Ess_M2ePro_Model_Log_Abstract::PRIORITY_HIGH
);

Mage::helper('M2ePro/Module_Exception')->process($exception);
}
}
}

// ---------------------------------------

private function processAmazonOrders($response, Ess_M2ePro_Model_Account $account)
{
/** @var Ess_M2ePro_Model_Amazon_Account $amazonAccount */
$amazonAccount = $account->getChildObject();

$ordersLastSynchronization = $amazonAccount->getData('orders_last_synchronization');

$orders = array();

foreach ($response as $orderData) {
$currentOrderUpdateDate = $orderData['purchase_update_date'];

if (strtotime($currentOrderUpdateDate) > strtotime($ordersLastSynchronization)) {
$ordersLastSynchronization = $currentOrderUpdateDate;
}

/** @var $orderBuilder Ess_M2ePro_Model_Amazon_Order_Builder */
$orderBuilder = Mage::getModel('M2ePro/Amazon_Order_Builder');
Expand All @@ -141,8 +133,6 @@ private function processAmazonOrders($response, Ess_M2ePro_Model_Account $accoun
$orders[] = $order;
}

$amazonAccount->setData('orders_last_synchronization', $ordersLastSynchronization)->save();

return $orders;
}

Expand Down Expand Up @@ -178,24 +168,6 @@ private function createMagentoOrders($amazonOrders)

//########################################

/**
* @return Ess_M2ePro_Model_Account
*/
protected function getAccount()
{
return $this->getObjectByParam('Account','account_id');
}

/**
* @return Ess_M2ePro_Model_Marketplace
*/
protected function getMarketplace()
{
return $this->getAccount()->getChildObject()->getMarketplace();
}

//########################################

private function getSynchronizationLog()
{
if (!is_null($this->synchronizationLog)) {
Expand Down
Loading

0 comments on commit dba2737

Please sign in to comment.