<script>
const tableSparepart = document.getElementById('table-sparepart');
const tableSparepartShow = document.getElementById('table-sparepart-list');
const tableDetail = document.getElementById('table-detail-item');
const itemDeleteButton = document.querySelectorAll(".item-delete");
const itemDiskonInput = document.querySelectorAll(".item-diskon");
const itemDiskonPersenInput = document.querySelectorAll(".item-diskon-persen");
const itemQtyInput = document.querySelectorAll(".qty");
const uangBayarInput = document.getElementById("uang_bayar");
const pencarian = document.getElementById("cari");
let itemId = document.querySelectorAll(".item_id");
// console.log(itemId);
const numberFormat = new Intl.NumberFormat({
style: 'currency'
});
function loadList() {
const nilaiPencarian = pencarian.value;
fetch("{{ route('get-item') }}?cari=" + nilaiPencarian, {
method: "GET"
})
.then(
(response) => {
return response.json();
}
)
.then((data) => {
const rowCount = tableSparepart.querySelector('tbody');
while (rowCount.firstChild) {
rowCount.removeChild(rowCount.firstChild);
}
let items = data;
items.data.forEach(item => {
let namaSparepartText = document.createTextNode(item.nama_item);
let hargaJualText = document.createTextNode(numberFormat.format(item.harga_item));
let hargaBeliText = document.createTextNode(numberFormat.format(item.biaya_item));
let stok_tokos = item.stok_item_tokos.filter(toko => toko.toko_id == document
.getElementById("toko_id").value);
var itemStok = item.stok_item_tokos.length > 0 ? stok_tokos.reduce((sum, toko) => sum +
toko.stok, 0) : item.stok;
let stokText = document.createTextNode(itemStok);
let pilihButtonText = document.createTextNode(`Pilih`);
let pilihButton = document.createElement("button");
pilihButton.classList.add('btn', 'btn-primary', 'item-add');
// pilihButton.setAttribute('data-id', item.id);
pilihButton.setAttribute('type', "button");
pilihButton.dataset.id = item.id;
pilihButton.appendChild(pilihButtonText);
let jumlahInput = document.createElement("input");
jumlahInput.setAttribute('type', "number");
jumlahInput.setAttribute('value', 0);
jumlahInput.setAttribute('min', 0);
jumlahInput.classList.add('form-control', 'jumlah-input');
if (itemStok == null || itemStok <= 0) {
jumlahInput.setAttribute('disabled', true);
jumlahInput.setAttribute('title', "Stok Habis!")
pilihButton.setAttribute('disabled', true);
pilihButton.setAttribute('title', "Stok Habis!")
}
let row = tableSparepart.querySelector("tbody").insertRow(-1);
let namaSparepart = row.insertCell(0);
let hargaJual = row.insertCell(1);
let hargaBeli = row.insertCell(2);
let stok = row.insertCell(3);
let jumlah = row.insertCell(4);
let pilih = row.insertCell(5);
if (window.innerWidth < 768) {
namaSparepart.setAttribute('style', 'width: 150px;')
} else {
namaSparepart.setAttribute('style', 'max-width: 150px;')
}
namaSparepart.appendChild(namaSparepartText);
hargaJual.appendChild(hargaJualText);
hargaBeli.appendChild(hargaBeliText);
stok.appendChild(stokText);
jumlah.appendChild(jumlahInput);
pilih.appendChild(pilihButton);
addEventPilih(pilihButton);
});
});
}
tableSparepartShow.addEventListener("click", function() {
loadList();
pencarian.addEventListener('input', function() {
// Mengambil nilai input pencarian
loadList();
});
});
//memunculkan data pada modal table item
//menambahkan item ke dalam detail penjualan
function addEventPilih(element) {
element.addEventListener('click', function(event) {
let jumlah = this.parentElement.parentElement.childNodes[4];
let jumlahValue = jumlah.querySelector(".jumlah-input").value;
fetch("{{ route('get-item') }}?search=" + this.dataset.id, {
method: "GET"
})
.then(
(response) => {
return response.json();
}
)
.then((data) => {
let items = data;
items.data.forEach(item => {
let stok_tokos = item.stok_item_tokos.filter(toko => toko.toko_id ==
document.getElementById("toko_id").value);
var itemStok = item.stok_item_tokos.length > 0 ? stok_tokos.reduce((sum,
toko) => sum + toko.stok, 0) : item.stok;
if (jumlahValue > itemStok) {
minAlert();
} else {
if (jumlahValue > 0) {
addDetailBarangRow();
}
}
function minAlert() {
let text =
"Stok Barang Akan Minus ! Apakah anda ingin melanjutkannya?";
if (confirm(text) == true) {
addDetailBarangRow();
}
}
function addDetailBarangRow() {
//new text element
showAlert(item.nama_item);
let namaSparepartText = document.createTextNode(item.nama_item);
let hargaJualText = document.createTextNode(numberFormat.format(item
.harga_item));
let hargaBeliText = document.createTextNode(numberFormat.format(item
.biaya_item));
let jumlahText = document.createTextNode(jumlahValue);
let subTotalText = document.createTextNode(numberFormat.format(item
.harga_item * jumlahValue));
let hapusButtonText = document.createTextNode("X");
let hapusButton = document.createElement("button");
hapusButton.classList.add("btn", "btn-danger", "item-delete");
hapusButton.appendChild(hapusButtonText);
hapusButton.setAttribute('type', "button");
let diskonInput = document.createElement("input");
diskonInput.classList.add("item-diskon");
diskonInput.setAttribute("name", "diskon[]");
diskonInput.setAttribute("type", "number");
diskonInput.setAttribute("min", 0);
diskonInput.setAttribute("value", 0);
let diskonPersenInput = document.createElement("input");
diskonPersenInput.classList.add("item-diskon-persen");
diskonPersenInput.setAttribute("name", "diskon_persen[]");
diskonPersenInput.setAttribute("type", "number");
diskonPersenInput.setAttribute("min", 0);
diskonPersenInput.setAttribute("value", 0);
//hidden input for form request
let jumlahInput = document.createElement("input");
jumlahInput.classList.add("qty");
jumlahInput.setAttribute('name', "jumlah[]");
jumlahInput.setAttribute('type', "number");
// jumlahInput.setAttribute('max', item.stok);
jumlahInput.setAttribute('min', 0);
jumlahInput.setAttribute('value', jumlahValue);
// jumlahInput.setAttribute('hidden', true);
//hidden input for form request
let idInput = document.createElement("input");
idInput.setAttribute('name', "id[]");
idInput.classList.add("item_id");
idInput.setAttribute('type', "text");
idInput.setAttribute('value', item.id);
idInput.setAttribute('hidden', true);
let hargaInput = document.createElement("input");
hargaInput.setAttribute('name', "harga[]");
hargaInput.classList.add("harga");
hargaInput.setAttribute('type', "number");
hargaInput.setAttribute('value', item.harga_item);
hargaInput.setAttribute('hidden', true);
let biayaInput = document.createElement("input");
biayaInput.setAttribute('name', "biaya_item[]");
biayaInput.classList.add("harga");
biayaInput.setAttribute('type', "number");
biayaInput.setAttribute('value', item.biaya_item);
biayaInput.setAttribute('hidden', true);
let namaInput = document.createElement("input");
namaInput.setAttribute('name', "nama_item[]");
namaInput.classList.add("nama_item");
namaInput.setAttribute('type', "text");
namaInput.setAttribute('value', item.nama_item);
namaInput.setAttribute('hidden', true);
let row = tableDetail.querySelector("tbody").insertRow(-1);
let cell0 = row.insertCell(0);
let cell1 = row.insertCell(1);
let cell2 = row.insertCell(2);
let cell3 = row.insertCell(3);
let cell4 = row.insertCell(4);
let cell5 = row.insertCell(5);
cell5.classList.add("subTotal");
let cell6 = row.insertCell(6);
cell0.appendChild(namaSparepartText);
cell1.appendChild(diskonInput);
cell2.appendChild(diskonPersenInput)
cell4.appendChild(hargaJualText);
cell5.appendChild(subTotalText);
// jumlah.appendChild(jumlahText);
cell3.appendChild(jumlahInput);
cell3.appendChild(idInput);
cell3.appendChild(hargaInput);
cell3.appendChild(biayaInput);
cell3.appendChild(namaInput);
cell6.appendChild(hapusButton);
let detail = [item.id, jumlahValue];
addEventHapus(hapusButton);
addEventDiskon(diskonInput);
countSubTotal(diskonPersenInput);
addEventQty(jumlahInput);
countGrandTotal();
}
});
});
})
}
function showAlert(item) {
alert(item + " Ditambahkan");
}
function addEventHapus(element) {
element.addEventListener("click", function(event) {
this.parentElement.parentElement.remove();
countGrandTotal();
});
}
function addEventDiskon(e) {
countSubTotal(e);
}
function addEventQty(e) {
countSubTotal(e);
}
itemDeleteButton.forEach((e) => {
e.addEventListener("click", function(event) {
this.parentElement.parentElement.remove();
countGrandTotal();
});
});
itemDiskonInput.forEach((e) => {
countSubTotal(e);
});
itemDiskonPersenInput.forEach((e) => {
countSubTotal(e);
});
itemQtyInput.forEach((e) => {
countSubTotal(e);
});
uangBayarInput.addEventListener("input", function(event) {
countGrandTotal();
});
function countSubTotal(e) {
e.addEventListener("input", function(event) {
let qty = this.parentElement.parentElement.children[3].children[0];
let hargaItem = this.parentElement.parentElement.children[4];
let subTotalElement = this.parentElement.parentElement.children[5];
let diskonInput = this.parentElement.parentElement.children[1].children[0];
let diskonPersenInput = this.parentElement.parentElement.children[2].children[0];
const harga = this.parentElement.parentElement.children[3].children[2];
let hargaDiskon = Math.max(parseFloat(harga.value) - diskonInput.value, 0);
let hargaDiskonPersen = Math.max((diskonPersenInput.value / 100) * harga.value, 0);
let subtotal = (hargaDiskon - hargaDiskonPersen) * qty.value;
subTotalElement.textContent = numberFormat.format(subtotal);
countGrandTotal();
});
}
function countGrandTotal() {
let kembaliInput = document.getElementById("kembali");
let uangBayarInput = document.getElementById("uang_bayar");
let subTotal = document.querySelectorAll(".subTotal");
let grandTotal = document.querySelector("#grandTotal");
let inputgrandTotal = document.getElementById("input_grandTotal");
let grandTotalValue = 0;
let kembaliValue = 0;
subTotal.forEach((element) => {
subTotalValue1 = element.textContent.replaceAll(",", "");
subTotalValue = parseFloat(subTotalValue1.replaceAll(".", ""));
grandTotalValue += subTotalValue;
});
let diskon_total = document.getElementById("diskon_total");
let diskon_persen_total = document.getElementById("diskon_persen_total");
let diskon_poin = document.getElementById("diskon_poin");
if (diskon_total.value > 0) {
grandTotalValue = grandTotalValue - diskon_total.value;
}
if (diskon_persen_total.value > 0) {
grandTotalValue = grandTotalValue - ((diskon_persen_total.value / 100) * grandTotalValue)
}
if (diskon_poin.value > 0) {
grandTotalValue = grandTotalValue - diskon_poin.value;
}
grandTotal.textContent = numberFormat.format(grandTotalValue);
inputgrandTotal.value = parseInt(grandTotalValue);
// kembaliValue = Math.max(0,uang_bayar.value - parseInt(grandTotalValue));
kembaliValue = uang_bayar.value - parseInt(grandTotalValue);
if (kembaliValue < 0) {
kembaliInput.value = "Uang Bayar Tidak Cukup";
} else if (kembaliInput >= 0) {
kembaliInput.value = numberFormat.format(parseInt(kembaliValue));
} else {
kembaliInput.value = numberFormat.format(parseInt(kembaliValue));
}
}
countGrandTotal();
document.getElementById("diskon_total").addEventListener('change', function() {
countGrandTotal();
});
document.getElementById("diskon_persen_total").addEventListener('change', function() {
countGrandTotal();
});
document.getElementById("diskon_poin").addEventListener('change', function() {
countGrandTotal();
});
document.getElementById("diskon_total").addEventListener('keyup', function() {
countGrandTotal();
});
document.getElementById("diskon_persen_total").addEventListener('keyup', function() {
countGrandTotal();
});
document.getElementById("diskon_poin").addEventListener('keyup', function() {
countGrandTotal();
});
</script>
Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]