app/Plugin/npka421/NpKakebaraiEvent.php line 600

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.         // 新規受注登録は対象外
  259.         if (empty($TargetOrder->getId())) {
  260.             return;
  261.         }
  262.         $OrderResult $this->orderResultRepository->find($OriginOrder->getId());
  263.         $npStatusId $OrderResult->getOrderStatus();
  264.         $fromStatus $OriginOrder->getOrderStatus();
  265.         $toStatus $TargetOrder->getOrderStatus();
  266.         // ステータスに変更があった場合のみチェックする.
  267.         if ($fromStatus->getId() != $toStatus->getId()) {
  268.             if (!$this->canTransition($toStatus->getId(), $npStatusId)) {
  269.                 $this->utilService->addError(
  270.                         trans('admin.order.failed_to_change_status__short_np', [
  271.                         '%npStatus%' => $this->orderStatusService->getNpStatusName($npStatusId),
  272.                     ]), 'admin');
  273.                 header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $OriginOrder->getId()]));
  274.                 exit();
  275.             }
  276.         }
  277.         switch ($request->get('mode')) {
  278.             // 登録・編集処理
  279.             case 'register':
  280.                 $operation $this->checkOperation($TargetOrder$OriginOrder);
  281.                 break;
  282.             // 請求依頼処理
  283.             case 'billing':
  284.                 $TargetOrder->setDiscount($OriginOrder->getDiscount());
  285.                 $TargetOrder->setDeliveryFeeTotal($OriginOrder->getDeliveryFeeTotal());
  286.                 $TargetOrder->setCharge($OriginOrder->getCharge());
  287.                 $this->doBilling($TargetOrder);
  288.                 break;
  289.             // 再審査依頼処理
  290.             case 'reauthori':
  291.                 $this->doReauthori($TargetOrder);
  292.                 break;
  293.         }
  294.     }
  295.     /**
  296.      * 受注登録・編集処理完了
  297.      *
  298.      * @param EventArgs $event
  299.      */
  300.     public function onAdminOrderEditIndexComplete(EventArgs $event)
  301.     {
  302.         $OriginOrder $event->getArgument('OriginOrder');
  303.         $TargetOrder $event->getArgument('TargetOrder');
  304.         // 支払方法の取得
  305.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  306.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  307.         $Payment $TargetOrder->getPayment();
  308.         if (empty($Payment)) {
  309.             return false;
  310.         }
  311.         // 支払方法が対象外
  312.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  313.             return false;
  314.         }
  315.         // 新規受注登録の場合
  316.         if (empty($OriginOrder->getId())) {
  317.             // 取引登録依頼
  318.             $this->transactionsRegistrationsRequestsService
  319.                 ->postRequest($TargetOrder);
  320.             return;
  321.         }
  322.         // 処理判定
  323.         $operation $this->checkOperation($TargetOrder);
  324.         switch ($operation) {
  325.             // 取引登録依頼
  326.             case 1:
  327.                 $this->transactionsRegistrationsRequestsService
  328.                     ->postRequest($TargetOrder);
  329.                 break;
  330.             // 取引修正依頼
  331.             case 2:
  332.                 // 登録前後での変更有無を確認
  333.                 $originJson $this->transactionsModificationsRequestsService
  334.                     ->createRequestJson($OriginOrder);
  335.                 $targetJson $this->transactionsModificationsRequestsService
  336.                     ->createRequestJson($TargetOrder);
  337.                 if ($originJson == $targetJson) {
  338.                     // 変更は無いので取引修正依頼は行わない
  339.                     return;
  340.                 }
  341.                 $this->transactionsModificationsRequestsService
  342.                     ->postRequest($TargetOrder);
  343.                 break;
  344.             // 取引キャンセル依頼
  345.             case 3:
  346.                 $this->transactionsCancelRequestsService
  347.                     ->postRequest($TargetOrder);
  348.                 break;
  349.             // 処理無し
  350.             default:
  351.                 // nop
  352.         }
  353.     }
  354.     /**
  355.      * 指定ステータスに遷移できるかどうかを判定.
  356.      *
  357.      * @param $to 遷移先ステータス
  358.      * @param $npStatus NPステータス
  359.      * 
  360.      * @return boolean 指定ステータスに遷移できる場合はtrue
  361.      */
  362.     private function canTransition($to$npStatus)
  363.     {
  364.         $ngNpStatuses = [
  365.             $this->eccubeConfig['np_status_transactions_registrations_ng'],
  366.             $this->eccubeConfig['np_status_transactions_authorizations_pd'],
  367.             $this->eccubeConfig['np_status_transactions_authorizations_ng'],
  368.             $this->eccubeConfig['np_status_transactions_modifications_ng'],
  369.             $this->eccubeConfig['np_status_transactions_cancel_ng'],
  370.             $this->eccubeConfig['np_status_billings_ng']
  371.         ];
  372.         if (in_array($npStatus$ngNpStatuses)) {
  373.             if ($to !== OrderStatus::CANCEL) {
  374.                 return false;
  375.             }
  376.         }
  377.         return true;
  378.     }
  379.     /**
  380.      * 請求依頼処理
  381.      *
  382.      * @param Order $Order
  383.      */
  384.     private function doBilling(Order $Order)
  385.     {
  386.         // 受注処理結果取得
  387.         $OrderResult $this->orderResultRepository->find($Order->getId());
  388.         if (empty($OrderResult)) {
  389.             $this->utilService->addError('admin.common.save_error''admin');
  390.             return;
  391.         }
  392.         // 請求依頼使用可否判定
  393.         if ($this->orderStatusService->canBilling($Order$OrderResult) === false) {
  394.             $this->utilService->addError('admin.order.billing.error''admin');
  395.             return;
  396.         }
  397.         // 請求依頼依頼
  398.         $result $this->billingsRequestsService
  399.             ->postRequest($Order);
  400.         if ($result) {
  401.             $this->utilService->addSuccess('admin.common.save_complete''admin');
  402.             // 受注ステータスの更新を画面に反映
  403.             header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  404.             exit();
  405.         }
  406.     }
  407.     /**
  408.      * 再審査依頼処理
  409.      *
  410.      * @param Order $Order
  411.      */
  412.     private function doReauthori(Order $Order)
  413.     {
  414.         // 受注処理結果取得
  415.         $OrderResult $this->orderResultRepository->find($Order->getId());
  416.         if (empty($OrderResult)) {
  417.             $this->utilService->addError('admin.common.save_error''admin');
  418.             return;
  419.         }
  420.         // 再審査依頼使用可否判定
  421.         if ($this->orderStatusService->canReauthori($Order$OrderResult) === false) {
  422.             $this->utilService->addError('admin.order.reauthori.error''admin');
  423.             return;
  424.         }
  425.         // 取引登録依頼
  426.         $result $this->transactionsRegistrationsRequestsService
  427.             ->postRequest($Order);
  428.         if ($result) {
  429.             $this->utilService->addSuccess('admin.common.save_complete''admin');
  430.             // リダイレクトするとflushされないのでスルー
  431. //            header('Location: ' . $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  432. //            exit();
  433.         }
  434.     }
  435.     /**
  436.      * 処理判定
  437.      *
  438.      * @param Order $Order
  439.      * @param Order|null $OriginOrder
  440.      * @param boolean $errorView
  441.      * @return integer -1:エラーあり 0:処理無し 1:取引登録依頼 2:取引修正依頼 3:取引キャンセル依頼
  442.      */
  443.     private function checkOperation(Order $OrderOrder $OriginOrder null$errorView true)
  444.     {
  445.         $operation 0;
  446.         // 支払方法の取得
  447.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  448.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  449.         $Payment $Order->getPayment();
  450.         if (empty($Payment)) {
  451.             return $operation;
  452.         }
  453.         // 支払方法をチェック
  454.         if (empty($OriginOrder)) {
  455.             if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  456.                 return $operation;
  457.             }
  458.         } else {
  459.             $OriginPayment $OriginOrder->getPayment();
  460.             if (empty($OriginPayment)) {
  461.                 return $operation;
  462.             }
  463.             if ($OriginPayment->getId() != $NpKakebaraiPayment->getId()) {
  464.                 return $operation;
  465.             }
  466.         }
  467.         // 受注結果取得
  468.         $OrderResult $this->orderResultRepository->find($Order->getId());
  469.         if (empty($OrderResult)) {
  470.             $status 0;
  471.         } else {
  472.             $status $OrderResult->getOrderStatus();
  473.         }
  474.         // キャンセル以外
  475.         if ($Order->getOrderStatus()->getId() != OrderStatus::CANCEL) {
  476.             switch ($status) {
  477.                 // 未登録
  478.                 case 0:
  479.                 // 取引登録NG
  480.                 case $this->eccubeConfig['np_status_transactions_registrations_ng']:
  481.                 // 取引キャンセルOK
  482.                 case $this->eccubeConfig['np_status_transactions_cancel_ok']:
  483.                     // 取引登録依頼処理
  484.                     $operation 1;
  485.                     break;
  486.                 // 取引審査OK
  487.                 case $this->eccubeConfig['np_status_transactions_authorizations_ok']:
  488.                 // 取引審査NG
  489.                 case $this->eccubeConfig['np_status_transactions_authorizations_ng']:
  490.                 // 請求依頼OK
  491.                 case $this->eccubeConfig['np_status_billings_ok']:
  492.                 // 取引修正NG
  493.                 case $this->eccubeConfig['np_status_transactions_modifications_ng']:
  494.                 // 取引審査保留
  495.                 case $this->eccubeConfig['np_status_transactions_authorizations_pd']:
  496.                     // 取引修正依頼処理
  497.                     $operation 2;
  498.                     break;
  499.                 // エラー
  500.                 default:
  501.                     if ($errorView) {
  502.                         $this->utilService->addError('admin.order.modification.error''admin');
  503.                         header('Location: ' $this->utilService->generateUrl('admin_order_edit', ['id' => $Order->getId()]));
  504.                         exit();
  505.                     }
  506.                     $operation = -1;
  507.             }
  508.         }
  509.         return $operation;
  510.     }
  511.     /**
  512.      * キャンセル処理.
  513.      *
  514.      * @param Event $event
  515.      */
  516.     public function onCancel(Event $event)
  517.     {
  518.         /* @var Order $Order */
  519.         $Order $event->getSubject()->getOrder();
  520.         // 支払方法の取得
  521.         $NpKakebaraiPayment $this->npKakebaraiPaymentRepository
  522.             ->findOneBy(['payment_type' => $this->eccubeConfig['np_payment_invoice']]);
  523.         $Payment $Order->getPayment();
  524.         if (empty($Payment)) {
  525.             return;
  526.         }
  527.         // 支払方法をチェック
  528.         if ($Payment->getId() != $NpKakebaraiPayment->getId()) {
  529.             return;
  530.         }
  531.         // 受注結果取得
  532.         $OrderResult $this->orderResultRepository->find($Order->getId());
  533.         if (empty($OrderResult)) {
  534.             $status 0;
  535.         } else {
  536.             $status $OrderResult->getOrderStatus();
  537.         }
  538.         switch ($status) {
  539.             // 取引審査OK
  540.             case $this->eccubeConfig['np_status_transactions_authorizations_ok']:
  541.             // 取引審査NG
  542.             case $this->eccubeConfig['np_status_transactions_authorizations_ng']:
  543.             // 請求依頼OK
  544.             case $this->eccubeConfig['np_status_billings_ok']:
  545.             // 取引修正NG
  546.             case $this->eccubeConfig['np_status_transactions_modifications_ng']:
  547.             // 取引審査保留
  548.             case $this->eccubeConfig['np_status_transactions_authorizations_pd']:
  549.                 // 取引キャンセル依頼処理
  550.                 $this->transactionsCancelRequestsService
  551.                     ->postRequest($Order);
  552.                 break;
  553.         }
  554.     }
  555. }