Skip to content

Commit 600a036

Browse files
author
shawon-majid
committed
bill generation done
1 parent fee2e6a commit 600a036

File tree

5 files changed

+166
-8
lines changed

5 files changed

+166
-8
lines changed
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import { Request, Response } from "express";
2+
import errorWrapper from "../middlewares/errorWrapper";
3+
import { PrismaClient } from "@prisma/client";
4+
import { adminLog } from "../services/logdata";
5+
6+
const prisma = new PrismaClient();
7+
8+
const generateBillFortheWeek = errorWrapper(
9+
async (req: Request, res: Response) => {
10+
const { stsId, contractorId } = req.body;
11+
12+
const sts = await prisma.sTS.findUnique({
13+
where: {
14+
id: stsId,
15+
},
16+
});
17+
18+
if (!sts) {
19+
res.status(404).json({ message: "STS not found" });
20+
}
21+
22+
// get the entries for the week
23+
24+
const contractorEntries = await prisma.sTSContractorEntry.findMany({
25+
where: {
26+
stsId,
27+
contractorId,
28+
entryTime: {
29+
gte: new Date(new Date().setDate(new Date().getDate() - 7)),
30+
},
31+
},
32+
});
33+
34+
const totalWasteOfWeek = contractorEntries.reduce(
35+
(acc, entry) => acc + Number(entry.wasteWeight),
36+
0
37+
);
38+
39+
const requiredWaste = Number(sts?.requiredWastePerWeek);
40+
const PaymentPerTon = Number(sts?.paymentPerTon);
41+
42+
const basicPay = totalWasteOfWeek * PaymentPerTon;
43+
44+
const deficit = Math.max(0, requiredWaste - totalWasteOfWeek);
45+
46+
const fine = deficit * PaymentPerTon;
47+
48+
const paymentAmount = basicPay - fine;
49+
50+
const contractorBill = await prisma.contractorBill.create({
51+
data: {
52+
stsId,
53+
contractorId,
54+
billNo: Math.floor(Math.random() * 1000000),
55+
weightCollected: totalWasteOfWeek,
56+
weightRequired: requiredWaste,
57+
fine: fine,
58+
paymentPerTon: PaymentPerTon,
59+
deficit: deficit,
60+
paymentAmount: paymentAmount,
61+
},
62+
});
63+
64+
adminLog(
65+
"Bill Generated",
66+
`Bill Generated for Contractor ${contractorId} for STS ${sts?.name}`
67+
);
68+
69+
res.status(201).json(contractorBill);
70+
},
71+
{ statusCode: 400, message: "Couldn't generate bill" }
72+
);
73+
74+
const getAllContractorBills = errorWrapper(
75+
async (req: Request, res: Response) => {
76+
const contractorBills = await prisma.contractorBill.findMany();
77+
res.status(200).json(contractorBills);
78+
},
79+
{ statusCode: 404, message: "Contractor Bills not found" }
80+
);
81+
82+
const getContractorBillById = errorWrapper(
83+
async (req: Request, res: Response) => {
84+
const { contractorBillId } = req.params;
85+
const contractorBill = await prisma.contractorBill.findUnique({
86+
where: {
87+
id: contractorBillId,
88+
},
89+
});
90+
91+
if (!contractorBill) {
92+
res.status(404).json({ message: "Contractor Bill not found" });
93+
}
94+
95+
res.status(200).json(contractorBill);
96+
},
97+
{ statusCode: 404, message: "Contractor Bill not found" }
98+
);
99+
100+
export { generateBillFortheWeek, getAllContractorBills, getContractorBillById };

server/src/prisma/schema.prisma

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@ model Contractor {
158158
createdAt DateTime @default(now())
159159
updatedAt DateTime @updatedAt
160160
STSContractorEntry STSContractorEntry[]
161+
ContractorBill ContractorBill[]
161162
}
162163

