Fix: refs #7126 Tests back
gitea/salix/pipeline/pr-dev There was a failure building this commit Details

This commit is contained in:
Guillermo Bonet 2024-05-10 11:05:20 +02:00
parent eea04520ad
commit b1e136aa47
5 changed files with 72 additions and 50 deletions

View File

@ -27,28 +27,42 @@ module.exports = Self => {
}); });
Self.getWasteByItem = async(buyer, family, options) => { Self.getWasteByItem = async(buyer, family, options) => {
const models = Self.app.models;
const myOptions = {}; const myOptions = {};
if (typeof options == 'object') if (typeof options == 'object')
Object.assign(myOptions, options); Object.assign(myOptions, options);
const user = await models.VnUser.findOne({
fields: ['id'],
where: {name: buyer}
});
const itemType = await models.ItemType.findOne({
fields: ['id'],
where: {name: family}
}, options);
const date = Date.vnNew(); const date = Date.vnNew();
date.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0);
const wastes = await Self.rawSql(` const wastes = await Self.rawSql(`
SELECT *, 100 * dwindle / total AS percentage SELECT *, 100 * dwindle / total percentage
FROM ( FROM (
SELECT buyer, SELECT u.name buyer,
ws.family, it.name family,
ws.itemFk, w.itemFk,
sum(ws.saleTotal) AS total, SUM(w.saleTotal) total,
sum(ws.saleWaste) AS dwindle SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
FROM bs.waste ws FROM bs.waste w
WHERE buyer = ? AND family = ? JOIN account.user u ON u.id = w.buyerFk
AND year = YEAR(TIMESTAMPADD(WEEK,-1, ?)) JOIN vn.itemType it ON it.id = w.itemTypeFk
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1) WHERE w.buyerFk = ? AND w.itemTypeFk = ?
GROUP BY buyer, itemFk AND w.year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
) sub AND w.week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
ORDER BY family, percentage DESC`, [buyer, family, date, date], myOptions); GROUP BY w.buyerFk, w.itemFk
) sub
ORDER BY family, percentage DESC
`, [user.id, itemType.id, date, date], myOptions);
const details = []; const details = [];

View File

@ -22,31 +22,37 @@ module.exports = Self => {
const date = Date.vnNew(); const date = Date.vnNew();
date.setHours(0, 0, 0, 0); date.setHours(0, 0, 0, 0);
const wastes = await Self.rawSql(` const wastes = await Self.rawSql(`
SELECT *, 100 * dwindle / total AS percentage SELECT *, 100 * dwindle / total percentage
FROM ( FROM (
SELECT buyer, SELECT u.name buyer,
ws.family, it.name family,
sum(ws.saleTotal) AS total, w.itemFk,
sum(ws.saleWaste) AS dwindle SUM(w.saleTotal) total,
FROM bs.waste ws SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
WHERE year = YEAR(TIMESTAMPADD(WEEK,-1, ?)) FROM bs.waste w
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1) JOIN account.user u ON u.id = w.buyerFk
GROUP BY buyer, family JOIN vn.itemType it ON it.id = w.itemTypeFk
) sub WHERE year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
ORDER BY percentage DESC`, [date, date], myOptions); AND week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
GROUP BY buyerFk, itemTypeFk
) sub
ORDER BY percentage DESC
`, [date, date], myOptions);
const wastesTotal = await Self.rawSql(` const wastesTotal = await Self.rawSql(`
SELECT *, 100 * dwindle / total AS percentage SELECT *, 100 * dwindle / total percentage
FROM ( FROM (
SELECT buyer, SELECT u.name buyer,
sum(ws.saleTotal) AS total, SUM(w.saleTotal) total,
sum(ws.saleWaste) AS dwindle SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
FROM bs.waste ws FROM bs.waste w
WHERE year = YEAR(TIMESTAMPADD(WEEK,-1, ?)) JOIN account.user u ON u.id = w.buyerFk
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1) WHERE w.year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
GROUP BY buyer AND w.week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
GROUP BY w.buyerFk
) sub ) sub
ORDER BY percentage DESC`, [date, date], myOptions); ORDER BY percentage DESC
`, [date, date], myOptions);
const details = []; const details = [];

View File

@ -1,18 +1,18 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Item getWasteByItem()', () => { fdescribe('Item getWasteByItem()', () => {
it('should check for the waste breakdown by worker and item', async() => { it('should check for the waste breakdown by worker and item', async() => {
const tx = await models.Item.beginTransaction({}); const tx = await models.Item.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
try { try {
const result = await models.Item.getWasteByItem('CharlesXavier', 'Cymbidium', options); const result = await models.Item.getWasteByItem('buyer', 'Crisantemo', options);
const length = result.length; const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
expect(anyResult.buyer).toEqual('CharlesXavier'); expect(anyResult.buyer).toEqual('buyer');
expect(anyResult.family).toEqual('Cymbidium'); expect(anyResult.family).toEqual('Crisantemo');
expect(anyResult.lines.length).toBeGreaterThanOrEqual(2); expect(anyResult.lines.length).toBeGreaterThanOrEqual(2);
await tx.rollback(); await tx.rollback();

View File

@ -1,6 +1,6 @@
const models = require('vn-loopback/server/server').models; const models = require('vn-loopback/server/server').models;
describe('Item getWasteByWorker()', () => { fdescribe('Item getWasteByWorker()', () => {
it('should check for the waste breakdown for every worker', async() => { it('should check for the waste breakdown for every worker', async() => {
const tx = await models.Item.beginTransaction({}); const tx = await models.Item.beginTransaction({});
const options = {transaction: tx}; const options = {transaction: tx};
@ -11,9 +11,9 @@ describe('Item getWasteByWorker()', () => {
const length = result.length; const length = result.length;
const anyResult = result[Math.floor(Math.random() * Math.floor(length))]; const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
expect(anyResult.buyer).toMatch(/(CharlesXavier|HankPym|DavidCharlesHaller)/); expect(anyResult.buyer).toMatch(/(buyer|it)/);
expect(anyResult.total).toBeGreaterThanOrEqual(1000); expect(anyResult.total).toBeGreaterThanOrEqual(1000);
expect(anyResult.lines.length).toBeGreaterThanOrEqual(3); expect(anyResult.lines.length).toBeGreaterThanOrEqual(1);
await tx.rollback(); await tx.rollback();
} catch (e) { } catch (e) {

View File

@ -1,11 +1,13 @@
SELECT *, 100 * dwindle / total AS percentage SELECT *, 100 * dwindle / total `percentage`
FROM ( FROM (
SELECT buyer, SELECT u.name buyer,
sum(saleTotal) as total, SUM(saleTotal) total,
sum(saleWaste) as dwindle SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
FROM bs.waste w FROM bs.waste w
JOIN vn.time t ON w.year = t.year AND w.week = t.week JOIN account.user u ON u.id = w.buyerFk
WHERE t.dated = DATE_ADD(CURDATE(), INTERVAL -1 WEEK) JOIN vn.itemType it ON it.id = w.itemTypeFk
GROUP BY buyer WHERE w.year = YEAR(util.VN_CURDATE() - INTERVAL 1 WEEK)
AND w.week = WEEK(util.VN_CURDATE() - INTERVAL 1 WEEK, 4)
GROUP BY buyerFk
) sub ) sub
ORDER BY percentage DESC; ORDER BY `percentage` DESC;