535 lines
20 KiB
PHP
Executable File
535 lines
20 KiB
PHP
Executable File
@extends('layouts.backapp')
|
|
|
|
@section('content')
|
|
<div class="kt-portlet kt-portlet--mobile" id="kt_blockui_datatable">
|
|
<div class="kt-portlet__head kt-portlet__head--lg">
|
|
<div class="kt-portlet__head-label">
|
|
<span class="kt-portlet__head-icon">
|
|
<i class="kt-font-brand flaticon2-plus"></i>
|
|
</span>
|
|
<h3 class="kt-portlet__head-title">Tambah Opnames</h3>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="kt-portlet__body">
|
|
@if ($errors->any())
|
|
<div class="alert alert-danger">
|
|
<ul class="mb-0">
|
|
@foreach ($errors->all() as $error)
|
|
<li>{{ $error }}</li>
|
|
@endforeach
|
|
</ul>
|
|
</div>
|
|
@endif
|
|
|
|
<form id="opname-form" action="{{ route('opnames.store') }}" method="POST">
|
|
@csrf
|
|
<div class="row">
|
|
<div class="col-md-6">
|
|
<div class="form-group">
|
|
<label for="dealer">Dealer <span class="text-danger">*</span></label>
|
|
@php
|
|
$oldDealer = old('dealer');
|
|
$dealerValue = is_array($oldDealer) ? '' : $oldDealer;
|
|
@endphp
|
|
<select name="dealer" id="dealer" class="form-control select2 @error('dealer') is-invalid @enderror" required>
|
|
<option value="">Pilih Dealer</option>
|
|
@foreach($dealers as $dealer)
|
|
<option value="{{ $dealer->id }}" {{ $dealerValue == $dealer->id ? 'selected' : '' }}>
|
|
{{ $dealer->name }}
|
|
</option>
|
|
@endforeach
|
|
</select>
|
|
@error('dealer')
|
|
<div class="invalid-feedback">{{ $message }}</div>
|
|
@enderror
|
|
</div>
|
|
</div>
|
|
<div class="col-md-6">
|
|
<div class="form-group">
|
|
<label for="note">Catatan</label>
|
|
@php
|
|
$oldNote = old('note');
|
|
$noteValue = is_array($oldNote) ? '' : $oldNote;
|
|
@endphp
|
|
<textarea name="note" id="note" class="form-control @error('note') is-invalid @enderror"
|
|
rows="2" placeholder="Catatan opname">{{ $noteValue }}</textarea>
|
|
@error('note')
|
|
<div class="invalid-feedback">{{ $message }}</div>
|
|
@enderror
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="kt-separator kt-separator--border-dashed kt-separator--space-lg kt-separator--portlet-fit"></div>
|
|
|
|
<div class="row mb-3">
|
|
<div class="col">
|
|
<h4>Detail Produk</h4>
|
|
</div>
|
|
<div class="col text-right">
|
|
<button type="button" class="btn btn-success btn-sm" id="btn-add-row">
|
|
<i class="flaticon2-plus"></i> Tambah Produk
|
|
</button>
|
|
</div>
|
|
</div>
|
|
|
|
<div class="table-responsive">
|
|
<table class="table table-bordered table-hover" id="product-table">
|
|
<thead class="thead-light">
|
|
<tr>
|
|
<th style="width: 35%">Produk <span class="text-danger">*</span></th>
|
|
<th style="width: 15%">Stok Sistem <span class="text-danger">*</span></th>
|
|
<th style="width: 15%">Stok Fisik <span class="text-danger">*</span></th>
|
|
<th style="width: 30%">Catatan</th>
|
|
<th style="width: 5%" class="text-center">Aksi</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
@php
|
|
$oldProducts = old('product', []);
|
|
$oldSystemQuantities = old('system_quantity', []);
|
|
$oldPhysicalQuantities = old('physical_quantity', []);
|
|
$oldItemNotes = old('item_notes', []);
|
|
@endphp
|
|
<tr class="product-row">
|
|
<td>
|
|
<select name="product[0]" class="form-control product-select select2 @error('product.0') is-invalid @enderror" required>
|
|
<option value="">Pilih Produk</option>
|
|
@foreach($products as $product)
|
|
<option value="{{ $product->id }}" {{ (isset($oldProducts[0]) && $oldProducts[0] == $product->id) ? 'selected' : '' }}>
|
|
{{ $product->name }}
|
|
</option>
|
|
@endforeach
|
|
</select>
|
|
@error('product.0')
|
|
<div class="invalid-feedback d-block">{{ $message }}</div>
|
|
@enderror
|
|
</td>
|
|
<td>
|
|
<div class="input-group">
|
|
<input type="number" name="system_quantity[0]"
|
|
class="form-control system-quantity @error('system_quantity.0') is-invalid @enderror"
|
|
step="0.01" min="0"
|
|
value="{{ $oldSystemQuantities[0] ?? '0' }}"
|
|
readonly>
|
|
</div>
|
|
@error('system_quantity.0')
|
|
<div class="invalid-feedback d-block">{{ $message }}</div>
|
|
@enderror
|
|
</td>
|
|
<td>
|
|
<div class="input-group">
|
|
<input type="number" name="physical_quantity[0]"
|
|
class="form-control @error('physical_quantity.0') is-invalid @enderror"
|
|
step="0.01" min="0"
|
|
value="{{ $oldPhysicalQuantities[0] ?? '' }}"
|
|
required>
|
|
</div>
|
|
@error('physical_quantity.0')
|
|
<div class="invalid-feedback d-block">{{ $message }}</div>
|
|
@enderror
|
|
</td>
|
|
<td>
|
|
<input type="text" name="item_notes[0]"
|
|
class="form-control @error('item_notes.0') is-invalid @enderror"
|
|
value="{{ $oldItemNotes[0] ?? '' }}"
|
|
placeholder="(wajib jika ada perbedaan stock)">
|
|
@error('item_notes.0')
|
|
<div class="invalid-feedback d-block">{{ $message }}</div>
|
|
@enderror
|
|
</td>
|
|
<td class="text-center">
|
|
<button type="button" class="btn btn-danger btn-sm btn-remove-row" disabled>
|
|
<i class="flaticon2-trash"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
|
|
<div class="kt-separator kt-separator--border-dashed kt-separator--space-lg kt-separator--portlet-fit"></div>
|
|
|
|
<div class="form-group">
|
|
<button type="submit" class="btn btn-primary btn-md">
|
|
Simpan
|
|
</button>
|
|
<a href="{{ route('opnames.index') }}" class="btn btn-secondary btn-md">
|
|
Batal
|
|
</a>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Template untuk baris produk baru -->
|
|
<template id="product-row-template">
|
|
<tr class="product-row">
|
|
<td>
|
|
<select name="product[]" class="form-control product-select select2" required>
|
|
<option value="">Pilih Produk</option>
|
|
@foreach($products as $product)
|
|
<option value="{{ $product->id }}">{{ $product->name }}</option>
|
|
@endforeach
|
|
</select>
|
|
</td>
|
|
<td>
|
|
<div class="input-group">
|
|
<input type="number" name="system_quantity[]" class="form-control system-quantity"
|
|
step="0.01" min="0" value="0" readonly>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<div class="input-group">
|
|
<input type="number" name="physical_quantity[]" class="form-control"
|
|
step="0.01" min="0" value="" required>
|
|
</div>
|
|
</td>
|
|
<td>
|
|
<input type="text" name="item_notes[]" class="form-control"
|
|
value="" placeholder="(wajib jika ada perbedaan stock)">
|
|
</td>
|
|
<td class="text-center">
|
|
<button type="button" class="btn btn-danger btn-sm btn-remove-row">
|
|
<i class="flaticon2-trash"></i>
|
|
</button>
|
|
</td>
|
|
</tr>
|
|
</template>
|
|
@endsection
|
|
|
|
@section('javascripts')
|
|
<script>
|
|
$(document).ready(function () {
|
|
console.log("Opnames create.js loaded - SweetAlert version");
|
|
|
|
$(".select2").select2({
|
|
placeholder: "Pilih...",
|
|
allowClear: true,
|
|
});
|
|
|
|
// Initialize select2 for all product selects
|
|
function initializeProductSelects() {
|
|
$(".product-select").select2({
|
|
placeholder: "Pilih Produk...",
|
|
allowClear: true,
|
|
width: "100%",
|
|
});
|
|
}
|
|
|
|
// Initial initialization
|
|
initializeProductSelects();
|
|
|
|
// 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]);
|
|
}
|
|
});
|
|
|
|
// Handle physical quantity changes using event delegation
|
|
$(document).on(
|
|
"change input",
|
|
'input[name^="physical_quantity"]',
|
|
function () {
|
|
calculateDifference(this);
|
|
}
|
|
);
|
|
|
|
// 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("");
|
|
|
|
// Append to DOM first
|
|
tbody.append(newRow);
|
|
|
|
// Initialize select2 for the new row AFTER it's added to DOM
|
|
tbody.find("tr:last-child .product-select").select2({
|
|
placeholder: "Pilih Produk...",
|
|
allowClear: true,
|
|
width: "100%",
|
|
});
|
|
|
|
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) {
|
|
const $row = $(this);
|
|
const $select = $row.find('select[name^="product"]');
|
|
|
|
// Destroy select2 before changing attributes
|
|
if ($select.data("select2")) {
|
|
$select.select2("destroy");
|
|
}
|
|
|
|
$select.attr("name", `product[${index}]`);
|
|
$row.find('input[name^="system_quantity"]').attr(
|
|
"name",
|
|
`system_quantity[${index}]`
|
|
);
|
|
$row.find('input[name^="physical_quantity"]').attr(
|
|
"name",
|
|
`physical_quantity[${index}]`
|
|
);
|
|
$row.find('input[name^="item_notes"]').attr(
|
|
"name",
|
|
`item_notes[${index}]`
|
|
);
|
|
|
|
// Reinitialize select2
|
|
$select.select2({
|
|
placeholder: "Pilih Produk...",
|
|
allowClear: true,
|
|
width: "100%",
|
|
});
|
|
});
|
|
}
|
|
|
|
// Update calculateDifference function - make it globally accessible
|
|
window.calculateDifference = function (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"]');
|
|
|
|
// Round both values to 2 decimal places for comparison
|
|
const roundedSystemQty = Math.round(systemQty * 100) / 100;
|
|
const roundedPhysicalQty = Math.round(physicalQty * 100) / 100;
|
|
|
|
if (roundedSystemQty !== roundedPhysicalQty) {
|
|
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();
|
|
Swal.fire({
|
|
icon: "error",
|
|
title: "Oops...",
|
|
text: "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();
|
|
Swal.fire({
|
|
icon: "error",
|
|
title: "Oops...",
|
|
text: "Produk tidak boleh duplikat!",
|
|
});
|
|
return false;
|
|
}
|
|
|
|
// Cek produk kosong
|
|
if (products.includes("")) {
|
|
e.preventDefault();
|
|
Swal.fire({
|
|
icon: "error",
|
|
title: "Oops...",
|
|
text: "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();
|
|
|
|
// Round both values to 2 decimal places for comparison
|
|
const roundedSystemQty = Math.round(systemQty * 100) / 100;
|
|
const roundedPhysicalQty = Math.round(physicalQty * 100) / 100;
|
|
|
|
if (roundedSystemQty !== roundedPhysicalQty && !note) {
|
|
hasInvalidNotes = true;
|
|
$(this).addClass("table-danger");
|
|
}
|
|
});
|
|
|
|
if (hasInvalidNotes) {
|
|
e.preventDefault();
|
|
Swal.fire({
|
|
icon: "error",
|
|
title: "Oops...",
|
|
text: "Catatan wajib diisi untuk produk yang memiliki perbedaan stock!",
|
|
});
|
|
return false;
|
|
}
|
|
});
|
|
|
|
// Initial stock data load if dealer is selected
|
|
if ($("#dealer").val()) {
|
|
fetchStockData();
|
|
}
|
|
});
|
|
|
|
</script>
|
|
@endsection
|