/
CreateMagentoOrder.php
executable file
·86 lines (71 loc) · 2.49 KB
/
CreateMagentoOrder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php
/**
* @author M2E Pro Developers Team
* @copyright M2E LTD
* @license Commercial use is forbidden
*/
namespace Ess\M2ePro\Controller\Adminhtml\Amazon\Order;
use Ess\M2ePro\Controller\Adminhtml\Amazon\Order;
/**
* Class \Ess\M2ePro\Controller\Adminhtml\Amazon\Order\CreateMagentoOrder
*/
class CreateMagentoOrder extends Order
{
public function execute()
{
$ids = $this->getRequestIds();
$isForce = (bool)$this->getRequest()->getParam('force');
$warnings = 0;
$errors = 0;
foreach ($ids as $id) {
/** @var \Ess\M2ePro\Model\Order $order */
$order = $this->amazonFactory->getObjectLoaded('Order', (int)$id);
$order->getLog()->setInitiator(\Ess\M2ePro\Helper\Data::INITIATOR_USER);
if ($order->getMagentoOrderId() !== null && !$isForce) {
$warnings++;
continue;
}
// Create magento order
// ---------------------------------------
try {
$order->createMagentoOrder($isForce);
} catch (\Exception $e) {
$errors++;
}
// ---------------------------------------
if ($order->getChildObject()->canCreateInvoice()) {
$order->createInvoice();
}
$order->createShipments();
// ---------------------------------------
$order->updateMagentoOrderStatus();
// ---------------------------------------
}
if (!$errors && !$warnings) {
$this->messageManager->addSuccess($this->__('Magento Order(s) were created.'));
}
if ($errors) {
$this->messageManager->addError(
$this->__(
'%count% Magento order(s) were not created. Please <a target="_blank" href="%url%">view Log</a>
for the details.',
$errors,
$this->getUrl('*/amazon_log_order')
)
);
}
if ($warnings) {
$this->messageManager->addWarning(
$this->__(
'%count% Magento order(s) are already created for the selected amazon order(s).',
$warnings
)
);
}
if (count($ids) == 1) {
return $this->_redirect('*/*/view', ['id' => $ids[0]]);
} else {
return $this->_redirect($this->redirect->getRefererUrl());
}
}
}