salix/modules/item/back/models/item.js

73 lines
2.7 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
let UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
2018-07-06 14:32:23 +00:00
require('../methods/item/filter')(Self);
require('../methods/item/clone')(Self);
require('../methods/item/updateTaxes')(Self);
require('../methods/item/getBalance')(Self);
2020-08-31 13:03:00 +00:00
require('../methods/item/lastEntriesFilter')(Self);
require('../methods/item/getSummary')(Self);
require('../methods/item/getCard')(Self);
2018-11-12 12:12:25 +00:00
require('../methods/item/regularize')(Self);
2019-02-18 06:57:15 +00:00
require('../methods/item/getVisibleAvailable')(Self);
2019-01-16 07:46:40 +00:00
require('../methods/item/new')(Self);
2021-03-30 08:28:44 +00:00
require('../methods/item/getWasteByWorker')(Self);
require('../methods/item/getWasteByItem')(Self);
2020-02-17 11:47:03 +00:00
require('../methods/item/createIntrastat')(Self);
2021-11-16 12:56:09 +00:00
require('../methods/item/activeBuyers')(Self);
2022-09-27 12:20:57 +00:00
require('../methods/item/buyerWasteEmail')(Self);
2022-10-03 11:35:49 +00:00
require('../methods/item/labelPdf')(Self);
2018-01-10 11:49:52 +00:00
Self.validatesPresenceOf('originFk', {message: 'Cannot be blank'});
Self.observe('before save', async function(ctx) {
2023-01-25 13:44:00 +00:00
let instance = ctx.currentInstance;
2023-01-20 12:34:36 +00:00
let oldInstance;
let newInstance;
ctx.hookState.oldInstance === undefined ? oldInstance = false : oldInstance = ctx.hookState.oldInstance;
ctx.hookState.newInstance === undefined ? newInstance = false : newInstance = ctx.hookState.newInstance;
if (oldInstance.image && newInstance.image) {
2023-01-25 13:44:00 +00:00
let image = newInstance.image.split('-')[0];
let user = newInstance.image.split('-')[1];
ctx.hookState.newInstance.image = image;
let query = `UPDATE vn.item SET doPhoto=0 WHERE id = ${instance.id}`;
2023-01-20 12:34:36 +00:00
await Self.rawSql(query);
2023-01-25 13:44:00 +00:00
let logQuery =
'INSERT INTO vn.itemLog(originFk, userFk, action, creationDate, description, oldInstance, newInstance) ' +
'VALUES (?, ?, ?, ?, ?, ?, ?)';
let logParams = [
instance.id,
user,
'update',
new Date(),
'Image',
JSON.stringify(oldInstance),
JSON.stringify(newInstance)
];
await Self.rawSql(logQuery, logParams);
2023-01-20 12:34:36 +00:00
}
await Self.availableId(ctx);
});
Self.availableId = async function(ctx) {
if (ctx.isNewInstance) {
try {
let query = `SELECT i1.id + 1 as id FROM vn.item i1
LEFT JOIN vn.item i2 ON i1.id + 1 = i2.id
WHERE i2.id IS NULL ORDER BY i1.id LIMIT 1`;
let newId = await Self.rawSql(query);
ctx.instance.id = newId[0].id;
2018-08-20 08:06:00 +00:00
return ctx.instance.id;
} catch (e) {
throw new UserError(e);
}
}
};
2017-12-19 11:29:35 +00:00
};