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

@ -1292,6 +1292,8 @@ model QuotationProductServiceWorker {
model Invoice {
id String @id @default(cuid())
code String
quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade)
quotationId String
@ -1316,6 +1318,8 @@ enum PaymentStatus {
model Payment {
id String @id @default(cuid())
code String?
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
invoiceId String @unique

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 })),

View file

@ -128,6 +128,24 @@ export class QuotationPayment extends Controller {
if (!record) throw notFoundError("Payment");
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 =
body.paymentStatus === "PaymentSuccess" && record.paymentStatus !== "PaymentSuccess"
? await tx.runningNo.upsert({
where: {
key: `RECEIPT_${year}${month}`,
},
create: {
key: `RECEIPT_${year}${month}`,
value: 1,
},
update: { value: { increment: 1 } },
})
: null;
const quotation = record.invoice.quotation;
const payment = await tx.payment.update({
@ -152,6 +170,7 @@ export class QuotationPayment extends Controller {
await tx.quotation.update({
where: { id: quotation.id },
data: {
code: last ? `RE${year}${month}${last.toString().padStart(6, "0")}` : undefined,
quotationStatus:
paymentSum._sum.amount || 0 >= quotation.finalPrice
? "PaymentSuccess"

View file

@ -395,7 +395,6 @@ export class QuotationController extends Controller {
const currentYear = new Date().getFullYear();
const currentMonth = new Date().getMonth() + 1;
const currentDate = new Date().getDate();
const lastQuotation = await tx.runningNo.upsert({
where: {