feat: add saldo balance tracking and item notes support for orders and reports
This commit is contained in:
+58
-21
@@ -306,6 +306,15 @@ export async function updateOrder(order_id: string, data: {
|
||||
if (order.status === 'CLOSE') {
|
||||
return { success: false, error: 'Order dengan status CLOSE tidak dapat diedit.' }
|
||||
}
|
||||
|
||||
// Check if order date is today
|
||||
const orderDate = new Date(order.date)
|
||||
const today = new Date()
|
||||
if (orderDate.getDate() !== today.getDate() ||
|
||||
orderDate.getMonth() !== today.getMonth() ||
|
||||
orderDate.getFullYear() !== today.getFullYear()) {
|
||||
return { success: false, error: 'Hanya pesanan hari ini yang dapat diedit.' }
|
||||
}
|
||||
|
||||
// Delete existing available items and insert updated ones
|
||||
await prisma.availableItem.deleteMany({
|
||||
@@ -387,11 +396,11 @@ export async function getOrderDetail(order_id: string) {
|
||||
})
|
||||
}
|
||||
|
||||
export async function updateSubmissionPayment(submission_id: string, bill: number | null, payment_status: string, paid_amount: number | null = null) {
|
||||
export async function updateSubmissionPayment(submission_id: string, bill: number | null, payment_status: string, paid_amount: number | null = null, saldo_used: number = 0) {
|
||||
try {
|
||||
await prisma.submission.update({
|
||||
where: { id: submission_id },
|
||||
data: { bill, payment_status, paid_amount }
|
||||
data: { bill, payment_status, paid_amount, saldo_used }
|
||||
})
|
||||
revalidatePath(`/my-orders`)
|
||||
revalidatePath(`/my-purchases`)
|
||||
@@ -411,7 +420,8 @@ export async function updateBulkSubmissionPayment(submission_ids: string[], paym
|
||||
where: { id: sub.id },
|
||||
data: {
|
||||
payment_status,
|
||||
paid_amount: use_balance ? 0 : (sub.paid_amount != null ? sub.paid_amount : sub.bill)
|
||||
paid_amount: use_balance ? (sub.paid_amount || 0) : (sub.paid_amount != null ? sub.paid_amount : sub.bill),
|
||||
saldo_used: use_balance ? Math.max(0, (sub.bill || 0) - (sub.paid_amount || 0)) : 0
|
||||
}
|
||||
}))
|
||||
await prisma.$transaction(ops)
|
||||
@@ -561,13 +571,24 @@ export async function getMyPurchases(user_id: string) {
|
||||
export async function submitOrder(data: {
|
||||
order_id: string;
|
||||
user_id: string;
|
||||
items: Array<{ name: string; qty: number; is_custom: boolean }>;
|
||||
items: Array<{ name: string; qty: number; is_custom: boolean; note?: string }>;
|
||||
}) {
|
||||
try {
|
||||
if (data.items.length === 0) {
|
||||
return { success: false, error: 'Pilih minimal 1 item.' }
|
||||
}
|
||||
|
||||
const order = await prisma.order.findUnique({ where: { id: data.order_id } })
|
||||
if (!order) return { success: false, error: 'Pesanan tidak ditemukan.' }
|
||||
if (order.status !== 'OPEN') return { success: false, error: 'Pesanan sudah ditutup atau masih draft.' }
|
||||
|
||||
const itemsToCreate = data.items.map(item => ({
|
||||
name: item.name,
|
||||
qty: item.qty,
|
||||
is_custom: item.is_custom,
|
||||
note: item.note || null
|
||||
}))
|
||||
|
||||
const existing = await prisma.submission.findFirst({
|
||||
where: { order_id: data.order_id, user_id: data.user_id }
|
||||
})
|
||||
@@ -578,7 +599,7 @@ export async function submitOrder(data: {
|
||||
where: { id: existing.id },
|
||||
data: {
|
||||
items: {
|
||||
create: data.items
|
||||
create: itemsToCreate
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -588,7 +609,7 @@ export async function submitOrder(data: {
|
||||
order_id: data.order_id,
|
||||
user_id: data.user_id,
|
||||
items: {
|
||||
create: data.items
|
||||
create: itemsToCreate
|
||||
}
|
||||
}
|
||||
})
|
||||
@@ -650,8 +671,7 @@ export async function getCreatorReport(creator_id: string, startDate?: Date, end
|
||||
export async function getBalancesAsCreator(creator_id: string) {
|
||||
const submissions = await prisma.submission.findMany({
|
||||
where: {
|
||||
order: { creator_id },
|
||||
payment_status: 'LUNAS'
|
||||
order: { creator_id }
|
||||
},
|
||||
include: {
|
||||
user: { select: { id: true, name: true, photo: true } }
|
||||
@@ -661,12 +681,21 @@ export async function getBalancesAsCreator(creator_id: string) {
|
||||
const balanceMap = new Map<string, { user: any, amount: number }>()
|
||||
|
||||
submissions.forEach(sub => {
|
||||
if (sub.paid_amount == null || sub.bill == null) return
|
||||
const diff = sub.paid_amount - sub.bill
|
||||
if (!balanceMap.has(sub.user.id)) {
|
||||
balanceMap.set(sub.user.id, { user: sub.user, amount: diff })
|
||||
let diff = 0;
|
||||
if (sub.payment_status === 'LUNAS') {
|
||||
if (sub.bill != null) {
|
||||
diff = (sub.paid_amount || 0) - sub.bill;
|
||||
}
|
||||
} else {
|
||||
balanceMap.get(sub.user.id)!.amount += diff
|
||||
diff = -(sub.saldo_used || 0);
|
||||
}
|
||||
|
||||
if (diff !== 0) {
|
||||
if (!balanceMap.has(sub.user.id)) {
|
||||
balanceMap.set(sub.user.id, { user: sub.user, amount: diff })
|
||||
} else {
|
||||
balanceMap.get(sub.user.id)!.amount += diff
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
@@ -676,8 +705,7 @@ export async function getBalancesAsCreator(creator_id: string) {
|
||||
export async function getBalancesAsSubmittor(user_id: string) {
|
||||
const submissions = await prisma.submission.findMany({
|
||||
where: {
|
||||
user_id,
|
||||
payment_status: 'LUNAS'
|
||||
user_id
|
||||
},
|
||||
include: {
|
||||
order: {
|
||||
@@ -691,13 +719,22 @@ export async function getBalancesAsSubmittor(user_id: string) {
|
||||
const balanceMap = new Map<string, { creator: any, amount: number }>()
|
||||
|
||||
submissions.forEach(sub => {
|
||||
if (sub.paid_amount == null || sub.bill == null) return
|
||||
const diff = sub.paid_amount - sub.bill
|
||||
const creator = sub.order.creator
|
||||
if (!balanceMap.has(creator.id)) {
|
||||
balanceMap.set(creator.id, { creator: creator, amount: diff })
|
||||
let diff = 0;
|
||||
if (sub.payment_status === 'LUNAS') {
|
||||
if (sub.bill != null) {
|
||||
diff = (sub.paid_amount || 0) - sub.bill;
|
||||
}
|
||||
} else {
|
||||
balanceMap.get(creator.id)!.amount += diff
|
||||
diff = -(sub.saldo_used || 0);
|
||||
}
|
||||
|
||||
if (diff !== 0) {
|
||||
const creator = sub.order.creator
|
||||
if (!balanceMap.has(creator.id)) {
|
||||
balanceMap.set(creator.id, { creator: creator, amount: diff })
|
||||
} else {
|
||||
balanceMap.get(creator.id)!.amount += diff
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user