feat: add code gen to credit note

This commit is contained in:
Methapon2001 2025-01-07 09:39:21 +07:00
parent 16ceabe3df
commit b9b6745bb6
2 changed files with 41 additions and 21 deletions

View file

@ -261,29 +261,47 @@ export class CreditNoteController extends Controller {
return a + c.productService.product.price;
}, 0);
const record = await prisma.creditNote.create({
include: {
requestWork: {
include: {
request: true,
},
},
quotation: true,
},
data: {
value,
requestWork: {
connect: body.requestWorkId.map((v) => ({
id: v,
})),
},
quotationId: body.quotationId,
},
});
this.setStatus(HttpStatus.CREATED);
return record;
return await prisma.$transaction(
async (tx) => {
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth() + 1;
const last = await tx.runningNo.upsert({
where: {
key: `CREDIT_NOTE_${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}`,
},
create: {
key: `CREDIT_NOTE_${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}`,
value: 1,
},
update: { value: { increment: 1 } },
});
return await prisma.creditNote.create({
include: {
requestWork: {
include: {
request: true,
},
},
quotation: true,
},
data: {
code: `CN${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${last.value.toString().padStart(6, "0")}`,
value,
requestWork: {
connect: body.requestWorkId.map((v) => ({
id: v,
})),
},
quotationId: body.quotationId,
},
});
},
{ isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
}
@Put("{creditNoteId}")