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

@ -1582,6 +1582,8 @@ enum CreditNoteStatus {
model CreditNote { model CreditNote {
id String @id @default(cuid()) id String @id @default(cuid())
code String
creditNoteStatus CreditNoteStatus @default(Pending) creditNoteStatus CreditNoteStatus @default(Pending)
value Float @default(0) value Float @default(0)

View file

@ -261,7 +261,25 @@ export class CreditNoteController extends Controller {
return a + c.productService.product.price; return a + c.productService.product.price;
}, 0); }, 0);
const record = await prisma.creditNote.create({ this.setStatus(HttpStatus.CREATED);
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: { include: {
requestWork: { requestWork: {
include: { include: {
@ -271,6 +289,7 @@ export class CreditNoteController extends Controller {
quotation: true, quotation: true,
}, },
data: { data: {
code: `CN${currentYear.toString().padStart(2, "0")}${currentMonth.toString().padStart(2, "0")}${last.value.toString().padStart(6, "0")}`,
value, value,
requestWork: { requestWork: {
connect: body.requestWorkId.map((v) => ({ connect: body.requestWorkId.map((v) => ({
@ -280,10 +299,9 @@ export class CreditNoteController extends Controller {
quotationId: body.quotationId, quotationId: body.quotationId,
}, },
}); });
},
this.setStatus(HttpStatus.CREATED); { isolationLevel: Prisma.TransactionIsolationLevel.Serializable },
);
return record;
} }
@Put("{creditNoteId}") @Put("{creditNoteId}")