<script>
jQuery('body').on('click', '#btn_get_data', function() {
loc_href();
});
function loc_href() {
var idoutlet = jQuery('#idoutlet').val();
var duedate = jQuery('#duedate').val();
location.href = app_url + 'dstb_clean/add/?idoutlet=' + idoutlet + '&duedate=' + duedate;
}
jQuery('body').on('click', '.btn-detail-bill', function() {
var code = jQuery(this).attr('data-code');
var id = jQuery(this).attr('data-id');
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/get_invoice',
type: 'POST',
dataType: 'JSON',
data: { code: code, id: id },
success: function(response) {
if (response.err_code == 0) {
jQuery('.az-modal-detail .modal-body').html(response.data);
show_modal('detail');
}
else {
bootbox.alert({ title: "Error", message: response.err_message });
}
hide_loading();
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.btn-save-dstb', function() {
var hasBill = jQuery('#form_dstb .chk-bill:checked').length > 0;
var hasPU = jQuery('#form_dstb .chk-pu-item:checked').length > 0;
if (!hasBill && !hasPU) {
bootbox.alert('Harap memilih (checklist) minimal 1 Bill atau 1 Barang PU terlebih dahulu.');
return;
}
var driver = jQuery('#iddriver').val();
if (!driver || driver === '') {
bootbox.alert('Harap pilih Driver terlebih dahulu.');
return;
}
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/save_dstb',
type: 'POST',
dataType: 'JSON',
data: jQuery('#form_dstb').serialize(),
success: function(response) {
if (response.err_code == 0) {
location.href = app_url + 'dstb_clean';
}
else {
hide_loading();
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.btn-pending', function() {
var id = jQuery(this).attr('data-id');
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/get_pending',
type: 'POST',
dataType: 'JSON',
data: { id: id },
success: function(response) {
hide_loading();
if (response.err_code == 0) {
show_modal('pending');
jQuery('#pending_description').val(response.data);
jQuery('#idpending').val(id);
}
else {
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.btn-action-save-pending', function() {
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/save_pending_description',
type: 'POST',
dataType: 'JSON',
data: jQuery('#form_pending').serialize(),
success: function(response) {
hide_loading();
if (response.err_code == 0) {
hide_modal('pending');
}
else {
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.btn-stock-position', function() {
var id = jQuery(this).attr('data-id');
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/get_stock_position',
type: 'POST',
dataType: 'JSON',
data: { id: id },
success: function(response) {
hide_loading();
if (response.err_code == 0) {
show_modal('stock_position');
jQuery('#stock_position').val(response.data);
jQuery('#idstock_position').val(id);
}
else {
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.btn-action-save-stock-position', function() {
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/save_stock_position',
type: 'POST',
dataType: 'JSON',
data: jQuery('#form_stock_position').serialize(),
success: function(response) {
hide_loading();
if (response.err_code == 0) {
hide_modal('stock_position');
}
else {
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
});
jQuery('body').on('click', '.chk-bill', function() {
if (jQuery(this).is(':checked')) {
jQuery(this).parents('tr').find('.btn-pending').addClass('hide');
jQuery(this).parents('tr').find('.btn-stock-position').addClass('hide');
}
else {
jQuery(this).parents('tr').find('.btn-pending').removeClass('hide');
jQuery(this).parents('tr').find('.btn-stock-position').removeClass('hide');
}
});
var pu_current_tg_id = '';
jQuery('body').on('click', '.btn-sebagian', function() {
pu_current_tg_id = jQuery(this).attr('data-id');
var billCode = jQuery(this).attr('data-code');
open_pu_modal(pu_current_tg_id, billCode, false);
});
jQuery('body').on('click', '.btn-edit-pu', function() {
pu_current_tg_id = jQuery(this).attr('data-id');
var billCode = jQuery(this).attr('data-code');
open_pu_modal(pu_current_tg_id, billCode, true);
});
function open_pu_modal(tgId, billCode, isEdit) {
jQuery('#form_pu')[0].reset();
jQuery('#pu_id_tg').val(tgId);
jQuery('#table_pu_items tbody').html('<tr><td colspan="4" class="text-center">Memuat data...</td></tr>');
jQuery('#listReasons').html('');
var tomorrow = new Date();
tomorrow.setDate(tomorrow.getDate() + 1);
var dateStr = tomorrow.toISOString().split('T')[0];
jQuery('#form_pu input[name="est_finish_date"]').val(dateStr);
load_pu_reasons();
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/get_pu_items',
type: 'POST',
dataType: 'JSON',
data: { idtransaction_group: tgId },
success: function(response) {
hide_loading();
if (response.err_code == 0) {
render_pu_items(response.items);
if (isEdit) {
load_pu_saved_data(tgId);
}
show_modal('modal_pu');
}
else {
bootbox.alert(response.err_message);
}
},
error: function() { hide_loading(); }
});
}
function render_pu_items(items) {
var html = '';
if (!items || items.length === 0) {
html = '<tr><td colspan="4" class="text-center">Tidak ada item.</td></tr>';
}
else {
var tot = items.length;
for (var i = 0; i < items.length; i++) {
var item = items[i];
var note = item.trx_note || '';
var num = (i + 1) + '/' + tot;
html += '<tr>';
html += '<td align="center"><input type="checkbox" class="chk-pu-select" name="selected_items[]" value="' + item.idtransaction + '"></td>';
html += '<td align="center"><span class="pu-item-num">' + num + '</span></td>';
html += '<td>' + item.product_name + ' (' + item.item_code + ')</td>';
html += '<td>' + note + '</td>';
html += '</tr>';
}
}
jQuery('#table_pu_items tbody').html(html);
jQuery('#checkAllItemsPU').prop('checked', false);
}
function load_pu_saved_data(tgId) {
jQuery.ajax({
url: app_url + 'dstb_clean/get_pu_data',
type: 'POST',
dataType: 'JSON',
data: { idtransaction_group: tgId },
success: function(response) {
if (response.err_code == 0 && response.pu_data.length > 0) {
var savedIds = [];
var reason = '';
var estDate = '';
for (var i = 0; i < response.pu_data.length; i++) {
savedIds.push(String(response.pu_data[i].idtransaction));
if (reason === '') { reason = response.pu_data[i].reason; }
if (estDate === '') { estDate = response.pu_data[i].est_finish_date; }
}
jQuery('.chk-pu-select').each(function() {
if (savedIds.indexOf(jQuery(this).val()) !== -1) {
jQuery(this).prop('checked', true);
}
});
jQuery('#pu_reason').val(reason);
if (estDate) {
jQuery('#form_pu input[name="est_finish_date"]').val(estDate);
}
}
}
});
}
function load_pu_reasons() {
jQuery.ajax({
url: app_url + 'dstb_clean/get_pu_reasons',
type: 'POST',
dataType: 'JSON',
success: function(response) {
var html = '';
if (response.reasons && response.reasons.length > 0) {
for (var i = 0; i < response.reasons.length; i++) {
html += '<option value="' + response.reasons[i] + '">';
}
}
jQuery('#listReasons').html(html);
}
});
}
jQuery('body').on('change', '#checkAllItemsPU', function() {
var isChecked = jQuery(this).is(':checked');
jQuery('.chk-pu-select').prop('checked', isChecked);
});
jQuery('body').on('click', '.btn-action-save-pu', function() {
var selectedItems = [];
jQuery('.chk-pu-select:checked').each(function() {
selectedItems.push(jQuery(this).val());
});
var reason = jQuery('#pu_reason').val();
var estDate = jQuery('#form_pu input[name="est_finish_date"]').val();
if (selectedItems.length === 0) {
bootbox.confirm({
title: 'Batalkan PU?',
message: 'Tidak ada barang yang dipilih. Batalkan PU dan kirim semua barang?',
buttons: {
confirm: { label: 'Ya, Batalkan PU', className: 'btn-danger' },
cancel: { label: 'Tidak', className: 'btn-default' }
},
callback: function(result) {
if (result) { cancel_pu(pu_current_tg_id); }
}
});
return;
}
if (!reason || reason.trim() === '') {
bootbox.alert('Alasan penundaan wajib diisi.');
return;
}
if (!estDate) {
bootbox.alert('Tanggal estimasi selesai PU wajib diisi.');
return;
}
var formData = new FormData();
formData.append('idtransaction_group', jQuery('#pu_id_tg').val());
formData.append('reason', reason);
formData.append('est_finish_date', estDate);
for (var i = 0; i < selectedItems.length; i++) {
formData.append('selected_items[]', selectedItems[i]);
}
var fileInput = jQuery('#form_pu input[name="photo_proof"]')[0];
if (fileInput && fileInput.files.length > 0) {
formData.append('photo_proof', fileInput.files[0]);
}
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/save_pu',
type: 'POST',
dataType: 'JSON',
data: formData,
processData: false,
contentType: false,
success: function(response) {
hide_loading();
if (response.err_code == 0) {
hide_modal('modal_pu');
bootbox.alert({
title: 'Berhasil',
message: 'Data PU berhasil disimpan.',
callback: function() { location.reload(); }
});
}
else {
bootbox.alert(response.err_message);
}
},
error: function() {
hide_loading();
bootbox.alert('Terjadi kesalahan saat menyimpan.');
}
});
});
function cancel_pu(tgId) {
show_loading();
jQuery.ajax({
url: app_url + 'dstb_clean/delete_pu',
type: 'POST',
dataType: 'JSON',
data: { idtransaction_group: tgId },
success: function(response) {
hide_loading();
if (response.err_code == 0) {
hide_modal('modal_pu');
bootbox.alert({
title: 'Berhasil',
message: 'PU dibatalkan. Semua barang akan dikirim.',
callback: function() { location.reload(); }
});
}
else {
bootbox.alert(response.err_message);
}
},
error: function() {
hide_loading();
bootbox.alert('Terjadi kesalahan saat membatalkan PU.');
}
});
}
</script>
<script>
(function(){
function dpOf($t){
return $t.data('DateTimePicker') || $t.data('datetimepicker');
}
function targetOf($el){
var $w = $el.closest('.input-group');
return ($w.length ? $w : $el);
}
function ensure($el){
if (!jQuery.fn.datetimepicker) { return null; }
var $t = targetOf($el);
if (!dpOf($t)) {
try { $t.datetimepicker({ format: 'DD-MM-YYYY' }); } catch(e){}
}
return dpOf($t) || null;
}
jQuery(document).ready(function(){
if (jQuery('#duedate').length) { ensure(jQuery('#duedate')); }
});
jQuery('body').on('focus click', '#duedate', function(){
var dp = ensure(jQuery(this));
if (dp && dp.show) { dp.show(); }
});
})();
</script>