Skip to content

Commit

Permalink
Merge pull request #35 from contardi/master
Browse files Browse the repository at this point in the history
Feat: send source_code as warehouse code when it exists - v2.8.0
  • Loading branch information
daniellimaintelipost authored Nov 27, 2024
2 parents 83ad6a0 + 5cda66d commit 83c70f4
Show file tree
Hide file tree
Showing 14 changed files with 189 additions and 51 deletions.
32 changes: 27 additions & 5 deletions Client/ShipmentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@

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;
use Intelipost\Shipping\Api\ShipmentRepositoryInterface;
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
{
Expand Down Expand Up @@ -45,6 +46,11 @@ class ShipmentOrder
/** @var RequestLabel */
protected $requestLabel;

/**
* @var GetSourcesForOrder
*/
private $getSourcesForOrder;

/**
* @param ShipmentOrder\Customer $customer
* @param ShipmentOrder\Volume $volume
Expand All @@ -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,
Expand All @@ -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;
Expand All @@ -74,6 +81,7 @@ public function __construct(
$this->shipmentRepository = $shipmentRepository;
$this->timezone = $timezone;
$this->requestLabel = $label;
$this->getSourcesForOrder = $getAllocatedSourcesForOrder;
}

/**
Expand Down Expand Up @@ -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'),
Expand All @@ -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');
Expand Down
4 changes: 2 additions & 2 deletions Cron/CreateOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, \'%\')';
Expand Down
4 changes: 2 additions & 2 deletions Cron/ReadyForShipmentOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Cron/RetryOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions Cron/ShipOrder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 9 additions & 5 deletions Model/Carrier/Intelipost.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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);
Expand All @@ -416,18 +416,22 @@ 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;
}

/**
* @param $response
* @param $cartQty
* @return array
*/
public function getVolumes($response, $cartQty)
public function getVolumes($response, $cartQty): array
{
$volumes = [];
$volCount = count($response['content']['volumes']);
Expand Down
78 changes: 78 additions & 0 deletions Model/ResourceModel/GetSourcesForOrder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

declare(strict_types=1);

namespace Intelipost\Shipping\Model\ResourceModel;

use Magento\Framework\App\ResourceConnection;

/**
* Get allocated sources for specified order.
*/
class GetSourcesForOrder
{
/**
* @var ResourceConnection
*/
private $resourceConnection;

/**
* @param ResourceConnection $resourceConnection
*/
public function __construct(
ResourceConnection $resourceConnection
) {
$this->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);
}
}
55 changes: 31 additions & 24 deletions Observer/Sales/OrderPlaceAfter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -58,8 +59,7 @@ public function __construct(
ShipmentFactory $shipmentFactory,
ShipmentResourceModel $shipmentResource,
StoreManagerInterface $storeManager
)
{
) {
$this->intelipostQuote = $intelipostQuote;
$this->helper = $intelipostHelper;
$this->sessionManager = $sessionManager;
Expand All @@ -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 = [];
Expand Down Expand Up @@ -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'];
Expand Down Expand Up @@ -236,7 +220,6 @@ public function setShipmentOrder($resultJson)
}

$orderIndex++;

$this->shipmentResource->save($shipment);
}
}
Expand Down Expand Up @@ -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;
}
}
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.7.5",
"version": "2.8.0",
"require": {
"guzzlehttp/guzzle": ">=6.3.3"
},
Expand Down
Loading

0 comments on commit 83c70f4

Please sign in to comment.