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: Shipment info analysis #545

9 changes: 9 additions & 0 deletions alma/alma.php
Original file line number Diff line number Diff line change
Expand Up @@ -754,4 +754,13 @@ public function hookActionGetProductPropertiesBefore($params)
{
return $this->runHookController('actionGetProductPropertiesBefore', $params);
}

public function hookActionObjectUpdateAfter($params)
{
$orderFactory = new Alma\PrestaShop\Factories\OrderFactory();
$clientHelper = new Alma\PrestaShop\Helpers\ClientHelper();
$carrierFactory = new Alma\PrestaShop\Factories\CarrierFactory();
$actionObjectUpdateAfter = new Alma\PrestaShop\Controllers\Hook\ActionObjectUpdateAfter($orderFactory, $clientHelper, $carrierFactory);
$actionObjectUpdateAfter->run($params);
}
}
2 changes: 1 addition & 1 deletion alma/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"require": {
"php": "^5.6 || ~7.0 || ~7.1 || ~7.2 || ~7.3 || ~7.4 || ~8.0 || ~8.1",
"alma/alma-php-client": "^2.0.7",
"alma/alma-php-client": "^2.1.0",
"ext-json": "*",
"ext-openssl": "*",
"prestashop/prestashop-accounts-installer": "^v1.0.4",
Expand Down
192 changes: 192 additions & 0 deletions alma/controllers/hook/ActionObjectUpdateAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?php

namespace Alma\PrestaShop\Controllers\Hook;

use Alma\API\Client;
use Alma\API\Exceptions\AlmaException;
use Alma\PrestaShop\Exceptions\AlmaActionObjectUpdateException;
use Alma\PrestaShop\Exceptions\ClientException;
use Alma\PrestaShop\Factories\CarrierFactory;
use Alma\PrestaShop\Factories\OrderFactory;
use Alma\PrestaShop\Helpers\ClientHelper;
use Alma\PrestaShop\Helpers\ConstantsHelper;
use Alma\PrestaShop\Logger;

if (!defined('_PS_VERSION_')) {
exit;
}

class ActionObjectUpdateAfter
{
/**
* @var OrderFactory
*/
private $orderFactory;

/**
* @var ClientHelper
*/
private $clientHelper;

/**
* @var CarrierFactory
*/
private $carrierFactory;

public function __construct($orderFactory, $clientHelper, $carrierFactory)
{
$this->orderFactory = $orderFactory;
$this->clientHelper = $clientHelper;
$this->carrierFactory = $carrierFactory;
}

/**
* Send tracking information to Alma only for Alma Orders
*
* @param $params
*
* @return void
*/
public function run($params)
webaaz marked this conversation as resolved.
Show resolved Hide resolved
{
try {
$orderCarrier = $this->checkParamsContainValidOrderCarrierObject($params);
$carrier = $this->carrierFactory->create($orderCarrier->id_carrier);
$order = $this->getAlmaOrderFromOrderCarrierOrderId($orderCarrier->id_order);
$almaPaymentExternalId = $this->getAlmaPaymentExternalId($order);
$almaClient = $this->getAlmaClient();
$orderExternalId = $this->getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId);
$almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url);
} catch (AlmaActionObjectUpdateException $e) {
return;
} catch (AlmaException $e) {
Logger::instance()->error('[Alma] - Add tracking error: ' . $e->getMessage());
}
}

/**
* Check if params Object is set and is an OrderCarrier
*
* @param $params
*
* @return \OrderCarrierCore $orderCarrier
*
* @throws AlmaActionObjectUpdateException
*/
private function checkParamsContainValidOrderCarrierObject($params)
{
if (
!isset($params['object']) ||
!($params['object'] instanceof \OrderCarrierCore) ||
$params['object']->tracking_number === ''
) {
throw new AlmaActionObjectUpdateException('Object is not an OrderCarrier');
}

return $params['object'];
}

/**
* Get Alma Order from OrderCarrier OrderId
* Throw Exception for no Alma Order
*
* @param $orderId
*
* @return \Order
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaOrderFromOrderCarrierOrderId($orderId)
{
try {
$order = $this->orderFactory->create($orderId);
} catch (\PrestaShopException $e) {
Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $orderId);
throw new AlmaActionObjectUpdateException('Impossible to get Order');
}
if ($order->module != ConstantsHelper::ALMA_MODULE_NAME) {
throw new AlmaActionObjectUpdateException('Order is not an Alma Order');
}

return $order;
}

/**
* Get Alma Payment External Id from Order
* Throw Exception if no Payment or no Alma Payment External Id in Order
*
* @param \Order $order
*
* @return string
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaPaymentExternalId($order)
{
if (empty($order->getOrderPayments())) {
throw new AlmaActionObjectUpdateException('Order is not an Alma Order');
}
foreach ($order->getOrderPayments() as $orderPayment) {
/** @var \OrderPayment $orderPayment */
if (isset($orderPayment->transaction_id)) {
return $orderPayment->transaction_id;
}
}
Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference);
throw new AlmaActionObjectUpdateException('No Alma Payment External Id');
}

/**
* Get Alma Client or throw Exception
*
* @return Client
*
* @throws AlmaActionObjectUpdateException
*/
private function getAlmaClient()
{
try {
return $this->clientHelper->getAlmaClient();
} catch (ClientException $e) {
Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage());
throw new AlmaActionObjectUpdateException('Impossible to get Alma Client');
}
}

/**
* Get or Create Alma Order External Id
*
* @param $almaClient
* @param $order
* @param $almaPaymentExternalId
*
* @return mixed|null
*
* @throws AlmaActionObjectUpdateException
*/
private function getOrCreateAlmaOrderExternalId($almaClient, $order, $almaPaymentExternalId)
{
try {
$almaPayment = $almaClient->payments->fetch($almaPaymentExternalId);

$orderExternalId = null;
foreach ($almaPayment->orders as $almaOrder) {
if ($order->reference === $almaOrder->getMerchantReference()) {
$orderExternalId = $almaOrder->getExternalId();
}
}
if (!isset($orderExternalId)) {
$almaOrder = $almaClient->payments->addOrder($almaPaymentExternalId, [
'merchant_reference' => $order->reference,
]
);
$orderExternalId = $almaOrder->getExternalId();
}

return $orderExternalId;
} catch (AlmaException $e) {
Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage());
throw new AlmaActionObjectUpdateException('Impossible to get or create Alma Order');
}
}
}
11 changes: 11 additions & 0 deletions alma/exceptions/AlmaActionObjectUpdateException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Alma\PrestaShop\Exceptions;

if (!defined('_PS_VERSION_')) {
exit;
}

class AlmaActionObjectUpdateException extends AlmaException
{
}
19 changes: 19 additions & 0 deletions alma/lib/Factories/OrderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Alma\PrestaShop\Factories;

class OrderFactory
{
/**
* @param $id
*
* @return \Order
*
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function create($id = null, $id_lang = null)
{
return new \Order($id, $id_lang);
}
}
1 change: 1 addition & 0 deletions alma/lib/Helpers/HookHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function __construct()
public static $almaHooks = [
'moduleRoutes' => 'all',
'actionAdminControllerInitBefore' => 'all',
'actionObjectUpdateAfter' => 'all',
'header' => 'all',
'displayHeader' => 'all',
'displayBackOfficeHeader' => 'all',
Expand Down
Loading