2017-05-31 15:24:46 +03:00
|
|
|
<?php
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2017-05-31 15:24:46 +03:00
|
|
|
namespace Retailcrm\Retailcrm\Model\History;
|
|
|
|
|
|
|
|
class Exchange
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
private $api;
|
|
|
|
private $config;
|
|
|
|
private $helper;
|
|
|
|
private $logger;
|
|
|
|
private $resourceConfig;
|
|
|
|
private $customerFactory;
|
|
|
|
private $quote;
|
|
|
|
private $customerRepository;
|
|
|
|
private $product;
|
|
|
|
private $shipconfig;
|
|
|
|
private $quoteManagement;
|
|
|
|
private $registry;
|
|
|
|
private $cacheTypeList;
|
|
|
|
private $order;
|
|
|
|
private $orderManagement;
|
|
|
|
private $eventManager;
|
|
|
|
private $objectManager;
|
|
|
|
private $orderInterface;
|
|
|
|
private $storeManager;
|
|
|
|
private $regionFactory;
|
|
|
|
|
|
|
|
public function __construct(
|
|
|
|
\Magento\Framework\App\ObjectManager $objectManager,
|
|
|
|
\Retailcrm\Retailcrm\Helper\Data $helper,
|
|
|
|
\Magento\Framework\App\Config\ScopeConfigInterface $config,
|
|
|
|
\Magento\Config\Model\ResourceModel\Config $resourceConfig,
|
|
|
|
\Magento\Customer\Model\CustomerFactory $customerFactory,
|
|
|
|
\Magento\Quote\Model\QuoteFactory $quote,
|
|
|
|
\Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
|
|
|
|
\Magento\Catalog\Model\Product $product,
|
|
|
|
\Magento\Shipping\Model\Config $shipconfig,
|
|
|
|
\Magento\Quote\Model\QuoteManagement $quoteManagement,
|
|
|
|
\Magento\Framework\Registry $registry,
|
|
|
|
\Magento\Framework\App\Cache\TypeListInterface $cacheTypeList,
|
|
|
|
\Magento\Sales\Api\Data\OrderInterface $orderInterface,
|
|
|
|
\Magento\Sales\Api\OrderManagementInterface $orderManagement,
|
|
|
|
\Magento\Framework\Event\Manager $eventManager,
|
|
|
|
\Retailcrm\Retailcrm\Model\Logger\Logger $logger,
|
|
|
|
\Magento\Sales\Model\Order $order,
|
|
|
|
\Magento\Store\Model\StoreManagerInterface $storeManager,
|
|
|
|
\Magento\Directory\Model\RegionFactory $regionFactory,
|
|
|
|
\Retailcrm\Retailcrm\Helper\Proxy $api
|
|
|
|
) {
|
|
|
|
$this->shipconfig = $shipconfig;
|
|
|
|
$this->logger = $logger;
|
|
|
|
$this->helper = $helper;
|
|
|
|
$this->config = $config;
|
|
|
|
$this->resourceConfig = $resourceConfig;
|
|
|
|
$this->customerFactory = $customerFactory;
|
|
|
|
$this->quote = $quote;
|
|
|
|
$this->customerRepository = $customerRepository;
|
|
|
|
$this->product = $product;
|
|
|
|
$this->quoteManagement = $quoteManagement;
|
|
|
|
$this->registry = $registry;
|
|
|
|
$this->cacheTypeList = $cacheTypeList;
|
|
|
|
$this->orderInterface = $orderInterface;
|
|
|
|
$this->orderManagement = $orderManagement;
|
|
|
|
$this->eventManager = $eventManager;
|
|
|
|
$this->objectManager = $objectManager;
|
|
|
|
$this->order = $order;
|
|
|
|
$this->storeManager = $storeManager;
|
|
|
|
$this->regionFactory = $regionFactory;
|
|
|
|
$this->api = $api;
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get orders history from CRM
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function ordersHistory()
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
if (!$this->api->isConfigured()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->registry->register('RETAILCRM_HISTORY', true);
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
$historyFilter = [];
|
|
|
|
$historyOrder = [];
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$historyStart = $this->config->getValue('retailcrm/general/filter_history');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
if ($historyStart && $historyStart > 0) {
|
|
|
|
$historyFilter['sinceId'] = $historyStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (true) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$response = $this->api->ordersHistory($historyFilter);
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
if ($response === false) {
|
2018-04-28 11:39:23 +03:00
|
|
|
return false;
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!$response->isSuccessful()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderH = isset($response['history']) ? $response['history'] : [];
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
if (empty($orderH)) {
|
2018-03-12 16:34:48 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$historyOrder = array_merge($historyOrder, $orderH);
|
|
|
|
$end = array_pop($orderH);
|
|
|
|
$historyFilter['sinceId'] = $end['id'];
|
|
|
|
|
|
|
|
if ($response['pagination']['totalPageCount'] == 1) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->resourceConfig->saveConfig(
|
|
|
|
'retailcrm/general/filter_history',
|
|
|
|
$historyFilter['sinceId'],
|
|
|
|
'default',
|
|
|
|
0
|
|
|
|
);
|
|
|
|
$this->cacheTypeList->cleanType('config');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
$orders = self::assemblyOrder($historyOrder);
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($orders, 'OrderHistory');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
$this->processOrders($orders);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2018-04-28 11:39:23 +03:00
|
|
|
}
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->registry->register('RETAILCRM_HISTORY', false);
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Process orders
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $orders
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function processOrders($orders)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($orders, 'processOrders');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
if (!empty($orders)) {
|
|
|
|
foreach ($orders as $order) {
|
|
|
|
if (isset($order['externalId']) && !empty($order['externalId'])) {
|
|
|
|
$this->doUpdate($order);
|
|
|
|
} else {
|
|
|
|
$this->doCreate($order);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create new order from CRM
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $order
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function doCreate($order)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($order, 'doCreate');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$payments = $this->config->getValue('retailcrm/Payment');
|
|
|
|
$shippings = $this->config->getValue('retailcrm/Shipping');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$region = $this->regionFactory->create();
|
|
|
|
$store = $this->storeManager->getStore();
|
|
|
|
$websiteId = $this->storeManager->getStore()->getWebsiteId();
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$customer = $this->customerFactory->create();
|
2018-03-12 16:34:48 +03:00
|
|
|
$customer->setWebsiteId($websiteId);
|
|
|
|
|
|
|
|
if (isset($order['customer']['externalId'])) {
|
|
|
|
$customer->load($order['customer']['externalId']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$customer->getId()) {
|
|
|
|
//If not avilable then create this customer
|
|
|
|
$customer->setWebsiteId($websiteId)
|
|
|
|
->setStore($store)
|
|
|
|
->setFirstname($order['firstName'])
|
|
|
|
->setLastname($order['lastName'])
|
|
|
|
->setEmail($order['email'])
|
|
|
|
->setPassword($order['email']);
|
|
|
|
try {
|
|
|
|
$customer->save();
|
|
|
|
} catch (\Exception $exception) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeRow($exception->getMessage());
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->api->customersFixExternalIds(
|
2018-03-12 16:34:48 +03:00
|
|
|
[
|
|
|
|
[
|
|
|
|
'id' => $order['customer']['id'],
|
|
|
|
'externalId' => $customer->getId()
|
|
|
|
]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create object of quote
|
2018-04-28 11:39:23 +03:00
|
|
|
$quote = $this->quote->create();
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
//set store for which you create quote
|
2018-04-28 11:39:23 +03:00
|
|
|
$quote->setStore($store);
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
// if you have allready buyer id then you can load customer directly
|
2018-04-28 11:39:23 +03:00
|
|
|
$customer = $this->customerRepository->getById($customer->getId());
|
2018-03-12 16:34:48 +03:00
|
|
|
$quote->setCurrency();
|
|
|
|
$quote->assignCustomer($customer); //Assign quote to customer
|
|
|
|
|
|
|
|
//add items in quote
|
2018-04-28 11:39:23 +03:00
|
|
|
foreach ($order['items'] as $item) {
|
|
|
|
$product = $this->product->load($item['offer']['externalId']);
|
2018-03-12 16:34:48 +03:00
|
|
|
$product->setPrice($item['initialPrice']);
|
|
|
|
$quote->addProduct(
|
|
|
|
$product,
|
2018-04-28 11:39:23 +03:00
|
|
|
(int)$item['quantity']
|
2018-03-12 16:34:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$products = [];
|
|
|
|
|
|
|
|
foreach ($order['items'] as $item) {
|
|
|
|
$products[$item['offer']['externalId']] = ['qty' => $item['quantity']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderData = [
|
2018-04-28 11:39:23 +03:00
|
|
|
'currency_id' => $this->storeManager->getStore()->getCurrentCurrency()->getCode(),
|
2018-03-12 16:34:48 +03:00
|
|
|
'email' => $order['email'],
|
|
|
|
'shipping_address' => [
|
|
|
|
'firstname' => $order['firstName'],
|
|
|
|
'lastname' => $order['lastName'],
|
|
|
|
'street' => $order['delivery']['address']['street'],
|
|
|
|
'city' => $order['delivery']['address']['city'],
|
|
|
|
'country_id' => $order['countryIso'],
|
|
|
|
'region' => $order['delivery']['address']['region'],
|
|
|
|
'postcode' => $order['delivery']['address']['index'],
|
|
|
|
'telephone' => $order['phone'],
|
|
|
|
'save_in_address_book' => 1
|
|
|
|
],
|
|
|
|
'items'=> $products
|
|
|
|
];
|
|
|
|
|
|
|
|
$region->loadByName($order['delivery']['address']['region'], $order['countryIso']);
|
|
|
|
|
|
|
|
if ($region->getId()) {
|
|
|
|
$orderData['shipping_address']['region_id'] = $region->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$shippings = array_flip(array_filter($shippings));
|
|
|
|
$payments = array_flip(array_filter($payments));
|
|
|
|
|
|
|
|
$ShippingMethods = $this->getAllShippingMethodsCode($shippings[$order['delivery']['code']]);
|
|
|
|
|
|
|
|
//Set Address to quote
|
|
|
|
$quote->getBillingAddress()->addData($orderData['shipping_address']);
|
|
|
|
$quote->getShippingAddress()->addData($orderData['shipping_address']);
|
|
|
|
|
|
|
|
$shippingAddress = $quote->getShippingAddress();
|
|
|
|
$shippingAddress->setCollectShippingRates(true)
|
|
|
|
->collectShippingRates()
|
|
|
|
->setShippingMethod($ShippingMethods);
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
if ($this->api->getVersion() == 'v4') {
|
2018-03-12 16:34:48 +03:00
|
|
|
$paymentType = $order['paymentType'];
|
2018-04-28 11:39:23 +03:00
|
|
|
} elseif ($this->api->getVersion() == 'v5') {
|
2018-03-12 16:34:48 +03:00
|
|
|
if ($order['payments']) {
|
|
|
|
$paymentType = $this->getPaymentMethod($order['payments']);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$quote->setPaymentMethod($payments[$paymentType]);
|
|
|
|
$quote->setInventoryProcessed(false);
|
|
|
|
|
|
|
|
$quote->save();
|
|
|
|
|
|
|
|
// Set Sales Order Payment
|
|
|
|
$quote->getPayment()->importData(['method' => $payments[$paymentType]]);
|
|
|
|
|
|
|
|
// Collect Totals & Save Quote
|
|
|
|
$quote->collectTotals()->save();
|
|
|
|
|
|
|
|
// Create Order From Quote
|
2018-04-28 11:39:23 +03:00
|
|
|
$magentoOrder = $this->quoteManagement->submit($quote);
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
$increment_id = $magentoOrder->getId();
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->api->ordersFixExternalIds(
|
2018-03-12 16:34:48 +03:00
|
|
|
[
|
|
|
|
[
|
|
|
|
'id' => $order['id'],
|
|
|
|
'externalId' => $increment_id
|
|
|
|
]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create old edited order
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $order
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function doCreateUp($order)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($order, 'doCreateUp');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$response = $this->api->ordersGet($order['id'], $by = 'id');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
if (!$response->isSuccessful()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($response['order'])) {
|
|
|
|
$order = $response['order'];
|
|
|
|
}
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$payments = $this->config->getValue('retailcrm/Payment');
|
|
|
|
$shippings = $this->config->getValue('retailcrm/Shipping');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$region = $this->regionFactory->create();
|
|
|
|
$store = $this->storeManager->getStore();
|
|
|
|
$websiteId = $this->storeManager->getStore()->getWebsiteId();
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$customer = $this->customerFactory->create();
|
2018-03-12 16:34:48 +03:00
|
|
|
$customer->setWebsiteId($websiteId);
|
|
|
|
|
|
|
|
if (isset($order['customer']['externalId'])) {
|
|
|
|
$customer->load($order['customer']['externalId']); // load customet by external id
|
|
|
|
}
|
|
|
|
|
|
|
|
//Create object of quote
|
2018-04-28 11:39:23 +03:00
|
|
|
$quote = $this->quote->create();
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
//set store for which you create quote
|
|
|
|
$quote->setStore($store);
|
|
|
|
$quote->setCurrency();
|
|
|
|
|
|
|
|
// if you have allready buyer id then you can load customer directly
|
|
|
|
if ($customer->getId()) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$customer = $this->customerRepository->getById($customer->getId());
|
2018-03-12 16:34:48 +03:00
|
|
|
$quote->assignCustomer($customer); //Assign quote to customer
|
|
|
|
} else {
|
|
|
|
$quote->setCustomerEmail($order['email']);
|
|
|
|
$quote->setCustomerIsGuest(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
//add items in quote
|
2018-04-28 11:39:23 +03:00
|
|
|
foreach ($order['items'] as $item) {
|
|
|
|
$product = $this->product->load($item['offer']['externalId']);
|
2018-03-12 16:34:48 +03:00
|
|
|
$product->setPrice($item['initialPrice']);
|
|
|
|
$quote->addProduct(
|
|
|
|
$product,
|
2018-04-28 11:39:23 +03:00
|
|
|
(int)$item['quantity']
|
2018-03-12 16:34:48 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$products = [];
|
|
|
|
|
|
|
|
foreach ($order['items'] as $item) {
|
|
|
|
$products[$item['offer']['externalId']] = ['qty' => $item['quantity']];
|
|
|
|
}
|
|
|
|
|
|
|
|
$orderData = [
|
2018-04-28 11:39:23 +03:00
|
|
|
'currency_id' => $this->storeManager->getStore()->getCurrentCurrency()->getCode(),
|
2018-03-12 16:34:48 +03:00
|
|
|
'email' => $order['email'],
|
2018-04-28 11:39:23 +03:00
|
|
|
'shipping_address' => [
|
2018-03-12 16:34:48 +03:00
|
|
|
'firstname' => $order['firstName'],
|
|
|
|
'lastname' => $order['lastName'],
|
|
|
|
'street' => $order['delivery']['address']['street'],
|
|
|
|
'city' => $order['delivery']['address']['city'],
|
|
|
|
'country_id' => $order['countryIso'],//US
|
|
|
|
'region' => $order['delivery']['address']['region'],
|
|
|
|
'postcode' => $order['delivery']['address']['index'],
|
|
|
|
'telephone' => $order['phone'],
|
|
|
|
'save_in_address_book' => 1
|
2018-04-28 11:39:23 +03:00
|
|
|
],
|
2018-03-12 16:34:48 +03:00
|
|
|
'items'=> $products
|
|
|
|
];
|
|
|
|
|
|
|
|
$region->loadByName($order['delivery']['address']['region'], $order['countryIso']);
|
|
|
|
|
|
|
|
if ($region->getId()) {
|
|
|
|
$orderData['shipping_address']['region_id'] = $region->getId();
|
|
|
|
}
|
|
|
|
|
|
|
|
$shippings = array_flip(array_filter($shippings));
|
|
|
|
$payments = array_flip(array_filter($payments));
|
|
|
|
|
|
|
|
$ShippingMethods = $this->getAllShippingMethodsCode($shippings[$order['delivery']['code']]);
|
|
|
|
|
|
|
|
//Set Address to quote
|
|
|
|
$quote->getBillingAddress()->addData($orderData['shipping_address']);
|
|
|
|
$quote->getShippingAddress()->addData($orderData['shipping_address']);
|
|
|
|
|
|
|
|
$shippingAddress = $quote->getShippingAddress();
|
|
|
|
$shippingAddress->setCollectShippingRates(true)
|
|
|
|
->collectShippingRates()
|
|
|
|
->setShippingMethod($ShippingMethods);
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
if ($this->api->getVersion() == 'v4') {
|
2018-03-12 16:34:48 +03:00
|
|
|
$paymentType = $order['paymentType'];
|
2018-04-28 11:39:23 +03:00
|
|
|
} elseif ($this->api->getVersion() == 'v5') {
|
2018-03-12 16:34:48 +03:00
|
|
|
$paymentType = $this->getPaymentMethod($order['payments'], false);
|
|
|
|
}
|
|
|
|
|
|
|
|
$quote->setPaymentMethod($payments[$paymentType]);
|
|
|
|
$quote->setInventoryProcessed(false);
|
|
|
|
|
|
|
|
$originalId = $order['externalId'];
|
2018-04-28 11:39:23 +03:00
|
|
|
$oldOrder = $this->orderInterface->load($originalId);
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
$orderDataUp = [
|
|
|
|
'original_increment_id' => $oldOrder->getIncrementId(),
|
|
|
|
'relation_parent_id' => $oldOrder->getId(),
|
|
|
|
'relation_parent_real_id' => $oldOrder->getIncrementId(),
|
|
|
|
'edit_increment' => $oldOrder->getEditIncrement() + 1,
|
|
|
|
'increment_id' => $oldOrder->getIncrementId() . '-' . ($oldOrder->getEditIncrement() + 1)
|
|
|
|
];
|
|
|
|
|
2017-05-31 15:24:46 +03:00
|
|
|
$quote->setReservedOrderId($orderDataUp['increment_id']);
|
2018-03-12 16:34:48 +03:00
|
|
|
$quote->save();
|
|
|
|
|
|
|
|
// Set Sales Order Payment
|
|
|
|
$quote->getPayment()->importData(['method' => $payments[$paymentType]]);
|
|
|
|
|
|
|
|
// Collect Totals & Save Quote
|
|
|
|
$quote->collectTotals()->save();
|
|
|
|
|
|
|
|
// Create Order From Quote
|
2018-04-28 11:39:23 +03:00
|
|
|
$magentoOrder = $this->quoteManagement->submit($quote, $orderDataUp);
|
2018-03-12 16:34:48 +03:00
|
|
|
$oldOrder->setStatus('canceled')->save();
|
|
|
|
$increment_id = $magentoOrder->getId();
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->api->ordersFixExternalIds(
|
2018-03-12 16:34:48 +03:00
|
|
|
[
|
|
|
|
[
|
|
|
|
'id' => $order['id'],
|
|
|
|
'externalId' => $increment_id
|
|
|
|
]
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Edit order
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $order
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
private function doUpdate($order)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($order, 'doUpdate');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$Status = $this->config->getValue('retailcrm/Status');
|
2018-03-12 16:34:48 +03:00
|
|
|
$Status = array_flip(array_filter($Status));
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$magentoOrder = $this->order->load($order['externalId']);
|
2018-03-12 16:34:48 +03:00
|
|
|
$magentoOrderArr = $magentoOrder->getData();
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$this->logger->writeDump($magentoOrderArr, 'magentoOrderArr');
|
|
|
|
$this->logger->writeDump($Status, 'status');
|
2018-03-12 16:34:48 +03:00
|
|
|
|
|
|
|
if ((!empty($order['order_edit'])) && ($order['order_edit'] == 1)) {
|
|
|
|
$this->doCreateUp($order);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($order['status'])) {
|
|
|
|
$change = $Status[$order['status']];
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
if ($change == 'canceled') {
|
|
|
|
$this->orderManagement->cancel($magentoOrderArr['entity_id']);
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
$order_status = $this->order->load($magentoOrder->getId());
|
|
|
|
$order_status->setStatus($change);
|
|
|
|
$order_status->save();
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assembly orders from history
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $orderHistory
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return array $orders
|
|
|
|
*/
|
|
|
|
public static function assemblyOrder($orderHistory)
|
|
|
|
{
|
|
|
|
$orders = [];
|
|
|
|
|
|
|
|
foreach ($orderHistory as $change) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$orderId = $change['order']['id'];
|
2018-03-12 16:34:48 +03:00
|
|
|
$change['order'] = self::removeEmpty($change['order']);
|
|
|
|
|
|
|
|
if (isset($change['order']['items'])) {
|
|
|
|
$items = [];
|
|
|
|
|
|
|
|
foreach ($change['order']['items'] as $item) {
|
|
|
|
if (isset($change['created'])) {
|
|
|
|
$item['create'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
$items[$item['id']] = $item;
|
|
|
|
}
|
|
|
|
|
|
|
|
$change['order']['items'] = $items;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($change['order']['contragent']['contragentType'])) {
|
|
|
|
$change['order']['contragentType'] = self::newValue($change['order']['contragent']['contragentType']);
|
|
|
|
unset($change['order']['contragent']);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($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')
|
|
|
|
&& ($change['field'] != 'order_product.status')
|
|
|
|
&& ($change['field'] != 'payment_status')
|
|
|
|
&& ($change['field'] != 'prepay_sum')
|
|
|
|
) {
|
|
|
|
$orders[$change['order']['id']]['order_edit'] = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($change['item'])) {
|
|
|
|
if (isset($orders[$change['order']['id']]['items'])
|
2018-04-28 11:39:23 +03:00
|
|
|
&& $orders[$change['order']['id']]['items'][$change['item']['id']]
|
2018-03-12 16:34:48 +03:00
|
|
|
) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']] = array_merge(
|
|
|
|
$orders[$change['order']['id']]['items'][$change['item']['id']],
|
|
|
|
$change['item']
|
|
|
|
);
|
|
|
|
} else {
|
2018-03-12 16:34:48 +03:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($change['newValue']) && $change['field'] == 'order_product.quantity') {
|
|
|
|
$orders[$change['order']['id']]['order_edit'] = 1;
|
|
|
|
}
|
|
|
|
} else {
|
2018-04-28 11:39:23 +03:00
|
|
|
if (isset($fields['delivery'][$change['field']])
|
|
|
|
&& $fields['delivery'][$change['field']] == 'service'
|
|
|
|
) {
|
|
|
|
$orders[$orderId]['delivery']['service']['code'] = self::newValue($change['newValue']);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (isset($fields['delivery'][$change['field']])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$field = $fields['delivery'][$change['field']];
|
|
|
|
$orders[$orderId]['delivery'][$field] = self::newValue($change['newValue']);
|
|
|
|
unset($field);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (isset($fields['orderAddress'][$change['field']])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$field = $fields['orderAddress'][$change['field']];
|
|
|
|
$orders[$orderId]['delivery']['address'][$field] = self::newValue($change['newValue']);
|
|
|
|
unset($field);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (isset($fields['integrationDelivery'][$change['field']])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$field = $fields['integrationDelivery'][$change['field']];
|
|
|
|
$orders[$orderId]['delivery']['service'][$field] = self::newValue($change['newValue']);
|
|
|
|
unset($field);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (isset($fields['customerContragent'][$change['field']])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$field = $fields['customerContragent'][$change['field']];
|
|
|
|
$orders[$orderId][$field] = self::newValue($change['newValue']);
|
|
|
|
unset($field);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (strripos($change['field'], 'custom_') !== false) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$field = str_replace('custom_', '', $change['field']);
|
|
|
|
$orders[$orderId]['customFields'][$field] = self::newValue($change['newValue']);
|
|
|
|
unset($field);
|
2018-03-12 16:34:48 +03:00
|
|
|
} elseif (isset($fields['order'][$change['field']])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$orders[$orderId][$fields['order'][$change['field']]] = self::newValue($change['newValue']);
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($change['created'])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$orders[$orderId]['create'] = 1;
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($change['deleted'])) {
|
2018-04-28 11:39:23 +03:00
|
|
|
$orders[$orderId]['deleted'] = 1;
|
2018-03-12 16:34:48 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $orders;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove empty elements
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $inputArray
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return array $outputArray
|
|
|
|
*/
|
|
|
|
public static function removeEmpty($inputArray)
|
|
|
|
{
|
|
|
|
$outputArray = [];
|
|
|
|
|
|
|
|
if (!empty($inputArray)) {
|
|
|
|
foreach ($inputArray as $key => $element) {
|
2018-04-28 11:39:23 +03:00
|
|
|
if (!empty($element) || $element === 0 || $element === '0') {
|
2018-03-12 16:34:48 +03:00
|
|
|
if (is_array($element)) {
|
|
|
|
$element = self::removeEmpty($element);
|
|
|
|
}
|
|
|
|
|
|
|
|
$outputArray[$key] = $element;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $outputArray;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set new value
|
|
|
|
*
|
|
|
|
* @param mixed $value
|
|
|
|
*
|
|
|
|
* @return string $value
|
|
|
|
*/
|
|
|
|
public static function newValue($value)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
if (isset($value['code'])) {
|
2018-03-12 16:34:48 +03:00
|
|
|
return $value['code'];
|
2018-04-28 11:39:23 +03:00
|
|
|
} else {
|
2018-03-12 16:34:48 +03:00
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get shipping methods
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param string $mcode
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getAllShippingMethodsCode($mcode)
|
|
|
|
{
|
2018-04-28 11:39:23 +03:00
|
|
|
$activeCarriers = $this->shipconfig->getActiveCarriers();
|
2018-03-12 16:34:48 +03:00
|
|
|
$storeScope = \Magento\Store\Model\ScopeInterface::SCOPE_STORE;
|
|
|
|
|
2018-04-28 11:39:23 +03:00
|
|
|
foreach ($activeCarriers as $carrierCode => $carrierModel) {
|
2018-03-12 16:34:48 +03:00
|
|
|
$options = [];
|
|
|
|
|
|
|
|
if ($carrierMethods = $carrierModel->getAllowedMethods()) {
|
|
|
|
foreach ($carrierMethods as $methodCode => $method) {
|
|
|
|
$code = $carrierCode . '_'. $methodCode;
|
|
|
|
|
|
|
|
if ($mcode == $carrierCode) {
|
|
|
|
$methods[$mcode] = $code;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $methods[$mcode];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get payment type for api v5
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @param array $payments
|
|
|
|
* @param boolean $newOrder
|
2018-04-28 11:39:23 +03:00
|
|
|
*
|
2018-03-12 16:34:48 +03:00
|
|
|
* @return mixed
|
|
|
|
*/
|
2018-04-28 11:39:23 +03:00
|
|
|
private function getPaymentMethod($payments, $newOrder = true)
|
2018-03-12 16:34:48 +03:00
|
|
|
{
|
|
|
|
if (count($payments) == 1 || $newOrder) {
|
|
|
|
$payment = reset($payments);
|
|
|
|
} elseif (count($payments) > 1 && !$newOrder) {
|
|
|
|
foreach ($payments as $paymentCrm) {
|
|
|
|
if (isset($paymentCrm['externalId'])) {
|
|
|
|
$payment = $paymentCrm;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($payment)) {
|
|
|
|
return $payment['type'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|