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

45 lines
1.3 KiB
JavaScript

module.exports = function(Self) {
require('../methods/receipt/filter')(Self);
require('../methods/receipt/createReceipt')(Self);
Self.validateBinded('amountPaid', isNotZero, {
message: 'Amount cannot be zero',
allowNull: false,
allowBlank: false
});
function isNotZero(value) {
return !isNaN(value) && value != 0;
}
Self.validateBinded('companyFk', isOfficialCompany, {
message: 'Company has to be official'
});
async function isOfficialCompany(value) {
const company = await Self.app.models.Company.findById(value);
return company.isOfficial;
}
Self.observe('before save', async function(ctx) {
if (ctx.isNewInstance) {
let token = ctx.options.accessToken;
let userId = token && token.userId;
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
});
}
});
};