Skip to content

Commit

Permalink
[PT-1020] Invoice Generation from API is not working (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-pugach-mondu authored Feb 10, 2024
1 parent 34e362e commit 9598d2d
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
80 changes: 80 additions & 0 deletions Plugin/ProcessShipmentSave.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php
namespace Mondu\Mondu\Plugin;

use Magento\Framework\Exception\LocalizedException;
use Mondu\Mondu\Helpers\InvoiceOrderHelper;
use Mondu\Mondu\Helpers\Log;
use Mondu\Mondu\Helpers\Logger\Logger as MonduFileLogger;

class ProcessShipmentSave
{
/**
* @var MonduFileLogger
*/
private $monduFileLogger;

/**
* @var Log
*/
protected $monduLogger;

/**
* @var InvoiceOrderHelper
*/
private $invoiceOrderHelper;

/**
* @param MonduFileLogger $monduFileLogger
* @param Log $logger
* @param InvoiceOrderHelper $invoiceOrderHelper
*/
public function __construct(
MonduFileLogger $monduFileLogger,
Log $logger,
InvoiceOrderHelper $invoiceOrderHelper
) {
$this->monduFileLogger = $monduFileLogger;
$this->monduLogger = $logger;
$this->invoiceOrderHelper = $invoiceOrderHelper;
}

/**
* After Save Processing
*
* @param \Magento\Sales\Model\Order\ShipmentRepository\Interceptor $subject
* @param $shipment
*
* @return mixed
* @throws LocalizedException
*/
public function afterSave(
\Magento\Sales\Model\Order\ShipmentRepository\Interceptor $subject,
$shipment
) {
$order = $shipment->getOrder();
$monduLog = $this->monduLogger->getLogCollection($order->getData('mondu_reference_id'));

if ($monduLog->getSkipShipObserver()) {
$this->monduFileLogger
->info(
'Already invoiced using invoice orders action, skipping',
['orderNumber' => $order->getIncrementId()]
);

return $shipment;
}

$monduId = $order->getData('mondu_reference_id');
$this->monduLogger->syncOrder($monduId);

if (!$this->monduLogger->canShipOrder($monduId)) {
throw new LocalizedException(
__('Can\'t ship order: Mondu order state must be confirmed or partially_shipped')
);
}

$this->invoiceOrderHelper->handleInvoiceOrder($order, $shipment, $monduLog);

return $shipment;
}
}
3 changes: 3 additions & 0 deletions etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,7 @@
<type name="Magento\Payment\Helper\Data">
<plugin sortOrder="10" name="monduPaymentData" type="Mondu\Mondu\Helpers\DataPlugin"/>
</type>
<type name="Magento\Sales\Model\Order\ShipmentRepository">
<plugin sortOrder="10" name="processShipmentSave" type="Mondu\Mondu\Plugin\ProcessShipmentSave" />
</type>
</config>

0 comments on commit 9598d2d

Please sign in to comment.