163164
model Vehicle {
@@ -189,14 +190,19 @@ model Vehicle {
189190
}
190191

191192
model STS {
192-
id String @id @default(uuid())
193+
id String @id @default(uuid())
193194
name String?
194195
wardNumber String?
195-
capacity Decimal? @db.Decimal(10, 2)
196-
currentTotalWaste Decimal? @db.Decimal(10, 2)
197-
latitude Decimal
198-
longitude Decimal
199-
Vehicle Vehicle[]
196+
capacity Decimal? @db.Decimal(10, 2)
197+
currentTotalWaste Decimal? @db.Decimal(10, 2)
198+
199+
requiredWastePerWeek Decimal? @db.Decimal(10, 2)
200+
paymentPerTon Decimal? @db.Decimal(10, 2)
201+
fine Decimal? @db.Decimal(10, 2)
202+
203+
latitude Decimal
204+
longitude Decimal
205+
Vehicle Vehicle[]
200206
201207
STSVehicleEntry STSVehicleEntry[]
202208
manager User[]
@@ -211,6 +217,7 @@ model STS {
211217
Area Area[]
212218
CollectionPlan CollectionPlan[]
213219
STSContractorEntry STSContractorEntry[]
220+
ContractorBill ContractorBill[]
214221
}
215222

216223
model Landfill {
@@ -257,9 +264,10 @@ model STSContractorEntry {
257264
contractor Contractor @relation(fields: [contractorId], references: [id], onDelete: Cascade)
258265
contractorId String
259266
260-
wasteType String?
267+
wasteType String?
268+
261269
wasteWeight Decimal? @db.Decimal(10, 2)
262-
entryTime DateTime?
270+
entryTime DateTime? @default(now())
263271
exitTime DateTime?
264272
265273
vehicleNumber String?
@@ -268,6 +276,25 @@ model STSContractorEntry {
268276
updatedAt DateTime @updatedAt
269277
}
270278

279+
model ContractorBill {
280+
id String @id @default(uuid())
281+
contractor Contractor @relation(fields: [contractorId], references: [id], onDelete: Cascade)
282+
contractorId String
283+
sts STS @relation(fields: [stsId], references: [id], onDelete: Cascade)
284+
stsId String
285+
billNo Int @unique @default(autoincrement())
286+
paymentPerTon Decimal? @db.Decimal(10, 2)
287+
deficit Decimal? @db.Decimal(10, 2)
288+
289+
weightRequired Decimal? @db.Decimal(10, 2)
290+
weightCollected Decimal? @db.Decimal(10, 2)
291+
paymentAmount Decimal? @db.Decimal(10, 2)
292+
fine Decimal? @db.Decimal(10, 2)
293+
294+
createdAt DateTime @default(now())
295+
updatedAt DateTime @updatedAt
296+
}
297+
271298
model LandfillVehicleEntry {
272299
id String @id @default(uuid())
273300
landfill Landfill @relation(fields: [landfillId], references: [id], onDelete: Cascade)

server/src/prisma/seed.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,10 @@ const stsData: Prisma.STSCreateInput[] = [
485485
name: "Mohakhali STS",
486486
wardNumber: "13",
487487
capacity: 1000,
488+
fine: 10.5,
489+
paymentPerTon: 10.5,
488490
currentTotalWaste: 220,
491+
requiredWastePerWeek: 500,
489492
latitude: 23.777742178642388,
490493
longitude: 90.40575221162331,
491494
},
@@ -494,6 +497,9 @@ const stsData: Prisma.STSCreateInput[] = [
494497
name: "Gulshan STS",
495498
wardNumber: "2",
496499
capacity: 2000,
500+
paymentPerTon: 10.5,
501+
fine: 10.5,
502+
requiredWastePerWeek: 500,
497503
currentTotalWaste: 225,
498504
latitude: 23.792464932754005,
499505
longitude: 90.40782465254337,
@@ -504,15 +510,21 @@ const stsData: Prisma.STSCreateInput[] = [
504510
name: "Bonani STS",
505511
wardNumber: "4",
506512
capacity: 1500,
513+
fine: 10.5,
514+
requiredWastePerWeek: 500,
507515
currentTotalWaste: 240,
516+
paymentPerTon: 10.5,
508517
latitude: 23.793630794902622,
509518
longitude: 90.40660514416635,
510519
},
511520
{
512521
id: "sts4",
513522
name: "Badda STS",
523+
paymentPerTon: 10.5,
514524
wardNumber: "4",
525+
fine: 10.5,
515526
capacity: 1500,
527+
requiredWastePerWeek: 500,
516528
currentTotalWaste: 275,
517529
latitude: 23.78042151306244,
518530
longitude: 90.42669427037866,
@@ -521,8 +533,11 @@ const stsData: Prisma.STSCreateInput[] = [
521533
id: "sts5",
522534
name: "Jatrabari STS",
523535
wardNumber: "4",
536+
fine: 10.5,
537+
paymentPerTon: 10.5,
524538
capacity: 1500,
525539
currentTotalWaste: 290,
540+
requiredWastePerWeek: 500,
526541
latitude: 23.710484797357275,
527542
longitude: 90.43479693063576,
528543
},
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import express from "express";
2+
const router = express.Router();
3+
4+
import {
5+
generateBillFortheWeek,
6+
getAllContractorBills,
7+
getContractorBillById,
8+
} from "../controllers/contractorBills";
9+
10+
router.route("/generate").post(generateBillFortheWeek);
11+
router.route("/").get(getAllContractorBills);
12+
router.route("/:contractorBillId").get(getContractorBillById);
13+
14+
export default router;

server/src/routes/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import employeeRoute from "./employee";
2121
import collectionPlanRoute from "./collectionPlans";
2222
import issueRoute from "./issue";
2323
import stsContractorRoute from "./stsContractor";
24+
import contractorBillRoute from "./contractor-bills";
2425

2526
router.use("/auth", authRoute);
2627
router.use("/users", userRoute);
@@ -43,5 +44,6 @@ router.use("/employees", employeeRoute);
4344
router.use("/collection-plans", collectionPlanRoute);
4445
router.use("/issues", issueRoute);
4546
router.use("/sts-contractor-entry", stsContractorRoute);
47+
router.use("/contractor-bills", contractorBillRoute);
4648

4749
export default router;

0 commit comments

Comments
 (0)