refactor: store credit note value in database
This commit is contained in:
parent
027bb49c2a
commit
53453e37db
2 changed files with 110 additions and 4 deletions
|
|
@ -1584,6 +1584,8 @@ model CreditNote {
|
||||||
|
|
||||||
creditNoteStatus CreditNoteStatus @default(Pending)
|
creditNoteStatus CreditNoteStatus @default(Pending)
|
||||||
|
|
||||||
|
value Float @default(0)
|
||||||
|
|
||||||
quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade)
|
quotation Quotation @relation(fields: [quotationId], references: [id], onDelete: Cascade)
|
||||||
quotationId String
|
quotationId String
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,7 +25,7 @@ import {
|
||||||
import HttpError from "../interfaces/http-error";
|
import HttpError from "../interfaces/http-error";
|
||||||
import HttpStatus from "../interfaces/http-status";
|
import HttpStatus from "../interfaces/http-status";
|
||||||
import { notFoundError } from "../utils/error";
|
import { notFoundError } from "../utils/error";
|
||||||
import { Prisma } from "@prisma/client";
|
import { CreditNoteStatus, Prisma } from "@prisma/client";
|
||||||
import { queryOrNot } from "../utils/relation";
|
import { queryOrNot } from "../utils/relation";
|
||||||
import { RequestWorkStatus } from "../generated/kysely/types";
|
import { RequestWorkStatus } from "../generated/kysely/types";
|
||||||
|
|
||||||
|
|
@ -217,6 +217,15 @@ export class CreditNoteController extends Controller {
|
||||||
id: { in: body.requestWorkId },
|
id: { in: body.requestWorkId },
|
||||||
},
|
},
|
||||||
include: {
|
include: {
|
||||||
|
stepStatus: true,
|
||||||
|
productService: {
|
||||||
|
include: {
|
||||||
|
product: true,
|
||||||
|
work: {
|
||||||
|
include: { productOnWork: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
request: {
|
request: {
|
||||||
include: {
|
include: {
|
||||||
quotation: {
|
quotation: {
|
||||||
|
|
@ -237,6 +246,21 @@ export class CreditNoteController extends Controller {
|
||||||
requestWork.map((item) => permissionCheck(req.user, item.request.quotation.registeredBranch)),
|
requestWork.map((item) => permissionCheck(req.user, item.request.quotation.registeredBranch)),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
const value = requestWork.reduce((a, c) => {
|
||||||
|
const serviceChargeStepCount = c.productService.work?.productOnWork.find(
|
||||||
|
(v) => v.productId === c.productService.productId,
|
||||||
|
)?.stepCount;
|
||||||
|
|
||||||
|
const successCount = c.stepStatus.filter((v) => v.workStatus === "Completed").length;
|
||||||
|
|
||||||
|
if (serviceChargeStepCount && successCount) {
|
||||||
|
return (
|
||||||
|
a + c.productService.product.price - c.productService.product.serviceCharge * successCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return a + c.productService.product.price;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
const record = await prisma.creditNote.create({
|
const record = await prisma.creditNote.create({
|
||||||
include: {
|
include: {
|
||||||
requestWork: {
|
requestWork: {
|
||||||
|
|
@ -247,6 +271,7 @@ export class CreditNoteController extends Controller {
|
||||||
quotation: true,
|
quotation: true,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
|
value,
|
||||||
requestWork: {
|
requestWork: {
|
||||||
connect: body.requestWorkId.map((v) => ({
|
connect: body.requestWorkId.map((v) => ({
|
||||||
id: v,
|
id: v,
|
||||||
|
|
@ -297,16 +322,49 @@ export class CreditNoteController extends Controller {
|
||||||
},
|
},
|
||||||
id: { in: body.requestWorkId },
|
id: { in: body.requestWorkId },
|
||||||
},
|
},
|
||||||
|
include: {
|
||||||
|
stepStatus: true,
|
||||||
|
productService: {
|
||||||
|
include: {
|
||||||
|
product: true,
|
||||||
|
work: {
|
||||||
|
include: { productOnWork: true },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
request: {
|
||||||
|
include: {
|
||||||
|
quotation: {
|
||||||
|
include: {
|
||||||
|
registeredBranch: { include: branchRelationPermInclude(req.user) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (requestWork.length !== body.requestWorkId.length) {
|
if (requestWork.length !== body.requestWorkId.length) {
|
||||||
throw new HttpError(HttpStatus.BAD_REQUEST, "Not Match", "reqNotMet");
|
throw new HttpError(HttpStatus.BAD_REQUEST, "Not Match", "reqNotMet");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const value = requestWork.reduce((a, c) => {
|
||||||
|
const serviceChargeStepCount = c.productService.work?.productOnWork.find(
|
||||||
|
(v) => v.productId === c.productService.productId,
|
||||||
|
)?.stepCount;
|
||||||
|
|
||||||
|
const successCount = c.stepStatus.filter((v) => v.workStatus === "Completed").length;
|
||||||
|
|
||||||
|
if (serviceChargeStepCount && successCount) {
|
||||||
|
return (
|
||||||
|
a + c.productService.product.price - c.productService.product.serviceCharge * successCount
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return a + c.productService.product.price;
|
||||||
|
}, 0);
|
||||||
|
|
||||||
const record = await prisma.creditNote.update({
|
const record = await prisma.creditNote.update({
|
||||||
where: {
|
where: { id: creditNoteId },
|
||||||
id: creditNoteId,
|
|
||||||
},
|
|
||||||
include: {
|
include: {
|
||||||
requestWork: {
|
requestWork: {
|
||||||
include: {
|
include: {
|
||||||
|
|
@ -316,6 +374,7 @@ export class CreditNoteController extends Controller {
|
||||||
quotation: true,
|
quotation: true,
|
||||||
},
|
},
|
||||||
data: {
|
data: {
|
||||||
|
value,
|
||||||
requestWork: {
|
requestWork: {
|
||||||
disconnect: creditNoteData.requestWork
|
disconnect: creditNoteData.requestWork
|
||||||
.map((item) => ({
|
.map((item) => ({
|
||||||
|
|
@ -354,3 +413,48 @@ export class CreditNoteController extends Controller {
|
||||||
return await prisma.creditNote.delete({ where: { id: creditNoteId } });
|
return await prisma.creditNote.delete({ where: { id: creditNoteId } });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Route("api/v1/credit-note/{creditNoteId}")
|
||||||
|
@Tags("Credit Note")
|
||||||
|
export class CreditNoteActionController extends Controller {
|
||||||
|
async #checkPermission(user: RequestWithUser["user"], id: string) {
|
||||||
|
const creditNoteData = await prisma.creditNote.findFirst({
|
||||||
|
where: { id },
|
||||||
|
include: {
|
||||||
|
requestWork: true,
|
||||||
|
quotation: {
|
||||||
|
include: {
|
||||||
|
registeredBranch: { include: branchRelationPermInclude(user) },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!creditNoteData) throw notFoundError("Credit Note");
|
||||||
|
await permissionCheck(user, creditNoteData.quotation.registeredBranch);
|
||||||
|
return creditNoteData;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Post("status")
|
||||||
|
@Security("keycloak", MANAGE_ROLES)
|
||||||
|
async updateStatus(
|
||||||
|
@Request() req: RequestWithUser,
|
||||||
|
@Path() creditNoteId: string,
|
||||||
|
@Body() body: CreditNoteStatus,
|
||||||
|
) {
|
||||||
|
await this.#checkPermission(req.user, creditNoteId);
|
||||||
|
return await prisma.creditNote.update({
|
||||||
|
where: { id: creditNoteId },
|
||||||
|
include: {
|
||||||
|
requestWork: {
|
||||||
|
include: {
|
||||||
|
request: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
quotation: true,
|
||||||
|
},
|
||||||
|
data: {
|
||||||
|
creditNoteStatus: body,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue