2018-10-01 06:17:36 +00:00
|
|
|
|
|
|
|
const ParameterizedSQL = require('vn-loopback/node_modules/loopback-connector').ParameterizedSQL;
|
|
|
|
|
2018-07-03 13:00:33 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
Self.remoteMethod('listSaleTracking', {
|
|
|
|
description: 'Returns all ticket sale trackings',
|
|
|
|
accessType: 'READ',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'filter',
|
|
|
|
type: 'Object',
|
2018-10-30 12:58:02 +00:00
|
|
|
description: 'Filter defining where and paginated data'
|
2018-07-03 13:00:33 +00:00
|
|
|
}],
|
|
|
|
returns: {
|
2018-10-30 12:58:02 +00:00
|
|
|
type: ['Object'],
|
2018-07-03 13:00:33 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/listSaleTracking`,
|
|
|
|
verb: 'get'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.listSaleTracking = async filter => {
|
2018-10-01 06:17:36 +00:00
|
|
|
let stmt = new ParameterizedSQL(`
|
|
|
|
SELECT
|
|
|
|
st.id,
|
|
|
|
s.ticketFk,
|
|
|
|
s.quantity,
|
|
|
|
s.concept,
|
|
|
|
s.itemFk,
|
|
|
|
st.originalQuantity,
|
|
|
|
st.created,
|
|
|
|
st.workerFk,
|
|
|
|
w.firstName,
|
|
|
|
w.name,
|
|
|
|
ste.name AS state
|
|
|
|
FROM saleTracking st
|
|
|
|
JOIN sale s ON s.id = st.saleFk
|
|
|
|
JOIN worker w ON w.id = st.workerFk
|
|
|
|
JOIN state ste ON ste.id = st.stateFk`);
|
2018-07-03 13:00:33 +00:00
|
|
|
|
2018-10-30 12:58:02 +00:00
|
|
|
stmt.merge(Self.makeSuffix(filter));
|
2018-07-03 13:00:33 +00:00
|
|
|
|
2018-10-01 06:17:36 +00:00
|
|
|
let trackings = await Self.rawStmt(stmt);
|
2018-07-03 13:00:33 +00:00
|
|
|
|
|
|
|
let salesFilter = {
|
|
|
|
include: [
|
|
|
|
{
|
|
|
|
relation: 'item',
|
|
|
|
scope: {
|
|
|
|
fields: ['itemFk', 'name'],
|
|
|
|
include: {
|
|
|
|
relation: 'tags',
|
|
|
|
scope: {
|
|
|
|
fields: ['tagFk', 'value'],
|
|
|
|
include: {
|
|
|
|
relation: 'tag',
|
|
|
|
scope: {
|
|
|
|
fields: ['name']
|
|
|
|
}
|
|
|
|
},
|
|
|
|
limit: 6
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
],
|
|
|
|
where: {ticketFk: filter.where.ticketFk}
|
|
|
|
};
|
|
|
|
|
|
|
|
let sales = await Self.app.models.Sale.find(salesFilter);
|
|
|
|
|
|
|
|
trackings.forEach(tracking => {
|
|
|
|
sales.forEach(sale => {
|
|
|
|
if (tracking.itemFk == sale.itemFk)
|
|
|
|
tracking.item = sale.item();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return trackings;
|
|
|
|
};
|
|
|
|
};
|