7837-testToMaster_2432 #2834
|
@ -27,28 +27,42 @@ module.exports = Self => {
|
|||
});
|
||||
|
||||
Self.getWasteByItem = async(buyer, family, options) => {
|
||||
const models = Self.app.models;
|
||||
const myOptions = {};
|
||||
|
||||
if (typeof options == 'object')
|
||||
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();
|
||||
date.setHours(0, 0, 0, 0);
|
||||
const wastes = await Self.rawSql(`
|
||||
SELECT *, 100 * dwindle / total AS percentage
|
||||
FROM (
|
||||
SELECT buyer,
|
||||
ws.family,
|
||||
ws.itemFk,
|
||||
sum(ws.saleTotal) AS total,
|
||||
sum(ws.saleWaste) AS dwindle
|
||||
FROM bs.waste ws
|
||||
WHERE buyer = ? AND family = ?
|
||||
AND year = YEAR(TIMESTAMPADD(WEEK,-1, ?))
|
||||
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1)
|
||||
GROUP BY buyer, itemFk
|
||||
) sub
|
||||
ORDER BY family, percentage DESC`, [buyer, family, date, date], myOptions);
|
||||
SELECT *, 100 * dwindle / total percentage
|
||||
FROM (
|
||||
SELECT u.name buyer,
|
||||
it.name family,
|
||||
w.itemFk,
|
||||
SUM(w.saleTotal) total,
|
||||
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
|
||||
FROM bs.waste w
|
||||
JOIN account.user u ON u.id = w.buyerFk
|
||||
JOIN vn.itemType it ON it.id = w.itemTypeFk
|
||||
WHERE w.buyerFk = ? AND w.itemTypeFk = ?
|
||||
AND w.year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
|
||||
AND w.week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
|
||||
GROUP BY w.buyerFk, w.itemFk
|
||||
) sub
|
||||
ORDER BY family, percentage DESC
|
||||
`, [user.id, itemType.id, date, date], myOptions);
|
||||
|
||||
const details = [];
|
||||
|
||||
|
|
|
@ -22,31 +22,37 @@ module.exports = Self => {
|
|||
const date = Date.vnNew();
|
||||
date.setHours(0, 0, 0, 0);
|
||||
const wastes = await Self.rawSql(`
|
||||
SELECT *, 100 * dwindle / total AS percentage
|
||||
FROM (
|
||||
SELECT buyer,
|
||||
ws.family,
|
||||
sum(ws.saleTotal) AS total,
|
||||
sum(ws.saleWaste) AS dwindle
|
||||
FROM bs.waste ws
|
||||
WHERE year = YEAR(TIMESTAMPADD(WEEK,-1, ?))
|
||||
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1)
|
||||
GROUP BY buyer, family
|
||||
) sub
|
||||
ORDER BY percentage DESC`, [date, date], myOptions);
|
||||
SELECT *, 100 * dwindle / total percentage
|
||||
FROM (
|
||||
SELECT u.name buyer,
|
||||
it.name family,
|
||||
w.itemFk,
|
||||
SUM(w.saleTotal) total,
|
||||
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
|
||||
FROM bs.waste w
|
||||
JOIN account.user u ON u.id = w.buyerFk
|
||||
JOIN vn.itemType it ON it.id = w.itemTypeFk
|
||||
WHERE year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
|
||||
AND week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
|
||||
GROUP BY buyerFk, itemTypeFk
|
||||
) sub
|
||||
ORDER BY percentage DESC
|
||||
`, [date, date], myOptions);
|
||||
|
||||
const wastesTotal = await Self.rawSql(`
|
||||
SELECT *, 100 * dwindle / total AS percentage
|
||||
SELECT *, 100 * dwindle / total percentage
|
||||
FROM (
|
||||
SELECT buyer,
|
||||
sum(ws.saleTotal) AS total,
|
||||
sum(ws.saleWaste) AS dwindle
|
||||
FROM bs.waste ws
|
||||
WHERE year = YEAR(TIMESTAMPADD(WEEK,-1, ?))
|
||||
AND week = WEEK(TIMESTAMPADD(WEEK,-1, ?), 1)
|
||||
GROUP BY buyer
|
||||
SELECT u.name buyer,
|
||||
SUM(w.saleTotal) total,
|
||||
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
|
||||
FROM bs.waste w
|
||||
JOIN account.user u ON u.id = w.buyerFk
|
||||
WHERE w.year = YEAR(TIMESTAMPADD(WEEK, -1, ?))
|
||||
AND w.week = WEEK(TIMESTAMPADD(WEEK, -1, ?), 1)
|
||||
GROUP BY w.buyerFk
|
||||
) sub
|
||||
ORDER BY percentage DESC`, [date, date], myOptions);
|
||||
ORDER BY percentage DESC
|
||||
`, [date, date], myOptions);
|
||||
|
||||
const details = [];
|
||||
|
||||
|
|
|
@ -1,18 +1,18 @@
|
|||
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() => {
|
||||
const tx = await models.Item.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
|
||||
try {
|
||||
const result = await models.Item.getWasteByItem('CharlesXavier', 'Cymbidium', options);
|
||||
const result = await models.Item.getWasteByItem('buyer', 'Crisantemo', options);
|
||||
|
||||
const length = result.length;
|
||||
const anyResult = result[Math.floor(Math.random() * Math.floor(length))];
|
||||
|
||||
expect(anyResult.buyer).toEqual('CharlesXavier');
|
||||
expect(anyResult.family).toEqual('Cymbidium');
|
||||
expect(anyResult.buyer).toEqual('buyer');
|
||||
expect(anyResult.family).toEqual('Crisantemo');
|
||||
expect(anyResult.lines.length).toBeGreaterThanOrEqual(2);
|
||||
|
||||
await tx.rollback();
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
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() => {
|
||||
const tx = await models.Item.beginTransaction({});
|
||||
const options = {transaction: tx};
|
||||
|
@ -11,9 +11,9 @@ describe('Item getWasteByWorker()', () => {
|
|||
const length = result.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.lines.length).toBeGreaterThanOrEqual(3);
|
||||
expect(anyResult.lines.length).toBeGreaterThanOrEqual(1);
|
||||
|
||||
await tx.rollback();
|
||||
} catch (e) {
|
||||
|
|
|
@ -1,11 +1,13 @@
|
|||
SELECT *, 100 * dwindle / total AS percentage
|
||||
SELECT *, 100 * dwindle / total `percentage`
|
||||
FROM (
|
||||
SELECT buyer,
|
||||
sum(saleTotal) as total,
|
||||
sum(saleWaste) as dwindle
|
||||
SELECT u.name buyer,
|
||||
SUM(saleTotal) total,
|
||||
SUM(w.saleInternalWaste + w.saleExternalWaste) dwindle
|
||||
FROM bs.waste w
|
||||
JOIN vn.time t ON w.year = t.year AND w.week = t.week
|
||||
WHERE t.dated = DATE_ADD(CURDATE(), INTERVAL -1 WEEK)
|
||||
GROUP BY buyer
|
||||
JOIN account.user u ON u.id = w.buyerFk
|
||||
JOIN vn.itemType it ON it.id = w.itemTypeFk
|
||||
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
|
||||
ORDER BY percentage DESC;
|
||||
ORDER BY `percentage` DESC;
|
||||
|
|
Loading…
Reference in New Issue