2022-10-28 12:49:22 +00:00
|
|
|
const {Email} = require('vn-print');
|
2022-11-21 09:49:19 +00:00
|
|
|
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',
|
2022-11-21 09:49:19 +00:00
|
|
|
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'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2022-11-21 09:49:19 +00:00
|
|
|
Self.balanceCompensationEmail = async(ctx, id) => {
|
2022-10-28 12:49:22 +00:00
|
|
|
const models = Self.app.models;
|
2022-11-21 09:49:19 +00:00
|
|
|
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);
|
2022-11-21 09:49:19 +00:00
|
|
|
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);
|
2022-11-21 09:49:19 +00:00
|
|
|
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
|
|
|
|
2022-11-21 09:49:19 +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`);
|
2022-11-21 09:49:19 +00:00
|
|
|
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
|
|
|
};
|
|
|
|
};
|