refactor: add code gen for receipt and invoice
This commit is contained in:
parent
daebea490f
commit
dddb434914
4 changed files with 39 additions and 1 deletions
|
|
@ -1292,6 +1292,8 @@ model QuotationProductServiceWorker {
|
||||||
model Invoice {
|
model Invoice {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
|
|
||||||
|
code String
|
||||||
|
|
||||||
quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade)
|
quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade)
|
||||||
quotationId String
|
quotationId String
|
||||||
|
|
||||||
|
|
@ -1316,6 +1318,8 @@ enum PaymentStatus {
|
||||||
model Payment {
|
model Payment {
|
||||||
id String @id @default(cuid())
|
id String @id @default(cuid())
|
||||||
|
|
||||||
|
code String?
|
||||||
|
|
||||||
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
|
invoice Invoice @relation(fields: [invoiceId], references: [id], onDelete: Cascade)
|
||||||
invoiceId String @unique
|
invoiceId String @unique
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -108,6 +108,21 @@ export class InvoiceController extends Controller {
|
||||||
await permissionCheck(req.user, quotation.registeredBranch);
|
await permissionCheck(req.user, quotation.registeredBranch);
|
||||||
|
|
||||||
return await prisma.$transaction(async (tx) => {
|
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({
|
const record = await tx.quotation.update({
|
||||||
include: {
|
include: {
|
||||||
paySplit: {
|
paySplit: {
|
||||||
|
|
@ -122,6 +137,7 @@ export class InvoiceController extends Controller {
|
||||||
return await tx.invoice.create({
|
return await tx.invoice.create({
|
||||||
data: {
|
data: {
|
||||||
quotationId: body.quotationId,
|
quotationId: body.quotationId,
|
||||||
|
code: `IV${year}${month}${last.toString().padStart(6, "0")}`,
|
||||||
amount: body.amount,
|
amount: body.amount,
|
||||||
installments: {
|
installments: {
|
||||||
connect: record.paySplit.map((v) => ({ id: v.id })),
|
connect: record.paySplit.map((v) => ({ id: v.id })),
|
||||||
|
|
|
||||||
|
|
@ -128,6 +128,24 @@ export class QuotationPayment extends Controller {
|
||||||
if (!record) throw notFoundError("Payment");
|
if (!record) throw notFoundError("Payment");
|
||||||
|
|
||||||
return await prisma.$transaction(async (tx) => {
|
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 quotation = record.invoice.quotation;
|
||||||
|
|
||||||
const payment = await tx.payment.update({
|
const payment = await tx.payment.update({
|
||||||
|
|
@ -152,6 +170,7 @@ export class QuotationPayment extends Controller {
|
||||||
await tx.quotation.update({
|
await tx.quotation.update({
|
||||||
where: { id: quotation.id },
|
where: { id: quotation.id },
|
||||||
data: {
|
data: {
|
||||||
|
code: last ? `RE${year}${month}${last.toString().padStart(6, "0")}` : undefined,
|
||||||
quotationStatus:
|
quotationStatus:
|
||||||
paymentSum._sum.amount || 0 >= quotation.finalPrice
|
paymentSum._sum.amount || 0 >= quotation.finalPrice
|
||||||
? "PaymentSuccess"
|
? "PaymentSuccess"
|
||||||
|
|
|
||||||
|
|
@ -395,7 +395,6 @@ export class QuotationController extends Controller {
|
||||||
|
|
||||||
const currentYear = new Date().getFullYear();
|
const currentYear = new Date().getFullYear();
|
||||||
const currentMonth = new Date().getMonth() + 1;
|
const currentMonth = new Date().getMonth() + 1;
|
||||||
const currentDate = new Date().getDate();
|
|
||||||
|
|
||||||
const lastQuotation = await tx.runningNo.upsert({
|
const lastQuotation = await tx.runningNo.upsert({
|
||||||
where: {
|
where: {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue