From adf1f85a6b8c9fe3c9ff3d2f38172ad09bb109fc Mon Sep 17 00:00:00 2001
From: Maicon Antonio <maicon@bizcommerce.com.br>
Date: Fri, 17 Mar 2023 15:49:42 +0000
Subject: [PATCH 1/4] feat: add logical for integration with shipment number

---
 Client/AbstractShipment.php    |  8 +++++++-
 Client/ShipmentOrder.php       |  8 +++++++-
 Cron/CreateOrder.php           | 26 +++++++++++++++++++++-----
 Cron/ReadyForShipmentOrder.php | 24 ++++++++++++++++++------
 Cron/RetryOrder.php            | 23 ++++++++++++++++++-----
 Cron/ShipOrder.php             | 24 ++++++++++++++++++------
 etc/adminhtml/system/push.xml  | 11 ++++++++++-
 7 files changed, 99 insertions(+), 25 deletions(-)

diff --git a/Client/AbstractShipment.php b/Client/AbstractShipment.php
index eca6faa..2561751 100644
--- a/Client/AbstractShipment.php
+++ b/Client/AbstractShipment.php
@@ -55,11 +55,17 @@ public function __construct(
      */
     public function prepareRequestBody($shipment)
     {
+        $byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
+
         $date = $this->timezone->date();
         $eventDate = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
 
         $body = new \stdClass();
-        $body->order_number = $shipment->getData('order_increment_id');
+        if ($byShipment) {
+            $body->order_number = 'E-' . $shipment->getData('shipment_increment_id');
+        } else {
+            $body->order_number = $shipment->getData('order_increment_id');
+        }
         $body->event_date  = str_replace(' ', 'T', $eventDate);
 
         return [$body];
diff --git a/Client/ShipmentOrder.php b/Client/ShipmentOrder.php
index 591991d..015d821 100644
--- a/Client/ShipmentOrder.php
+++ b/Client/ShipmentOrder.php
@@ -125,6 +125,8 @@ public function execute($shipment)
      */
     public function getShipment($shipment)
     {
+        $byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
+
         $customerData = $this->shipmentCustomer->getInformation(
             $shipment->getData('order_entity_id'),
             $shipment->getCustomerTaxvat()
@@ -135,7 +137,11 @@ public function getShipment($shipment)
         $estimateDate = (string) $shipment->getData('delivery_estimate_date_exact_iso');
 
         $body = new \stdClass();
-        $body->order_number = $shipment->getData('order_increment_id');
+        if ($byShipment) {
+            $body->order_number = 'E-' . $shipment->getData('shipment_increment_id');
+        } else {
+            $body->order_number = $shipment->getData('order_increment_id');
+        }
         $body->sales_order_number = $shipment->getData('increment_id');
         $body->quote_id = $shipment->getData('quote_id');
         $body->delivery_method_id = $shipment->getData('delivery_method_id');
diff --git a/Cron/CreateOrder.php b/Cron/CreateOrder.php
index ffb5fc9..24f71cb 100644
--- a/Cron/CreateOrder.php
+++ b/Cron/CreateOrder.php
@@ -42,17 +42,33 @@ public function __construct(
     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');
 
         if ($enable) {
             $statuses = explode(',', $status);
 
             $collection = $this->collectionFactory->create();
-            $collection->getSelect()->join(
-                ['so' => $collection->getConnection()->getTableName('sales_order')],
-                'main_table.order_increment_id = so.increment_id',
-                ['increment_id']
-            );
+            if ($byShipment) {
+                $collection->getSelect()
+                    ->join(
+                        ['so' => $collection->getConnection()->getTableName('sales_order')],
+                        'main_table.order_increment_id = so.increment_id',
+                        ['increment_id']
+                    )
+                    ->join(
+                        ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
+                        'so.entity_id = ss.order_id',
+                        ['increment_id AS shipment_increment_id']
+                    )
+                ;
+            } else {
+                $collection->getSelect()->join(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                );
+            }
             $collection
                 ->addFieldToFilter('status', ['in' => $statuses])
                 ->addFieldToFilter('main_table.intelipost_status', Shipment::STATUS_PENDING);
diff --git a/Cron/ReadyForShipmentOrder.php b/Cron/ReadyForShipmentOrder.php
index 8b46f8f..60cbd62 100644
--- a/Cron/ReadyForShipmentOrder.php
+++ b/Cron/ReadyForShipmentOrder.php
@@ -48,17 +48,29 @@ public function __construct(
     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');
 
         if ($enable) {
             /** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
             $collection = $this->collectionFactory->create();
-            $collection->getSelect()->joinLeft(
-                ['so' => $collection->getConnection()->getTableName('sales_order')],
-                'main_table.order_increment_id = so.increment_id',
-                ['increment_id']
-            );
-
+            if ($byShipment) {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                )->join(
+                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
+                    'so.entity_id = ss.order_id',
+                    ['increment_id AS shipment_increment_id']
+                );
+            } else {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                );
+            }
             $collection->addFieldToFilter('status', ['eq' => $status])
                 ->addFieldToFilter(
                     'main_table.intelipost_status',
diff --git a/Cron/RetryOrder.php b/Cron/RetryOrder.php
index a7e0b59..fca8ee3 100644
--- a/Cron/RetryOrder.php
+++ b/Cron/RetryOrder.php
@@ -42,17 +42,30 @@ public function __construct(
     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');
 
         if ($enable) {
             $statuses = explode(',', $status);
 
             $collection = $this->collectionFactory->create();
-            $collection->getSelect()->joinLeft(
-                ['so' => $collection->getConnection()->getTableName('sales_order')],
-                'main_table.order_increment_id = so.increment_id',
-                ['increment_id']
-            );
+            if ($byShipment) {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                )->join(
+                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
+                    'so.entity_id = ss.order_id',
+                    ['increment_id AS shipment_increment_id']
+                );
+            } else {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                );
+            }
             $collection
                 ->addFieldToFilter('status', ['in' => $statuses])
                 ->addFieldToFilter('main_table.intelipost_status', Shipment::STATUS_ERROR);
diff --git a/Cron/ShipOrder.php b/Cron/ShipOrder.php
index dfc002c..a38a331 100644
--- a/Cron/ShipOrder.php
+++ b/Cron/ShipOrder.php
@@ -42,17 +42,29 @@ public function __construct(
     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');
 
         if ($enable) {
             /** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
             $collection = $this->collectionFactory->create();
-            $collection->getSelect()->joinLeft(
-                ['so' => $collection->getConnection()->getTableName('sales_order')],
-                'main_table.order_increment_id = so.increment_id',
-                ['increment_id']
-            );
-
+            if ($byShipment) {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                )->join(
+                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
+                    'so.entity_id = ss.order_id',
+                    ['increment_id AS shipment_increment_id']
+                );
+            } else {
+                $collection->getSelect()->joinLeft(
+                    ['so' => $collection->getConnection()->getTableName('sales_order')],
+                    'main_table.order_increment_id = so.increment_id',
+                    ['increment_id']
+                );
+            }
             $collection->addFieldToFilter('status', ['eq' => $status])
                 ->addFieldToFilter(
                     'main_table.intelipost_status',
diff --git a/etc/adminhtml/system/push.xml b/etc/adminhtml/system/push.xml
index 59ac0f9..039c073 100644
--- a/etc/adminhtml/system/push.xml
+++ b/etc/adminhtml/system/push.xml
@@ -25,7 +25,16 @@
                 <field id="enabled">1</field>
             </depends>
         </field>
-        <field id="cron_frequency" translate="label" type="select" sortOrder="21" showInDefault="1" showInWebsite="0" showInStore="0">
+        <field id="order_by_shipment" translate="label comment" type="select" sortOrder="21" showInDefault="1" showInWebsite="1" showInStore="1">
+            <source_model>Magento\Config\Model\Config\Source\Yesno</source_model>
+            <label>Send items to Intelipost by Shipment</label>
+            <config_path>intelipost_push/order_status/order_by_shipment</config_path>
+            <depends>
+                <field id="enabled">1</field>
+                <field id="enable_cron">1</field>
+            </depends>
+        </field>
+        <field id="cron_frequency" translate="label" type="select" sortOrder="22" showInDefault="1" showInWebsite="0" showInStore="0">
             <label>Frequency</label>
             <source_model>Intelipost\Shipping\Model\Config\Source\Frequency</source_model>
             <backend_model>Intelipost\Shipping\Model\Config\CronConfig</backend_model>

From 64547e1210793706fd7c0d5828ed20c243beeb02 Mon Sep 17 00:00:00 2001
From: Maicon Antonio <maicon@bizcommerce.com.br>
Date: Wed, 29 Mar 2023 11:25:36 -0300
Subject: [PATCH 2/4] feat: add functionality to send items for Intelipost by
 shipments

---
 Api/Data/ShipmentInterface.php                | 13 ++++++++-
 Client/AbstractShipment.php                   |  6 ++--
 Client/ShipmentOrder.php                      | 11 ++-----
 Controller/Webhook/Index.php                  |  5 ++--
 Cron/CreateOrder.php                          | 29 ++++++-------------
 Cron/ImportLabels.php                         |  9 +++++-
 Cron/ReadyForShipmentOrder.php                | 24 ++++++---------
 Cron/RetryOrder.php                           | 21 +++++---------
 Cron/ShipOrder.php                            | 21 +++++---------
 Model/Carrier/Intelipost.php                  |  2 +-
 Model/ResourceModel/ShipmentRepository.php    |  4 +--
 Model/Shipment.php                            | 16 ++++++++++
 Observer/Sales/OrderPlaceAfter.php            |  8 ++---
 etc/db_schema.xml                             |  1 +
 i18n/en_US.csv                                |  1 +
 i18n/pt_BR.csv                                |  1 +
 .../intelipost_shipping_shipments_listing.xml | 12 ++------
 17 files changed, 88 insertions(+), 96 deletions(-)

diff --git a/Api/Data/ShipmentInterface.php b/Api/Data/ShipmentInterface.php
index 31ef9e6..f6dae7d 100644
--- a/Api/Data/ShipmentInterface.php
+++ b/Api/Data/ShipmentInterface.php
@@ -9,9 +9,9 @@
 
 interface ShipmentInterface
 {
-
     const ENTITY_ID = 'entity_id';
     const ORDER_INCREMENT_ID = 'order_increment_id';
+    const INTELIPOST_SHIPMENT_ID = 'intelipost_shipment_id';
     const DELIVERY_METHOD_ID = 'delivery_method_id';
     const DELIVERY_ESTIMATE_BUSINESS_DAYS = 'delivery_estimate_business_days';
     const SHIPMENT_ORDERS_TYPE = 'shipment_orders_type';
@@ -80,6 +80,17 @@ public function getDeliveryEstimateBusinessDays();
      */
     public function setDeliveryEstimateBusinessDays($deliveryEstimateBusinessDays);
 
+    /**
+    * @return string
+    */
+    public function getIntelipostShipmentId();
+
+    /**
+     * @param $intelipostShipmentId
+     * @return void
+     */
+    public function setIntelipostShipmentId($intelipostShipmentId);
+
     /**
      * @return string
      */
diff --git a/Client/AbstractShipment.php b/Client/AbstractShipment.php
index 2561751..45f6200 100644
--- a/Client/AbstractShipment.php
+++ b/Client/AbstractShipment.php
@@ -61,10 +61,10 @@ public function prepareRequestBody($shipment)
         $eventDate = $date->format(\Magento\Framework\Stdlib\DateTime::DATETIME_PHP_FORMAT);
 
         $body = new \stdClass();
-        if ($byShipment) {
-            $body->order_number = 'E-' . $shipment->getData('shipment_increment_id');
-        } else {
+        if (!$byShipment) {
             $body->order_number = $shipment->getData('order_increment_id');
+        } else {
+            $body->order_number = $shipment->getData('intelipost_shipment_id');
         }
         $body->event_date  = str_replace(' ', 'T', $eventDate);
 
diff --git a/Client/ShipmentOrder.php b/Client/ShipmentOrder.php
index 015d821..e444996 100644
--- a/Client/ShipmentOrder.php
+++ b/Client/ShipmentOrder.php
@@ -125,8 +125,6 @@ public function execute($shipment)
      */
     public function getShipment($shipment)
     {
-        $byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
-
         $customerData = $this->shipmentCustomer->getInformation(
             $shipment->getData('order_entity_id'),
             $shipment->getCustomerTaxvat()
@@ -137,13 +135,9 @@ public function getShipment($shipment)
         $estimateDate = (string) $shipment->getData('delivery_estimate_date_exact_iso');
 
         $body = new \stdClass();
-        if ($byShipment) {
-            $body->order_number = 'E-' . $shipment->getData('shipment_increment_id');
-        } else {
-            $body->order_number = $shipment->getData('order_increment_id');
-        }
-        $body->sales_order_number = $shipment->getData('increment_id');
+        $body->order_number = $shipment->getData('intelipost_shipment_id');
         $body->quote_id = $shipment->getData('quote_id');
+        $body->sales_order_number = $shipment->getData('increment_id');
         $body->delivery_method_id = $shipment->getData('delivery_method_id');
         $body->estimated_delivery_date = str_replace(' ', 'T', $estimateDate);
         $body->customer_shipping_costs = $shipment->getData('customer_shipping_costs');
@@ -297,7 +291,6 @@ public function getNowDateTime()
      */
     protected function checkExistingOrder($incrementId, $result)
     {
-
         if ($result['status'] == Intelipost::RESPONSE_STATUS_ERROR && isset($result['messages'])) {
             if (count($result['messages']) == 1) {
                 $message = $result['messages'][0];
diff --git a/Controller/Webhook/Index.php b/Controller/Webhook/Index.php
index 3f3bbaa..cfc09b1 100644
--- a/Controller/Webhook/Index.php
+++ b/Controller/Webhook/Index.php
@@ -112,11 +112,12 @@ public function execute()
                 $trackingCode = $body['tracking_code'] ?? null;
                 $trackingUrl = $body['tracking_url'] ?? null;
                 $incrementId = $body['order_number'] ?? null;
+                $orderIncrementId = $body['sales_order_number'] ?? null;
                 $intelipostStatus = isset($body['history']) ? $body['history']['shipment_order_volume_state'] : null;
 
                 $this->saveWebhook($request->getContent(), $incrementId, $intelipostStatus);
                 $this->updateTrackingCode($incrementId, $trackingCode, $trackingUrl, $intelipostStatus);
-                $this->updateOrderStatus($incrementId, $body);
+                $this->updateOrderStatus($orderIncrementId, $body);
             } catch (\Exception $e) {
                 $this->helper->getLogger()->error($e->getMessage());
                 $this->saveWebhookMessage($e->getMessage());
@@ -258,7 +259,7 @@ public function updateTrackingCode($incrementId, $trackingCode, $trackingUrl, $i
     {
         if ($trackingCode || $trackingUrl) {
             try {
-                $shipment = $this->shipmentRepository->getByOrderIncrementId($incrementId);
+                $shipment = $this->shipmentRepository->getByIntelipostShipmentId($incrementId);
                 $shipment->setIntelipostStatus($intelipostStatus);
                 $shipment->setTrackingCode($trackingCode);
                 $shipment->setTrackingUrl($trackingUrl);
diff --git a/Cron/CreateOrder.php b/Cron/CreateOrder.php
index 24f71cb..147ee1b 100644
--- a/Cron/CreateOrder.php
+++ b/Cron/CreateOrder.php
@@ -42,33 +42,22 @@ public function __construct(
     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');
+        $byShipment = (boolean) $this->helper->getConfig('order_by_shipment', 'order_status', 'intelipost_push');
 
         if ($enable) {
             $statuses = explode(',', $status);
-
-            $collection = $this->collectionFactory->create();
             if ($byShipment) {
-                $collection->getSelect()
-                    ->join(
-                        ['so' => $collection->getConnection()->getTableName('sales_order')],
-                        'main_table.order_increment_id = so.increment_id',
-                        ['increment_id']
-                    )
-                    ->join(
-                        ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
-                        'so.entity_id = ss.order_id',
-                        ['increment_id AS shipment_increment_id']
-                    )
-                ;
+                $cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
             } else {
-                $collection->getSelect()->join(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                );
+                $cond = 'main_table.order_increment_id = so.increment_id';
             }
+            $collection = $this->collectionFactory->create();
+            $collection->getSelect()->join(
+                ['so' => $collection->getConnection()->getTableName('sales_order')],
+                $cond,
+                ['increment_id']
+            );
             $collection
                 ->addFieldToFilter('status', ['in' => $statuses])
                 ->addFieldToFilter('main_table.intelipost_status', Shipment::STATUS_PENDING);
diff --git a/Cron/ImportLabels.php b/Cron/ImportLabels.php
index ab323ee..72bc0d6 100644
--- a/Cron/ImportLabels.php
+++ b/Cron/ImportLabels.php
@@ -49,6 +49,8 @@ public function __construct(
     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');
+
         if ($enable) {
             try {
                 $statuses = [
@@ -57,9 +59,14 @@ public function execute()
                 ];
 
                 $collection = $this->collectionFactory->create();
+                if ($byShipment) {
+                    $cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
+                } else {
+                    $cond = 'main_table.order_increment_id = so.increment_id';
+                }
                 $collection->getSelect()->join(
                     ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
+                    $cond,
                     ['increment_id']
                 );
                 $collection->getSelect()->joinLeft(
diff --git a/Cron/ReadyForShipmentOrder.php b/Cron/ReadyForShipmentOrder.php
index 60cbd62..242b098 100644
--- a/Cron/ReadyForShipmentOrder.php
+++ b/Cron/ReadyForShipmentOrder.php
@@ -52,26 +52,20 @@ public function execute()
         $status = $this->helper->getConfig('status_to_ready_to_ship', 'order_status', 'intelipost_push');
 
         if ($enable) {
+            $statuses = explode(',', $status);
             /** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
             $collection = $this->collectionFactory->create();
             if ($byShipment) {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                )->join(
-                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
-                    'so.entity_id = ss.order_id',
-                    ['increment_id AS shipment_increment_id']
-                );
+                $cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
             } else {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                );
+                $cond = 'main_table.order_increment_id = so.increment_id';
             }
-            $collection->addFieldToFilter('status', ['eq' => $status])
+            $collection->getSelect()->joinLeft(
+                ['so' => $collection->getConnection()->getTableName('sales_order')],
+                $cond,
+                ['increment_id']
+            );
+            $collection->addFieldToFilter('status', ['in' => $statuses])
                 ->addFieldToFilter(
                     'main_table.intelipost_status',
                     ['neq' => Shipment::STATUS_READY_FOR_SHIPMENT]
diff --git a/Cron/RetryOrder.php b/Cron/RetryOrder.php
index fca8ee3..e52c36c 100644
--- a/Cron/RetryOrder.php
+++ b/Cron/RetryOrder.php
@@ -50,22 +50,15 @@ public function execute()
 
             $collection = $this->collectionFactory->create();
             if ($byShipment) {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                )->join(
-                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
-                    'so.entity_id = ss.order_id',
-                    ['increment_id AS shipment_increment_id']
-                );
+                $cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
             } else {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                );
+                $cond = 'main_table.order_increment_id = so.increment_id';
             }
+            $collection->getSelect()->joinLeft(
+                ['so' => $collection->getConnection()->getTableName('sales_order')],
+                $cond,
+                ['increment_id']
+            );
             $collection
                 ->addFieldToFilter('status', ['in' => $statuses])
                 ->addFieldToFilter('main_table.intelipost_status', Shipment::STATUS_ERROR);
diff --git a/Cron/ShipOrder.php b/Cron/ShipOrder.php
index a38a331..a6a0e02 100644
--- a/Cron/ShipOrder.php
+++ b/Cron/ShipOrder.php
@@ -49,22 +49,15 @@ public function execute()
             /** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
             $collection = $this->collectionFactory->create();
             if ($byShipment) {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                )->join(
-                    ['ss' => $collection->getConnection()->getTableName('sales_shipment')],
-                    'so.entity_id = ss.order_id',
-                    ['increment_id AS shipment_increment_id']
-                );
+                $cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
             } else {
-                $collection->getSelect()->joinLeft(
-                    ['so' => $collection->getConnection()->getTableName('sales_order')],
-                    'main_table.order_increment_id = so.increment_id',
-                    ['increment_id']
-                );
+                $cond = 'main_table.order_increment_id = so.increment_id';
             }
+            $collection->getSelect()->joinLeft(
+                ['so' => $collection->getConnection()->getTableName('sales_order')],
+                $cond,
+                ['increment_id']
+            );
             $collection->addFieldToFilter('status', ['eq' => $status])
                 ->addFieldToFilter(
                     'main_table.intelipost_status',
diff --git a/Model/Carrier/Intelipost.php b/Model/Carrier/Intelipost.php
index 59687ca..a1be846 100644
--- a/Model/Carrier/Intelipost.php
+++ b/Model/Carrier/Intelipost.php
@@ -202,7 +202,7 @@ public function collectRates(RateRequest $request)
             $child['available_scheduling_dates'] = null;
             $schedulingEnabled = $child['scheduling_enabled'] ?? false;
 
-            if ($schedulingEnabled) {
+            if ($schedulingEnabled && $pageName) {
                 if ($calendarOnlyCheckout && strcmp($pageName, 'checkout')) {
                     continue;
                 }
diff --git a/Model/ResourceModel/ShipmentRepository.php b/Model/ResourceModel/ShipmentRepository.php
index 349c019..bdb5947 100644
--- a/Model/ResourceModel/ShipmentRepository.php
+++ b/Model/ResourceModel/ShipmentRepository.php
@@ -100,10 +100,10 @@ public function getById($id)
      * {@inheritdoc}
      * @throws NoSuchEntityException
      */
-    public function getByOrderIncrementId($orderIncrementId)
+    public function getByIntelipostShipmentId($intelipostShipmentId)
     {
         $shipment = $this->shipmentFactory->create();
-        $this->resource->load($shipment, $orderIncrementId, 'order_increment_id');
+        $this->resource->load($shipment, $intelipostShipmentIdId, 'intelipost_shipment_id');
         if (!$shipment->getId()) {
             throw new NoSuchEntityException(__('Item with id "%1" does not exist.', $orderIncrementId));
         }
diff --git a/Model/Shipment.php b/Model/Shipment.php
index 7650a06..610afad 100644
--- a/Model/Shipment.php
+++ b/Model/Shipment.php
@@ -90,6 +90,22 @@ public function setDeliveryEstimateBusinessDays($deliveryEstimateBusinessDays)
         $this->setData(self::DELIVERY_ESTIMATE_BUSINESS_DAYS, $deliveryEstimateBusinessDays);
     }
 
+    /**
+     * @inheritDoc
+     */
+    public function getIntelipostShipmentId()
+    {
+        return $this->getData(self::INTELIPOST_SHIPMENT_ID);
+    }
+
+    /**
+     * @inheritDoc
+     */
+    public function setIntelipostShipmentId($intelipostShipmentId)
+    {
+        $this->setData(self::INTELIPOST_SHIPMENT_ID, $intelipostShipmentId);
+    }
+
     /**
      * @inheritDoc
      */
diff --git a/Observer/Sales/OrderPlaceAfter.php b/Observer/Sales/OrderPlaceAfter.php
index c1dfd3e..7c21929 100644
--- a/Observer/Sales/OrderPlaceAfter.php
+++ b/Observer/Sales/OrderPlaceAfter.php
@@ -205,13 +205,13 @@ public function setShipmentOrder($resultJson)
         foreach ($resultJson['quotes'] as $quotes) {
             $shipment = $this->shipmentFactory->create();
             $shipment->setOrderIncrementId($orderNumber);
-            if ($orderIndex != 1) {
-                $shipment->setOrderIncrementId($orderNumber . '-' . $orderIndex);
-            }
-
             $shipment->setQuoteId($quotes['quote_id']);
             $shipment->setDeliveryMethodId($this->getMethodId($quotes['delivery_method_id']));
             $shipment->setDeliveryEstimateBusinessDays($quotes['delivery_estimated_delivery_business_day']);
+            $shipment->setIntelipostShipmentId($orderNumber);
+            if ($orderIndex != 1) {
+                $shipment->setIntelipostShipmentId($orderNumber . '-' . $orderIndex);
+            }
             $shipment->setShipmentOrderType('NORMAL');
             $shipment->setShipmentOrderSubType('NORMAL');
             $shipment->setDeliveryMethodType($quotes['delivery_method_type']);
diff --git a/etc/db_schema.xml b/etc/db_schema.xml
index d49ff65..7b939f8 100644
--- a/etc/db_schema.xml
+++ b/etc/db_schema.xml
@@ -82,6 +82,7 @@
         <column xsi:type="varchar" name="order_increment_id" length="255" comment="Order Number"/>
         <column xsi:type="int" name="delivery_method_id" padding="11" comment="Delivery Method ID"/>
         <column xsi:type="int" name="delivery_estimate_business_days" padding="11" comment="Delivery Estimate Business Days" />
+        <column xsi:type="varchar" name="intelipost_shipment_id" length="255" comment="Intelipost Shipment ID" />
         <column xsi:type="varchar" name="shipment_orders_type" length="255" comment="Shipment Orders Type" />
         <column xsi:type="varchar" name="shipment_orders_sub_type" length="255" comment="Shipment Orders Sub Type" />
         <column xsi:type="varchar" name="delivery_method_type" length="255" comment="Delivery Method Type" />
diff --git a/i18n/en_US.csv b/i18n/en_US.csv
index b487053..f6fe80b 100644
--- a/i18n/en_US.csv
+++ b/i18n/en_US.csv
@@ -46,6 +46,7 @@ Enabled,Enabled
 "Send Orders to Intelipost","Send Orders to Intelipost"
 "If enabled it'll send order automatically to Intelipost","If enabled it'll send order automatically to Intelipost"
 "Send orders to Intelipost by cron","Send orders to Intelipost by cron"
+"Send items to Intelipost by Shipment","Send items to Intelipost by Shipment"
 "Status to Create Orders on Intelipost","Status to Create Orders on Intelipost"
 "If enabled, it'll create an order on Intelipost when the order are in one of these status","If enabled, it'll create an order on Intelipost when the order are in one of these status"
 "Create Shipment","Create Shipment"
diff --git a/i18n/pt_BR.csv b/i18n/pt_BR.csv
index ae40158..bf1a29e 100644
--- a/i18n/pt_BR.csv
+++ b/i18n/pt_BR.csv
@@ -47,6 +47,7 @@ Enabled,Habilitado
 "Send Orders to Intelipost","Enviar pedidos para a Intelipost"
 "If enabled it'll send order automatically to Intelipost","Se habilitado, irá enviar os pedidos automaticamente para a Intelipost"
 "Send orders to Intelipost by cron","Enviar pedidos para a Intelipost por cron"
+"Send items to Intelipost by Shipment","Enviar itens para Intelipost por Remessa"
 "Status to Create Orders on Intelipost","Status para criar os pedidos na Intelipost"
 "If enabled, it'll create an order on Intelipost when the order are in one of these status","Se habilitado, irá criar o pedido na Intelipost quando o pedido na loja estiver com os seguintes status"
 "Create Shipment","Criar Envio"
diff --git a/view/adminhtml/ui_component/intelipost_shipping_shipments_listing.xml b/view/adminhtml/ui_component/intelipost_shipping_shipments_listing.xml
index a44a730..aa8e772 100644
--- a/view/adminhtml/ui_component/intelipost_shipping_shipments_listing.xml
+++ b/view/adminhtml/ui_component/intelipost_shipping_shipments_listing.xml
@@ -105,23 +105,15 @@
             </settings>
         </column>
 
-        <column name="quote_id">
+        <column name="intelipost_shipment_id" sortOrder="40">
             <settings>
                 <filter>text</filter>
                 <bodyTmpl>ui/grid/cells/text</bodyTmpl>
-                <label translate="true">Quote ID</label>
+                <label translate="true">Intelipost Shipment ID</label>
                 <resizeDefaultWidth>100</resizeDefaultWidth>
             </settings>
         </column>
 
-        <column name="decription">
-            <settings>
-                <filter>text</filter>
-                <bodyTmpl>ui/grid/cells/text</bodyTmpl>
-                <label translate="true">Description</label>
-            </settings>
-        </column>
-
         <column name="delivery_method_name">
             <settings>
                 <filter>text</filter>

From 9fcac210aaad1ddf0853e2058b64749097167acf Mon Sep 17 00:00:00 2001
From: Thiago Contardi <thiago@bizcommerce.com.br>
Date: Wed, 29 Mar 2023 21:02:36 +0000
Subject: [PATCH 3/4] feat: fix items for Magento 2.4.6

---
 Helper/Data.php              | 2 +-
 Model/Carrier/Intelipost.php | 4 ++--
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/Helper/Data.php b/Helper/Data.php
index 6d17054..abb8655 100644
--- a/Helper/Data.php
+++ b/Helper/Data.php
@@ -587,7 +587,7 @@ public function getPageName()
         if ($this->isAdmin()) {
             $result = 'admin';
         } else {
-            $originalPathInfo = $this->_request->getOriginalPathInfo();
+            $originalPathInfo = (string) $this->_request->getOriginalPathInfo();
             if (!strcmp($originalPathInfo, '/intelipost/product/shipping/')) {
                 $result = 'product';
             }
diff --git a/Model/Carrier/Intelipost.php b/Model/Carrier/Intelipost.php
index 59687ca..8460a5a 100644
--- a/Model/Carrier/Intelipost.php
+++ b/Model/Carrier/Intelipost.php
@@ -306,8 +306,8 @@ public function getProductData($request, $postData)
 
             // Type
             if (
-                !strcmp($item->getProductType(), 'configurable')
-                || !strcmp($item->getProductType(), 'bundle')
+                !strcmp((string) $item->getProductType(), 'configurable')
+                || !strcmp((string) $item->getProductType(), 'bundle')
             ) {
                 $parentSku = $product->getSku();
                 $cartItems[$parentSku] = $item;

From 295c57a97b4d6d005c3f0fe6d242f4aaf47a7273 Mon Sep 17 00:00:00 2001
From: Thiago Contardi <thiagocontardi@hotmail.com>
Date: Wed, 12 Apr 2023 14:01:25 -0300
Subject: [PATCH 4/4] feat: version 2.6.0

---
 composer.json  | 2 +-
 etc/module.xml | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/composer.json b/composer.json
index 969d08b..bc919b1 100644
--- a/composer.json
+++ b/composer.json
@@ -2,7 +2,7 @@
     "name": "intelipost/magento2",
     "description": "Intelipost Shipping",
     "type": "magento2-module",
-    "version": "2.5.0",
+    "version": "2.6.0",
     "require": {
         "guzzlehttp/guzzle": ">=6.3.3"
     },
diff --git a/etc/module.xml b/etc/module.xml
index 2a2a230..5885eb1 100644
--- a/etc/module.xml
+++ b/etc/module.xml
@@ -7,7 +7,7 @@
  */
 -->
 <config>
-    <module name="Intelipost_Shipping" setup_version="2.5.0">
+    <module name="Intelipost_Shipping" setup_version="2.6.0">
         <sequence>
             <module name="Magento_Quote"/>
         </sequence>