salix/modules/client/back/methods/receipt/balanceCompensationEmail.js

53 lines
1.7 KiB
JavaScript
Raw Permalink Normal View History

2022-10-28 12:49:22 +00:00
const {Email} = require('vn-print');
const UserError = require('vn-loopback/util/user-error');
2022-10-28 12:49:22 +00:00
module.exports = Self => {
2022-11-02 14:54:23 +00:00
Self.remoteMethodCtx('balanceCompensationEmail', {
2022-10-28 12:49:22 +00:00
description: 'Sends the debit balances compensation email with an attached PDF',
accessType: 'WRITE',
accepts: [
{
arg: 'id',
2022-11-02 14:54:23 +00:00
type: 'Number',
2022-10-28 12:49:22 +00:00
required: true,
description: 'The receipt id',
http: {source: 'path'}
2022-10-28 12:49:22 +00:00
}
],
returns: {
type: ['object'],
root: true
},
http: {
2022-11-02 14:54:23 +00:00
path: '/:id/balance-compensation-email',
2022-10-28 12:49:22 +00:00
verb: 'POST'
}
});
Self.balanceCompensationEmail = async(ctx, id) => {
2022-10-28 12:49:22 +00:00
const models = Self.app.models;
const receipt = await models.Receipt.findById(id, {fields: ['clientFk', 'bankFk']});
2024-03-05 09:25:23 +00:00
const bank = await models.Accounting.findById(receipt.bankFk);
if (!bank)
2022-11-22 13:24:19 +00:00
throw new UserError(`Receipt's bank was not found`);
2022-10-28 12:49:22 +00:00
2022-11-22 13:24:19 +00:00
const accountingType = await models.AccountingType.findById(bank.accountingTypeFk);
if (!(accountingType && accountingType.code == 'compensation'))
2022-11-22 13:24:19 +00:00
throw new UserError(`This receipt was not compensated`);
2022-10-28 12:49:22 +00:00
const client = await models.Client.findById(receipt.clientFk, {fields: ['email']});
if (!client.email)
2022-11-22 13:24:19 +00:00
throw new UserError(`Client's email was not found`);
else {
const email = new Email('balance-compensation', {
lang: ctx.req.getLocale(),
recipient: client.email + ',administracion@verdnatura.es',
id
});
return email.send();
}
2022-10-28 12:49:22 +00:00
};
};