partial update create mutations

This commit is contained in:
2025-06-12 00:33:59 +07:00
parent 0b211915f1
commit a5e1348436
32 changed files with 2883 additions and 548 deletions

View File

@@ -0,0 +1,261 @@
$(document).ready(function () {
// Initialize DataTable
var table = $("#mutations-table").DataTable({
processing: true,
serverSide: true,
ajax: {
url: $("#mutations-table").data("url"),
type: "GET",
},
columns: [
{
data: "DT_RowIndex",
name: "DT_RowIndex",
orderable: false,
searchable: false,
width: "5%",
},
{
data: "mutation_number",
name: "mutation_number",
width: "12%",
},
{
data: "created_at",
name: "created_at",
width: "12%",
},
{
data: "from_dealer",
name: "fromDealer.name",
width: "15%",
},
{
data: "to_dealer",
name: "toDealer.name",
width: "15%",
},
{
data: "requested_by",
name: "requestedBy.name",
width: "12%",
},
{
data: "total_items",
name: "total_items",
width: "8%",
className: "text-center",
},
{
data: "status",
name: "status",
width: "12%",
className: "text-center",
},
{
data: "action",
name: "action",
orderable: false,
searchable: false,
width: "15%",
className: "text-center",
},
],
order: [[2, "desc"]], // Order by created_at desc
pageLength: 10,
responsive: true,
language: {
processing: "Memuat data...",
lengthMenu: "Tampilkan _MENU_ data per halaman",
zeroRecords: "Data tidak ditemukan",
info: "Menampilkan _START_ sampai _END_ dari _TOTAL_ data",
infoEmpty: "Menampilkan 0 sampai 0 dari 0 data",
infoFiltered: "(difilter dari _MAX_ total data)",
},
});
// Handle Receive Button Click
$(document).on("click", ".btn-receive", function () {
var mutationId = $(this).data("id");
$("#receiveModal" + mutationId).modal("show");
});
// Handle Approve Button Click
$(document).on("click", ".btn-approve", function () {
var mutationId = $(this).data("id");
// Load mutation details via AJAX
$.ajax({
url: "/warehouse/mutations/" + mutationId + "/details",
type: "GET",
beforeSend: function () {
$("#mutation-details" + mutationId).html(
'<div class="text-center">' +
'<div class="spinner-border" role="status">' +
'<span class="sr-only">Loading...</span>' +
"</div>" +
"<p>Memuat detail produk...</p>" +
"</div>"
);
},
success: function (response) {
var detailsHtml = "<h6>Detail Produk:</h6>";
detailsHtml += '<div class="table-responsive">';
detailsHtml += '<table class="table table-sm">';
detailsHtml += "<thead>";
detailsHtml += "<tr>";
detailsHtml += "<th>Produk</th>";
detailsHtml += "<th>Diminta</th>";
detailsHtml += "<th>Disetujui</th>";
detailsHtml += "<th>Stock Tersedia</th>";
detailsHtml += "</tr>";
detailsHtml += "</thead>";
detailsHtml += "<tbody>";
response.details.forEach(function (detail, index) {
detailsHtml += "<tr>";
detailsHtml += "<td>" + detail.product.name + "</td>";
detailsHtml +=
"<td>" +
parseFloat(detail.quantity_requested).toLocaleString() +
"</td>";
detailsHtml += "<td>";
detailsHtml +=
'<input type="number" name="details[' +
detail.id +
'][quantity_approved]" ';
detailsHtml += 'class="form-control form-control-sm" ';
detailsHtml += 'value="' + detail.quantity_requested + '" ';
detailsHtml +=
'min="0" max="' +
Math.min(
detail.quantity_requested,
detail.available_stock
) +
'" ';
detailsHtml += 'step="0.01" required>';
detailsHtml += "</td>";
detailsHtml +=
"<td>" +
parseFloat(detail.available_stock).toLocaleString() +
"</td>";
detailsHtml += "</tr>";
});
detailsHtml += "</tbody>";
detailsHtml += "</table>";
detailsHtml += "</div>";
$("#mutation-details" + mutationId).html(detailsHtml);
},
error: function () {
$("#mutation-details" + mutationId).html(
'<div class="alert alert-danger">Gagal memuat detail produk</div>'
);
},
});
$("#approveModal" + mutationId).modal("show");
});
// Handle other button clicks
$(document).on("click", ".btn-reject", function () {
var mutationId = $(this).data("id");
$("#rejectModal" + mutationId).modal("show");
});
$(document).on("click", ".btn-complete", function () {
var mutationId = $(this).data("id");
$("#completeModal" + mutationId).modal("show");
});
// Handle Cancel Button Click with SweetAlert
$(document).on("click", ".btn-cancel", function () {
var mutationId = $(this).data("id");
if (typeof Swal !== "undefined") {
Swal.fire({
title: "Batalkan Mutasi?",
text: "Apakah Anda yakin ingin membatalkan mutasi ini?",
icon: "warning",
showCancelButton: true,
confirmButtonColor: "#d33",
cancelButtonColor: "#3085d6",
confirmButtonText: "Ya, Batalkan",
cancelButtonText: "Batal",
}).then((result) => {
if (result.isConfirmed) {
cancelMutation(mutationId);
}
});
} else {
if (confirm("Apakah Anda yakin ingin membatalkan mutasi ini?")) {
cancelMutation(mutationId);
}
}
});
function cancelMutation(mutationId) {
$.ajax({
url: "/warehouse/mutations/" + mutationId + "/cancel",
type: "POST",
data: {
_token: $('meta[name="csrf-token"]').attr("content"),
},
success: function (response) {
if (typeof Swal !== "undefined") {
Swal.fire({
title: "Berhasil!",
text: "Mutasi berhasil dibatalkan",
icon: "success",
timer: 2000,
showConfirmButton: false,
});
} else {
alert("Mutasi berhasil dibatalkan");
}
table.ajax.reload();
},
error: function (xhr) {
var errorMsg =
xhr.responseJSON?.message || "Gagal membatalkan mutasi";
if (typeof Swal !== "undefined") {
Swal.fire({
title: "Error!",
text: errorMsg,
icon: "error",
});
} else {
alert("Error: " + errorMsg);
}
},
});
}
// Handle form submissions with loading state
$(document).on("submit", ".approve-form", function () {
$(this)
.find('button[type="submit"]')
.prop("disabled", true)
.html("Memproses...");
});
// Auto-calculate approved quantity based on available stock
$(document).on("input", 'input[name*="quantity_approved"]', function () {
var maxValue = parseFloat($(this).attr("max"));
var currentValue = parseFloat($(this).val());
if (maxValue && currentValue > maxValue) {
$(this).val(maxValue);
$(this).addClass("is-invalid");
if (!$(this).siblings(".invalid-feedback").length) {
$(this).after(
'<div class="invalid-feedback">Jumlah melebihi stock yang tersedia</div>'
);
}
} else {
$(this).removeClass("is-invalid");
$(this).siblings(".invalid-feedback").remove();
}
});
});