/*******************************************************************
App. Name : INVENTORY AND SALES SYSTEMS
Module : /apps/js/tab/produk.js
Desc. : JS - Tabel Master Barang
Created By : Siau Pheng (siaupheng@3fonia.com).
Created Date : May 19th, 2010.
Last Modified : January 10th, 2026.
(c) 2010 - 2026, 3FONIA Software; WWW.3FONIA.COM.
*******************************************************************/
var ProdukJS = {
PHP: '',
KODE: '',
loading: function(){
$('#butFilter').click(() => this.filter_show());
this.data_query('tabel-produk');
$('#Cari_Produk').on({
keydown: (e) => {
var key = e.keyCode || e.which || 0;
if (key==13 && e.target.value!='') this.data_query('tabel-produk');
},
click: () => $('#Cari_Produk').select()
});
$('#butSearch').click(() => this.data_query('tabel-produk'));
$('#butAll').click(() => {
$('#Cari_Produk').val('');
this.data_query('tabel-produk');
});
FoniaJS.setFocus('#Cari_Produk');
},
filter_show: function(){
new Request({
url: this.PHP,
sendParam: ['data=detail-filter','Filter_Produk'],
onSuccess: (txt) => {
this.winFilter = FoniaJS.showWindow('Filter', 'Filter Produk Sesuai Grup', txt, 400, [{},{i:'butTutup'},{i:'butWSimpan',v:'Simpan',c:'success'}], () => {
$('#winFilter').find('#butWSimpan').click(() => this.filter_simpan());
});
$('#Fi_Produk').focus();
}
}).send();
},
filter_simpan: function(){
$('#Filter_Produk').val($('#Fi_Produk').val());
FoniaJS.closeWindow(this.winFilter);
this.data_query('tabel-produk');
},
produk_show: function(mode, ID){
new Request.JSON({
url: this.PHP,
sendParam: ['data=detail-produk','mode='+mode,'InvID='+ID],
onSuccess: (ajson) => {
this.winDetail = FoniaJS.showWindow('Detail', 'Detail Barang', ajson.html, 'xl', [{},{i:'butWBtl',v:'Batal',c:'danger'},{i:'butWSimpan',v:'Simpan',c:'success'}], () => {
$('#winDetail')
.find('#butWSimpan').click(() => this.produk_simpan()).end()
.find('#butWBtl').click(() => this.produk_batal());
this.data_query('tabel-kemasan');
});
}
}).send();
},
produk_restore: function(acell){
new Request.JSON({
url: this.PHP,
sendParam: ['data=restore-detail','InvID='+acell[0]],
onSuccess: (ajson) => {
this.winBRest = FoniaJS.showWindow('BRest', 'Informasi Backup Barang', ajson.html, 'xl', [], () => this.data_query('restore-tabel'));
}
}).send();
},
produk_restore_select: function(acell){
FoniaJS.showConfirm('Konfirmasi', 'Yakin akan restore data Barang ke versi tanggal : "'+acell[0]+'" ?
HARAP KONFIRMASI KEMBALI, TERIMA KASIH!', () => {
new Request.JSON({
url: this.PHP,
sendParam: ['data=restore-proses','InvID','MD5Key='+acell[1]],
onSuccess: (adata) => {
if (adata.status==true){
FoniaJS.showInfo('Sukses restore Data Barang ini!');
}else{
FoniaJS.showInfo('Ada masalah dalam proses restore!');
}
}
}).send();
}, 'danger');
},
produk_restore_form: function(acell, div){
new Request.JSON({
url: this.PHP,
sendParam: ['data=restore-form','InvID','MD5Key='+acell[1]],
onSuccess: (adata) => $(div).html(adata.html)
}).send();
},
produk_tambah: function(){
this.produk_show('add', 0);
},
produk_ubah: function(acell){
this.produk_show('edit', acell[0]);
},
produk_hapus: function(acell){
FoniaJS.showConfirm('Konfirmasi', 'Yakin akan hapus "'+acell[1]+'" ?', () => {
new Request.JSON({
url: this.PHP,
sendParam: ['data=save','mode=del','InvID='+acell[0]],
onSuccess: (adata) => {
if (adata.status==true){
this.data_query('tabel-produk');
FoniaJS.showInfo('Proses hapus data sukses!');
}else{
FoniaJS.showInfo('Ada masalah dalam proses hapus data!');
}
}
}).send();
}, 'danger');
},
produk_simpan: function(){
new Request.JSON({
url: this.PHP,
sendParam: ['data=save','mode','InvID','Grup_InvID','MerekID','Nama_Produk='+encodeURIComponent($('#Nama_Produk').val()),'StokID','NormalID','Harga_Beli','Harga_Tambah','Stok_Min','Stok_Max','Aktif'],
onSuccess: (adata) => {
if (adata.status==true){
this.data_query('tabel-produk');
FoniaJS.showInfo('Proses simpan data sukses!');
}else{
FoniaJS.showInfo('Ada masalah dalam proses simpan data!');
}
FoniaJS.closeWindow(this.winDetail);
}
}).send();
},
produk_batal: function(){
if ($('#mode').val()=='add'){
FoniaJS.showConfirm('Konfirmasi', 'Yakin akan batalkan Penambahan Barang ?', () => {
new Request.JSON({
url: this.PHP,
sendParam: ['data=save','mode=cancel','InvID','mode_before=add'],
onSuccess: () => this.data_query('tabel-produk')
}).send();
}, 'danger');
return false;
}
FoniaJS.showConfirm('Konfirmasi', 'Yakin akan batalkan Perubahan Detail Barang ?', () => {
new Request.JSON({
url: this.PHP,
sendParam: ['data=save','mode=cancel','InvID'],
onSuccess: () => FoniaJS.closeWindow(this.winDetail)
}).send();
}, 'danger');
},
kemasan_batal: function(){
var dt = $('#divKemasanTabel').DataTable(), tr, l_row;
tr = $('#butBatalKemasan').closest('tr');
l_row = dt.row(tr.prev('tr'));
if (l_row.child.isShown()) l_row.nodes().to$().find('.fon-tabel-dt').click();
if (l_row.data()[0]=='0') l_row.remove().draw(true);
},
kemasan_add: function(){
var dt = $('#divKemasanTabel').DataTable(), l_row;
dt.order([0,'desc']);
l_row = dt.row(':last');
if (!dt.data().count() || l_row.data()[0]!=='0'){
if (FoniaJS.user_level(2)) {
dt.row.add(['0','','SKU_BARU','',0,0,0,0,0,'']).draw(true);
} else dt.row.add(['0','','SKU_BARU','',0,0,'']).draw(true);
l_row = dt.row(':last');
}
if (!l_row.child.isShown()) l_row.nodes().to$().find('.fon-tabel-dt').click();
},
kemasan_form: function(acell, div){
new Request.JSON({
url: this.PHP,
sendParam: ['data=detail-kemasan','InvID','InvKemasanID='+acell[0],'Nama_Satuan='+$('#StokID').find(':selected').text()],
onSuccess: (adata) => {
$(div).html(adata.html);
$(div)
.find('#butBatalKemasan').click(() => this.kemasan_batal()).end()
.find('#butSimpanKemasan').click(() => this.kemasan_check());
this.calc_Modal();
this.calc_Harga(1);
}
}).send();
},
kemasan_del: function(acell){
FoniaJS.showConfirm('Konfirmasi', 'Yakin akan hapus "'+acell[1]+'" ?', () => {
new Request.JSONF({
url: this.PHP,
sendParam: ['data=tabel-kemasan','mode','mode_detail=delete','InvID','InvKemasanID='+acell[0]],
onSuccess: () => FoniaJS.showInfo('Proses hapus data sukses!')
}).send();
}, 'danger');
},
kemasan_save: function(){
if ($('#Qty').val()==0){
FoniaJS.showInfo('Mohon isi jumlah Qty / Kemasan !');
return false;
}
new Request.JSONF({
url: this.PHP,
sendParam: ['data=tabel-kemasan','mode','mode_detail=update','InvID','InvKemasanID','SatuanID','SatuanLamaID','Kode_Barcode','Qty','Harga_Jual'],
onSuccess: () => FoniaJS.showInfo('Sukses menyimpan kemasan produk.')
}).send();
},
kemasan_check: function(){
if ($('#SatuanID').val()==$('#SatuanLamaID').val()) {
this.kemasan_save();
return;
}
new Request.JSONF({
url: this.PHP,
sendParam: ['data=check-kemasan','InvID','SatuanID'],
onSuccess: (adata) => {
if (adata.ada==true) {
FoniaJS.showInfo("Nama kemasan telah ada dalam sistem!");
} else {
this.kemasan_save();
}
}
}).send();
},
data_query: function(tipe, mode){
if (tipe=="tabel-produk"){
new Request.JSONF({
url: this.PHP,
sendParam: ['data=tabel-produk','Cari_Produk','Filter_Produk'],
onSuccess: () => { $('#Cari_Produk').val('').focus(); }
}).send();
} else if (tipe=="tabel-kemasan"){
new Request.JSONF({
url: this.PHP,
sendParam: ['data=tabel-kemasan','mode','InvID']
}).send();
} else if (tipe=="restore-tabel"){
new Request.JSONF({
url: this.PHP,
sendParam: ['data=restore-tabel','InvID']
}).send();
}
},
hitung_HPP: function(){
$('#Harga_Pokok').val(FoniaJS.FloatToStr(FoniaJS.StrToFloat($('#Harga_Beli').val()) + FoniaJS.StrToFloat($('#Harga_Tambah').val()), 0));
},
calc_Modal: function(){
$('#Harga_Modal').val(FoniaJS.FloatToStr(FoniaJS.StrToFloat($('#Harga_Pokok').val()) * FoniaJS.StrToFloat($('#Qty').val())));
this.calc_Harga(1);
},
calc_Harga: function(tipe){
var harga = FoniaJS.StrToFloat($('#Harga_Jual').val());
var pjual = FoniaJS.StrToFloat($('#Persen_Jual').val());
var hmodal = FoniaJS.StrToFloat($('#Harga_Modal').val());
if (tipe==1){
$('#Persen_Jual').val(FoniaJS.FloatToStr((harga - hmodal) / harga * 100, 2));
} else {
$('#Harga_Jual').val(FoniaJS.FloatToStr(hmodal / (100 - pjual) * 100));
}
}
};