refactor: add code gen for receipt and invoice

This commit is contained in:
Methapon Metanipat 2024-11-07 09:27:17 +07:00
parent daebea490f
commit dddb434914
4 changed files with 39 additions and 1 deletions

View file

@ -108,6 +108,21 @@ export class InvoiceController extends Controller {
await permissionCheck(req.user, quotation.registeredBranch);
return await prisma.$transaction(async (tx) => {
const current = new Date();
const year = `${current.getFullYear()}`.slice(-2).padStart(2, "0");
const month = `${current.getMonth() + 1}`.padStart(2, "0");
const last = await tx.runningNo.upsert({
where: {
key: `INVOICE_${year}${month}`,
},
create: {
key: `INVOICE_${year}${month}`,
value: 1,
},
update: { value: { increment: 1 } },
});
const record = await tx.quotation.update({
include: {
paySplit: {
@ -122,6 +137,7 @@ export class InvoiceController extends Controller {
return await tx.invoice.create({
data: {
quotationId: body.quotationId,
code: `IV${year}${month}${last.toString().padStart(6, "0")}`,
amount: body.amount,
installments: {
connect: record.paySplit.map((v) => ({ id: v.id })),