2021-02-12 12:18:02 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
|
|
|
|
2018-10-19 06:40:32 +00:00
|
|
|
module.exports = function(Self) {
|
|
|
|
require('../methods/receipt/filter')(Self);
|
2020-12-16 07:33:08 +00:00
|
|
|
|
|
|
|
Self.validateBinded('amountPaid', isNotZero, {
|
|
|
|
message: 'Amount cannot be zero',
|
|
|
|
allowNull: false,
|
|
|
|
allowBlank: false
|
|
|
|
});
|
|
|
|
|
|
|
|
function isNotZero(value) {
|
|
|
|
return !isNaN(value) && value != 0;
|
|
|
|
}
|
|
|
|
|
2021-01-21 09:25:25 +00:00
|
|
|
Self.validateAsync('companyFk', isOfficialCompany, {
|
2020-12-16 07:33:08 +00:00
|
|
|
message: 'Company has to be official'
|
|
|
|
});
|
|
|
|
|
2021-01-27 08:14:37 +00:00
|
|
|
async function isOfficialCompany(err, done) {
|
|
|
|
const hasCompany = await Self.app.models.Company.exists(this.companyFk);
|
|
|
|
if (!hasCompany) err();
|
|
|
|
done();
|
2020-12-16 07:33:08 +00:00
|
|
|
}
|
2018-10-24 12:10:48 +00:00
|
|
|
|
|
|
|
Self.observe('before save', async function(ctx) {
|
|
|
|
if (ctx.isNewInstance) {
|
2021-02-12 12:18:02 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
|
|
|
ctx.instance.workerFk = loopBackContext.active.accessToken.userId;
|
2020-12-16 07:33:08 +00:00
|
|
|
await Self.app.models.Till.create({
|
2021-02-12 12:18:02 +00:00
|
|
|
workerFk: ctx.instance.workerFk,
|
2020-12-16 07:33:08 +00:00
|
|
|
bankFk: ctx.instance.bankFk,
|
|
|
|
in: ctx.instance.amountPaid,
|
|
|
|
concept: ctx.instance.description,
|
|
|
|
dated: ctx.instance.payed,
|
|
|
|
serie: 'A',
|
|
|
|
isAccountable: true,
|
|
|
|
number: ctx.instance.clientFk,
|
|
|
|
companyFk: ctx.instance.companyFk
|
|
|
|
});
|
2018-10-24 12:10:48 +00:00
|
|
|
}
|
|
|
|
});
|
2018-10-19 06:40:32 +00:00
|
|
|
};
|