production filtering in temporary table

This commit is contained in:
Daniel Herrero 2017-11-08 13:42:33 +01:00
parent 97da7e5c10
commit 061ae64c50
4 changed files with 77 additions and 48 deletions

View File

@ -83,7 +83,15 @@ export default class ProductionIndex {
event.preventDefault();
}
onChildSubmit(filter) {
this.searchTickets(filter);
let newFilter = {};
Object.keys(filter).forEach(
field => {
if (filter[field] !== null) {
newFilter[field] = filter[field];
}
}
);
this.searchTickets(newFilter);
this.onChildCancel();
}
onChildCancel() {
@ -111,7 +119,7 @@ export default class ProductionIndex {
}
refreshTickets() {
this.filter = {};
this.filter.warehouseFk = this.userProfile.warehouseId;
this.filter.warehouseFk = this.$.displayValue = this.userProfile.warehouseId;
this.search = null;
}
onChangeWareHouse(warehouse) {
@ -130,7 +138,7 @@ export default class ProductionIndex {
this.hourItems.push({id: i, name: hour});
}
this.filter.warehouseFk = this.$.displayValue = this.userProfile.warehouseId;
this.searchTickets(this.filter);
// this.searchTickets(this.filter);
}
}

View File

@ -52,7 +52,7 @@ html [vn-center], .vn-center{
}
.list-element{
padding: 4px 0px;
padding: 8px 0 0 0;
border-bottom: 1px solid $color-medium-grey;
i {
color: $color-orange;

View File

@ -2,7 +2,7 @@ module.exports = function(Self) {
Self.setup = function() {
Self.super_.setup.call(this);
let disableMethods = {
/* let disableMethods = {
create: true,
replaceOrCreate: true,
patchOrCreate: true,
@ -22,7 +22,7 @@ module.exports = function(Self) {
};
for (let method in disableMethods) {
// this.disableRemoteMethod(method, disableMethods[method]);
}
} */
};
Self.defineScope = function(serverFilter) {
@ -108,6 +108,10 @@ module.exports = function(Self) {
this.remoteMethod(methodName, args);
};
Self.getConnection = function(cb) {
this.dataSource.connector.client.getConnection(cb);
};
Self.connectToService = function(ctx, dataSource) {
this.app.dataSources[dataSource].connector.remotes.auth = {
bearer: new Buffer(ctx.req.accessToken.id).toString('base64'),

View File

@ -1,62 +1,79 @@
module.exports = function(FakeProduction) {
FakeProduction.defineScope();
module.exports = function(Self) {
Self.defineScope();
FakeProduction.list = function(ctx, filter, cb) {
Self.list = function(ctx, filter, callback) {
var page = filter.page - 1;
var limit = filter.limit * page;
var offset = (page + 1) * filter.limit;
let daysTickets = 0;
let warehouseFk = filter.where.warehouseFk;
delete filter.limit;
delete filter.page;
delete filter.where.warehouseFk;
let query = `call salix.production_control_source( ? , ?)`;
var params = [filter.where.warehouseFk, daysTickets];
FakeProduction.rawSql(query, params)
.then(function(response) {
getValue();
})
.catch(function(response) {
cb(response, null);
});
let getValue = () => {
let call = `call salix.production_control_source(? , ?)`;
var params = [warehouseFk, daysTickets];
let conn;
Self.getConnection((err, connection) => {
if (err) {
onFinish(err);
}
conn = connection;
conn.query(call, params, getValue);
});
function getValue(err) {
if (err) {
onFinish(err);
}
if (filter.where && filter.where.q) {
var newFilter = {
and: [
{
or: [
{agency: {regexp: filter.where.q}},
{state: {regexp: filter.where.q}}
]
}
]
};
let regexQ = new RegExp(filter.where.q);
delete filter.where.q;
let newFilter = {
and: [
{
or: [
{agency: {regexp: regexQ}},
{state: {regexp: regexQ}}
]
}
]
};
delete filter.where.q;
if (Object.keys(filter.where).length) {
Object.keys(filter.where).forEach(
key => {
let field = {};
field[key] = filter.where[key];
newFilter.and.push(field);
}
);
}
if (Object.keys(filter.where).length) {
Object.keys(filter.where).forEach(
key => {
let field = {};
field[key] = filter.where[key];
newFilter.and.push(field);
}
);
}
filter.where = newFilter;
}
FakeProduction.connectToService(ctx, "client");
this.find(filter, function(err, tickets) {
FakeProduction.disconnectFromService("client");
(err) ? cb(err, null) : cb(null, (sum(tickets)));
});
};
function sum(tickets){
let where = Self.dataSource.connector.buildWhere(Self.modelName, filter.where);
let query = `SELECT * FROM FakeProduction_tmp ${where.sql} GROUP BY RouteFk ORDER BY routeFk`;
conn.query(query, where.params, onFinish);
}
function onFinish(err, results) {
conn.query('DROP TEMPORARY TABLE IF EXISTS FakeProduction_tmp');
conn.release();
if (err)
callback(err);
callback(null, sum(results));
}
function sum(tickets) {
var obj = {lines: 0, m3: 0};
tickets.forEach(function(t) {
tickets.forEach(function(t, i) {
obj.lines += t.lines;
obj.m3 += t.m3;
if (tickets[i].problem)
tickets[i].problem = tickets[i].problem.trim();
}, this);
obj.m3 = obj.m3.toFixed(2);
obj.total = tickets.length;