153 lines
4.9 KiB
JavaScript
153 lines
4.9 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',
|
|
accessType: 'READ',
|
|
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'
|
|
},
|
|
accessScopes: ['DEFAULT', 'read:multimedia']
|
|
});
|
|
|
|
Self.createReceipt = async(ctx, options) => {
|
|
const models = Self.app.models;
|
|
const args = ctx.args;
|
|
const date = Date.vnNew();
|
|
date.setHours(0, 0, 0, 0);
|
|
|
|
let tx;
|
|
const myOptions = {userId: ctx.req.accessToken.userId};
|
|
|
|
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 originalClient = await models.Client.findById(args.clientFk, null, myOptions);
|
|
const bank = await models.Accounting.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');
|
|
|
|
// Check compensation account exists
|
|
await models.Client.getClientOrSupplierReference(args.compensationAccount, myOptions);
|
|
|
|
await Self.rawSql(
|
|
`CALL vn.ledger_doCompensation(?, ?, ?, ?, ?, ?, ?)`,
|
|
[
|
|
date,
|
|
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(
|
|
`CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew);
|
|
SELECT @xdiarioNew ledger;`,
|
|
[
|
|
null,
|
|
date,
|
|
bank.account,
|
|
originalClient.accountingAccount,
|
|
description,
|
|
args.amountPaid,
|
|
0,
|
|
0,
|
|
'',
|
|
'',
|
|
null,
|
|
null,
|
|
false,
|
|
args.companyFk
|
|
],
|
|
myOptions
|
|
);
|
|
|
|
await Self.rawSql(
|
|
`CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew);`,
|
|
[
|
|
xdiarioNew.ledger,
|
|
date,
|
|
originalClient.accountingAccount,
|
|
bank.account,
|
|
description,
|
|
0,
|
|
args.amountPaid,
|
|
0,
|
|
'',
|
|
'',
|
|
null,
|
|
null,
|
|
false,
|
|
args.companyFk
|
|
],
|
|
myOptions
|
|
);
|
|
}
|
|
const newReceipt = await models.Receipt.create(args, myOptions);
|
|
if (tx) await tx.commit();
|
|
|
|
return newReceipt;
|
|
} catch (e) {
|
|
if (tx) await tx.rollback();
|
|
throw e;
|
|
}
|
|
};
|
|
};
|