Files
CKB/public/js/warehouse_management/opnames/create.js
2025-06-13 15:17:49 +07:00

2 lines
5.3 KiB
JavaScript

(()=>{function t(t){return function(t){if(Array.isArray(t))return e(t)}(t)||function(t){if("undefined"!=typeof Symbol&&null!=t[Symbol.iterator]||null!=t["@@iterator"])return Array.from(t)}(t)||function(t,n){if(!t)return;if("string"==typeof t)return e(t,n);var a=Object.prototype.toString.call(t).slice(8,-1);"Object"===a&&t.constructor&&(a=t.constructor.name);if("Map"===a||"Set"===a)return Array.from(t);if("Arguments"===a||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(a))return e(t,n)}(t)||function(){throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}()}function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,a=new Array(e);n<e;n++)a[n]=t[n];return a}$(document).ready((function(){function e(){var t=$("#dealer").val();if(t){var e=$(".product-select").map((function(){return $(this).val()})).get().filter((function(t){return""!==t}));0!==e.length&&$.ajax({url:"/warehouse/opnames/get-stock-data",method:"POST",data:{_token:$('meta[name="csrf-token"]').attr("content"),dealer_id:t,product_ids:e},success:function(t){t.stocks&&$(".product-row").each((function(){var e=$(this).find(".product-select").val(),n=$(this).find(".system-quantity"),a=$(this).find('input[name^="physical_quantity"]'),r=a.val();e&&void 0!==t.stocks[e]?(n.val(t.stocks[e]),r&&a.val(r),calculateDifference(n[0])):(n.val("0"),calculateDifference(n[0]))}))},error:function(t){console.error("Error fetching stock data:",t.responseText)}})}}function n(){var t=$(".product-row").length;$(".btn-remove-row").prop("disabled",t<=1)}console.log("Opnames create.js loaded - SweetAlert version"),$(".select2").select2({placeholder:"Pilih...",allowClear:!0}),$(".product-select").select2({placeholder:"Pilih Produk...",allowClear:!0,width:"100%"}),$("#dealer").change((function(){e()})),$(document).on("change",".product-select",(function(){var t=$(this).closest("tr"),n=$(this).val(),a=t.find(".system-quantity"),r=t.find('input[name^="physical_quantity"]'),i=r.val();n?e():(a.val("0"),i&&r.val(i),calculateDifference(a[0]))})),$(document).on("change input",'input[name^="physical_quantity"]',(function(){calculateDifference(this)})),$("#btn-add-row").click((function(){var t=document.getElementById("product-row-template"),e=$("#product-table tbody"),a=t.content.cloneNode(!0),r=$(".product-row").length;$(a).find('select[name="product[]"]').attr("name","product[".concat(r,"]")),$(a).find('input[name="system_quantity[]"]').attr("name","system_quantity[".concat(r,"]")),$(a).find('input[name="physical_quantity[]"]').attr("name","physical_quantity[".concat(r,"]")),$(a).find('input[name="item_notes[]"]').attr("name","item_notes[".concat(r,"]")),$(a).find('input[name="system_quantity[]"]').addClass("system-quantity").attr("readonly",!0).val("0"),$(a).find("select").val(""),$(a).find("input:not(.system-quantity)").val(""),e.append(a),e.find("tr:last-child .product-select").select2({placeholder:"Pilih Produk...",allowClear:!0,width:"100%"}),n()})),$(document).on("click",".btn-remove-row",(function(){$(this).closest("tr").remove(),n(),$(".product-row").each((function(t){var e=$(this),n=e.find('select[name^="product"]');n.data("select2")&&n.select2("destroy"),n.attr("name","product[".concat(t,"]")),e.find('input[name^="system_quantity"]').attr("name","system_quantity[".concat(t,"]")),e.find('input[name^="physical_quantity"]').attr("name","physical_quantity[".concat(t,"]")),e.find('input[name^="item_notes"]').attr("name","item_notes[".concat(t,"]")),n.select2({placeholder:"Pilih Produk...",allowClear:!0,width:"100%"})}))})),window.calculateDifference=function(t){var e=$(t).closest("tr"),n=parseFloat(e.find(".system-quantity").val())||0,a=parseFloat(e.find('input[name^="physical_quantity"]').val())||0,r=e.find('input[name^="item_notes"]');Math.round(100*n)/100!==Math.round(100*a)/100?(r.addClass("is-invalid"),r.attr("required",!0),r.attr("placeholder","Catatan wajib diisi karena ada perbedaan stock"),e.addClass("table-warning")):(r.removeClass("is-invalid"),r.removeAttr("required"),r.attr("placeholder","Catatan item"),e.removeClass("table-warning"))},$(document).on("keydown",".system-quantity",(function(t){return t.preventDefault(),!1})),$(document).on("paste",".system-quantity",(function(t){return t.preventDefault(),!1})),$("#opname-form").submit((function(e){if(!$("#dealer").val())return e.preventDefault(),Swal.fire({icon:"error",title:"Oops...",text:"Silakan pilih dealer terlebih dahulu!"}),!1;var n=$('select[name^="product"]').map((function(){return $(this).val()})).get(),a=t(new Set(n));if(n.length!==a.length)return e.preventDefault(),Swal.fire({icon:"error",title:"Oops...",text:"Produk tidak boleh duplikat!"}),!1;if(n.includes(""))return e.preventDefault(),Swal.fire({icon:"error",title:"Oops...",text:"Semua produk harus dipilih!"}),!1;var r=!1;return $(".product-row").each((function(){var t=parseFloat($(this).find('input[name^="system_quantity"]').val())||0,e=parseFloat($(this).find('input[name^="physical_quantity"]').val())||0,n=$(this).find('input[name^="item_notes"]').val();Math.round(100*t)/100===Math.round(100*e)/100||n||(r=!0,$(this).addClass("table-danger"))})),r?(e.preventDefault(),Swal.fire({icon:"error",title:"Oops...",text:"Catatan wajib diisi untuk produk yang memiliki perbedaan stock!"}),!1):void 0})),$("#dealer").val()&&e()}))})();
//# sourceMappingURL=create.js.map