Archived
1
0
Fork 0

Filter orders and sandwiches to have those paying

This commit is contained in:
Ethanell 2020-09-11 12:59:47 +02:00
parent e4981ff5ef
commit 59005d8d6e
5 changed files with 30 additions and 18 deletions

View file

@ -8,20 +8,25 @@ router.get("/", sessionCheck(2), async (req, res) => {
let date = req.query.date ? req.query.date : (new Date()).toISOString().substring(0,10);
let orders = {};
for (let i of await models.SandwichOrder.findAll({where: {date: date}})) {
i.Order = await models.Order.findByPk(i.OrderId);
i.Sandwich = await models.Sandwich.findByPk(i.SandwichName);
let name = i.Order.firstName + " " + i.Order.lastName;
for (let o of await models.Order.findAll({
where: {paid: true},
include: [{
model: models.Sandwich,
through: {
where: {date: date}
},
required: true
}]
})) {
let name = o.firstName + " " + o.lastName;
if (!(i.Order.DepartmentName in orders))
orders[i.Order.DepartmentName] = {};
if (!(o.DepartmentName in orders))
orders[o.DepartmentName] = {};
if (!(name in orders[i.Order.DepartmentName]))
orders[i.Order.DepartmentName][name] = {};
if (!(name in orders[o.DepartmentName]))
orders[o.DepartmentName][name] = {};
if (!(i.Order.id in orders[i.Order.DepartmentName][name]))
orders[i.Order.DepartmentName][name][i.Order.id] = [];
orders[i.Order.DepartmentName][name][i.Order.id].push(i);
orders[o.DepartmentName][name][o.id] = o.Sandwiches;
}
res.render("orders", {title: "SOD - Orders", orders: orders, date: date});
});

View file

@ -11,7 +11,7 @@ router.get("/", sessionCheck(0), async (req, res) => {
title: "SOD - Profile",
departments: await models.Department.findAll(),
orders: await models.Order.findAll({
where: {UserUsername: req.session.user.username},
where: {UserUsername: req.session.user.username, paid: true},
include: models.Sandwich,
order: ["date"]
})

View file

@ -10,10 +10,17 @@ router.get("/", sessionCheck(1), async (req, res) => {
res.render("sandwiches", {
title: "SOD - Sandwiches",
sandwiches: await models.SandwichOrder.findAll({
attributes: ["SandwichName", [sequelize.fn("COUNT", sequelize.col("SandwichName")), "number"]],
where: {date: date},
group: "SandwichName"
sandwiches: await models.Sandwich.findAll({
attributes: ["name", [sequelize.fn("COUNT", sequelize.col("name")), "number"]],
include: [{
model: models.Order,
where: {paid: true},
through: {
where: {date: date}
},
required: true
}],
group: "name"
}),
date: date});
});

View file

@ -17,6 +17,6 @@ block content
h4 #{__("order")} N°#{id}
each sandwich in order
div.sandwich
h4= sandwich.Sandwich.name
h4= sandwich.name
script(src="/javascripts/orders.js")

View file

@ -8,6 +8,6 @@ block content
h1=__("sandwiches")
each sandwich in sandwiches
div.sandwich
h2 #{sandwich.SandwichName}: #{sandwich.dataValues.number}
h2 #{sandwich.name}: #{sandwich.dataValues.number}
script(src="/javascripts/sandwiches.js")