Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: possibility to use more than one quotes to orders #27

Merged
merged 7 commits into from
Apr 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Api/Data/ShipmentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
*/
Expand Down
8 changes: 7 additions & 1 deletion Client/AbstractShipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = $shipment->getData('order_increment_id');
} else {
$body->order_number = $shipment->getData('intelipost_shipment_id');
}
$body->event_date = str_replace(' ', 'T', $eventDate);

return [$body];
Expand Down
5 changes: 2 additions & 3 deletions Client/ShipmentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ public function getShipment($shipment)
$estimateDate = (string) $shipment->getData('delivery_estimate_date_exact_iso');

$body = new \stdClass();
$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');
Expand Down Expand Up @@ -291,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];
Expand Down
5 changes: 3 additions & 2 deletions Controller/Webhook/Index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
9 changes: 7 additions & 2 deletions Cron/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,19 @@ public function execute()
{
$enable = $this->helper->getConfig('enable_cron', '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);

if ($byShipment) {
$cond = 'main_table.intelipost_shipment_id LIKE CONCAT(\'%\', so.increment_id, \'%\')';
} else {
$cond = 'main_table.order_increment_id = so.increment_id';
}
$collection = $this->collectionFactory->create();
$collection->getSelect()->join(
['so' => $collection->getConnection()->getTableName('sales_order')],
'main_table.order_increment_id = so.increment_id',
$cond,
['increment_id']
);
$collection
Expand Down
9 changes: 8 additions & 1 deletion Cron/ImportLabels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand All @@ -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(
Expand Down
12 changes: 9 additions & 3 deletions Cron/ReadyForShipmentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,24 @@ 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) {
$statuses = explode(',', $status);
/** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
$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()->joinLeft(
['so' => $collection->getConnection()->getTableName('sales_order')],
'main_table.order_increment_id = so.increment_id',
$cond,
['increment_id']
);

$collection->addFieldToFilter('status', ['eq' => $status])
$collection->addFieldToFilter('status', ['in' => $statuses])
->addFieldToFilter(
'main_table.intelipost_status',
['neq' => Shipment::STATUS_READY_FOR_SHIPMENT]
Expand Down
8 changes: 7 additions & 1 deletion Cron/RetryOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,21 @@ 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();
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()->joinLeft(
['so' => $collection->getConnection()->getTableName('sales_order')],
'main_table.order_increment_id = so.increment_id',
$cond,
['increment_id']
);
$collection
Expand Down
9 changes: 7 additions & 2 deletions Cron/ShipOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +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_shipped', 'order_status', 'intelipost_push');

if ($enable) {
/** @var \Intelipost\Shipping\Model\ResourceModel\Shipment\Collection $collection */
$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()->joinLeft(
['so' => $collection->getConnection()->getTableName('sales_order')],
'main_table.order_increment_id = so.increment_id',
$cond,
['increment_id']
);

$collection->addFieldToFilter('status', ['eq' => $status])
->addFieldToFilter(
'main_table.intelipost_status',
Expand Down
2 changes: 1 addition & 1 deletion Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
Expand Down
6 changes: 3 additions & 3 deletions Model/Carrier/Intelipost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions Model/ResourceModel/ShipmentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
16 changes: 16 additions & 0 deletions Model/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
8 changes: 4 additions & 4 deletions Observer/Sales/OrderPlaceAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
11 changes: 10 additions & 1 deletion etc/adminhtml/system/push.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions etc/db_schema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down
1 change: 1 addition & 0 deletions i18n/en_US.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions i18n/pt_BR.csv
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down