Update Sales.tsx

This commit is contained in:
ssbg04 authored and GitHub committed 2025-12-06 15:59:55 +08:00
1 parent 54e5b52562
commit 61b1e97d10
1 file changed
+34 -24
+34 -24
View File
@@ -118,35 +118,44 @@ const Sales = () => {
};
const submitUpdate = async () => {
if (!selectedSale?.id) return;
setProcessing(true);
if (!selectedSale || !selectedSale.id) return;
setProcessing(true);
try {
const newStatus: "void" | "refunded" = actionType === "void" ? "void" : "refunded";
try {
const newStatus: "void" | "refunded" =
actionType === "void" ? "void" : "refunded";
// Update stock only on refund
if (actionType === "refund") {
for (const item of selectedSale.items) {
await updateProductStock(item.product_id, item.quantity);
}
}
// --- UPDATE STOCK BACK ---
for (const item of selectedSale.items) {
// Find the product first
const product = await getProducts().then((list) =>
list.find((p) => p.id === item.id)
);
if (!product) continue;
await updateSaleStatus(selectedSale.id, newStatus, reason);
const newQuantity = product.quantity + item.quantity;
const newSoldCount =
product.sold - item.quantity >= 0 ? product.sold - item.quantity : 0;
setSelectedSale({
...selectedSale,
status: newStatus,
refund_reason: reason,
refunded_at: new Date().toISOString(),
});
loadData();
setActionType("view");
setShowModal(false);
} finally {
setProcessing(false);
await updateProductStock(product.id, newQuantity, newSoldCount);
}
};
await updateSaleStatus(selectedSale.id, newStatus, reason);
const updatedSale: Sale = {
...selectedSale,
status: newStatus,
refund_reason: reason,
refunded_at: new Date().toISOString(),
};
setSelectedSale(updatedSale);
loadData();
setActionType("view");
} finally {
setProcessing(false);
}
};
const handlePrint = () => {
window.print();
@@ -763,3 +772,4 @@ export default Sales;