app/Plugin/VeriTrans4G2/Resource/template/admin/Order/index.twig line 1

Open in your IDE?
  1. {#
  2. Copyright (c) 2018 VeriTrans Inc., a Digital Garage company. All rights reserved.
  3. http://www.veritrans.co.jp/
  4. #}
  5. <script>
  6. $(function() {
  7.     // 決済状況 列見出し名
  8.     var PAY_STATUS_COL_LABEL = '決済状況';
  9.     // 決済状況 追加先の列番号
  10.     var PAY_STATUS_COL_BEFORE_INDEX = 4;
  11.     // 決済状況 売上
  12.     var PAY_STATUS_CAPTURE = 2;
  13.     // 決済状況 取消
  14.     var PAY_STATUS_CANCEL = 3;
  15.     var shippingInfoMap = JSON.parse('{{ vt4g.shippingInfoMap|json_encode(constant('JSON_UNESCAPED_UNICODE'))|raw }}');
  16.     var payStatusLabel  = JSON.parse('{{ vt4g.payStatusLabel|json_encode(constant('JSON_UNESCAPED_UNICODE'))|raw }}');
  17.     // 注文一覧テーブル要素
  18.     var $orderTable       = $('#search_result');
  19.     var $orderTableHeader = $orderTable.find('thead');
  20.     var $orderTableBody   = $orderTable.find('tbody');
  21.     // テーブルヘッダ行に決済状況列を追加
  22.     $orderTableHeader.find('th:eq('+(PAY_STATUS_COL_BEFORE_INDEX-1)+')').after('<th class="border-top-0 pt-2 pb-2 text-center text-nowrap">'+PAY_STATUS_COL_LABEL+'</th>');
  23.     // 注文レコードに決済状況列を追加
  24.     var $orderRowList = $orderTableBody.find('tr');
  25.     $.each($orderRowList, function(idx, row) {
  26.         var $row          = $(row);
  27.         var $idCheckbox   = $row.find('input[name="ids[]"]');
  28.         var shippingId    = $idCheckbox.val();
  29.         var orderId       = shippingInfoMap[shippingId]['orderId'];
  30.         var captureUrl    = '{{ url('vt4g_admin_order_edit_capture', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
  31.         var cancelUrl     = '{{ url('vt4g_admin_order_edit_cancel', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
  32.         var rollbackUrl   = '{{ url('vt4g_admin_order_edit_rollback', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
  33.         var payStatusCode = shippingInfoMap[shippingId]['payStatus'] || null;
  34.         var payStatus     = payStatusCode
  35.             ? payStatusLabel[payStatusCode]
  36.             : '';
  37.         $idCheckbox.attr({
  38.             'data-order-id': orderId,
  39.             'data-capture-url': captureUrl,
  40.             'data-cancel-url': cancelUrl,
  41.             'data-rollback-url': rollbackUrl
  42.         });
  43.         $row.find('td:eq('+(PAY_STATUS_COL_BEFORE_INDEX-1)+')').after('<td class="align-middle text-center">'+payStatus+'</td>');
  44.     });
  45.     {# ↓EC-CUBE本体側 confirmationModal_js.twig(L41-)を参考に実装 ↓ #}
  46.     var BulkPayStatusUpdate = function(modal, eventTarget) {
  47.         ConfirmationModal.call(this, modal);
  48.         this.eventTarget = eventTarget;
  49.     };
  50.     // extend super class
  51.     BulkPayStatusUpdate.prototype = Object.create(ConfirmationModal.prototype, {
  52.         constructor: {
  53.             value: ConfirmationModal
  54.         },
  55.         modalTitle: {
  56.             value: '決済状況の変更'
  57.         },
  58.         getTotalCount: {
  59.             value: function() {
  60.                 var $checkedList = $('input[data-id]:checked');
  61.                 // 注文IDの重複を除外
  62.                 var orderIdList = $checkedList.get().reduce(function(acc, val, idx) {
  63.                     var orderId = val.getAttribute('data-order-id');
  64.                     if (acc.indexOf(orderId) === -1) {
  65.                         acc.push(orderId);
  66.                     }
  67.                     return acc;
  68.                 }, []);
  69.                 return orderIdList.length;
  70.             }
  71.         },
  72.         progress: {
  73.             value: function(result, progress) {
  74.                 // プログレスバーの更新
  75.                 $('.progress-bar', this.modal).css('width', (++this.currentCount / this.totalCount * 100) + '%');
  76.                 if (result['message']) {
  77.                     var badge = (result['status'] == 'OK')
  78.                         ? '<span class="badge bg-success text-dark">OK</span>'
  79.                         : '<span class="badge bg-warning text-dark">NOTICE</span>';
  80.                     $('<li>'+badge+' </li>')
  81.                         .append($('<span></span>').text(result['message']))
  82.                         .appendTo('#bulkErrors');
  83.                 }
  84.                 if (this.currentCount >= this.totalCount) {
  85.                     progress.resolve();
  86.                 }
  87.             }
  88.         },
  89.         getPromises: {
  90.             value: function(progress) {
  91.                 var requestOrderIdList = [];
  92.                 var promises = [];
  93.                 // チェックされているチェックボックスごとに処理を行う
  94.                 $('input[data-id]:checked').each(function(idx, elm) {
  95.                     var orderId = elm.getAttribute('data-order-id');
  96.                     var url = ($('#vt4g_option_bulk_pay_status').val() == PAY_STATUS_CAPTURE)
  97.                         ? elm.getAttribute('data-capture-url')
  98.                         : elm.getAttribute('data-cancel-url');
  99.                     var params = {bulk: true};
  100.                     // 既にリクエストが行われている注文の場合はスキップ
  101.                     if (requestOrderIdList.indexOf(orderId) !== -1) {
  102.                         return;
  103.                     }
  104.                     // リクエストを行う注文IDを保持
  105.                     requestOrderIdList.push(orderId);
  106.                     var promise = $.post(url, params)
  107.                         .fail(function(err) {
  108.                             progress.reject();
  109.                             ConfirmationModal.prototype.fail.call(this);
  110.                         })
  111.                         .always(function(data) {
  112.                             progress.notifyWith(data);
  113.                         });
  114.                     promises.push(promise);
  115.                 });
  116.                 return promises;
  117.             }
  118.         }
  119.     });
  120.     // プログレスバーの表示を制御
  121.     var handleClickUpdatePayStatus = function(e) {
  122.         var eventTarget = $(e.currentTarget);
  123.         var modal = $('#sentUpdateModal');
  124.         if (!$('#vt4g_option_bulk_pay_status').val()) {
  125.             alert('決済状況を選択してください');
  126.             return;
  127.         }
  128.         updater = new BulkPayStatusUpdate(modal, eventTarget);
  129.         var bootstrapModal = new bootstrap.Modal(modal.get(0));
  130.         bootstrapModal.show();
  131.         modal.find('.modal-title').text(updater.modalTitle);
  132.         modal.find('.modal-body > p.modal-message').text("{{ 'admin.order.bulk_action__in_progress_message'|trans }}");
  133.         modal.find('button').hide();
  134.         $('#bulk-options').hide();
  135.         modal.find('.progress').show();
  136.         updater.totalCount = updater.getTotalCount();
  137.         var progress = new $.Deferred();
  138.         progress.progress(function() {
  139.             updater.progress(this, progress);
  140.         })
  141.         .fail(function() {
  142.             updater.fail(this);
  143.         })
  144.         .always(function() {
  145.             updater.always(this);
  146.         });
  147.         updater.getPromises(progress);
  148.     };
  149.     {# ↑EC-CUBE本体側 confirmationModal_js.twig(L41-)を参考に実装 ↑ #}
  150.     // 決済状況更新用のプルダウン追加
  151.     var $target = $('#btn_bulk_status').parents('.btn-bulk-wrapper');
  152.     var contents =
  153.         '<div class="col btn-bulk-wrapper d-none">'+
  154.         '   <div class="d-inline-block mr-2">'+
  155.         '       <select class="form-select" id="vt4g_option_bulk_pay_status">'+
  156.         '           <option value="" selected>決済状況の変更</option>'+
  157.                     {% for value, label in vt4g.controlPayStatusMap %}
  158.         '                <option value="{{ value }}">{{ label }}</option>'+
  159.                     {% endfor %}
  160.         '       </select>'+
  161.         '   </div>'+
  162.         '   <button type="button" id="vt4g_btn_bulk_pay_status" class="btn btn-ec-regular mr-2 progressModal" data-type="status" data-bulk-update="true" data-submit="bulk">'+
  163.         '       決定'+
  164.         '   </button>'+
  165.         '</div>';
  166.     $target.css('max-width', '240px').after(contents);
  167.     $target.addClass('col-auto');
  168.     $('#vt4g_btn_bulk_pay_status').on('click', handleClickUpdatePayStatus);
  169.     // ロールバック処理
  170.     function BulkRollbackOrder(modal, relatedTarget) {
  171.         ConfirmationModal.call(this, modal, relatedTarget);
  172.     }
  173.     // extends ConfirmationModal
  174.     BulkRollbackOrder.prototype = Object.create(ConfirmationModal.prototype, {
  175.         constructor: {
  176.             value: ConfirmationModal
  177.         },
  178.         modalTitle: {
  179.             value: "{{ 'vt4g_plugin.admin.order.rollback.modal.title'|trans }}"
  180.         },
  181.         modalMessage: {
  182.             value: "{{ 'vt4g_plugin.admin.order.rollback.modal.message'|trans }}"
  183.         },
  184.         modalButton: {
  185.             value: "{{ 'vt4g_plugin.admin.order.rollback.modal.button'|trans }}"
  186.         },
  187.         getTotalCount: {
  188.             value: function() {
  189.                 var $checkedList = $('input[data-id]:checked');
  190.                 // 注文IDの重複を除外
  191.                 var orderIdList = $checkedList.get().reduce(function(acc, val, idx) {
  192.                     var orderId = val.getAttribute('data-order-id');
  193.                     if (acc.indexOf(orderId) === -1) {
  194.                         acc.push(orderId);
  195.                     }
  196.                     return acc;
  197.                 }, []);
  198.                 return orderIdList.length;
  199.             }
  200.         },
  201.         progress: {
  202.             value: function(result, progress) {
  203.                 // プログレスバーの更新
  204.                 $('.progress-bar', this.modal).css('width', (++this.currentCount / this.totalCount * 100) + '%');
  205.                 if (result['message']) {
  206.                     var badge = (result['status'] == 'OK')
  207.                         ? '<span class="badge bg-success text-dark">OK</span>'
  208.                         : '<span class="badge bg-warning text-dark">NOTICE</span>';
  209.                     $('<li>'+badge+' </li>')
  210.                         .append($('<span></span>').text(result['message']))
  211.                         .appendTo('#bulkVt4gRollbackResult');
  212.                 }
  213.                 if (this.currentCount >= this.totalCount) {
  214.                     progress.resolve();
  215.                     $('#bulkVt4gRollbackComplete').show();
  216.                 }
  217.             }
  218.         },
  219.         getPromises: {
  220.             value: function(progress) {
  221.                 var requestOrderIdList = [];
  222.                 var postList = [];
  223.                 // チェックされているチェックボックスごとに処理を行う
  224.                 $('input[data-id]:checked').each(function(idx, elm) {
  225.                     var orderId = elm.getAttribute('data-order-id');
  226.                     // 既にリクエストが行われている注文の場合はスキップ
  227.                     if (requestOrderIdList.indexOf(orderId) !== -1) {
  228.                         return;
  229.                     }
  230.                     requestOrderIdList.push(orderId);
  231.                     postList.push({
  232.                         'url': elm.getAttribute('data-rollback-url')
  233.                     });
  234.                 });
  235.                 // confirmationModal_js.twigのBulkStatusUpdateにあるコメントに倣って同期処理で実行
  236.                 var callback = function () {
  237.                     var urls = postList.shift();
  238.                     var url = urls.url;
  239.                     var params = {bulk: true};
  240.                     $.post(url, params)
  241.                         .done(function () {
  242.                             if (postList.length) {
  243.                                 callback();
  244.                             }
  245.                         })
  246.                         .fail(function(err) {
  247.                             progress.reject();
  248.                             ConfirmationModal.prototype.fail.call(this);
  249.                         })
  250.                         .always(function(data) {
  251.                             progress.notifyWith(data);
  252.                         });
  253.                 }
  254.                 callback();
  255.             }
  256.         }
  257.     });
  258.     // ロールバック処理用モーダルの表示を制御
  259.     var handleClickRollbackOrder = function(e) {
  260.         var eventTarget = $(e.currentTarget);
  261.         var modal = $('#vt4gRollbackOrderModal');
  262.         updater = new BulkRollbackOrder(modal, eventTarget);
  263.         var bootstrapModal = new bootstrap.Modal(modal.get(0));
  264.         bootstrapModal.show();
  265.         var eventTarget = $(e.currentTarget);
  266.         $('.modal-title', modal).text(updater.modalTitle);
  267.         $('.modal-body > p.modal-message', modal).text(updater.modalMessage);
  268.         $('#bulkVt4gRollback')
  269.                 .attr({
  270.                     'data-bulk-update': eventTarget.data('bulk-update'),
  271.                     'data-type': eventTarget.data('type'),
  272.                     'data-update-status-url': eventTarget.data('update-status-url'),
  273.                     'data-notify-mail-url': eventTarget.data('notify-mail-url'),
  274.                     'data-update-status-id': eventTarget.data('update-status-id')
  275.                 })
  276.                 .text(updater.modalButton);
  277.     }
  278.     // ロールバック処理用プログレスバーの表示を制御
  279.     var submitRollbackOrder = function(e) {
  280.         var eventTarget = $(e.currentTarget);
  281.         var modal = $('#vt4gRollbackOrderModal');
  282.         updater = new BulkRollbackOrder(modal, eventTarget);
  283.         var bootstrapModal = new bootstrap.Modal(modal.get(0));
  284.         bootstrapModal.show();
  285.         modal.find('.modal-title').text(updater.modalTitle);
  286.         modal.find('.modal-body > p.modal-message').text("{{ 'admin.order.bulk_action__in_progress_message'|trans }}");
  287.         modal.find('button').hide();
  288.         $('#bulk-options').hide();
  289.         modal.find('.progress').show();
  290.         updater.totalCount = updater.getTotalCount();
  291.         var progress = new $.Deferred();
  292.         progress.progress(function() {
  293.             updater.progress(this, progress);
  294.         })
  295.         .fail(function() {
  296.             updater.fail(this);
  297.         })
  298.         .always(function() {
  299.             updater.always(this);
  300.         });
  301.         updater.getPromises(progress);
  302.     }
  303.     var $rollbackButtonTarget = $('#vt4g_btn_bulk_pay_status').parents('.btn-bulk-wrapper');
  304.     var rollbackContents =
  305.         '<div class="col btn-bulk-wrapper d-none">'+
  306.         '   <button type="button" id="vt4g_btn_bulk_rollback_order" class="btn btn-ec-regular mr-2 progressModal" data-type="status" data-bulk-update="true" data-submit="bulk">'+
  307.         '       ベリトランス4G ロールバック'+
  308.         '   </button>'+
  309.         '</div>';
  310.     $rollbackButtonTarget.css('max-width', '240px').after(rollbackContents);
  311.     var $rollbackModalTarget = $('#bulkDeleteModal').parents('.c-primaryCol');
  312.     var rollbackModalContents =
  313.         '<div class="modal fade" id="vt4gRollbackOrderModal" tabindex="-1" role="dialog" aria-labelledby="vt4gRollbackOrderModal" aria-hidden="true" data-bs-keyboard="false" data-bs-backdrop="static">'+
  314.         '    <div class="modal-dialog modal-lg" role="document">'+
  315.         '        <div class="modal-content">'+
  316.         '            <div class="modal-header">'+
  317.         '                <h5 class="modal-title fw-bold"><!--index.twig--></h5>'+
  318.         '                <button class="btn-close" type="button" data-bs-dismiss="modal" aria-label="Close"></button>'+
  319.         '            </div>'+
  320.         '            <div class="modal-body">'+
  321.         '                <p class="modal-message"></p>'+
  322.         '                <ul id="bulkVt4gRollbackResult"></ul>'+
  323.         '                <div class="progress" style="display: none">'+
  324.         '                    <div class="progress-bar progress-bar-striped progress-bar-animated" role="progressbar" style="width: 0%" aria-valuenow="0" aria-valuemin="0" aria-valuemax="100"></div>'+
  325.         '                </div>'+
  326.         '            </div>'+
  327.         '            <div class="modal-footer">'+
  328.         '                <button class="btn btn-ec-sub" type="button" data-bs-dismiss="modal">{{ "admin.common.cancel"|trans }}</button>'+
  329.         '                <button id="bulkVt4gRollback" class="btn btn-ec-conversion" type="button"><!--index.twig--></button>'+
  330.         '                <button id="bulkVt4gRollbackComplete" class="btn btn-ec-regular" style="display: none" type="button">{{ "admin.common.close"|trans }}</button>'+
  331.         '            </div>'+
  332.         '        </div>'+
  333.         '    </div>'+
  334.         '</div>';
  335.     $rollbackModalTarget.after(rollbackModalContents);
  336.     $('#vt4g_btn_bulk_rollback_order').on('click', handleClickRollbackOrder);
  337.     $('#bulkVt4gRollback').on('click', submitRollbackOrder);
  338.     // ロールバック処理終了後の閉じるボタン
  339.     $('#bulkVt4gRollbackComplete').on('click', function() {
  340.         location.href = '{{ url('admin_order', { 'resume': 1 }) }}';
  341.     });
  342. });
  343. </script>