{#
Copyright (c) 2018 VeriTrans Inc., a Digital Garage company. All rights reserved.
http://www.veritrans.co.jp/
#}
<script>
$(function() {
// 決済状況 列見出し名
var PAY_STATUS_COL_LABEL = '決済状況';
// 決済状況 追加先の列番号
var PAY_STATUS_COL_BEFORE_INDEX = 4;
// 決済状況 売上
var PAY_STATUS_CAPTURE = 2;
// 決済状況 取消
var PAY_STATUS_CANCEL = 3;
var shippingInfoMap = JSON.parse('{{ vt4g.shippingInfoMap|json_encode(constant('JSON_UNESCAPED_UNICODE'))|raw }}');
var payStatusLabel = JSON.parse('{{ vt4g.payStatusLabel|json_encode(constant('JSON_UNESCAPED_UNICODE'))|raw }}');
// 注文一覧テーブル要素
var $orderTable = $('#search_result');
var $orderTableHeader = $orderTable.find('thead');
var $orderTableBody = $orderTable.find('tbody');
// テーブルヘッダ行に決済状況列を追加
$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>');
// 注文レコードに決済状況列を追加
var $orderRowList = $orderTableBody.find('tr');
$.each($orderRowList, function(idx, row) {
var $row = $(row);
var $idCheckbox = $row.find('input[name="ids[]"]');
var shippingId = $idCheckbox.val();
var orderId = shippingInfoMap[shippingId]['orderId'];
var captureUrl = '{{ url('vt4g_admin_order_edit_capture', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
var cancelUrl = '{{ url('vt4g_admin_order_edit_cancel', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
var rollbackUrl = '{{ url('vt4g_admin_order_edit_rollback', {orderId: '0'}) }}'.replace('vt4g_edit/0', 'vt4g_edit/'+orderId);
var payStatusCode = shippingInfoMap[shippingId]['payStatus'] || null;
var payStatus = payStatusCode
? payStatusLabel[payStatusCode]
: '';
$idCheckbox.attr({
'data-order-id': orderId,
'data-capture-url': captureUrl,
'data-cancel-url': cancelUrl,
'data-rollback-url': rollbackUrl
});
$row.find('td:eq('+(PAY_STATUS_COL_BEFORE_INDEX-1)+')').after('<td class="align-middle text-center">'+payStatus+'</td>');
});
{# ↓EC-CUBE本体側 confirmationModal_js.twig(L41-)を参考に実装 ↓ #}
var BulkPayStatusUpdate = function(modal, eventTarget) {
ConfirmationModal.call(this, modal);
this.eventTarget = eventTarget;
};
// extend super class
BulkPayStatusUpdate.prototype = Object.create(ConfirmationModal.prototype, {
constructor: {
value: ConfirmationModal
},
modalTitle: {
value: '決済状況の変更'
},
getTotalCount: {
value: function() {
var $checkedList = $('input[data-id]:checked');
// 注文IDの重複を除外
var orderIdList = $checkedList.get().reduce(function(acc, val, idx) {
var orderId = val.getAttribute('data-order-id');
if (acc.indexOf(orderId) === -1) {
acc.push(orderId);
}
return acc;
}, []);
return orderIdList.length;
}
},
progress: {
value: function(result, progress) {
// プログレスバーの更新
$('.progress-bar', this.modal).css('width', (++this.currentCount / this.totalCount * 100) + '%');
if (result['message']) {
var badge = (result['status'] == 'OK')
? '<span class="badge bg-success text-dark">OK</span>'
: '<span class="badge bg-warning text-dark">NOTICE</span>';
$('<li>'+badge+' </li>')
.append($('<span></span>').text(result['message']))
.appendTo('#bulkErrors');
}
if (this.currentCount >= this.totalCount) {
progress.resolve();
}
}
},
getPromises: {
value: function(progress) {
var requestOrderIdList = [];
var promises = [];
// チェックされているチェックボックスごとに処理を行う
$('input[data-id]:checked').each(function(idx, elm) {
var orderId = elm.getAttribute('data-order-id');
var url = ($('#vt4g_option_bulk_pay_status').val() == PAY_STATUS_CAPTURE)
? elm.getAttribute('data-capture-url')
: elm.getAttribute('data-cancel-url');
var params = {bulk: true};
// 既にリクエストが行われている注文の場合はスキップ
if (requestOrderIdList.indexOf(orderId) !== -1) {
return;
}
// リクエストを行う注文IDを保持
requestOrderIdList.push(orderId);
var promise = $.post(url, params)
.fail(function(err) {
progress.reject();
ConfirmationModal.prototype.fail.call(this);
})
.always(function(data) {
progress.notifyWith(data);
});
promises.push(promise);
});
return promises;
}
}
});
// プログレスバーの表示を制御
var handleClickUpdatePayStatus = function(e) {
var eventTarget = $(e.currentTarget);
var modal = $('#sentUpdateModal');
if (!$('#vt4g_option_bulk_pay_status').val()) {
alert('決済状況を選択してください');
return;
}
updater = new BulkPayStatusUpdate(modal, eventTarget);
var bootstrapModal = new bootstrap.Modal(modal.get(0));
bootstrapModal.show();
modal.find('.modal-title').text(updater.modalTitle);
modal.find('.modal-body > p.modal-message').text("{{ 'admin.order.bulk_action__in_progress_message'|trans }}");
modal.find('button').hide();
$('#bulk-options').hide();
modal.find('.progress').show();
updater.totalCount = updater.getTotalCount();
var progress = new $.Deferred();
progress.progress(function() {
updater.progress(this, progress);
})
.fail(function() {
updater.fail(this);
})
.always(function() {
updater.always(this);
});
updater.getPromises(progress);
};
{# ↑EC-CUBE本体側 confirmationModal_js.twig(L41-)を参考に実装 ↑ #}
// 決済状況更新用のプルダウン追加
var $target = $('#btn_bulk_status').parents('.btn-bulk-wrapper');
var contents =
'<div class="col btn-bulk-wrapper d-none">'+
' <div class="d-inline-block mr-2">'+
' <select class="form-select" id="vt4g_option_bulk_pay_status">'+
' <option value="" selected>決済状況の変更</option>'+
{% for value, label in vt4g.controlPayStatusMap %}
' <option value="{{ value }}">{{ label }}</option>'+
{% endfor %}
' </select>'+
' </div>'+
' <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">'+
' 決定'+
' </button>'+
'</div>';
$target.css('max-width', '240px').after(contents);
$target.addClass('col-auto');
$('#vt4g_btn_bulk_pay_status').on('click', handleClickUpdatePayStatus);
// ロールバック処理
function BulkRollbackOrder(modal, relatedTarget) {
ConfirmationModal.call(this, modal, relatedTarget);
}
// extends ConfirmationModal
BulkRollbackOrder.prototype = Object.create(ConfirmationModal.prototype, {
constructor: {
value: ConfirmationModal
},
modalTitle: {
value: "{{ 'vt4g_plugin.admin.order.rollback.modal.title'|trans }}"
},
modalMessage: {
value: "{{ 'vt4g_plugin.admin.order.rollback.modal.message'|trans }}"
},
modalButton: {
value: "{{ 'vt4g_plugin.admin.order.rollback.modal.button'|trans }}"
},
getTotalCount: {
value: function() {
var $checkedList = $('input[data-id]:checked');
// 注文IDの重複を除外
var orderIdList = $checkedList.get().reduce(function(acc, val, idx) {
var orderId = val.getAttribute('data-order-id');
if (acc.indexOf(orderId) === -1) {
acc.push(orderId);
}
return acc;
}, []);
return orderIdList.length;
}
},
progress: {
value: function(result, progress) {
// プログレスバーの更新
$('.progress-bar', this.modal).css('width', (++this.currentCount / this.totalCount * 100) + '%');
if (result['message']) {
var badge = (result['status'] == 'OK')
? '<span class="badge bg-success text-dark">OK</span>'
: '<span class="badge bg-warning text-dark">NOTICE</span>';
$('<li>'+badge+' </li>')
.append($('<span></span>').text(result['message']))
.appendTo('#bulkVt4gRollbackResult');
}
if (this.currentCount >= this.totalCount) {
progress.resolve();
$('#bulkVt4gRollbackComplete').show();
}
}
},
getPromises: {
value: function(progress) {
var requestOrderIdList = [];
var postList = [];
// チェックされているチェックボックスごとに処理を行う
$('input[data-id]:checked').each(function(idx, elm) {
var orderId = elm.getAttribute('data-order-id');
// 既にリクエストが行われている注文の場合はスキップ
if (requestOrderIdList.indexOf(orderId) !== -1) {
return;
}
requestOrderIdList.push(orderId);
postList.push({
'url': elm.getAttribute('data-rollback-url')
});
});
// confirmationModal_js.twigのBulkStatusUpdateにあるコメントに倣って同期処理で実行
var callback = function () {
var urls = postList.shift();
var url = urls.url;
var params = {bulk: true};
$.post(url, params)
.done(function () {
if (postList.length) {
callback();
}
})
.fail(function(err) {
progress.reject();
ConfirmationModal.prototype.fail.call(this);
})
.always(function(data) {
progress.notifyWith(data);
});
}
callback();
}
}
});
// ロールバック処理用モーダルの表示を制御
var handleClickRollbackOrder = function(e) {
var eventTarget = $(e.currentTarget);
var modal = $('#vt4gRollbackOrderModal');
updater = new BulkRollbackOrder(modal, eventTarget);
var bootstrapModal = new bootstrap.Modal(modal.get(0));
bootstrapModal.show();
var eventTarget = $(e.currentTarget);
$('.modal-title', modal).text(updater.modalTitle);
$('.modal-body > p.modal-message', modal).text(updater.modalMessage);
$('#bulkVt4gRollback')
.attr({
'data-bulk-update': eventTarget.data('bulk-update'),
'data-type': eventTarget.data('type'),
'data-update-status-url': eventTarget.data('update-status-url'),
'data-notify-mail-url': eventTarget.data('notify-mail-url'),
'data-update-status-id': eventTarget.data('update-status-id')
})
.text(updater.modalButton);
}
// ロールバック処理用プログレスバーの表示を制御
var submitRollbackOrder = function(e) {
var eventTarget = $(e.currentTarget);
var modal = $('#vt4gRollbackOrderModal');
updater = new BulkRollbackOrder(modal, eventTarget);
var bootstrapModal = new bootstrap.Modal(modal.get(0));
bootstrapModal.show();
modal.find('.modal-title').text(updater.modalTitle);
modal.find('.modal-body > p.modal-message').text("{{ 'admin.order.bulk_action__in_progress_message'|trans }}");
modal.find('button').hide();
$('#bulk-options').hide();
modal.find('.progress').show();
updater.totalCount = updater.getTotalCount();
var progress = new $.Deferred();
progress.progress(function() {
updater.progress(this, progress);
})
.fail(function() {
updater.fail(this);
})
.always(function() {
updater.always(this);
});
updater.getPromises(progress);
}
var $rollbackButtonTarget = $('#vt4g_btn_bulk_pay_status').parents('.btn-bulk-wrapper');
var rollbackContents =
'<div class="col btn-bulk-wrapper d-none">'+
' <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">'+
' ベリトランス4G ロールバック'+
' </button>'+
'</div>';
$rollbackButtonTarget.css('max-width', '240px').after(rollbackContents);
var $rollbackModalTarget = $('#bulkDeleteModal').parents('.c-primaryCol');
var rollbackModalContents =
'<div class="modal fade" id="vt4gRollbackOrderModal" tabindex="-1" role="dialog" aria-labelledby="vt4gRollbackOrderModal" aria-hidden="true" data-bs-keyboard="false" data-bs-backdrop="static">'+
' <div class="modal-dialog modal-lg" role="document">'+
' <div class="modal-content">'+
' <div class="modal-header">'+
' <h5 class="modal-title fw-bold"><!--index.twig--></h5>'+
' <button class="btn-close" type="button" data-bs-dismiss="modal" aria-label="Close"></button>'+
' </div>'+
' <div class="modal-body">'+
' <p class="modal-message"></p>'+
' <ul id="bulkVt4gRollbackResult"></ul>'+
' <div class="progress" style="display: none">'+
' <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>'+
' </div>'+
' </div>'+
' <div class="modal-footer">'+
' <button class="btn btn-ec-sub" type="button" data-bs-dismiss="modal">{{ "admin.common.cancel"|trans }}</button>'+
' <button id="bulkVt4gRollback" class="btn btn-ec-conversion" type="button"><!--index.twig--></button>'+
' <button id="bulkVt4gRollbackComplete" class="btn btn-ec-regular" style="display: none" type="button">{{ "admin.common.close"|trans }}</button>'+
' </div>'+
' </div>'+
' </div>'+
'</div>';
$rollbackModalTarget.after(rollbackModalContents);
$('#vt4g_btn_bulk_rollback_order').on('click', handleClickRollbackOrder);
$('#bulkVt4gRollback').on('click', submitRollbackOrder);
// ロールバック処理終了後の閉じるボタン
$('#bulkVt4gRollbackComplete').on('click', function() {
location.href = '{{ url('admin_order', { 'resume': 1 }) }}';
});
});
</script>