diff --git a/Client/ShipmentOrder.php b/Client/ShipmentOrder.php
index e38fe61..6850c3b 100644
--- a/Client/ShipmentOrder.php
+++ b/Client/ShipmentOrder.php
@@ -8,7 +8,6 @@
namespace Intelipost\Shipping\Client;
-use Intelipost\Shipping\Client\Intelipost;
use Intelipost\Shipping\Client\Labels as RequestLabel;
use Intelipost\Shipping\Helper\Api;
use Intelipost\Shipping\Helper\Data;
@@ -16,6 +15,8 @@
use Intelipost\Shipping\Model\Shipment;
use Magento\Framework\Stdlib\DateTime;
use Magento\Framework\Stdlib\DateTime\TimezoneInterface;
+use Intelipost\Shipping\Model\ResourceModel\GetSourcesForOrder;
+use Magento\Sales\Model\Order;
class ShipmentOrder
{
@@ -45,6 +46,11 @@ class ShipmentOrder
/** @var RequestLabel */
protected $requestLabel;
+ /**
+ * @var GetSourcesForOrder
+ */
+ private $getSourcesForOrder;
+
/**
* @param ShipmentOrder\Customer $customer
* @param ShipmentOrder\Volume $volume
@@ -54,7 +60,7 @@ class ShipmentOrder
* @param ShipmentRepositoryInterface $shipment
* @param Data $helper
* @param RequestLabel $label
- * @param LabelClient $labelClient
+ * GetSourceCodeByShipmentId $getSourceCodeByShipmentId
*/
public function __construct(
ShipmentOrder\Customer $customer,
@@ -64,7 +70,8 @@ public function __construct(
Api $helperApi,
ShipmentRepositoryInterface $shipmentRepository,
Data $helper,
- RequestLabel $label
+ RequestLabel $label,
+ GetSourcesForOrder $getAllocatedSourcesForOrder
) {
$this->shipmentCustomer = $customer;
$this->shipmentVolume = $volume;
@@ -74,6 +81,7 @@ public function __construct(
$this->shipmentRepository = $shipmentRepository;
$this->timezone = $timezone;
$this->requestLabel = $label;
+ $this->getSourcesForOrder = $getAllocatedSourcesForOrder;
}
/**
@@ -113,17 +121,18 @@ public function execute($shipment)
'increment_id' => $order->getIncrementId()
]);
- $requestBody = $this->getShipment($shipment);
+ $requestBody = $this->getShipment($order, $shipment);
$this->sendShipmentRequest($this->helper->serializeData($requestBody), $shipment);
return $this;
}
/**
+ * @param Order $order
* @param Shipment $shipment
* @return \stdClass
* @throws \Exception
*/
- public function getShipment($shipment)
+ public function getShipment($order, $shipment)
{
$customerData = $this->shipmentCustomer->getInformation(
$shipment->getData('order_entity_id'),
@@ -138,6 +147,19 @@ public function getShipment($shipment)
$body->order_number = $shipment->getData('intelipost_shipment_id')
?: $shipment->getData('order_increment_id');
$body->origin_zip_code = $shipment->getData('origin_zip_code');
+ $body->destination_zip_code = $shipment->getData('destination_zip_code');
+
+ $sendWarehouseCode = $this->helper->getConfig(
+ 'send_warehouse_code',
+ 'order_status',
+ 'intelipost_push'
+ );
+ if ($sendWarehouseCode) {
+ $sourceForOrder = $this->getSourcesForOrder->execute($order->getId());
+ if (!empty($sourceForOrder)) {
+ $body->origin_warehouse_code = $sourceForOrder['source_code'];
+ }
+ }
$body->quote_id = $shipment->getData('quote_id');
$body->sales_order_number = $shipment->getData('increment_id');
$body->delivery_method_id = $shipment->getData('delivery_method_id');
diff --git a/Cron/CreateOrder.php b/Cron/CreateOrder.php
index 147ee1b..0bc6d61 100644
--- a/Cron/CreateOrder.php
+++ b/Cron/CreateOrder.php
@@ -42,10 +42,10 @@ public function __construct(
public function execute()
{
$enable = $this->helper->getConfig('enable_cron', 'order_status', 'intelipost_push');
- $status = $this->helper->getConfig('status_to_create', 'order_status', 'intelipost_push');
+ $status = (string) $this->helper->getConfig('status_to_create', 'order_status', 'intelipost_push');
$byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
- if ($enable) {
+ if ($enable && $status) {
$statuses = explode(',', $status);
if ($byShipment) {
$cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
diff --git a/Cron/ReadyForShipmentOrder.php b/Cron/ReadyForShipmentOrder.php
index 242b098..5feb6e1 100644
--- a/Cron/ReadyForShipmentOrder.php
+++ b/Cron/ReadyForShipmentOrder.php
@@ -49,9 +49,9 @@ public function execute()
{
$enable = $this->helper->getConfig('enable_cron', 'order_status', 'intelipost_push');
$byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
- $status = $this->helper->getConfig('status_to_ready_to_ship', 'order_status', 'intelipost_push');
+ $status = (string) $this->helper->getConfig('status_to_ready_to_ship', 'order_status', 'intelipost_push');
- if ($enable) {
+ if ($enable && $status) {
$statuses = explode(',', $status);
/** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
$collection = $this->collectionFactory->create();
diff --git a/Cron/RetryOrder.php b/Cron/RetryOrder.php
index e52c36c..01f1706 100644
--- a/Cron/RetryOrder.php
+++ b/Cron/RetryOrder.php
@@ -43,9 +43,9 @@ public function execute()
{
$enable = $this->helper->getConfig('enable_cron', 'order_status', 'intelipost_push');
$byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
- $status = $this->helper->getConfig('status_to_create', 'order_status', 'intelipost_push');
+ $status = (string) $this->helper->getConfig('status_to_create', 'order_status', 'intelipost_push');
- if ($enable) {
+ if ($enable && $status) {
$statuses = explode(',', $status);
$collection = $this->collectionFactory->create();
diff --git a/Cron/ShipOrder.php b/Cron/ShipOrder.php
index a6a0e02..0e939bd 100644
--- a/Cron/ShipOrder.php
+++ b/Cron/ShipOrder.php
@@ -43,9 +43,9 @@ public function execute()
{
$enable = $this->helper->getConfig('enable_cron', 'order_status', 'intelipost_push');
$byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
- $status = $this->helper->getConfig('status_to_shipped', 'order_status', 'intelipost_push');
+ $status = (string) $this->helper->getConfig('status_to_shipped', 'order_status', 'intelipost_push');
- if ($enable) {
+ if ($enable && $status) {
/** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
$collection = $this->collectionFactory->create();
if ($byShipment) {
diff --git a/Model/Carrier/Intelipost.php b/Model/Carrier/Intelipost.php
index 84df839..8932e92 100644
--- a/Model/Carrier/Intelipost.php
+++ b/Model/Carrier/Intelipost.php
@@ -290,7 +290,7 @@ public function collectRates(RateRequest $request)
* @param $postData
* @return array
*/
- public function getProductData($request, $postData)
+ public function getProductData($request, $postData): array
{
// Default Config
$heightAttribute = $this->getConfigData('height_attribute');
@@ -395,7 +395,7 @@ public function getProductData($request, $postData)
* @param $qtdVolumes
* @return array
*/
- public function setProductsQuantity($qtdProducts, $qtdVolumes)
+ public function setProductsQuantity($qtdProducts, $qtdVolumes): array
{
$arrayVol = [];
$result = (int) ($qtdProducts / $qtdVolumes);
@@ -416,10 +416,14 @@ public function setProductsQuantity($qtdProducts, $qtdVolumes)
* @return false|string|null
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
- public function getOriginZipcode($request)
+ public function getOriginZipcode($request): string
{
// Zipcodes
- return $request->getOriginZipcode() ?: $this->getConfigData('source_zip');
+ $originPostCode = $request->getOriginZipcode() ?: (string) $this->getConfigData('source_zip');
+ if (!$originPostCode) {
+ $originPostCode = (string) $this->helper->getConfig('postcode', 'origin', 'shipping');
+ }
+ return $originPostCode;
}
/**
@@ -427,7 +431,7 @@ public function getOriginZipcode($request)
* @param $cartQty
* @return array
*/
- public function getVolumes($response, $cartQty)
+ public function getVolumes($response, $cartQty): array
{
$volumes = [];
$volCount = count($response['content']['volumes']);
diff --git a/Model/ResourceModel/GetSourcesForOrder.php b/Model/ResourceModel/GetSourcesForOrder.php
new file mode 100644
index 0000000..37bc411
--- /dev/null
+++ b/Model/ResourceModel/GetSourcesForOrder.php
@@ -0,0 +1,78 @@
+resourceConnection = $resourceConnection;
+ }
+
+ /**
+ * Get allocated sources by order ID
+ *
+ * @param int $orderId
+ * @return array
+ */
+ public function execute(int $orderId): array
+ {
+ $sources = [];
+ $shipmentsIds = $this->getShipmentIds($orderId);
+
+ /** Get sources for shipment ids */
+ if (!empty($shipmentsIds)) {
+ $connection = $this->resourceConnection->getConnection();
+ $sourceTableName = $this->resourceConnection->getTableName('inventory_source');
+ $shipmentSourceTableName = $this->resourceConnection->getTableName('inventory_shipment_source');
+
+ $select = $connection->select()
+ ->from(['inventory_source' => $sourceTableName])
+ ->joinInner(
+ ['shipment_source' => $shipmentSourceTableName],
+ 'shipment_source.source_code = inventory_source.source_code',
+ []
+ )
+ ->group('inventory_source.source_code')
+ ->where('shipment_source.shipment_id in (?)', $shipmentsIds);
+
+ $sources = $connection->fetchRow($select);
+ }
+
+ return $sources;
+ }
+
+ /**
+ * @param int $orderId
+ * @return mixed
+ */
+ public function getShipmentIds(int $orderId): mixed
+ {
+ $salesConnection = $this->resourceConnection->getConnection('sales');
+ $shipmentTableName = $this->resourceConnection->getTableName('sales_shipment', 'sales');
+ /** Get shipment ids for order */
+ $shipmentSelect = $salesConnection->select()
+ ->from(
+ ['sales_shipment' => $shipmentTableName],
+ ['shipment_id' => 'sales_shipment.entity_id']
+ )
+ ->where('sales_shipment.order_id = ?', $orderId);
+ return $salesConnection->fetchCol($shipmentSelect);
+ }
+}
diff --git a/Observer/Sales/OrderPlaceAfter.php b/Observer/Sales/OrderPlaceAfter.php
index 4b86ce8..0d08aeb 100644
--- a/Observer/Sales/OrderPlaceAfter.php
+++ b/Observer/Sales/OrderPlaceAfter.php
@@ -16,6 +16,7 @@
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\Session\SessionManager;
use Magento\Framework\Stdlib\CookieManagerInterface;
+use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;
class OrderPlaceAfter implements ObserverInterface
@@ -58,8 +59,7 @@ public function __construct(
ShipmentFactory $shipmentFactory,
ShipmentResourceModel $shipmentResource,
StoreManagerInterface $storeManager
- )
- {
+ ) {
$this->intelipostQuote = $intelipostQuote;
$this->helper = $intelipostHelper;
$this->sessionManager = $sessionManager;
@@ -75,30 +75,14 @@ public function __construct(
public function execute(Observer $observer)
{
try {
- /** @var \Magento\Sales\Model\Order $order */
+ /** @var Order $order */
$order = $observer->getOrder();
if (!$order->getIsVirtual() && $order->getShippingMethod()) {
- $resultQuotes = [];
- if (strpos($order->getShippingMethod(), '_') !== false) {
- $deliveryMethodId = explode("_", $order->getShippingMethod());
- if (count($deliveryMethodId) < 3) {
- return;
- }
-
- $deliveryMethodId = $deliveryMethodId[count($deliveryMethodId) - 2] .
- "_" .
- $deliveryMethodId[count($deliveryMethodId) - 1];
+ $resultQuotes = $this->getResultQuotes($order);
- foreach ($this->helper->getResultQuotes() as $quote) {
- if ($quote->getDeliveryMethodId() == $deliveryMethodId && $quote->getOrderId() == null) {
- $resultQuotes[] = $quote;
- }
- }
-
- if (empty($resultQuotes) && count($resultQuotes) == 0) {
- return;
- }
+ if (empty($resultQuotes)) {
+ return;
}
$stored = [];
@@ -200,7 +184,7 @@ public function execute(Observer $observer)
* @throws \Magento\Framework\Exception\AlreadyExistsException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
- public function setShipmentOrder($resultJson)
+ public function setShipmentOrder($resultJson): void
{
$orderIndex = 1;
$orderNumber = $resultJson['order_id'];
@@ -236,7 +220,6 @@ public function setShipmentOrder($resultJson)
}
$orderIndex++;
-
$this->shipmentResource->save($shipment);
}
}
@@ -270,4 +253,28 @@ public function setProductsArray($products)
}
return $productsArray;
}
+
+ /**
+ * @param Order $order
+ * @return array
+ */
+ public function getResultQuotes(Order $order): array
+ {
+ $resultQuotes = [];
+ if (strpos($order->getShippingMethod(), 'intelipost') !== false) {
+ $deliveryMethodId = explode("_", $order->getShippingMethod());
+ if (count($deliveryMethodId) == 3) {
+ $deliveryMethodId = $deliveryMethodId[count($deliveryMethodId) - 2] .
+ "_" .
+ $deliveryMethodId[count($deliveryMethodId) - 1];
+
+ foreach ($this->helper->getResultQuotes() as $quote) {
+ if ($quote->getDeliveryMethodId() == $deliveryMethodId && $quote->getOrderId() == null) {
+ $resultQuotes[] = $quote;
+ }
+ }
+ }
+ }
+ return $resultQuotes;
+ }
}
diff --git a/composer.json b/composer.json
index 3ff7511..6d1dc11 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
"name": "intelipost/magento2",
"description": "Intelipost Shipping",
"type": "magento2-module",
- "version": "2.7.5",
+ "version": "2.8.0",
"require": {
"guzzlehttp/guzzle": ">=6.3.3"
},
diff --git a/etc/adminhtml/system/push.xml b/etc/adminhtml/system/push.xml
index 039c073..5de7bf1 100644
--- a/etc/adminhtml/system/push.xml
+++ b/etc/adminhtml/system/push.xml
@@ -17,21 +17,30 @@
If enabled it'll send order automatically to Intelipost
intelipost_push/order_status/enabled
-
+
Magento\Config\Model\Config\Source\Yesno
-
- intelipost_push/order_status/enable_cron
+
+ intelipost_push/order_status/order_by_shipment
1
-
+
Magento\Config\Model\Config\Source\Yesno
-
- intelipost_push/order_status/order_by_shipment
+
+ intelipost_push/order_status/send_warehouse_code
+ When the order has the source attributed, it'll send it to Intelipost
+ The order must have shipment in order to allow the module get the source from the order
+
+ 1
+
+
+
+ Magento\Config\Model\Config\Source\Yesno
+
+ intelipost_push/order_status/enable_cron
1
- 1
diff --git a/etc/config.xml b/etc/config.xml
index 08784f1..c522624 100644
--- a/etc/config.xml
+++ b/etc/config.xml
@@ -9,6 +9,16 @@
+
+
+
+
+ 1
+ 1
+
+
+
+
diff --git a/etc/module.xml b/etc/module.xml
index 80383dd..51cb7a3 100644
--- a/etc/module.xml
+++ b/etc/module.xml
@@ -7,7 +7,7 @@
*/
-->
-
+
diff --git a/i18n/en_US.csv b/i18n/en_US.csv
index b1b5c50..3b2345d 100644
--- a/i18n/en_US.csv
+++ b/i18n/en_US.csv
@@ -185,3 +185,7 @@ Description,Description
"Number","Number"
"Complement","Complement"
"District","District"
+"Send items to Intelipost by Multi Shipment","Send items to Intelipost by Multi Shipment"
+"Send warehouse code to Intelipost","Send warehouse code to Intelipost"
+"When the order has the source attributed, it'll send it to Intelipost","When the order has the source attributed, it'll send it to Intelipost"
+"The order must have shipment in order to allow the module get the source from the order","The order must have shipment in order to allow the module get the source from the order"
diff --git a/i18n/pt_BR.csv b/i18n/pt_BR.csv
index 9a3493e..da2e019 100644
--- a/i18n/pt_BR.csv
+++ b/i18n/pt_BR.csv
@@ -187,3 +187,7 @@ Description,Descrição
"District","Bairro"
"Ship to Applicable Countries","Enviar para Países Aplicáveis"
"Ship to Specific Countries","Enviar para Países Específicos"
+"Send items to Intelipost by Multi Shipment","Enviar itens para Intelipost por Multi Envio"
+"Send warehouse code to Intelipost","Enviar código do armazém para Intelipost"
+"When the order has the source attributed, it'll send it to Intelipost","Quando o pedido tiver o atributo de origem, irá enviar para a Intelipost"
+"The order must have shipment in order to allow the module get the source from the order","O pedido deve ter um envio para permitir que o módulo pegue a origem do pedido"