158 lines
5.1 KiB
JavaScript
158 lines
5.1 KiB
JavaScript
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
module.exports = function(Self) {
|
|
Self.remoteMethodCtx('createReceipt', {
|
|
description: 'Creates receipt and its compensation if necessary',
|
|
accepts: [{
|
|
arg: 'clientFk',
|
|
type: 'number',
|
|
description: 'The client id',
|
|
http: {source: 'path'}
|
|
},
|
|
{
|
|
arg: 'payed',
|
|
type: 'Date',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'companyFk',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'bankFk',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'amountPaid',
|
|
type: 'number',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'description',
|
|
type: 'string',
|
|
required: true
|
|
},
|
|
{
|
|
arg: 'compensationAccount',
|
|
type: 'any'
|
|
}],
|
|
returns: {
|
|
root: true,
|
|
type: 'Object'
|
|
},
|
|
http: {
|
|
verb: 'post',
|
|
path: '/:clientFk/createReceipt'
|
|
}
|
|
});
|
|
|
|
Self.createReceipt = async(ctx, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
let tx;
|
|
const myOptions = {};
|
|
|
|
if (typeof options == 'object')
|
|
Object.assign(myOptions, options);
|
|
|
|
if (!myOptions.transaction) {
|
|
tx = await Self.beginTransaction({});
|
|
myOptions.transaction = tx;
|
|
}
|
|
|
|
try {
|
|
delete args.ctx; // Remove unwanted properties
|
|
const newReceipt = await models.Receipt.create(args, myOptions);
|
|
const originalClient = await models.Client.findById(args.clientFk, null, myOptions);
|
|
const bank = await models.Bank.findById(args.bankFk, null, myOptions);
|
|
const accountingType = await models.AccountingType.findById(bank.accountingTypeFk, null, myOptions);
|
|
|
|
if (accountingType.code == 'compensation') {
|
|
if (!args.compensationAccount)
|
|
throw new UserError('Compensation account is empty');
|
|
|
|
const supplierCompensation = await models.Supplier.findOne({
|
|
where: {
|
|
account: args.compensationAccount
|
|
}
|
|
}, myOptions);
|
|
|
|
let clientCompensation = {};
|
|
|
|
if (!supplierCompensation) {
|
|
clientCompensation = await models.Client.findOne({
|
|
where: {
|
|
accountingAccount: args.compensationAccount
|
|
}
|
|
}, myOptions);
|
|
}
|
|
if (!supplierCompensation && !clientCompensation)
|
|
throw new UserError('Invalid account');
|
|
|
|
await Self.rawSql(
|
|
`CALL vn.ledger_doCompensation(CURDATE(), ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
args.compensationAccount,
|
|
args.bankFk,
|
|
accountingType.receiptDescription + originalClient.accountingAccount,
|
|
args.amountPaid,
|
|
args.companyFk,
|
|
originalClient.accountingAccount
|
|
],
|
|
myOptions
|
|
);
|
|
} else if (accountingType.isAutoConciliated == true) {
|
|
const description = `${originalClient.id} : ${originalClient.socialName} - ${accountingType.receiptDescription}`;
|
|
const [xdiarioNew] = await Self.rawSql(
|
|
`SELECT xdiario_new(?, CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ledger;`,
|
|
[
|
|
null,
|
|
bank.account,
|
|
originalClient.accountingAccount,
|
|
description,
|
|
args.amountPaid,
|
|
0,
|
|
0,
|
|
'',
|
|
'',
|
|
null,
|
|
null,
|
|
false,
|
|
args.companyFk
|
|
],
|
|
myOptions
|
|
);
|
|
|
|
await Self.rawSql(
|
|
`SELECT xdiario_new(?, CURDATE(), ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);`,
|
|
[
|
|
xdiarioNew.ledger,
|
|
originalClient.accountingAccount,
|
|
bank.account,
|
|
description,
|
|
0,
|
|
args.amountPaid,
|
|
0,
|
|
'',
|
|
'',
|
|
null,
|
|
null,
|
|
false,
|
|
args.companyFk
|
|
],
|
|
myOptions
|
|
);
|
|
}
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
return newReceipt;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|