salix/modules/client/back/models/receipt.js

45 lines
1.3 KiB
JavaScript
Raw Normal View History

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) {
let token = ctx.options.accessToken;
let userId = token && token.userId;
2020-12-16 07:33:08 +00:00
ctx.instance.workerFk = userId;
await Self.app.models.Till.create({
workerFk: userId,
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
};