Spaces:
Sleeping
Sleeping
Upload 8 files
Browse files
controller/productController.js
CHANGED
|
@@ -40,12 +40,31 @@ export const getProducts = async (req, res) => {
|
|
| 40 |
const limit = parseInt(req.query.limit) || 5;
|
| 41 |
const skip = (page - 1) * limit;
|
| 42 |
const search = req.query.search || "";
|
|
|
|
| 43 |
|
|
|
|
| 44 |
const query = { tenant: req.user.tenant._id };
|
|
|
|
| 45 |
if (search) {
|
| 46 |
query.name = { $regex: search, $options: "i" };
|
| 47 |
}
|
| 48 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
const total = await Product.countDocuments(query);
|
| 50 |
const products = await Product.find(query)
|
| 51 |
.sort({ createdAt: -1 })
|
|
@@ -57,7 +76,7 @@ export const getProducts = async (req, res) => {
|
|
| 57 |
...product.toObject(),
|
| 58 |
isLowStock:
|
| 59 |
Number(product.stock) <=
|
| 60 |
-
Number(product.lowStockThreshold ||
|
| 61 |
})),
|
| 62 |
pagination: {
|
| 63 |
total,
|
|
|
|
| 40 |
const limit = parseInt(req.query.limit) || 5;
|
| 41 |
const skip = (page - 1) * limit;
|
| 42 |
const search = req.query.search || "";
|
| 43 |
+
const filter = req.query.filter || "all";
|
| 44 |
|
| 45 |
+
const defaultThreshold = Number(req.user.tenant.settings?.lowStockThreshold || 5);
|
| 46 |
const query = { tenant: req.user.tenant._id };
|
| 47 |
+
|
| 48 |
if (search) {
|
| 49 |
query.name = { $regex: search, $options: "i" };
|
| 50 |
}
|
| 51 |
|
| 52 |
+
if (filter === "low") {
|
| 53 |
+
query.$expr = {
|
| 54 |
+
$lte: [
|
| 55 |
+
{ $toDouble: "$stock" },
|
| 56 |
+
{ $toDouble: { $ifNull: ["$lowStockThreshold", defaultThreshold] } },
|
| 57 |
+
],
|
| 58 |
+
};
|
| 59 |
+
} else if (filter === "available") {
|
| 60 |
+
query.$expr = {
|
| 61 |
+
$gt: [
|
| 62 |
+
{ $toDouble: "$stock" },
|
| 63 |
+
{ $toDouble: { $ifNull: ["$lowStockThreshold", defaultThreshold] } },
|
| 64 |
+
],
|
| 65 |
+
};
|
| 66 |
+
}
|
| 67 |
+
|
| 68 |
const total = await Product.countDocuments(query);
|
| 69 |
const products = await Product.find(query)
|
| 70 |
.sort({ createdAt: -1 })
|
|
|
|
| 76 |
...product.toObject(),
|
| 77 |
isLowStock:
|
| 78 |
Number(product.stock) <=
|
| 79 |
+
Number(product.lowStockThreshold || defaultThreshold),
|
| 80 |
})),
|
| 81 |
pagination: {
|
| 82 |
total,
|
controller/reportController.js
CHANGED
|
@@ -39,7 +39,7 @@ const createRange = (req) => {
|
|
| 39 |
const start = req.query.start ? new Date(req.query.start) : new Date(end);
|
| 40 |
|
| 41 |
if (!req.query.start) {
|
| 42 |
-
start.
|
| 43 |
}
|
| 44 |
|
| 45 |
start.setHours(0, 0, 0, 0);
|
|
@@ -106,7 +106,7 @@ export const getDashboardOverview = async (req, res) => {
|
|
| 106 |
Expense.find({ tenant: tenantId }).sort({ date: -1 }),
|
| 107 |
Income.find({ tenant: tenantId }).sort({ date: -1 }),
|
| 108 |
Product.find({ tenant: tenantId }).sort({ stock: 1, createdAt: -1 }),
|
| 109 |
-
Customer.find({ tenant: tenantId }).sort({ createdAt: -1 }),
|
| 110 |
Supplier.find({ tenant: tenantId }).sort({ createdAt: -1 }),
|
| 111 |
SupplierInvoice.find({ tenant: tenantId }).populate("supplier", "name"),
|
| 112 |
User.find({ tenant: tenantId }).sort({ createdAt: -1 }),
|
|
|
|
| 39 |
const start = req.query.start ? new Date(req.query.start) : new Date(end);
|
| 40 |
|
| 41 |
if (!req.query.start) {
|
| 42 |
+
start.setMonth(start.getMonth() - 1);
|
| 43 |
}
|
| 44 |
|
| 45 |
start.setHours(0, 0, 0, 0);
|
|
|
|
| 106 |
Expense.find({ tenant: tenantId }).sort({ date: -1 }),
|
| 107 |
Income.find({ tenant: tenantId }).sort({ date: -1 }),
|
| 108 |
Product.find({ tenant: tenantId }).sort({ stock: 1, createdAt: -1 }),
|
| 109 |
+
Customer.find({ tenant: tenantId, isDeleted: false }).sort({ createdAt: -1 }),
|
| 110 |
Supplier.find({ tenant: tenantId }).sort({ createdAt: -1 }),
|
| 111 |
SupplierInvoice.find({ tenant: tenantId }).populate("supplier", "name"),
|
| 112 |
User.find({ tenant: tenantId }).sort({ createdAt: -1 }),
|