diff --git a/src/CmsOrderPlugin.php b/src/CmsOrderPlugin.php index 8ba2fe1..e093c43 100644 --- a/src/CmsOrderPlugin.php +++ b/src/CmsOrderPlugin.php @@ -6,21 +6,20 @@ use Baraja\Cms\Search\SearchablePlugin; -use Baraja\Doctrine\EntityManager; use Baraja\Plugin\BasePlugin; use Baraja\Plugin\SimpleComponent\Button; use Baraja\Shop\Order\Entity\Order; use Baraja\Shop\Order\Repository\OrderRepository; use Baraja\Url\Url; +use Doctrine\ORM\EntityManagerInterface; final class CmsOrderPlugin extends BasePlugin implements SearchablePlugin { private OrderRepository $orderRepository; - public function __construct( - private EntityManager $entityManager, - ) { + public function __construct(EntityManagerInterface $entityManager) + { $orderRepository = $entityManager->getRepository(Order::class); assert($orderRepository instanceof OrderRepository); $this->orderRepository = $orderRepository; diff --git a/src/Command/CheckOrderCommand.php b/src/Command/CheckOrderCommand.php index f155adc..19298a3 100644 --- a/src/Command/CheckOrderCommand.php +++ b/src/Command/CheckOrderCommand.php @@ -9,10 +9,12 @@ use Baraja\BankTransferAuthorizator\Transaction; use Baraja\Doctrine\EntityManager; use Baraja\Doctrine\EntityManagerException; +use Baraja\Shop\Currency\CurrencyManager; use Baraja\Shop\Order\Entity\Order; use Baraja\Shop\Order\Entity\OrderStatus; use Baraja\Shop\Order\OrderStatusManager; use Baraja\Shop\Order\Payment\OrderPaymentClient; +use Baraja\Shop\Order\Repository\OrderRepository; use Baraja\Shop\Order\Status\OrderWorkflow; use Baraja\Shop\Order\OrderPaymentManager; use Nette\Application\UI\InvalidLinkException; @@ -22,14 +24,21 @@ final class CheckOrderCommand extends Command { + private OrderRepository $orderRepository; + + public function __construct( private EntityManager $entityManager, private OrderStatusManager $orderStatusManager, private OrderPaymentManager $tm, private OrderPaymentClient $orderPaymentClient, + private CurrencyManager $currencyManager, private OrderWorkflow $workflow, ) { parent::__construct(); + $orderRepository = $entityManager->getRepository(Order::class); + assert($orderRepository instanceof OrderRepository); + $this->orderRepository = $orderRepository; } @@ -45,34 +54,19 @@ protected function configure(): void */ protected function execute(InputInterface $input, OutputInterface $output): int { - /** @var Order[] $orders */ - $orders = $this->entityManager->getRepository(Order::class) - ->createQueryBuilder('orderEntity') - ->leftJoin('orderEntity.status', 'status') - ->where('orderEntity.paid = FALSE') - ->andWhere('status.code = :code') - ->setParameter('code', OrderStatus::STATUS_NEW) - ->getQuery() - ->getResult(); + $orders = $this->orderRepository->getOrderForCheckPayment(); - /** @var array $unauthorizedVariables */ - $unauthorizedVariables = []; - /** @var array $orderByVariable */ - $orderByVariable = []; - /** @var array $unauthorizedVariablesForCheck */ - $unauthorizedVariablesForCheck = []; + $orderByVariable = $this->filterOrderByVariable($orders); + $unauthorizedVariablesForCheck = $this->filterUnauthorizedVariablesForCheck($orders); foreach ($orders as $order) { - /** @var numeric-string $number */ - $number = $order->getNumber(); - $numberInt = (int) $number; - $unauthorizedVariables[$number] = (float) $order->getPrice()->getValue(); - $orderByVariable[$number] = $order; - $unauthorizedVariablesForCheck[] = $numberInt; - - echo $number . ' [' . $order->getPrice() . ' ' . $order->getCurrencyCode() . ']'; - echo ' [' . $order->getInsertedDate()->format('Y-m-d H:i:s') . ']'; - $this->updateStatusByWorkflow($order); + echo sprintf( + '%s [%s %s] [%s]', + $order->getNumber(), + $order->getPrice(), + $order->getCurrencyCode(), + $order->getInsertedDate()->format('Y-m-d H:i:s'), + ); echo "\n"; } @@ -84,7 +78,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->entityManager->flush(); echo "\n\n" . '------' . "\n\n" . 'Authorized:' . "\n\n"; - $this->authOrders($unauthorizedVariables, $orderByVariable, $authorizator); + $this->authOrders($orderByVariable, $authorizator); echo "\n\n"; echo 'Saving...' . "\n\n"; @@ -92,6 +86,10 @@ protected function execute(InputInterface $input, OutputInterface $output): int $this->checkSentOrders(); $this->entityManager->flush(); + foreach ($this->orderRepository->getOrderForCheckPayment() as $order) { + $this->updateStatusByWorkflow($order); + } + return 0; } @@ -150,37 +148,41 @@ private function checkUnmatchedTransactions(array $unauthorizedVariables, Author /** - * @param array $unauthorizedVariables * @param array $orderByVariable */ - private function authOrders(array $unauthorizedVariables, array $orderByVariable, Authorizator $authorizator): void + private function authOrders(array $orderByVariable, Authorizator $authorizator): void { - /** @var callable&(callable(Transaction): void)[] $callback */ - $callback = function (Transaction $transaction) use ($orderByVariable): void { - assert($transaction instanceof \Baraja\FioPaymentAuthorizator\Transaction); - $entity = null; - if ($this->tm->transactionExist((int) $transaction->getIdTransaction()) === false) { - $entity = $this->tm->storeTransaction($transaction); - } - $variable = (string) $transaction->getVariableSymbol(); - if ($variable !== '' && isset($orderByVariable[$variable])) { - $order = $orderByVariable[$variable]; - $order->setPaid(true); - $this->orderStatusManager->setStatus($order, OrderStatus::STATUS_PAID); - $entity?->setOrder($order); + foreach ($this->currencyManager->getCurrencies() as $currency) { + $orders = $this->orderRepository->getOrderForCheckPayment($currency->getCode()); + $unauthorizedVariables = $this->filterUnauthorizedVariables($orders); + + /** @var callable&(callable(Transaction): void)[] $callback */ + $callback = function (Transaction $transaction) use ($orderByVariable): void { + assert($transaction instanceof \Baraja\FioPaymentAuthorizator\Transaction); + $entity = null; + if ($this->tm->transactionExist((int) $transaction->getIdTransaction()) === false) { + $entity = $this->tm->storeTransaction($transaction); + } + $variable = (string) $transaction->getVariableSymbol(); + if ($variable !== '' && isset($orderByVariable[$variable])) { + $order = $orderByVariable[$variable]; + $order->setPaid(true); + $this->orderStatusManager->setStatus($order, OrderStatus::STATUS_PAID); + $entity?->setOrder($order); + } + $this->entityManager->flush(); + }; + + try { + $authorizator->authOrders( + $unauthorizedVariables, + $callback, + $currency->getCode(), + 0.25, + ); + } catch (\Throwable $e) { + throw new \RuntimeException(sprintf('Can not authorize orders: %s', $e->getMessage()), 500, $e); } - $this->entityManager->flush(); - }; - - try { - $authorizator->authOrders( - $unauthorizedVariables, - $callback, - 'CZK', - 0.25, - ); - } catch (\Throwable $e) { - throw new \RuntimeException(sprintf('Can not authorize orders: %s', $e->getMessage()), 500, $e); } } @@ -202,4 +204,53 @@ private function checkSentOrders(): void $this->orderStatusManager->setStatus($order, OrderStatus::STATUS_DONE, force: true); } } + + + /** + * @param array $orders + * @return array + */ + private function filterUnauthorizedVariables(array $orders): array + { + $return = []; + foreach ($orders as $order) { + /** @var numeric-string $number */ + $number = $order->getNumber(); + $return[$number] = (float) $order->getPrice()->getValue(); + } + + return $return; + } + + + /** + * @param array $orders + * @return array + */ + private function filterOrderByVariable(array $orders): array + { + $return = []; + foreach ($orders as $order) { + /** @var numeric-string $number */ + $number = $order->getNumber(); + $return[$number] = $order; + } + + return $return; + } + + + /** + * @param array $orders + * @return array + */ + private function filterUnauthorizedVariablesForCheck(array $orders): array + { + $return = []; + foreach ($orders as $order) { + $return[] = (int) $order->getNumber(); + } + + return $return; + } } diff --git a/src/Entity/OrderFile.php b/src/Entity/OrderFile.php index 1781966..e01dd73 100644 --- a/src/Entity/OrderFile.php +++ b/src/Entity/OrderFile.php @@ -21,7 +21,7 @@ class OrderFile implements OrderDocument private Order $order; #[ORM\Column(type: 'string', length: 64, nullable: true)] - private ?string $number = null; + private ?string $number; #[ORM\Column(type: 'string', length: 128)] private string $label; @@ -55,9 +55,11 @@ public function getId(): int public static function getRelativePath(self $orderFile): string { - return 'order-file/' . $orderFile->getInsertedDate()->format('Y-m-d') - . '/' . $orderFile->getOrder()->getHash() - . '/' . $orderFile->getFilename(); + return sprintf('order-file/%s/%s/%s', + $orderFile->getInsertedDate()->format('Y-m-d'), + $orderFile->getOrder()->getHash(), + $orderFile->getFilename(), + ); }