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
113 changes: 113 additions & 0 deletions alma/controllers/hook/ActionObjectUpdateAfter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Alma\PrestaShop\Controllers\Hook;

use Alma\API\Exceptions\AlmaException;
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;
}

/**
* @param $params
*
* @return void
*/
public function run($params)
webaaz marked this conversation as resolved.
Show resolved Hide resolved
{
if (
!isset($params['object']) ||
!($params['object'] instanceof \OrderCarrierCore)
) {
return;
}

/** @var \OrderCarrier $orderCarrier */
$orderCarrier = $params['object'];
$idOrder = $orderCarrier->id_order;
if ($orderCarrier->tracking_number === '') {
return;
}
/* @var \OrderCore $order */
try {
$order = $this->orderFactory->create($idOrder);
} catch (\PrestaShopException $e) {
Logger::instance()->error('[Alma] - PrestaShopException - Impossible to get Order with id :' . $idOrder);

return;
}
if ($order->module != ConstantsHelper::ALMA_MODULE_NAME || empty($order->getOrderPayments())) {
return;
}

foreach ($order->getOrderPayments() as $orderPayment) {
/** @var \OrderPayment $orderPayment */
if (isset($orderPayment->transaction_id)) {
$almaPaymentExternalId = $orderPayment->transaction_id;
break;
}
}
if (!isset($almaPaymentExternalId)) {
Logger::instance()->error('[Alma] - No Alma Payment External Id in order ' . $order->reference);

return;
}
try {
$almaClient = $this->clientHelper->getAlmaClient();
} catch (ClientException $e) {
Logger::instance()->error('[Alma] - ClientException - ' . $e->getMessage());

return;
}

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();
}
$carrier = $this->carrierFactory->create((int) $orderCarrier->id_carrier);
$almaClient->orders->addTracking($orderExternalId, $carrier->name, $orderCarrier->tracking_number, $carrier->url);
} catch (AlmaException $e) {
Logger::instance()->error('[Alma] - AlmaException ' . $e->getMessage());
}
}
}
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