Skip to content

Commit

Permalink
6.44.1 (FINAL RELEASE)
Browse files Browse the repository at this point in the history
  • Loading branch information
m2epro committed Aug 11, 2023
1 parent 76bfbf3 commit 138c8b9
Show file tree
Hide file tree
Showing 11 changed files with 401 additions and 14 deletions.
4 changes: 4 additions & 0 deletions app/code/community/Ess/M2ePro/Model/Cron/Task/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ class Ess_M2ePro_Model_Cron_Task_Repository
'component' => Ess_M2ePro_Helper_Component_Walmart::NICK,
'group' => self::GROUP_WALMART,
),
Ess_M2ePro_Model_Cron_Task_Walmart_Order_ReceiveWithCancellationRequested::NICK => array(
'component' => Ess_M2ePro_Helper_Component_Walmart::NICK,
'group' => self::GROUP_WALMART,
),
Ess_M2ePro_Model_Cron_Task_Walmart_Order_CreateFailed::NICK => array(
'component' => Ess_M2ePro_Helper_Component_Walmart::NICK,
'group' => self::GROUP_WALMART,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,274 @@
<?php

/**
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/

class Ess_M2ePro_Model_Cron_Task_Walmart_Order_ReceiveWithCancellationRequested
extends Ess_M2ePro_Model_Cron_Task_Abstract
{
const NICK = 'walmart/order/receive_with_cancellation_requested';

const INTERVAL_TO_FIRST_CHECK_BUYER_CANCELLATION_REQUESTS = 259200; // 3 days
const INTERVAL_TO_COMMON_CHECK_BUYER_CANCELLATION_REQUESTS = 86400; // 1 day
const INTERVAL_FOR_ACCOUNT_SYNCHRONIZATION = 7200; // 2 hours

const REGISTRY_PREFIX = '/walmart/order/receive_with_cancellation_requested/by_account/';
const REGISTRY_SUFFIX = '/last_update/';

/** @var Ess_M2ePro_Helper_Data */
protected $_dataHelper;
/** @var Ess_M2ePro_Helper_Server_Maintenance */
protected $_serverMaintenanceHelper;
/** @var Ess_M2ePro_Helper_Module_Logger */
protected $_moduleLoggerHelper;
/** @var Ess_M2ePro_Model_Registry_Manager */
protected $_registryManager;
/** @var Ess_M2ePro_Model_Synchronization_Log */
protected $_synchronizationLog;
/** @var bool */
protected $_accountSynchronizationFail = false;

public function __construct()
{
$this->_dataHelper = Mage::helper('M2ePro');
$this->_serverMaintenanceHelper = Mage::helper('M2ePro/Server_Maintenance');
$this->_moduleLoggerHelper = Mage::helper('M2ePro/Module_Logger');
$this->_registryManager = Mage::getModel('M2ePro/Registry_Manager');
}

/**
* @return Ess_M2ePro_Model_Synchronization_Log
*/
protected function getSynchronizationLog()
{
if ($this->_synchronizationLog === null) {
$this->_synchronizationLog = parent::getSynchronizationLog();

$this->_synchronizationLog->setComponentMode(Ess_M2ePro_Helper_Component_Walmart::NICK);
$this->_synchronizationLog->setSynchronizationTask(Ess_M2ePro_Model_Synchronization_Log::TASK_ORDERS);
}

return $this->_synchronizationLog;
}

public function isPossibleToRun()
{
if ($this->_serverMaintenanceHelper->isNow()) {
return false;
}

return parent::isPossibleToRun();
}

protected function performActions()
{
/** @var $accountsCollection Mage_Core_Model_Resource_Db_Collection_Abstract */
$accountsCollection = Mage::helper('M2ePro/Component_Walmart')->getCollection('Account');

/** @var $account Ess_M2ePro_Model_Account **/
foreach ($accountsCollection->getItems() as $account) {
try {
$accountId = (int)$account->getId();
if (!$this->isItemsReceiveRequired($accountId)) {
continue;
}

$this->_accountSynchronizationFail = false;
$responseData = $this->receiveOrderItems($account, $this->getReceiveInterval($accountId));
$this->processOrderItems($responseData);

if (!$this->_accountSynchronizationFail) {
$this->updateLastReceiveDate($accountId);
}
} catch (Exception $exception) {
$message = $this->_dataHelper->__(
'The "Receive Orders with Buyer Cancellation Requested" '
. 'Action for Walmart Account "%title%" was completed with error.',
$account->getTitle()
);

$this->processTaskAccountException($message, __FILE__, __LINE__);
$this->processTaskException($exception);
}
}
}

/**
* @param int $accountId
*
* @return string
*/
protected function getRegistryKey($accountId)
{
return self::REGISTRY_PREFIX . $accountId . self::REGISTRY_SUFFIX;
}

/**
* @param int $accountId
* @return bool
*/
protected function isItemsReceiveRequired($accountId)
{
$lastUpdate = $this->_registryManager->getValue($this->getRegistryKey($accountId));
if (!$lastUpdate) {
return true;
}

$now = $this->_dataHelper->createCurrentGmtDateTime();
$lastUpdateDateTime = $this->_dataHelper->createGmtDateTime($lastUpdate);
return $now->getTimestamp() - $lastUpdateDateTime->getTimestamp() > self::INTERVAL_FOR_ACCOUNT_SYNCHRONIZATION;
}

/**
* @param int $accountId
* @return void
*/
protected function updateLastReceiveDate($accountId)
{
$now = $this->_dataHelper->createCurrentGmtDateTime();
$this->_registryManager->setValue(
$this->getRegistryKey($accountId),
$now->format('Y-m-d H:i:s')
);
}

/**
* @param int $accountId
*
* @return int
*/
protected function getReceiveInterval($accountId)
{
$lastUpdate = $this->_registryManager->getValue($this->getRegistryKey($accountId));

return $lastUpdate ?
self::INTERVAL_TO_COMMON_CHECK_BUYER_CANCELLATION_REQUESTS
: self::INTERVAL_TO_FIRST_CHECK_BUYER_CANCELLATION_REQUESTS;
}

/**
* @param Ess_M2ePro_Model_Account $account
* @param int $interval
* @return array|mixed
* @throws Exception
*/
protected function receiveOrderItems(Ess_M2ePro_Model_Account $account, $interval)
{
$fromDate = $this->_dataHelper->createCurrentGmtDateTime();
$fromDate->modify("-$interval seconds");

/** @var Ess_M2ePro_Model_Walmart_Connector_Dispatcher $dispatcherObject */
$dispatcherObject = Mage::getModel('M2ePro/Walmart_Connector_Dispatcher');
$connectorObj = $dispatcherObject->getConnector(
'orders',
'get',
'itemsCancellationRequested',
array(
'account' => $account->getChildObject()->getServerHash(),
'from_create_date' => $fromDate->format('Y-m-d H:i:s'),
)
);
$dispatcherObject->process($connectorObj);

$this->processResponseMessages($connectorObj->getResponseMessages());

return $connectorObj->getResponseData();
}

/**
* @param array $items
* @return void
* @throws Ess_M2ePro_Model_Exception_Logic
*/
protected function processOrderItems(array $items)
{
foreach ($items as $item) {
/** @var Ess_M2ePro_Model_Resource_Order_Item_Collection $collection */
$collection = Mage::helper('M2ePro/Component_Walmart')->getCollection('Order_Item');
$collection
->addFieldToFilter('walmart_order_id', $item['walmart_order_id'])
->addFieldToFilter('sku', $item['sku']);
$collection->getSelect()->join(
array(
'wo' => Mage::getResourceModel('M2ePro/Walmart_Order')->getMainTable()
),
'main_table.order_id=wo.order_id',
array()
);

/** @var Ess_M2ePro_Model_Order_Item $existItem */
$existItem = $collection->getFirstItem();

if (!$existItem->getId()) {
$this->_moduleLoggerHelper->process(
array(
'walmart_order_id' => $item['walmart_order_id'],
'sku' => $item['sku'],
),
'Walmart orders receive with cancellation requested task - cannot find order item'
);

continue;
}

$cancellationRequestSavedPreviously = $existItem->getChildObject()
->isBuyerCancellationRequested();
if ($cancellationRequestSavedPreviously) {
continue;
}

$existItem->getChildObject()
->setData('buyer_cancellation_requested', 1)
->save();

$this->notifyAboutBuyerCancellationRequested($existItem->getChildObject());
}
}

/**
* @param Ess_M2ePro_Model_Walmart_Order_Item $walmartOrderItem
* @return void
* @throws Ess_M2ePro_Model_Exception_Logic
*/
protected function notifyAboutBuyerCancellationRequested(Ess_M2ePro_Model_Walmart_Order_Item $walmartOrderItem)
{
$description = 'A buyer requested to cancel the item(s) "%item_name%" from the order #%order_number%.';

$walmartOrder = $walmartOrderItem->getWalmartOrder();
$walmartOrder->getParentObject()->addWarningLog(
$description,
array(
'!order_number' => $walmartOrder->getWalmartOrderId(),
'!item_name' => $walmartOrderItem->getTitle()
)
);
}

protected function processResponseMessages(array $messages = array())
{
/** @var Ess_M2ePro_Model_Connector_Connection_Response_Message_Set $messagesSet */
$messagesSet = Mage::getModel('M2ePro/Connector_Connection_Response_Message_Set');
$messagesSet->init($messages);

foreach ($messagesSet->getEntities() as $message) {
if (!$message->isError() && !$message->isWarning()) {
continue;
}

if ($message->isError()) {
$this->_accountSynchronizationFail = true;
}

$logType = $message->isError() ? Ess_M2ePro_Model_Log_Abstract::TYPE_ERROR
: Ess_M2ePro_Model_Log_Abstract::TYPE_WARNING;

$this->getSynchronizationLog()->addMessage(
$this->_dataHelper->__($message->getText()),
$logType
);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/**
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/

class Ess_M2ePro_Model_Walmart_Connector_Orders_Get_ItemsCancellationRequested
extends Ess_M2ePro_Model_Walmart_Connector_Command_RealTime
{
/**
* @return array
*/
public function getRequestData()
{
return array(
'account' => $this->_params['account'],
'from_create_date' => $this->_params['from_create_date'],
);
}

/**
* @return array
*/
protected function getCommand()
{
return array('orders', 'get', 'itemsCancellationRequested');
}

/**
* @return bool
*/
protected function validateResponse()
{
$responseData = $this->getResponse()->getData();

return isset($responseData['items']);
}

/**
* @return void
*/
protected function prepareResponseData()
{
$result = array();
$responseData = $this->getResponse()->getData();

foreach ($responseData['items'] as $item) {
$result[] = array(
'sku' => $item['sku'],
'walmart_order_id' => $item['walmart_order_id'],
);
}

$this->_responseData = $result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,13 @@ protected function getItemsToRefund(Ess_M2ePro_Model_Order $order, Mage_Sales_Mo
if ($item === null) {
continue;
}
$qtyPurchased = $item->getChildObject()->getQtyPurchased();

/**
* Walmart returns the same Order Item more than one time with single QTY. That data was merged
*/
$mergedOrderItems = $item->getChildObject()->getMergedWalmartOrderItemIds();
$walmartOrderItemsCount = 1 + count($mergedOrderItems);
while ($mergedOrderItemId = array_shift($mergedOrderItems)) {
if (!isset($data['refunded_qty'][$mergedOrderItemId])) {
$orderItemId = $mergedOrderItemId;
Expand All @@ -78,7 +80,7 @@ protected function getItemsToRefund(Ess_M2ePro_Model_Order $order, Mage_Sales_Mo
* - Walmart Order Item QTY is always equals 1
*/
$itemQtyRef = isset($data['refunded_qty'][$orderItemId]) ? $data['refunded_qty'][$orderItemId] : 0;
$itemQty = 1;
$itemQty = $qtyPurchased / $walmartOrderItemsCount;

if ($itemQtyRef >= $itemQty) {
continue;
Expand Down
Loading

0 comments on commit 138c8b9

Please sign in to comment.