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

2 lines
4.6 KiB
JavaScript

$(document).ready((function(){var t=1,a="";$(".select2").select2({placeholder:"Pilih...",allowClear:!0});var e=$(".product-select").first();function n(){var t=$(".product-row");$(".remove-product").prop("disabled",t.length<=1)}function i(t,a,e){$.ajax({url:"/warehouse/mutations/get-product-stock",method:"GET",data:{product_id:t,dealer_id:a},beforeSend:function(){e.find(".available-stock").html('<i class="la la-spinner la-spin"></i>')},success:function(t){var a=parseFloat(t.current_stock);e.find(".available-stock").text(a.toLocaleString()),e.find(".quantity-input").attr("max",a),a<=0?(e.find(".available-stock").addClass("text-danger").removeClass("text-muted"),e.find(".quantity-input").attr("readonly",!0).val("")):(e.find(".available-stock").removeClass("text-danger").addClass("text-muted"),e.find(".quantity-input").attr("readonly",!1))},error:function(){e.find(".available-stock").text("Error").addClass("text-danger")}})}e.length>0&&(a=e.html()),$("#from_dealer_id, #to_dealer_id").on("change",(function(){var t=$("#from_dealer_id").val(),a=$("#to_dealer_id").val();if(t&&a&&t===a)return $(this).val("").trigger("change"),Swal.fire({type:"error",title:"Oops...",text:"Dealer asal dan tujuan tidak boleh sama"}),!1;!function(){var t=$("#from_dealer_id").val();$(".product-row").each((function(){var a=$(this),e=a.find(".product-select").val();e&&t?i(e,t,a):(a.find(".available-stock").text("-"),a.find(".quantity-input").attr("max",""))}))}()})),$("#add-product").on("click",(function(){var e,i='\n <tr class="product-row" data-index="'.concat(e=t,'">\n <td>\n <select name="products[').concat(e,'][product_id]" class="form-control product-select" required>\n ').concat(a,'\n </select>\n </td>\n <td class="text-center">\n <span class="available-stock text-muted">-</span>\n </td>\n <td>\n <input type="number" \n name="products[').concat(e,'][quantity_requested]" \n class="form-control quantity-input" \n min="0.01" \n step="0.01" \n placeholder="0" \n required>\n </td>\n\n <td>\n <button type="button" class="btn btn-danger btn-sm remove-product">\n <i class="la la-trash"></i>\n </button>\n </td>\n </tr>\n ');$("#products-tbody").append(i),$('#products-tbody tr[data-index="'.concat(t,'"] .product-select')).select2({placeholder:"Pilih...",allowClear:!0}),t++,n()})),$(document).on("click",".remove-product",(function(){$(this).closest("tr").remove(),n(),$(".product-row").each((function(t){$(this).attr("data-index",t),$(this).find('select[name*="product_id"]').attr("name","products[".concat(t,"][product_id]")),$(this).find('input[name*="quantity_requested"]').attr("name","products[".concat(t,"][quantity_requested]"))})),t=$(".product-row").length})),$(document).on("change",".product-select",(function(){var t=$(this).closest("tr"),a=$(this).val(),e=$("#from_dealer_id").val();a&&e?i(a,e,t):(t.find(".available-stock").text("-"),t.find(".quantity-input").attr("max",""))})),$(document).on("input",".quantity-input",(function(){var t=parseFloat($(this).attr("max")),a=parseFloat($(this).val());t&&a>t?($(this).val(t),$(this).addClass("is-invalid"),$(this).siblings(".invalid-feedback").length||$(this).after('<div class="invalid-feedback">Quantity melebihi stock yang tersedia</div>')):($(this).removeClass("is-invalid"),$(this).siblings(".invalid-feedback").remove())})),$("#mutation-form").on("submit",(function(t){if(t.preventDefault(),!function(){var t=!0,a=$("#from_dealer_id").val(),e=$("#to_dealer_id").val();if(!a)return Swal.fire({type:"error",title:"Oops...",text:"Pilih dealer asal"}),!1;if(!e)return Swal.fire({type:"error",title:"Oops...",text:"Pilih dealer tujuan"}),!1;if(a===e)return Swal.fire({type:"error",title:"Oops...",text:"Dealer asal dan tujuan tidak boleh sama"}),!1;var n=$(".product-row");if(0===n.length)return Swal.fire({type:"error",title:"Oops...",text:"Tambahkan minimal satu produk"}),!1;var i=!1;return n.each((function(){var t=$(this).find(".product-select").val(),a=$(this).find(".quantity-input").val();t&&a&&parseFloat(a)>0&&(i=!0)})),i?t:(Swal.fire({type:"error",title:"Oops...",text:"Pilih minimal satu produk dengan quantity yang valid"}),!1)}())return!1;var a=$("#submit-btn");a.html(),a.prop("disabled",!0).html('<i class="la la-spinner la-spin"></i> Menyimpan...'),this.submit()}))}));
//# sourceMappingURL=create.js.map