Anons79 Mini Shell

Directory : /home/aplikasiposinfo/www/derfacell.aplikasipos.info/resources/views/penjualan/
Upload File :
Current File : /home/aplikasiposinfo/www/derfacell.aplikasipos.info/resources/views/penjualan/script.blade.php

    <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 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 stokText = document.createTextNode(item.stok);

                        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');

                        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 => {

                            if (jumlahValue > item.stok) {
                                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);

                                //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 namaSparepart = row.insertCell(0);
                                let hargaBeli = row.insertCell(1);
                                let jumlah = row.insertCell(2);
                                let hargaJual = row.insertCell(3);
                                let subTotal = row.insertCell(4);
                                subTotal.classList.add("subTotal");
                                let hapus = row.insertCell(5);

                                namaSparepart.appendChild(namaSparepartText);
                                hargaJual.appendChild(hargaJualText);
                                hargaBeli.appendChild(diskonInput);
                                subTotal.appendChild(subTotalText);
                                // jumlah.appendChild(jumlahText);
                                jumlah.appendChild(jumlahInput);
                                jumlah.appendChild(idInput);
                                jumlah.appendChild(hargaInput);
                                jumlah.appendChild(biayaInput);
                                jumlah.appendChild(namaInput);
                                hapus.appendChild(hapusButton);

                                let detail = [item.id, jumlahValue];

                                addEventHapus(hapusButton);
                                addEventDiskon(diskonInput);
                                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);
        });

        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[2].children[0];
                let hargaItem = this.parentElement.parentElement.children[3];
                let subTotalElement = this.parentElement.parentElement.children[4];
                let diskonInput = this.parentElement.parentElement.children[1].children[0];

                const harga = this.parentElement.parentElement.children[2].children[2];

                let hargaDiskon = Math.max(parseFloat(harga.value) - diskonInput.value, 0);

                // hargaItem.textContent = hargaDiskon;

                let subtotal = hargaDiskon * 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;

            });

            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();
    </script>

Anons79 File Manager Version 1.0, Coded By Anons79
Email: [email protected]