diff --git a/CHACNGELOG.md b/CHACNGELOG.md index bd1d9c5..dc59a57 100644 --- a/CHACNGELOG.md +++ b/CHACNGELOG.md @@ -1,3 +1,6 @@ +## 2019-10-26 v.2.4.2 +* Поддержка функции добавления одинакового товара в заказ как разные товарные позиции из CRM + ## 2018-12-25 v.2.4.1 * Удалена генерация externalId покупателя при заказе от незарегестрированного пользователя @@ -18,4 +21,4 @@ * При формировании каталога выгружаются выбранные атрибуты товаров * Улучшена выгрузка товаров в заказе (теперь учитываются конфигурируемые товары) * Добавлены переводы на русский и испанский языки -* Из обработки истории удален менеджер объектов \ No newline at end of file +* Из обработки истории удален менеджер объектов diff --git a/README.md b/README.md index 6d8b3d5..123a00b 100644 --- a/README.md +++ b/README.md @@ -29,4 +29,4 @@ By default ICML file is being generated by module every 4 hours. You can find fi composer require retailcrm/api-client-php ~5.0 ``` -This module is compatible with Magento up to version 2.2.3 +This module is compatible with Magento up to version 2.2.8 diff --git a/README.ru.md b/README.ru.md index 4e1f9ab..6bc4bbf 100644 --- a/README.ru.md +++ b/README.ru.md @@ -36,4 +36,4 @@ composer require retailcrm/api-client-php ~5.0 В конфигурационный файл `composer.json` вашего проекта будет добавлена библиотека [retailcrm/api-client-php](https://github.com/retailcrm/api-client-php), которая будет установлена в директорию `vendor/`. -Этот модуль совместим с Magento 2 до версии 2.2.3 +Этот модуль совместим с Magento 2 до версии 2.2.8 diff --git a/VERSION b/VERSION index 58073ef..8e8299d 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.4.1 \ No newline at end of file +2.4.2 diff --git a/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php index 9de6c0d..a384459 100644 --- a/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php +++ b/src/Block/Adminhtml/System/Config/Form/Fieldset/Payment.php @@ -81,9 +81,13 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Fieldset $html .= $this->_getHeaderHtml($element); if ($this->client->isConfigured()) { - $paymentMethods = $this->paymentConfig->getActiveMethods(); + $manager = \Magento\Framework\App\ObjectManager::getInstance(); + $paymentMethodListInterface = $manager->get(\Magento\Payment\Api\PaymentMethodListInterface::class); + $storeManagerInterface = $manager->get('Magento\Store\Model\StoreManagerInterface'); + $storeId = $storeManagerInterface->getStore()->getId(); + $paymentMethodList = $paymentMethodListInterface->getActiveList($storeId); - foreach ($paymentMethods as $code => $payment) { + foreach ($paymentMethodList as $code => $payment) { $html .= $this->_getFieldHtml($element, $payment); } } else { @@ -137,7 +141,7 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Fieldset * Get field html * * @param \Magento\Framework\Data\Form\Element\AbstractElement $fieldset - * @param \Magento\Payment\Model\Method\AbstractMethod $payment + * @param \Magento\Payment\Api\Data\PaymentMethodInterface $payment * * @return string */ @@ -145,9 +149,7 @@ class Payment extends \Magento\Config\Block\System\Config\Form\Fieldset { $configData = $this->getConfigData(); $path = 'retailcrm/' . $fieldset->getId() . '/' . $payment->getCode(); - $data = isset($configData[$path]) ? $configData[$path] : []; - $e = $this->_getDummyElement(); $field = $fieldset->addField( diff --git a/src/Model/History/Exchange.php b/src/Model/History/Exchange.php index b84347f..7a2d25d 100644 --- a/src/Model/History/Exchange.php +++ b/src/Model/History/Exchange.php @@ -165,7 +165,7 @@ class Exchange $shippings = $this->config->getValue('retailcrm/retailcrm_shipping'); $sites = $this->helper->getMappingSites(); - if ($sites) { + if ($sites && in_array($order['site'], $sites)) { $store = $this->storeManager->getStore($sites[$order['site']]); $websiteId = $store->getWebsiteId(); } else { @@ -178,7 +178,7 @@ class Exchange $customer->setWebsiteId($websiteId); if (isset($order['customer']['externalId'])) { - $customer->load($order['customer']['externalId']); + $customer = $this->customerRepository->getById($order['customer']['externalId']); } if (!$customer->getId()) { @@ -190,7 +190,7 @@ class Exchange ->setEmail($order['email']) ->setPassword($order['email']); try { - $customer->save(); + $this->customerRepository->save($customer); } catch (\Exception $exception) { $this->logger->writeRow($exception->getMessage()); } @@ -216,9 +216,32 @@ class Exchange $quote->setCurrency(); $quote->assignCustomer($customer); //Assign quote to customer - //add items in quote + $manager = \Magento\Framework\App\ObjectManager::getInstance(); + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ + $productRepository = $manager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + + $ditems = []; foreach ($order['items'] as $item) { - $product = $this->product->load($item['offer']['externalId']); + if (!isset($ditems[$item['offer']['externalId']]['quantity'])) { + $ditems[$item['offer']['externalId']] = [ + 'quantity' => null, + 'discountTotal' => null, + 'initialPrice' => null, + 'price_sum' => null, + 'price_item' => null + ]; + } + + $ditems[$item['offer']['externalId']]['quantity'] += $item['quantity']; + $ditems[$item['offer']['externalId']]['discountTotal'] += $item['quantity'] * $item['discountTotal']; + $ditems[$item['offer']['externalId']]['initialPrice'] = (float)$item['initialPrice']; + $ditems[$item['offer']['externalId']]['price_sum'] = $ditems[$item['offer']['externalId']]['initialPrice'] * $ditems[$item['offer']['externalId']]['quantity'] - $ditems[$item['offer']['externalId']]['discountTotal']; + $ditems[$item['offer']['externalId']]['price_item'] = $ditems[$item['offer']['externalId']]['price_sum'] / $ditems[$item['offer']['externalId']]['quantity']; + } + + //add items in quote + foreach ($ditems as $id =>$item) { + $product = $productRepository->getById($id,false, $store->getId(), false); $product->setPrice($item['initialPrice']); $quote->addProduct( $product, @@ -228,8 +251,8 @@ class Exchange $products = []; - foreach ($order['items'] as $item) { - $products[$item['offer']['externalId']] = ['qty' => $item['quantity']]; + foreach ($ditems as $id => $item) { + $products[$id] = ['product_id'=>$id,'qty' => $item['quantity']]; } $orderData = [ @@ -280,17 +303,19 @@ class Exchange $quote->setPaymentMethod($payments[$paymentType]); $quote->setInventoryProcessed(false); - $quote->save(); + /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */ + $quoteRepository = $manager->create(\Magento\Quote\Api\CartRepositoryInterface::class); + $quoteRepository->save($quote); // Set Sales Order Payment $quote->getPayment()->importData(['method' => $payments[$paymentType]]); // Collect Totals & Save Quote - $quote->collectTotals()->save(); + $quote->collectTotals(); + $quoteRepository->save($quote); // Create Order From Quote $magentoOrder = $this->quoteManagement->submit($quote); - $increment_id = $magentoOrder->getId(); $this->api->ordersFixExternalIds( @@ -312,8 +337,8 @@ class Exchange */ private function doCreateUp($order) { + $manager = \Magento\Framework\App\ObjectManager::getInstance(); $this->logger->writeDump($order, 'doCreateUp'); - $response = $this->api->ordersGet($order['id'], $by = 'id'); if (!$response->isSuccessful()) { @@ -330,7 +355,7 @@ class Exchange $region = $this->regionFactory->create(); $sites = $this->helper->getMappingSites(); - if ($sites) { + if ($sites && in_array($order['site'], $sites)) { $store = $this->storeManager->getStore($sites[$order['site']]); $websiteId = $store->getWebsiteId(); } else { @@ -342,7 +367,7 @@ class Exchange $customer->setWebsiteId($websiteId); if (isset($order['customer']['externalId'])) { - $customer->load($order['customer']['externalId']); // load customer by external id + $customer = $this->customerRepository->getById($order['customer']['externalId']); } //Create object of quote @@ -361,9 +386,31 @@ class Exchange $quote->setCustomerIsGuest(1); } - //add items in quote + $ditems = []; foreach ($order['items'] as $item) { - $product = $this->product->load($item['offer']['externalId']); + if (!isset($ditems[$item['offer']['externalId']]['quantity'])) { + $ditems[$item['offer']['externalId']] = [ + 'quantity' => null, + 'discountTotal' => null, + 'initialPrice' => null, + 'price_sum' => null, + 'price_item' => null + ]; + } + + $ditems[$item['offer']['externalId']]['quantity'] += $item['quantity']; + $ditems[$item['offer']['externalId']]['discountTotal'] += $item['quantity'] * $item['discountTotal']; + $ditems[$item['offer']['externalId']]['initialPrice'] = (float)$item['initialPrice']; + $ditems[$item['offer']['externalId']]['price_sum'] = $ditems[$item['offer']['externalId']]['initialPrice'] * $ditems[$item['offer']['externalId']]['quantity'] - $ditems[$item['offer']['externalId']]['discountTotal']; + $ditems[$item['offer']['externalId']]['price_item'] = $ditems[$item['offer']['externalId']]['price_sum'] / $ditems[$item['offer']['externalId']]['quantity']; + } + + /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ + $productRepository = $manager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + + //add items in quote + foreach ($ditems as $id => $item) { + $product = $productRepository->getById($id,false, $store->getId(), false); $product->setPrice($item['initialPrice']); $quote->addProduct( $product, @@ -373,8 +420,8 @@ class Exchange $products = []; - foreach ($order['items'] as $item) { - $products[$item['offer']['externalId']] = ['qty' => $item['quantity']]; + foreach ($ditems as $id => $item) { + $products[$id] = ['product_id'=>$id,'qty' => $item['quantity']]; } $orderData = [ @@ -435,16 +482,20 @@ class Exchange ]; $quote->setReservedOrderId($orderDataUp['increment_id']); - $quote->save(); + /** @var \Magento\Quote\Api\CartRepositoryInterface $quoteRepository */ + $quoteRepository = $manager->create(\Magento\Quote\Api\CartRepositoryInterface::class); + $quoteRepository->save($quote); // Set Sales Order Payment $quote->getPayment()->importData(['method' => $payments[$paymentType]]); // Collect Totals & Save Quote - $quote->collectTotals()->save(); + $quote->collectTotals(); + $quoteRepository->save($quote); // Create Order From Quote $magentoOrder = $this->quoteManagement->submit($quote, $orderDataUp); + $oldOrder->setStatus('canceled')->save(); $increment_id = $magentoOrder->getId(); @@ -508,11 +559,11 @@ class Exchange foreach ($orderHistory as $change) { $orderId = $change['order']['id']; + $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; @@ -551,7 +602,7 @@ class Exchange if (isset($change['item'])) { if (isset($orders[$change['order']['id']]['items']) - && $orders[$change['order']['id']]['items'][$change['item']['id']] + && isset($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']], diff --git a/src/Model/Service/Order.php b/src/Model/Service/Order.php index 5b34420..5ed2bb9 100644 --- a/src/Model/Service/Order.php +++ b/src/Model/Service/Order.php @@ -72,6 +72,8 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface ] ]; + $codeShop = $this->config->getValue('retailcrm/retailcrm_site/default'); + if ($shippingAddress->getData('country_id')) { $preparedOrder['countryIso'] = $shippingAddress->getData('country_id'); } @@ -87,7 +89,7 @@ class Order implements \Retailcrm\Retailcrm\Api\OrderManagerInterface 'type' => $this->config->getValue( 'retailcrm/retailcrm_payment/' . $order->getPayment()->getMethodInstance()->getCode() ), - 'externalId' => $order->getId(), + 'externalId' => $codeShop.$order->getId(), 'order' => [ 'externalId' => $order->getId(), ]