Skip to content

Commit

Permalink
Merge pull request #14 from ThomasDaSilva/fix_send_orders
Browse files Browse the repository at this point in the history
Fix Send Orders
  • Loading branch information
ThomasDaSilva authored Apr 10, 2024
2 parents 0a38c6d + 0624b4d commit 3de307d
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 25 deletions.
3 changes: 1 addition & 2 deletions Command/SendOrderCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ public function execute(InputInterface $input, OutputInterface $output): int

if ($response->success === 1)
{
$this->orderService->clearOrderQueueTable();
$output->write("Order Queue is now empty\n");
$this->orderService->setOrdersAsSend();
}
} catch (Exception $exception) {
$output->write($exception->getMessage());
Expand Down
2 changes: 1 addition & 1 deletion Config/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<language>en_US</language>
<language>fr_FR</language>
</languages>
<version>1.0.12</version>
<version>1.0.13</version>
<authors>
<author>
<name>Chabreuil Antoine</name>
Expand Down
50 changes: 32 additions & 18 deletions EventListeners/OrderListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,52 @@ public function __construct(
/**
* @throws PropelException
*/
public function sendOrderToQueue(OrderEvent $orderEvent): void
public function registerOrder(OrderEvent $event): void
{
$request = $this->requestStack->getCurrentRequest()->request;
$statusToExport = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY, '4'));

if (null !== GuaranteedOpinionOrderQueueQuery::create()->findOneByOrderId($orderEvent->getOrder()->getId()))
{
return;
if (in_array($event->getPlacedOrder()->getStatusId(), $statusToExport, false)) {
$guaranteedReviewsOrderQueue = new GuaranteedOpinionOrderQueue();
$guaranteedReviewsOrderQueue->setOrderId($event->getPlacedOrder()->getId())
->setStatus(0)
->setTreatedAt(new DateTime(''))
->save()
;
}
}

$orderStatuses = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY));
/**
* @param OrderEvent $event
* @throws PropelException
*/
public function checkOrderInQueue(OrderEvent $event): void
{
$statusToExport = explode(',', GuaranteedOpinion::getConfigValue(GuaranteedOpinion::STATUS_TO_EXPORT_CONFIG_KEY, '4'));

foreach ($orderStatuses as $orderStatus)
{
if ($orderStatus === $status = $request->get('status_id'))
{
$guaranteedOpinionOrderQueue = new GuaranteedOpinionOrderQueue();
$newStatus = $event->getOrder()->getStatusId();
$orderId = $event->getOrder()->getId();

$guaranteedOpinionOrderQueue
->setOrderId($orderEvent->getOrder()->getId())
->setTreatedAt(new DateTime(''))
->setStatus($status)
;
$guaranteedReviewsOrderQueue = GuaranteedOpinionOrderQueueQuery::create()
->filterByOrderId($orderId)
->findOne();

$guaranteedOpinionOrderQueue->save();
if (null !== $guaranteedReviewsOrderQueue) {
if (!in_array($newStatus, $statusToExport, false) && (int)$guaranteedReviewsOrderQueue->getStatus() === 0){
$guaranteedReviewsOrderQueue->delete();
}
} else if (in_array($newStatus, $statusToExport, false)){
$guaranteedReviewsOrderQueue = new GuaranteedOpinionOrderQueue();
$guaranteedReviewsOrderQueue->setOrderId($orderId)
->setStatus(0)
->save();
}
}

public static function getSubscribedEvents(): array
{
return array(
TheliaEvents::ORDER_UPDATE_STATUS => ["sendOrderToQueue", 192]
TheliaEvents::ORDER_UPDATE_STATUS => ["checkOrderInQueue", 64],
TheliaEvents::ORDER_PAY => ['registerOrder', 64],
);
}
}
14 changes: 10 additions & 4 deletions Service/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function prepareOrderRequest(): string
{
$jsonOrder = [];

$guaranteedOpinionOrders = GuaranteedOpinionOrderQueueQuery::create()->find();
$guaranteedOpinionOrders = GuaranteedOpinionOrderQueueQuery::create()->filterByTreatedAt(null)->findByStatus(0);

foreach ($guaranteedOpinionOrders as $guaranteedOpinionOrder) {
$jsonOrder[] = $this->orderToJsonObject($guaranteedOpinionOrder);
Expand Down Expand Up @@ -97,12 +97,18 @@ private function productToJsonObject(OrderProduct $orderProduct): array
/**
* @throws PropelException
*/
public function clearOrderQueueTable(): void
public function setOrdersAsSend(): void
{
$orders = GuaranteedOpinionOrderQueueQuery::create()->find();
$orders = GuaranteedOpinionOrderQueueQuery::create()
->filterByTreatedAt(null)
->findByStatus(0);

foreach ($orders as $order) {
$order->delete();
$order
->setTreatedAt(new \DateTime())
->setStatus(1);

$order->save();
}
}
}

0 comments on commit 3de307d

Please # to comment.