app/Plugin/npka421/NpKakebaraiEvent.php line 364

Open in your IDE?
  1. <?php
  2. namespace Plugin\npka421;
  3. use Plugin\npka421\Entity\OrderResult;
  4. use Plugin\npka421\Repository\ErrorInfoRepository;
  5. use Plugin\npka421\Repository\NpKakebaraiPaymentRepository;
  6. use Plugin\npka421\Repository\OrderResultRepository;
  7. use Plugin\npka421\Service\RestAPI\BillingsRequestsService;
  8. use Plugin\npka421\Service\RestAPI\TransactionsCancelRequestsService;
  9. use Plugin\npka421\Service\RestAPI\TransactionsModificationsRequestsService;
  10. use Plugin\npka421\Service\RestAPI\TransactionsRegistrationsRequestsService;
  11. use Plugin\npka421\Service\OrderStatusService;
  12. use Plugin\npka421\Service\UtilService;
  13. use Eccube\Common\EccubeConfig;
  14. use Eccube\Entity\Master\OrderStatus;
  15. use Eccube\Entity\Order;
  16. use Eccube\Event\EccubeEvents;
  17. use Eccube\Event\EventArgs;
  18. use Eccube\Event\TemplateEvent;
  19. use Eccube\Repository\OrderRepository;
  20. use Eccube\Repository\ShippingRepository;
  21. use Symfony\Component\EventDispatcher\EventSubscriberInterface;
  22. use Symfony\Component\Workflow\Event\Event;
  23. class NpKakebaraiEvent implements EventSubscriberInterface
  24. {
  25.     /**
  26.      * @var EccubeConfig
  27.      */
  28.     protected $eccubeConfig;
  29.     /**
  30.      * @var ErrorInfoRepository
  31.      */
  32.     protected $errorInfoRepository;
  33.     /**
  34.      * @var NpKakebaraiPaymentRepository
  35.      */
  36.     protected $npKakebaraiPaymentRepository;
  37.     /**
  38.      * @var OrderResultRepository
  39.      */
  40.     protected $orderResultRepository;
  41.     /**
  42.      * @var OrderRepository
  43.      */
  44.     protected $orderRepository;
  45.     /**
  46.      * @var ShippingRepository
  47.      */
  48.     protected $shippingRepository;
  49.     /**
  50.      * @var BillingsRequestsService
  51.      */
  52.     protected $billingsRequestsService;
  53.     /**
  54.      * @var TransactionsCancelRequestsService
  55.      */
  56.     protected $transactionsCancelRequestsService;
  57.     /**
  58.      * @var TransactionsModificationsRequestsService
  59.      */
  60.     protected $transactionsModificationsRequestsService;
  61.     /**
  62.      * @var TransactionsRegistrationsRequestsService
  63.      */
  64.     protected $transactionsRegistrationsRequestsService;
  65.     /**
  66.      * @var OrderStatusService
  67.      */
  68.     protected $orderStatusService;
  69.     /**
  70.      * @var UtilService
  71.      */
  72.     protected $utilService;
  73.     /**
  74.      * NpKakebaraiEvent constructor.
  75.      *
  76.      * @param EccubeConfig $eccubeConfig
  77.      * @param ErrorInfoRepository $errorInfoRepository
  78.      * @param NpKakebaraiPaymentRepository $npKakebaraiPaymentRepository
  79.      * @param OrderResultRepository $orderResultRepository
  80.      * @param OrderRepository $orderRepository
  81.      * @param ShippingRepository $shippingRepository
  82.      * @param BillingsRequestsService $billingsRequestsService
  83.      * @param TransactionsCancelRequestsService $transactionsCancelRequestsService
  84.      * @param TransactionsModificationsRequestsService $transactionsModificationsRequestsService
  85.      * @param TransactionsRegistrationsRequestsService $transactionsRegistrationsRequestsService
  86.      * @param OrderStatusService $orderStatusService
  87.      * @param UtilService $utilService
  88.      */
  89.     public function __construct(
  90.         EccubeConfig $eccubeConfig,
  91.         ErrorInfoRepository $errorInfoRepository,
  92.         NpKakebaraiPaymentRepository $npKakebaraiPaymentRepository,
  93.         OrderResultRepository $orderResultRepository,
  94.         OrderRepository $orderRepository,
  95.         ShippingRepository $shippingRepository,
  96.         BillingsRequestsService $billingsRequestsService,
  97.         TransactionsCancelRequestsService $transactionsCancelRequestsService,
  98.         TransactionsModificationsRequestsService $transactionsModificationsRequestsService,
  99.         TransactionsRegistrationsRequestsService $transactionsRegistrationsRequestsService,
  100.         OrderStatusService $orderStatusService,
  101.         UtilService $utilService
  102.     ) {
  103.         $this->eccubeConfig $eccubeConfig;
  104.         $this->errorInfoRepository $errorInfoRepository;
  105.         $this->npKakebaraiPaymentRepository $npKakebaraiPaymentRepository;
  106.         $this->orderResultRepository $orderResultRepository;
  107.         $this->orderRepository $orderRepository;
  108.         $this->shippingRepository $shippingRepository;
  109.         $this->billingsRequestsService $billingsRequestsService;
  110.         $this->transactionsCancelRequestsService $transactionsCancelRequestsService;
  111.         $this->transactionsModificationsRequestsService $transactionsModificationsRequestsService;
  112.         $this->transactionsRegistrationsRequestsService $transactionsRegistrationsRequestsService;
  113.         $this->orderStatusService $orderStatusService;
  114.         $this->utilService $utilService;
  115.     }
  116.     /**
  117.      * @return array
  118.      */
  119.     public static function getSubscribedEvents()
  120.     {
  121.         return [
  122.             // 受注一覧
  123.             '@admin/Order/index.twig' => 'onRenderAdminOrderIndex',
  124.             // 受注登録・編集画面表示
  125.             '@admin/Order/edit.twig' => 'onRenderAdminOrderEdit',
  126.             // 受注登録・編集処理前
  127.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_PROGRESS => 'onAdminOrderEditIndexProgress',
  128.             // 受注登録・編集処理完了
  129.             EccubeEvents::ADMIN_ORDER_EDIT_INDEX_COMPLETE => 'onAdminOrderEditIndexComplete',
  130.             // キャンセル処理
  131.             'workflow.order.transition.cancel' => 'onCancel',
  132.         ];
  133.     }
  134.     /**
  135.      * 受注一覧
  136.      *
  137.      * @param TemplateEvent $event
  138.      */
  139.     public function onRenderAdminOrderIndex(TemplateEvent $event)
  140.     {
  141.         // 表示対象の受注一覧を取得
  142.         $pagination $event->getParameter('pagination');
  143.         // 支払方法の取得
  144.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  145.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  146.         // NP処理ステータス一覧
  147.         $npStatusList $this->orderStatusService->getNpStatusList();
  148.         // 配送先ごとの注文ID・NP処理ステータス
  149.         $shippingInfoMap $this->getShippingInfoMap($pagination);
  150.         // テンプレートにセット
  151.         $event->setParameter('npStatusList'$npStatusList);
  152.         $event->setParameter('shippingInfoMap'$shippingInfoMap);
  153.         $event->addSnippet('@npka421/admin/Order/index_order_result.twig');
  154.     }
  155.     /**
  156.      * 受注リストから注文ID・NP処理ステータスを取得
  157.      *
  158.      * @param array $Orders
  159.      * @return array
  160.      */
  161.     private function getShippingInfoMap($Orders)
  162.     {
  163.         $shippingInfoMap = [];
  164.         $shippingIdList = [];
  165.         foreach ($Orders as $Order) {
  166.             foreach ($Order->getShippings() as $Shipping) {
  167.                 $shippingIdList[] = $Shipping->getId();
  168.             }
  169.         }
  170.         if (empty($shippingIdList)) {
  171.             return $shippingInfoMap;
  172.         }
  173.         // 配送先IDをもとに注文IDと決済ステータスを取得
  174.         $qb $this->shippingRepository->createQueryBuilder('s');
  175.         $qb
  176.             ->select(
  177.                 's.id AS shippingId',
  178.                 'o.id AS orderId',
  179.                 'r.order_status AS npStatus'
  180.             )
  181.             ->leftJoin(OrderResult::class, 'r''WITH''r.id = s.Order')
  182.             ->innerJoin(Order::class, 'o''WITH''o.id = s.Order')
  183.             ->where(
  184.                 $qb->expr()->in('s.id'$shippingIdList)
  185.             );
  186.         $result $qb->getQuery()->getResult();
  187.         if (empty($result)) {
  188.             return $shippingInfoMap;
  189.         }
  190.         foreach ($result as $row) {
  191.             $Order $this->orderRepository->find($row['orderId']);
  192.             $OrderResult $this->orderResultRepository->find($Order->getId());
  193.             $shippingInfoMap[$row['shippingId']] = [
  194.                 'orderId'   => $row['orderId'],
  195.                 'npStatus' => $row['npStatus'],
  196.                 'can_billing' => ($this->orderStatusService->canBilling($Order$OrderResult)) ? 0,
  197.                 'can_reauthori' => ($this->orderStatusService->canReauthori($Order$OrderResult)) ? 0,
  198.             ];
  199.         }
  200.         return $shippingInfoMap;
  201.     }
  202.     /**
  203.      * 受注登録・編集画面表示
  204.      *
  205.      * @param TemplateEvent $event
  206.      */
  207.     public function onRenderAdminOrderEdit(TemplateEvent $event)
  208.     {
  209.         $Order $event->getParameter('Order');
  210.         if (empty($Order) || empty($Order->getId())) {
  211.             return;
  212.         }
  213.         $Payment $Order->getPayment();
  214.         if (empty($Payment)) {
  215.             return;
  216.         }
  217.         // 支払方法の取得
  218.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository->findOneBy([
  219.             'payment_type' => $this->eccubeConfig['np_payment_invoice']
  220.         ]);
  221.         // 支払方法が対象外
  222.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  223.             return;
  224.         }
  225.         // 受注処理結果取得
  226.         $OrderResult $this->orderResultRepository->find($Order->getId());
  227.         if (empty($OrderResult)) {
  228.             return;
  229.         }
  230.         // NP処理ステータス名取得
  231.         $np_status_name $this->orderStatusService
  232.             ->getNpStatusName($OrderResult->getOrderStatus());
  233.         // エラー情報
  234.         $ErrorInfos $this->errorInfoRepository->findBy(
  235.             ['order_id' => $Order->getId()],
  236.             ['id' => 'ASC']
  237.         );
  238.         // テンプレートにセット
  239.         $event->setParameter('OrderResult'$OrderResult);
  240.         $event->setParameter('np_status_name'$np_status_name);
  241.         $event->setParameter('ErrorInfos'$ErrorInfos);
  242.         $event->setParameter('can_billing'$this->orderStatusService->canBilling($Order$OrderResult));
  243.         $event->setParameter('can_reauthori'$this->orderStatusService->canReauthori($Order$OrderResult));
  244.         // 表示領域追加
  245.         $snippet 'npka421/Resource/template/admin/Order/edit_order_result.twig';
  246.         $event->addSnippet($snippet);
  247.     }
  248.     /**
  249.      * 受注登録・編集処理前
  250.      *
  251.      * @param EventArgs $event
  252.      */
  253.     public function onAdminOrderEditIndexProgress(EventArgs $event)
  254.     {
  255.         $request $event->getRequest();
  256.         $OriginOrder $event->getArgument('OriginOrder');
  257.         $TargetOrder $event->getArgument('TargetOrder');
  258.         $Payment $TargetOrder->getPayment();
  259.         if (empty($Payment)) {
  260.             return;
  261.         }
  262.         // 支払方法の取得
  263.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository->findOneBy([
  264.             'payment_type' => $this->eccubeConfig['np_payment_invoice']
  265.         ]);
  266.         // 支払方法が対象外
  267.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  268.             return;
  269.         }
  270.         // 新規受注登録は対象外
  271.         if (empty($TargetOrder->getId())) {
  272.             return;
  273.         }
  274.         $OrderResult $this->orderResultRepository->find($OriginOrder->getId());
  275.         $npStatusId $OrderResult->getOrderStatus();
  276.         $fromStatus $OriginOrder->getOrderStatus();
  277.         $toStatus $TargetOrder->getOrderStatus();
  278.         // ステータスに変更があった場合のみチェックする.
  279.         if ($fromStatus->getId() != $toStatus->getId()) {
  280.             if (!$this->canTransition($toStatus->getId(), $npStatusId)) {
  281.                 $this->utilService->addError(
  282.                         trans('admin.order.failed_to_change_status__short_np', [
  283.                         '%npStatus%' => $this->orderStatusService->getNpStatusName($npStatusId),
  284.                     ]), 'admin');
  285.                 header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $OriginOrder->getId()]));
  286.                 exit();
  287.             }
  288.         }
  289.         switch ($request->get('mode')) {
  290.             // 登録・編集処理
  291.             case 'register':
  292.                 $operation $this->checkOperation($TargetOrder$OriginOrder);
  293.                 break;
  294.             // 請求依頼処理
  295.             case 'billing':
  296.                 $TargetOrder->setDiscount($OriginOrder->getDiscount());
  297.                 $TargetOrder->setDeliveryFeeTotal($OriginOrder->getDeliveryFeeTotal());
  298.                 $TargetOrder->setCharge($OriginOrder->getCharge());
  299.                 $this->doBilling($TargetOrder);
  300.                 break;
  301.             // 再審査依頼処理
  302.             case 'reauthori':
  303.                 $this->doReauthori($TargetOrder);
  304.                 break;
  305.         }
  306.     }
  307.     /**
  308.      * 受注登録・編集処理完了
  309.      *
  310.      * @param EventArgs $event
  311.      */
  312.     public function onAdminOrderEditIndexComplete(EventArgs $event)
  313.     {
  314.         $OriginOrder $event->getArgument('OriginOrder');
  315.         $TargetOrder $event->getArgument('TargetOrder');
  316.         // 支払方法の取得
  317.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  318.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  319.         $Payment $TargetOrder->getPayment();
  320.         if (empty($Payment)) {
  321.             return false;
  322.         }
  323.         // 支払方法が対象外
  324.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  325.             return false;
  326.         }
  327.         // 新規受注登録の場合
  328.         if (empty($OriginOrder->getId())) {
  329.             // 取引登録依頼
  330.             $this->transactionsRegistrationsRequestsService
  331.                 ->postRequest($TargetOrder);
  332.             return;
  333.         }
  334.         // 処理判定
  335.         $operation $this->checkOperation($TargetOrder);
  336.         switch ($operation) {
  337.             // 取引登録依頼
  338.             case 1:
  339.                 $this->transactionsRegistrationsRequestsService
  340.                     ->postRequest($TargetOrder);
  341.                 break;
  342.             // 取引修正依頼
  343.             case 2:
  344.                 // 登録前後での変更有無を確認
  345.                 $originJson $this->transactionsModificationsRequestsService
  346.                     ->createRequestJson($OriginOrder);
  347.                 $targetJson $this->transactionsModificationsRequestsService
  348.                     ->createRequestJson($TargetOrder);
  349.                 if ($originJson == $targetJson) {
  350.                     // 変更は無いので取引修正依頼は行わない
  351.                     return;
  352.                 }
  353.                 $this->transactionsModificationsRequestsService
  354.                     ->postRequest($TargetOrder);
  355.                 break;
  356.             // 取引キャンセル依頼
  357.             case 3:
  358.                 $this->transactionsCancelRequestsService
  359.                     ->postRequest($TargetOrder);
  360.                 break;
  361.             // 処理無し
  362.             default:
  363.                 // nop
  364.         }
  365.     }
  366.     /**
  367.      * 指定ステータスに遷移できるかどうかを判定.
  368.      *
  369.      * @param $to 遷移先ステータス
  370.      * @param $npStatus NPステータス
  371.      * 
  372.      * @return boolean 指定ステータスに遷移できる場合はtrue
  373.      */
  374.     private function canTransition($to$npStatus)
  375.     {
  376.         $ngNpStatuses = [
  377.             $this->eccubeConfig['np_status_transactions_registrations_ng'],
  378.             $this->eccubeConfig['np_status_transactions_authorizations_pd'],
  379.             $this->eccubeConfig['np_status_transactions_authorizations_ng'],
  380.             $this->eccubeConfig['np_status_transactions_modifications_ng'],
  381.             $this->eccubeConfig['np_status_transactions_cancel_ng'],
  382.             $this->eccubeConfig['np_status_billings_ng']
  383.         ];
  384.         if (in_array($npStatus$ngNpStatuses)) {
  385.             if ($to !== OrderStatus::CANCEL) {
  386.                 return false;
  387.             }
  388.         }
  389.         return true;
  390.     }
  391.     /**
  392.      * 請求依頼処理
  393.      *
  394.      * @param Order $Order
  395.      */
  396.     private function doBilling(Order $Order)
  397.     {
  398.         // 受注処理結果取得
  399.         $OrderResult $this->orderResultRepository->find($Order->getId());
  400.         if (empty($OrderResult)) {
  401.             $this->utilService->addError('admin.common.save_error''admin');
  402.             return;
  403.         }
  404.         // 請求依頼使用可否判定
  405.         if ($this->orderStatusService->canBilling($Order$OrderResult) === false) {
  406.             $this->utilService->addError('admin.order.billing.error''admin');
  407.             return;
  408.         }
  409.         // 請求依頼依頼
  410.         $result $this->billingsRequestsService
  411.             ->postRequest($Order);
  412.         if ($result) {
  413.             $this->utilService->addSuccess('admin.common.save_complete''admin');
  414.             // 受注ステータスの更新を画面に反映
  415.             header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  416.             exit();
  417.         }
  418.     }
  419.     /**
  420.      * 再審査依頼処理
  421.      *
  422.      * @param Order $Order
  423.      */
  424.     private function doReauthori(Order $Order)
  425.     {
  426.         // 受注処理結果取得
  427.         $OrderResult $this->orderResultRepository->find($Order->getId());
  428.         if (empty($OrderResult)) {
  429.             $this->utilService->addError('admin.common.save_error''admin');
  430.             return;
  431.         }
  432.         // 再審査依頼使用可否判定
  433.         if ($this->orderStatusService->canReauthori($Order$OrderResult) === false) {
  434.             $this->utilService->addError('admin.order.reauthori.error''admin');
  435.             return;
  436.         }
  437.         // 取引登録依頼
  438.         $result $this->transactionsRegistrationsRequestsService
  439.             ->postRequest($Order);
  440.         if ($result) {
  441.             $this->utilService->addSuccess('admin.common.save_complete''admin');
  442.             // リダイレクトするとflushされないのでスルー
  443. //            header('Location: ' . $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  444. //            exit();
  445.         }
  446.     }
  447.     /**
  448.      * 処理判定
  449.      *
  450.      * @param Order $Order
  451.      * @param Order|null $OriginOrder
  452.      * @param boolean $errorView
  453.      * @return integer -1:エラーあり 0:処理無し 1:取引登録依頼 2:取引修正依頼 3:取引キャンセル依頼
  454.      */
  455.     private function checkOperation(Order $OrderOrder $OriginOrder null$errorView true)
  456.     {
  457.         $operation 0;
  458.         // 支払方法の取得
  459.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  460.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  461.         $Payment $Order->getPayment();
  462.         if (empty($Payment)) {
  463.             return $operation;
  464.         }
  465.         // 支払方法をチェック
  466.         if (empty($OriginOrder)) {
  467.             if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  468.                 return $operation;
  469.             }
  470.         } else {
  471.             $OriginPayment $OriginOrder->getPayment();
  472.             if (empty($OriginPayment)) {
  473.                 return $operation;
  474.             }
  475.             if ($OriginPayment->getId() != $NpKakebaraiPayment->getId()) {
  476.                 return $operation;
  477.             }
  478.         }
  479.         // 受注結果取得
  480.         $OrderResult $this->orderResultRepository->find($Order->getId());
  481.         if (empty($OrderResult)) {
  482.             $status 0;
  483.         } else {
  484.             $status $OrderResult->getOrderStatus();
  485.         }
  486.         // キャンセル以外
  487.         if ($Order->getOrderStatus()->getId() != OrderStatus::CANCEL) {
  488.             switch ($status) {
  489.                 // 未登録
  490.                 case 0:
  491.                 // 取引登録NG
  492.                 case $this->eccubeConfig['np_status_transactions_registrations_ng']:
  493.                 // 取引キャンセルOK
  494.                 case $this->eccubeConfig['np_status_transactions_cancel_ok']:
  495.                     // 取引登録依頼処理
  496.                     $operation 1;
  497.                     break;
  498.                 // 取引審査OK
  499.                 case $this->eccubeConfig['np_status_transactions_authorizations_ok']:
  500.                 // 取引審査NG
  501.                 case $this->eccubeConfig['np_status_transactions_authorizations_ng']:
  502.                 // 請求依頼OK
  503.                 case $this->eccubeConfig['np_status_billings_ok']:
  504.                 // 取引修正NG
  505.                 case $this->eccubeConfig['np_status_transactions_modifications_ng']:
  506.                 // 取引審査保留
  507.                 case $this->eccubeConfig['np_status_transactions_authorizations_pd']:
  508.                     // 取引修正依頼処理
  509.                     $operation 2;
  510.                     break;
  511.                 // エラー
  512.                 default:
  513.                     if ($errorView) {
  514.                         $this->utilService->addError('admin.order.modification.error''admin');
  515.                         header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  516.                         exit();
  517.                     }
  518.                     $operation = -1;
  519.             }
  520.         }
  521.         return $operation;
  522.     }
  523.     /**
  524.      * キャンセル処理.
  525.      *
  526.      * @param Event $event
  527.      */
  528.     public function onCancel(Event $event)
  529.     {
  530.         /* @var Order $Order */
  531.         $Order $event->getSubject()->getOrder();
  532.         // 支払方法の取得
  533.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  534.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  535.         $Payment $Order->getPayment();
  536.         if (empty($Payment)) {
  537.             return;
  538.         }
  539.         // 支払方法をチェック
  540.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  541.             return;
  542.         }
  543.         // 受注結果取得
  544.         $OrderResult $this->orderResultRepository->find($Order->getId());
  545.         if (empty($OrderResult)) {
  546.             $status 0;
  547.         } else {
  548.             $status $OrderResult->getOrderStatus();
  549.         }
  550.         switch ($status) {
  551.             // 取引審査OK
  552.             case $this->eccubeConfig['np_status_transactions_authorizations_ok']:
  553.             // 取引審査NG
  554.             case $this->eccubeConfig['np_status_transactions_authorizations_ng']:
  555.             // 請求依頼OK
  556.             case $this->eccubeConfig['np_status_billings_ok']:
  557.             // 取引修正NG
  558.             case $this->eccubeConfig['np_status_transactions_modifications_ng']:
  559.             // 取引審査保留
  560.             case $this->eccubeConfig['np_status_transactions_authorizations_pd']:
  561.                 // 取引キャンセル依頼処理
  562.                 $this->transactionsCancelRequestsService
  563.                     ->postRequest($Order);
  564.                 break;
  565.         }
  566.     }
  567. }