$(document).ready(function () { $(".select2").select2({ placeholder: "Pilih...", allowClear: true, }); // Fungsi untuk mengambil data stok function fetchStockData() { const dealerId = $("#dealer").val(); if (!dealerId) return; const productIds = $(".product-select") .map(function () { return $(this).val(); }) .get() .filter((id) => id !== ""); if (productIds.length === 0) return; $.ajax({ url: "/warehouse/opnames/get-stock-data", method: "POST", data: { _token: $('meta[name="csrf-token"]').attr("content"), dealer_id: dealerId, product_ids: productIds, }, success: function (response) { if (response.stocks) { $(".product-row").each(function () { const productId = $(this).find(".product-select").val(); const systemQtyInput = $(this).find(".system-quantity"); const physicalQtyInput = $(this).find( 'input[name^="physical_quantity"]' ); // Simpan nilai physical quantity yang sudah ada const currentPhysicalQty = physicalQtyInput.val(); if ( productId && response.stocks[productId] !== undefined ) { systemQtyInput.val(response.stocks[productId]); // Kembalikan nilai physical quantity jika ada if (currentPhysicalQty) { physicalQtyInput.val(currentPhysicalQty); } calculateDifference(systemQtyInput[0]); } else { systemQtyInput.val("0"); calculateDifference(systemQtyInput[0]); } }); } }, error: function (xhr) { console.error("Error fetching stock data:", xhr.responseText); }, }); } // Update stok saat dealer berubah $("#dealer").change(function () { fetchStockData(); }); // Update stok saat produk berubah $(document).on("change", ".product-select", function () { const row = $(this).closest("tr"); const productId = $(this).val(); const systemQtyInput = row.find(".system-quantity"); const physicalQtyInput = row.find('input[name^="physical_quantity"]'); // Simpan nilai physical quantity yang sudah ada const currentPhysicalQty = physicalQtyInput.val(); if (productId) { fetchStockData(); } else { systemQtyInput.val("0"); // Kembalikan nilai physical quantity jika ada if (currentPhysicalQty) { physicalQtyInput.val(currentPhysicalQty); } calculateDifference(systemQtyInput[0]); } }); // Fungsi untuk menambah baris produk $("#btn-add-row").click(function () { const template = document.getElementById("product-row-template"); const tbody = $("#product-table tbody"); const newRow = template.content.cloneNode(true); const rowIndex = $(".product-row").length; // Update name attributes with correct index $(newRow) .find('select[name="product[]"]') .attr("name", `product[${rowIndex}]`); $(newRow) .find('input[name="system_quantity[]"]') .attr("name", `system_quantity[${rowIndex}]`); $(newRow) .find('input[name="physical_quantity[]"]') .attr("name", `physical_quantity[${rowIndex}]`); $(newRow) .find('input[name="item_notes[]"]') .attr("name", `item_notes[${rowIndex}]`); // Add system-quantity class dan pastikan readonly const systemQtyInput = $(newRow).find( 'input[name="system_quantity[]"]' ); systemQtyInput .addClass("system-quantity") .attr("readonly", true) .val("0"); // Reset semua nilai input di baris baru kecuali system quantity $(newRow).find("select").val(""); $(newRow).find("input:not(.system-quantity)").val(""); tbody.append(newRow); updateRemoveButtons(); }); // Fungsi untuk menghapus baris produk $(document).on("click", ".btn-remove-row", function () { $(this).closest("tr").remove(); updateRemoveButtons(); // Reindex semua baris setelah penghapusan reindexRows(); }); // Fungsi untuk update status tombol hapus function updateRemoveButtons() { const rows = $(".product-row").length; $(".btn-remove-row").prop("disabled", rows <= 1); } // Fungsi untuk reindex semua baris function reindexRows() { $(".product-row").each(function (index) { $(this) .find('select[name^="product"]') .attr("name", `product[${index}]`); $(this) .find('input[name^="system_quantity"]') .attr("name", `system_quantity[${index}]`); $(this) .find('input[name^="physical_quantity"]') .attr("name", `physical_quantity[${index}]`); $(this) .find('input[name^="item_notes"]') .attr("name", `item_notes[${index}]`); }); } // Update calculateDifference function function calculateDifference(input) { const row = $(input).closest("tr"); const systemQty = parseFloat(row.find(".system-quantity").val()) || 0; const physicalQty = parseFloat(row.find('input[name^="physical_quantity"]').val()) || 0; const noteInput = row.find('input[name^="item_notes"]'); if (Math.abs(systemQty - physicalQty) > 0.01) { noteInput.addClass("is-invalid"); noteInput.attr("required", true); noteInput.attr( "placeholder", "Catatan wajib diisi karena ada perbedaan stock" ); row.addClass("table-warning"); } else { noteInput.removeClass("is-invalid"); noteInput.removeAttr("required"); noteInput.attr("placeholder", "Catatan item"); row.removeClass("table-warning"); } } // Prevent manual editing of system quantity $(document).on("keydown", ".system-quantity", function (e) { e.preventDefault(); return false; }); $(document).on("paste", ".system-quantity", function (e) { e.preventDefault(); return false; }); // Validasi form sebelum submit $("#opname-form").submit(function (e) { const dealerId = $("#dealer").val(); if (!dealerId) { e.preventDefault(); alert("Silakan pilih dealer terlebih dahulu!"); return false; } const products = $('select[name^="product"]') .map(function () { return $(this).val(); }) .get(); // Cek duplikasi produk const uniqueProducts = [...new Set(products)]; if (products.length !== uniqueProducts.length) { e.preventDefault(); alert("Produk tidak boleh duplikat!"); return false; } // Cek produk kosong if (products.includes("")) { e.preventDefault(); alert("Semua produk harus dipilih!"); return false; } // Cek catatan untuk perbedaan stock let hasInvalidNotes = false; $(".product-row").each(function () { const systemQty = parseFloat( $(this).find('input[name^="system_quantity"]').val() ) || 0; const physicalQty = parseFloat( $(this).find('input[name^="physical_quantity"]').val() ) || 0; const note = $(this).find('input[name^="item_notes"]').val(); if (Math.abs(systemQty - physicalQty) > 0.01 && !note) { hasInvalidNotes = true; $(this).addClass("table-danger"); } }); if (hasInvalidNotes) { e.preventDefault(); alert( "Catatan wajib diisi untuk produk yang memiliki perbedaan stock!" ); return false; } }); // Initial stock data load if dealer is selected if ($("#dealer").val()) { fetchStockData(); } });