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'
|
|
|
|
});
|
|
|
|
|
|
|
|
async function isOfficialCompany(value) {
|
|
|
|
const company = await Self.app.models.Company.findById(value);
|
|
|
|
return company.isOfficial;
|
|
|
|
}
|
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
|
|
|
};
|