fix: restore working delete buttons with custom dialogs across modules and separate out-of-stock vs low-stock warning
This commit is contained in:
1 parent
f895f522d0
commit
199247a9a7
6 files changed
+347
-121
No files matched your search
@@ -545,9 +545,9 @@ export async function DELETE(req: Request) {
|
||||
return NextResponse.json({ error: "Sesi tidak valid" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (user.role !== "ADMIN" && user.role !== "MANAGER") {
|
||||
if (!["ADMIN", "MANAGER", "INVENTORY"].includes(user.role)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN) atau Manager yang dapat menghapus produk." },
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN), Manager, atau Staf Gudang yang dapat menghapus produk." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
@@ -563,9 +563,20 @@ export async function DELETE(req: Request) {
|
||||
return NextResponse.json({ error: "Produk tidak ditemukan" }, { status: 404 });
|
||||
}
|
||||
|
||||
// Bersihkan seluruh dependensi data produk secara terstruktur
|
||||
// Bersihkan seluruh dependensi data produk secara terstruktur agar tidak melanggar foreign key
|
||||
await prisma.stockItem.deleteMany({ where: { productId: id } });
|
||||
await prisma.stockMovement.deleteMany({ where: { productId: id } });
|
||||
|
||||
// Hapus alokasi batch yang terkait dengan produk ini
|
||||
await prisma.salesItemBatchAllocation.deleteMany({
|
||||
where: {
|
||||
OR: [
|
||||
{ purchaseBatch: { productId: id } },
|
||||
{ salesOrderItem: { productId: id } },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
await prisma.purchaseBatch.deleteMany({ where: { productId: id } });
|
||||
await prisma.purchaseOrderItem.deleteMany({ where: { productId: id } });
|
||||
await prisma.purchaseRequisitionItem.deleteMany({ where: { productId: id } });
|
||||
@@ -578,7 +589,7 @@ export async function DELETE(req: Request) {
|
||||
userName: user.name,
|
||||
action: "DELETE",
|
||||
module: "INVENTORY",
|
||||
description: `[SUPERADMIN] Menghapus produk ${product.name} (${product.sku}) secara permanen`,
|
||||
description: `[${user.role}] Menghapus produk ${product.name} (${product.sku}) secara permanen`,
|
||||
metadata: { productId: id, name: product.name, sku: product.sku },
|
||||
});
|
||||
|
||||
|
||||
@@ -1188,9 +1188,9 @@ export async function DELETE(req: Request) {
|
||||
return NextResponse.json({ error: "Sesi tidak valid" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (user.role !== "ADMIN" && user.role !== "MANAGER") {
|
||||
if (!["ADMIN", "MANAGER", "PURCHASING"].includes(user.role)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN) atau Manager yang memiliki wewenang menghapus data pembelian." },
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN), Manager, atau Tim Purchasing yang memiliki wewenang menghapus data pembelian." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1106,9 +1106,9 @@ export async function DELETE(req: Request) {
|
||||
return NextResponse.json({ error: "Sesi tidak valid" }, { status: 401 });
|
||||
}
|
||||
|
||||
if (user.role !== "ADMIN" && user.role !== "MANAGER") {
|
||||
if (!["ADMIN", "MANAGER", "SALES"].includes(user.role)) {
|
||||
return NextResponse.json(
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN) atau Manager yang memiliki wewenang menghapus Sales Order." },
|
||||
{ error: "Akses ditolak: Hanya Superadmin (ADMIN), Manager, atau Tim Penjualan yang memiliki wewenang menghapus Sales Order." },
|
||||
{ status: 403 }
|
||||
);
|
||||
}
|
||||
|
||||
+129
-23
@@ -101,7 +101,7 @@ export default function InventoryPage() {
|
||||
unit: "PCS",
|
||||
purchasePrice: 0,
|
||||
sellingPrice: 0,
|
||||
minStock: 5,
|
||||
minStock: 0,
|
||||
categoryId: "",
|
||||
warehouseId: "",
|
||||
initialQuantity: 0,
|
||||
@@ -115,6 +115,12 @@ export default function InventoryPage() {
|
||||
notes: "",
|
||||
});
|
||||
|
||||
// State Konfirmasi Hapus Produk (Bebas dari window.confirm yang sering diblokir browser)
|
||||
const [productToDelete, setProductToDelete] = useState<any>(null);
|
||||
const [isDeletingProduct, setIsDeletingProduct] = useState(false);
|
||||
const [deleteError, setDeleteError] = useState<string | null>(null);
|
||||
const [notificationMsg, setNotificationMsg] = useState<string | null>(null);
|
||||
|
||||
const [formError, setFormError] = useState<string | null>(null);
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
|
||||
@@ -173,6 +179,10 @@ export default function InventoryPage() {
|
||||
currentUser?.role
|
||||
);
|
||||
|
||||
const canDeleteProduct = ["ADMIN", "MANAGER", "INVENTORY"].includes(
|
||||
currentUser?.role
|
||||
);
|
||||
|
||||
// Sembunyikan nominal uang untuk staf gudang & purchasing sesuai aturan bisnis
|
||||
const canSeePrices = ["ADMIN", "MANAGER", "SALES", "ACCOUNTANT"].includes(
|
||||
currentUser?.role
|
||||
@@ -361,22 +371,22 @@ export default function InventoryPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteProduct = async (p: any) => {
|
||||
if (
|
||||
!confirm(
|
||||
`[SUPERADMIN] Apakah Anda yakin ingin menghapus produk "${p.name}" (${p.sku}) secara permanen? Seluruh data stok dan riwayat mutasi terkait produk ini akan dihapus.`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const handleConfirmDeleteProduct = async () => {
|
||||
if (!productToDelete) return;
|
||||
setIsDeletingProduct(true);
|
||||
setDeleteError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/inventory?id=${p.id}`, { method: "DELETE" });
|
||||
const res = await fetch(`/api/inventory?id=${productToDelete.id}`, { method: "DELETE" });
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
alert(data.message || "Produk berhasil dihapus.");
|
||||
loadData();
|
||||
if (!res.ok) throw new Error(data.error || "Gagal menghapus produk");
|
||||
setNotificationMsg(data.message || `Produk "${productToDelete.name}" berhasil dihapus.`);
|
||||
setProductToDelete(null);
|
||||
await loadData();
|
||||
setTimeout(() => setNotificationMsg(null), 5000);
|
||||
} catch (err: any) {
|
||||
alert(err.message || "Gagal menghapus produk");
|
||||
setDeleteError(err.message || "Gagal menghapus produk");
|
||||
} finally {
|
||||
setIsDeletingProduct(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -407,7 +417,21 @@ export default function InventoryPage() {
|
||||
const paginatedMovements = filteredMovements.slice((movPage - 1) * movPageSize, movPage * movPageSize);
|
||||
|
||||
const totalPhysicalUnits = products.reduce((s, p) => s + getProductStockForPT(p), 0);
|
||||
const lowStockCount = products.filter((p) => getProductStockForPT(p) <= p.minStock).length;
|
||||
|
||||
// Filter produk fisik yang stoknya benar-benar ada (>0) tapi di bawah atau sama dengan batas aman minimum
|
||||
const lowStockProducts = products.filter((p) => {
|
||||
if (p.isService) return false;
|
||||
const stock = getProductStockForPT(p);
|
||||
return stock > 0 && stock <= p.minStock;
|
||||
});
|
||||
const lowStockCount = lowStockProducts.length;
|
||||
|
||||
// Filter produk fisik yang stoknya benar-benar 0 (Stok Kosong / Belum Ada Barang Fisik)
|
||||
const outOfStockProducts = products.filter((p) => {
|
||||
if (p.isService) return false;
|
||||
return getProductStockForPT(p) === 0;
|
||||
});
|
||||
const outOfStockCount = outOfStockProducts.length;
|
||||
|
||||
const handleExportCSV = () => {
|
||||
if (activeTab === "catalog") {
|
||||
@@ -495,15 +519,37 @@ export default function InventoryPage() {
|
||||
{lowStockCount} Barang
|
||||
</div>
|
||||
<span className="text-[11px] text-slate-500">
|
||||
Di bawah batas minimum aman
|
||||
{lowStockCount > 0
|
||||
? `Di bawah batas minimum aman${outOfStockCount > 0 ? ` • ${outOfStockCount} Stok Kosong` : ""}`
|
||||
: outOfStockCount > 0
|
||||
? `${outOfStockCount} barang stok fisik 0 (kosong)`
|
||||
: "Semua stok dalam batas aman"}
|
||||
</span>
|
||||
</div>
|
||||
<div className="h-10 w-10 rounded-xl bg-rose-50 text-rose-600 flex items-center justify-center">
|
||||
<div
|
||||
className={`h-10 w-10 rounded-xl flex items-center justify-center ${
|
||||
lowStockCount > 0 ? "bg-rose-50 text-rose-600" : "bg-slate-50 text-slate-400"
|
||||
}`}
|
||||
>
|
||||
<AlertCircle className="h-5 w-5" />
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Banner Notifikasi Aksi */}
|
||||
{notificationMsg && (
|
||||
<div className="mb-4 p-3 bg-emerald-50 border border-emerald-200 text-emerald-800 rounded-lg text-xs font-medium flex items-center justify-between">
|
||||
<span>✓ {notificationMsg}</span>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setNotificationMsg(null)}
|
||||
className="text-emerald-700 hover:text-emerald-900 font-bold ml-2"
|
||||
>
|
||||
✕
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
|
||||
{/* Header Controls */}
|
||||
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4 mb-6">
|
||||
<div className="flex items-center p-1 bg-slate-200/80 rounded-lg text-xs font-semibold text-slate-600 w-fit">
|
||||
@@ -635,7 +681,8 @@ export default function InventoryPage() {
|
||||
) : (
|
||||
paginatedProducts.map((p) => {
|
||||
const pStock = getProductStockForPT(p);
|
||||
const isLow = !p.isService && pStock <= p.minStock;
|
||||
const isOutOfStock = !p.isService && pStock === 0;
|
||||
const isLow = !p.isService && pStock > 0 && pStock <= p.minStock;
|
||||
const relevantStockItems = (p.stockItems || []).filter((s: any) =>
|
||||
selectedCompanyFilter === "ALL" || (s.warehouse?.companyName || "PT EnterpriseCore Utama") === selectedCompanyFilter
|
||||
);
|
||||
@@ -692,6 +739,10 @@ export default function InventoryPage() {
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-bold bg-purple-50 text-purple-700 border border-purple-200">
|
||||
🌐 Non-Fisik ({p.unit})
|
||||
</span>
|
||||
) : isOutOfStock ? (
|
||||
<span className="inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-semibold bg-slate-100 text-slate-600 border border-slate-200">
|
||||
0 {p.unit} (Stok Kosong)
|
||||
</span>
|
||||
) : (
|
||||
<span
|
||||
className={`inline-flex items-center gap-1 px-2.5 py-1 rounded-full text-xs font-bold ${
|
||||
@@ -702,7 +753,7 @@ export default function InventoryPage() {
|
||||
>
|
||||
{isLow && <AlertCircle className="h-3 w-3" />}
|
||||
<span>
|
||||
{pStock} {p.unit}
|
||||
{pStock} {p.unit} {isLow ? "(Menipis)" : ""}
|
||||
</span>
|
||||
</span>
|
||||
)}
|
||||
@@ -750,11 +801,15 @@ export default function InventoryPage() {
|
||||
<span>Mutasi Stok</span>
|
||||
</button>
|
||||
)}
|
||||
{(currentUser?.role === "ADMIN" || currentUser?.role === "MANAGER") && (
|
||||
{canDeleteProduct && (
|
||||
<button
|
||||
onClick={() => handleDeleteProduct(p)}
|
||||
className="inline-flex items-center gap-1 px-2 py-1.5 rounded-md bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-xs font-medium"
|
||||
title="Hapus Produk Secara Permanen (Superadmin/Manager)"
|
||||
type="button"
|
||||
onClick={() => {
|
||||
setDeleteError(null);
|
||||
setProductToDelete(p);
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2 py-1.5 rounded-md bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-xs font-semibold shadow-2xs"
|
||||
title="Hapus Produk Secara Permanen"
|
||||
>
|
||||
<Trash2 className="h-3 w-3 text-rose-600" />
|
||||
<span>Hapus</span>
|
||||
@@ -1526,6 +1581,57 @@ export default function InventoryPage() {
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
{/* Modal Dialog Konfirmasi Hapus Produk (Aman dari pembatasan browser confirm) */}
|
||||
<Modal
|
||||
isOpen={!!productToDelete}
|
||||
onClose={() => !isDeletingProduct && setProductToDelete(null)}
|
||||
title="Konfirmasi Hapus Produk"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<div className="p-3 bg-rose-50 border border-rose-200 rounded-lg flex items-start gap-3">
|
||||
<div className="p-2 rounded-full bg-rose-100 text-rose-700 shrink-0">
|
||||
<Trash2 className="h-5 w-5" />
|
||||
</div>
|
||||
<div className="text-xs text-rose-800 space-y-1">
|
||||
<div className="font-bold text-sm">Hapus Produk Secara Permanen?</div>
|
||||
<p>
|
||||
Anda akan menghapus produk <span className="font-bold">"{productToDelete?.name}"</span> (SKU:{" "}
|
||||
<span className="font-mono font-semibold">{productToDelete?.sku}</span>).
|
||||
</p>
|
||||
<p className="text-[11px] text-rose-700">
|
||||
⚠️ Perhatian: Seluruh data riwayat mutasi stok, alokasi batch, dan keterkaitan data pada produk ini akan dihapus dari sistem.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{deleteError && (
|
||||
<div className="p-2.5 bg-rose-100 border border-rose-300 text-rose-800 rounded text-xs font-medium">
|
||||
{deleteError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-end gap-2 pt-2 border-t border-slate-200">
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeletingProduct}
|
||||
onClick={() => setProductToDelete(null)}
|
||||
className="px-4 py-2 border border-slate-300 rounded-lg text-slate-700 hover:bg-slate-100 text-xs font-semibold disabled:opacity-50"
|
||||
>
|
||||
Batal
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeletingProduct}
|
||||
onClick={handleConfirmDeleteProduct}
|
||||
className="px-4 py-2 bg-rose-600 hover:bg-rose-700 text-white rounded-lg text-xs font-semibold shadow-xs disabled:opacity-50 flex items-center gap-1.5"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
<span>{isDeletingProduct ? "Menghapus..." : "Ya, Hapus Permanen"}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
+120
-72
@@ -116,6 +116,21 @@ export default function PurchasesPage() {
|
||||
const [submitting, setSubmitting] = useState(false);
|
||||
const [formError, setFormError] = useState<string | null>(null);
|
||||
|
||||
// State Dialog Konfirmasi Hapus Data (Bebas dari window.confirm yang sering diblokir browser)
|
||||
const [deleteTarget, setDeleteTarget] = useState<{
|
||||
type: "po" | "batch" | "pr";
|
||||
id: string;
|
||||
title: string;
|
||||
subtitle: string;
|
||||
} | null>(null);
|
||||
const [isDeleting, setIsDeleting] = useState(false);
|
||||
const [deleteError, setDeleteError] = useState<string | null>(null);
|
||||
const [notificationMsg, setNotificationMsg] = useState<string | null>(null);
|
||||
|
||||
const canDeletePurchases = ["ADMIN", "MANAGER", "PURCHASING"].includes(
|
||||
currentUser?.role
|
||||
);
|
||||
|
||||
// Form PO
|
||||
const [selectedReqId, setSelectedReqId] = useState<string | null>(null);
|
||||
const [customPoNumber, setCustomPoNumber] = useState("");
|
||||
@@ -641,66 +656,25 @@ export default function PurchasesPage() {
|
||||
}
|
||||
};
|
||||
|
||||
// Superadmin / Manager Delete PO
|
||||
const handleDeletePO = async (po: any) => {
|
||||
const poId = po.id || po.purchaseOrderId || po.poNumber;
|
||||
if (
|
||||
!confirm(
|
||||
`[SUPERADMIN/MANAGER] Apakah Anda yakin ingin menghapus PO Vendor "${po.poNumber}" (${formatRupiah(
|
||||
po.totalAmount || 0
|
||||
)}) secara permanen? Data batch gudang dan alokasi stok terkait akan dihapus.`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
// Konfirmasi Penghapusan Data Pembelian via Modal Dialog (Bebas dari dialog browser confirm yang sering diblokir)
|
||||
const handleConfirmDeletePurchases = async () => {
|
||||
if (!deleteTarget) return;
|
||||
setIsDeleting(true);
|
||||
setDeleteError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/purchases?id=${poId}&type=po`, { method: "DELETE" });
|
||||
const res = await fetch(`/api/purchases?id=${deleteTarget.id}&type=${deleteTarget.type}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
alert(data.message || "PO Vendor berhasil dihapus.");
|
||||
loadData();
|
||||
if (!res.ok) throw new Error(data.error || "Gagal menghapus data");
|
||||
setNotificationMsg(data.message || "Data pembelian berhasil dihapus.");
|
||||
setDeleteTarget(null);
|
||||
await loadData();
|
||||
setTimeout(() => setNotificationMsg(null), 5000);
|
||||
} catch (err: any) {
|
||||
alert(err.message || "Gagal menghapus PO");
|
||||
}
|
||||
};
|
||||
|
||||
// Superadmin / Manager Delete Batch Saja
|
||||
const handleDeleteBatch = async (batch: any) => {
|
||||
if (
|
||||
!confirm(
|
||||
`[SUPERADMIN/MANAGER] Apakah Anda yakin ingin menghapus Batch FIFO "${batch.poNumber}" (${batch.product?.name})?`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`/api/purchases?id=${batch.id}&type=batch`, { method: "DELETE" });
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
alert(data.message || "Batch berhasil dihapus.");
|
||||
loadData();
|
||||
} catch (err: any) {
|
||||
alert(err.message || "Gagal menghapus batch");
|
||||
}
|
||||
};
|
||||
|
||||
// Superadmin Delete PR
|
||||
const handleDeletePR = async (req: any) => {
|
||||
if (
|
||||
!confirm(
|
||||
`[SUPERADMIN] Apakah Anda yakin ingin menghapus Pengajuan PR "${req.reqNumber}" secara permanen?`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const res = await fetch(`/api/purchases?id=${req.id}&type=pr`, { method: "DELETE" });
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
alert(data.message || "Pengajuan PR berhasil dihapus.");
|
||||
loadData();
|
||||
} catch (err: any) {
|
||||
alert(err.message || "Gagal menghapus PR");
|
||||
setDeleteError(err.message || "Gagal menghapus data");
|
||||
} finally {
|
||||
setIsDeleting(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1498,12 +1472,20 @@ export default function PurchasesPage() {
|
||||
</button>
|
||||
)}
|
||||
|
||||
{(currentUser?.role === "ADMIN" || currentUser?.role === "MANAGER") && (
|
||||
{canDeletePurchases && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDeletePO(po)}
|
||||
onClick={() => {
|
||||
setDeleteError(null);
|
||||
setDeleteTarget({
|
||||
type: "po",
|
||||
id: po.id || po.purchaseOrderId || po.poNumber,
|
||||
title: `Hapus PO Vendor "${po.poNumber}"`,
|
||||
subtitle: `Seluruh data batch gudang, alokasi stok penjualan, dan riwayat jurnal terkait PO ini (${formatRupiah(po.totalAmount || 0)}) akan dihapus secara permanen.`,
|
||||
});
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-[11px] font-semibold"
|
||||
title="Hapus PO Vendor Secara Permanen (Superadmin/Manager)"
|
||||
title="Hapus PO Vendor Secara Permanen"
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5 text-rose-600" />
|
||||
<span>Hapus</span>
|
||||
@@ -1661,12 +1643,20 @@ export default function PurchasesPage() {
|
||||
</span>
|
||||
) : null}
|
||||
|
||||
{(currentUser?.role === "ADMIN" || currentUser?.role === "MANAGER") && (
|
||||
{canDeletePurchases && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDeletePR(req)}
|
||||
onClick={() => {
|
||||
setDeleteError(null);
|
||||
setDeleteTarget({
|
||||
type: "pr",
|
||||
id: req.id || req.reqNumber,
|
||||
title: `Hapus Pengajuan PR "${req.reqNumber}"`,
|
||||
subtitle: `Pengajuan pembelian barang ini akan dihapus secara permanen.`,
|
||||
});
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-[11px] font-semibold ml-2"
|
||||
title="Hapus Pengajuan PR (Superadmin/Manager)"
|
||||
title="Hapus Pengajuan PR"
|
||||
>
|
||||
<Trash2 className="h-3.5 w-3.5 text-rose-600" />
|
||||
<span>Hapus</span>
|
||||
@@ -1900,17 +1890,19 @@ export default function PurchasesPage() {
|
||||
</td>
|
||||
|
||||
<td className="py-3.5 px-4 text-right align-top">
|
||||
{(currentUser?.role === "ADMIN" || currentUser?.role === "MANAGER") && (
|
||||
{canDeletePurchases && (
|
||||
<div className="flex flex-col items-end gap-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() =>
|
||||
handleDeletePO({
|
||||
id: b.purchaseOrderId || b.purchaseOrder?.id,
|
||||
poNumber: b.poNumber,
|
||||
totalAmount: b.initialQty * b.unitCost,
|
||||
})
|
||||
}
|
||||
onClick={() => {
|
||||
setDeleteError(null);
|
||||
setDeleteTarget({
|
||||
type: "po",
|
||||
id: b.purchaseOrderId || b.purchaseOrder?.id || b.poNumber,
|
||||
title: `Hapus Seluruh PO "${b.poNumber}"`,
|
||||
subtitle: `Data PO beserta seluruh batch dan alokasi terkait akan dihapus secara permanen.`,
|
||||
});
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2.5 py-1.5 rounded-lg bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-[11px] font-semibold whitespace-nowrap shadow-2xs"
|
||||
title="Hapus PO Vendor ini secara permanen"
|
||||
>
|
||||
@@ -1919,7 +1911,15 @@ export default function PurchasesPage() {
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDeleteBatch(b)}
|
||||
onClick={() => {
|
||||
setDeleteError(null);
|
||||
setDeleteTarget({
|
||||
type: "batch",
|
||||
id: b.id,
|
||||
title: `Hapus Batch FIFO "${b.poNumber}"`,
|
||||
subtitle: `Record batch untuk produk ${b.product?.name || "ini"} akan dihapus dari gudang.`,
|
||||
});
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2 py-1 rounded bg-slate-100 hover:bg-rose-50 text-slate-600 hover:text-rose-700 border border-slate-200 text-[10px] font-medium transition-colors whitespace-nowrap"
|
||||
title="Hapus hanya record batch FIFO ini"
|
||||
>
|
||||
@@ -3033,6 +3033,54 @@ export default function PurchasesPage() {
|
||||
</div>
|
||||
</form>
|
||||
</Modal>
|
||||
|
||||
{/* Modal Dialog Konfirmasi Hapus Data Pembelian (Bebas dari browser confirm blocking) */}
|
||||
<Modal
|
||||
isOpen={!!deleteTarget}
|
||||
onClose={() => !isDeleting && setDeleteTarget(null)}
|
||||
title="Konfirmasi Hapus Data Pembelian"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<div className="p-3 bg-rose-50 border border-rose-200 rounded-lg flex items-start gap-3">
|
||||
<div className="p-2 rounded-full bg-rose-100 text-rose-700 shrink-0">
|
||||
<Trash2 className="h-5 w-5" />
|
||||
</div>
|
||||
<div className="text-xs text-rose-800 space-y-1">
|
||||
<div className="font-bold text-sm">{deleteTarget?.title}</div>
|
||||
<p>{deleteTarget?.subtitle}</p>
|
||||
<p className="text-[11px] text-rose-700">
|
||||
⚠️ Tindakan ini permanen dan tidak dapat dibatalkan.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{deleteError && (
|
||||
<div className="p-2.5 bg-rose-100 border border-rose-300 text-rose-800 rounded text-xs font-medium">
|
||||
{deleteError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-end gap-2 pt-2 border-t border-slate-200">
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeleting}
|
||||
onClick={() => setDeleteTarget(null)}
|
||||
className="px-4 py-2 border border-slate-300 rounded-lg text-slate-700 hover:bg-slate-100 text-xs font-semibold disabled:opacity-50"
|
||||
>
|
||||
Batal
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeleting}
|
||||
onClick={handleConfirmDeletePurchases}
|
||||
className="px-4 py-2 bg-rose-600 hover:bg-rose-700 text-white rounded-lg text-xs font-semibold shadow-xs disabled:opacity-50 flex items-center gap-1.5"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
<span>{isDeleting ? "Menghapus..." : "Ya, Hapus Sekarang"}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
+79
-18
@@ -73,6 +73,12 @@ export default function SalesPage() {
|
||||
const [isRmaModalOpen, setIsRmaModalOpen] = useState(false);
|
||||
const [selectedRmaOrder, setSelectedRmaOrder] = useState<any>(null);
|
||||
const [selectedRmaItemId, setSelectedRmaItemId] = useState<string>("");
|
||||
|
||||
// State Dialog Konfirmasi Hapus Sales Order (Bebas dari dialog browser confirm yang sering diblokir)
|
||||
const [soToDelete, setSoToDelete] = useState<any>(null);
|
||||
const [isDeletingSO, setIsDeletingSO] = useState(false);
|
||||
const [deleteSOError, setDeleteSOError] = useState<string | null>(null);
|
||||
const [notificationMsg, setNotificationMsg] = useState<string | null>(null);
|
||||
const [rmaTab, setRmaTab] = useState<"SERVICE" | "REPLACE">("SERVICE");
|
||||
const [rmaServiceStatus, setRmaServiceStatus] = useState<string>("NEED_SERVICE");
|
||||
const [rmaServiceNotes, setRmaServiceNotes] = useState<string>("");
|
||||
@@ -183,6 +189,7 @@ export default function SalesPage() {
|
||||
}, []);
|
||||
|
||||
const canCreateOrder = ["ADMIN", "MANAGER", "SALES"].includes(currentUser?.role);
|
||||
const canDeleteSales = ["ADMIN", "MANAGER", "SALES"].includes(currentUser?.role);
|
||||
const canVerifyPayment = ["ADMIN", "MANAGER", "ACCOUNTANT"].includes(currentUser?.role);
|
||||
const canUpdateShipping = ["ADMIN", "MANAGER", "SALES", "INVENTORY"].includes(
|
||||
currentUser?.role
|
||||
@@ -702,24 +709,24 @@ export default function SalesPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteSalesOrder = async (order: any) => {
|
||||
if (
|
||||
!confirm(
|
||||
`[SUPERADMIN] Apakah Anda yakin ingin menghapus Sales Order "${order.orderNumber}" (${formatRupiah(
|
||||
order.totalAmount
|
||||
)}) secara permanen? Data mutasi keluar stok dan jurnal SO ini akan dihapus.`
|
||||
)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
const handleConfirmDeleteSalesOrder = async () => {
|
||||
if (!soToDelete) return;
|
||||
setIsDeletingSO(true);
|
||||
setDeleteSOError(null);
|
||||
try {
|
||||
const res = await fetch(`/api/sales?id=${order.id}`, { method: "DELETE" });
|
||||
const res = await fetch(`/api/sales?id=${soToDelete.id || soToDelete.orderNumber}`, {
|
||||
method: "DELETE",
|
||||
});
|
||||
const data = await res.json();
|
||||
if (!res.ok) throw new Error(data.error);
|
||||
alert(data.message || "Sales Order berhasil dihapus.");
|
||||
loadData();
|
||||
if (!res.ok) throw new Error(data.error || "Gagal menghapus Sales Order");
|
||||
setNotificationMsg(data.message || `Sales Order "${soToDelete.orderNumber}" berhasil dihapus.`);
|
||||
setSoToDelete(null);
|
||||
await loadData();
|
||||
setTimeout(() => setNotificationMsg(null), 5000);
|
||||
} catch (err: any) {
|
||||
alert(err.message || "Gagal menghapus Sales Order");
|
||||
setDeleteSOError(err.message || "Gagal menghapus Sales Order");
|
||||
} finally {
|
||||
setIsDeletingSO(false);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1352,12 +1359,15 @@ export default function SalesPage() {
|
||||
<span>Lengkap</span>
|
||||
</span>
|
||||
)}
|
||||
{(currentUser?.role === "ADMIN" || currentUser?.role === "MANAGER") && (
|
||||
{canDeleteSales && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => handleDeleteSalesOrder(o)}
|
||||
onClick={() => {
|
||||
setDeleteSOError(null);
|
||||
setSoToDelete(o);
|
||||
}}
|
||||
className="inline-flex items-center gap-1 px-2 py-1.5 rounded-md bg-rose-50 hover:bg-rose-100 text-rose-700 border border-rose-200 transition-colors text-[11px] font-semibold whitespace-nowrap shadow-2xs"
|
||||
title="Hapus Sales Order Secara Permanen (Superadmin/Manager)"
|
||||
title="Hapus Sales Order Secara Permanen"
|
||||
>
|
||||
<Trash2 className="h-3 w-3 text-rose-600" />
|
||||
<span>Hapus</span>
|
||||
@@ -3101,6 +3111,57 @@ export default function SalesPage() {
|
||||
</div>
|
||||
)}
|
||||
</Modal>
|
||||
|
||||
{/* Modal Dialog Konfirmasi Hapus Sales Order (Bebas dari browser confirm blocking) */}
|
||||
<Modal
|
||||
isOpen={!!soToDelete}
|
||||
onClose={() => !isDeletingSO && setSoToDelete(null)}
|
||||
title="Konfirmasi Hapus Sales Order"
|
||||
>
|
||||
<div className="space-y-4">
|
||||
<div className="p-3 bg-rose-50 border border-rose-200 rounded-lg flex items-start gap-3">
|
||||
<div className="p-2 rounded-full bg-rose-100 text-rose-700 shrink-0">
|
||||
<Trash2 className="h-5 w-5" />
|
||||
</div>
|
||||
<div className="text-xs text-rose-800 space-y-1">
|
||||
<div className="font-bold text-sm">Hapus Sales Order {soToDelete?.orderNumber}?</div>
|
||||
<p>
|
||||
Sales order senilai <span className="font-bold">{formatRupiah(soToDelete?.totalAmount || 0)}</span>{" "}
|
||||
untuk customer <span className="font-bold">{soToDelete?.customer?.name || "-"}</span> akan dihapus secara permanen.
|
||||
</p>
|
||||
<p className="text-[11px] text-rose-700">
|
||||
⚠️ Seluruh data pengeluaran stok, alokasi batch PO, dan jurnal transaksi terkait pesanan ini akan dibersihkan dari sistem.
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{deleteSOError && (
|
||||
<div className="p-2.5 bg-rose-100 border border-rose-300 text-rose-800 rounded text-xs font-medium">
|
||||
{deleteSOError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="flex items-center justify-end gap-2 pt-2 border-t border-slate-200">
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeletingSO}
|
||||
onClick={() => setSoToDelete(null)}
|
||||
className="px-4 py-2 border border-slate-300 rounded-lg text-slate-700 hover:bg-slate-100 text-xs font-semibold disabled:opacity-50"
|
||||
>
|
||||
Batal
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
disabled={isDeletingSO}
|
||||
onClick={handleConfirmDeleteSalesOrder}
|
||||
className="px-4 py-2 bg-rose-600 hover:bg-rose-700 text-white rounded-lg text-xs font-semibold shadow-xs disabled:opacity-50 flex items-center gap-1.5"
|
||||
>
|
||||
<Trash2 className="h-4 w-4" />
|
||||
<span>{isDeletingSO ? "Menghapus..." : "Ya, Hapus Sales Order"}</span>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</Modal>
|
||||
</AppShell>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user