diff --git a/modules/supplier/back/methods/supplier/createReceipt.js b/modules/supplier/back/methods/supplier/createReceipt.js new file mode 100644 index 000000000..c051a6751 --- /dev/null +++ b/modules/supplier/back/methods/supplier/createReceipt.js @@ -0,0 +1,173 @@ +const UserError = require('vn-loopback/util/user-error'); +// Insert payment + +// divisa = solo en caso que la moneda sea distinta a €, +module.exports = function(Self) { + Self.remoteMethodCtx('createReceipt', { + description: 'Creates receipt and its compensation if necessary', + accessType: 'READ', + accepts: [{ + arg: 'supplierFk', + type: 'number', + description: 'The supplier id', + http: {source: 'path'} + }, + { + arg: 'received', + type: 'Date', + required: true + }, + { + arg: 'dueDate', + type: 'Date', + required: true + }, + { + arg: 'companyFk', + type: 'number', + required: true + }, + { + arg: 'currencyFk', + type: 'number', + required: true + }, + { + arg: 'bankFk', + type: 'number', + required: true + }, + { + arg: 'payMethodFk', + type: 'number', + required: true + }, + { + arg: 'amount', + type: 'number', + required: true + }, + { + arg: 'concept', + type: 'string', + required: true + }, + { + arg: 'divisa', + type: 'number' + }, + { + arg: 'compensationAccount', + type: 'any' + }], + returns: { + root: true, + type: 'Object' + }, + http: { + verb: 'post', + path: '/:supplierFk/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 originalSupplier = await models.Supplier.findById(args.supplierFk, 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.Supplier.getClientOrSupplierReference(args.compensationAccount, myOptions); + + await Self.rawSql( + `CALL vn.ledger_doCompensation(?, ?, ?, ?, ?, ?, ?)`, + [ + date, + args.compensationAccount, + args.bankFk, + accountingType.receiptDescription + originalSupplier.accountingAccount, + args.amountPaid, + args.companyFk, + originalSupplier.accountingAccount + ], + myOptions + ); + } else if (accountingType.isAutoConciliated == true) { + const description = + `${originalSupplier.id} : ${originalSupplier.socialName} - ${accountingType.receiptDescription}`; + const [, [xdiarioNew]] = await Self.rawSql( + `CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew); + SELECT @xdiarioNew ledger;`, + [ + null, + date, + bank.account, + originalSupplier.accountingAccount, + description, + args.amountPaid, + 0, + 0, + '', + '', + null, + null, + false, + args.companyFk + ], + myOptions + ); + + await Self.rawSql( + `CALL xdiario_new(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, @xdiarioNew);`, + [ + xdiarioNew.ledger, + date, + originalSupplier.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; + } + }; +}; diff --git a/modules/supplier/back/methods/supplier/receipts.js b/modules/supplier/back/methods/supplier/receipts.js new file mode 100644 index 000000000..dce9c31d4 --- /dev/null +++ b/modules/supplier/back/methods/supplier/receipts.js @@ -0,0 +1,73 @@ + +const ParameterizedSQL = require('loopback-connector').ParameterizedSQL; +module.exports = Self => { + Self.remoteMethodCtx('receipts', { + description: 'Find all clients matched by the filter', + accessType: 'READ', + accepts: [ + { + arg: 'supplierId', + type: 'number', + description: 'The supplier id', + }, + { + arg: 'companyId', + type: 'number', + description: 'The company id', + }, + { + arg: 'currencyFk', + type: 'number', + description: 'The currency', + default: 1, + }, + { + arg: 'orderBy', + type: 'string', + description: 'The client fiscal id', + enum: ['issued', ' bookEntried', ' booked', ' dueDate'], + }, + { + arg: 'isConciliated', + default: false, + type: 'boolean', + }, + ], + returns: { + type: ['object'], + root: true + }, + http: { + path: `/receipts`, + verb: 'GET' + } + }); + + Self.receipts = async(ctx, filter, options) => { + const conn = Self.dataSource.connector; + const myOptions = {userId: ctx.req.accessToken.userId}; + const args = ctx.args; + + if (typeof options == 'object') + Object.assign(myOptions, options); + + let stmts = []; + stmts.push(new ParameterizedSQL('CALL vn.supplier_statementWithEntries(?,?,?,?,?,?)', [ + args.supplierId, + args.currencyFk ?? 1, + args.companyId, + args.orderBy ?? 'issued', + args.isConciliated ?? false, + false + ])); + stmts.push(` + SELECT * + FROM tmp.supplierStatement`); + stmts.push(`DROP TEMPORARY TABLE tmp.supplierStatement`); + const sql = ParameterizedSQL.join(stmts, ';'); + const results = await conn.executeStmt(sql); + const resultsIndex = stmts.length - 1; + const result = results[resultsIndex]; + return result; + }; +}; diff --git a/modules/supplier/back/models/supplier.js b/modules/supplier/back/models/supplier.js index 7e6908d57..20313801e 100644 --- a/modules/supplier/back/models/supplier.js +++ b/modules/supplier/back/models/supplier.js @@ -8,6 +8,8 @@ module.exports = Self => { require('../methods/supplier/updateFiscalData')(Self); require('../methods/supplier/consumption')(Self); require('../methods/supplier/freeAgencies')(Self); + require('../methods/supplier/createReceipt')(Self); + require('../methods/supplier/receipts')(Self); require('../methods/supplier/campaignMetricsPdf')(Self); require('../methods/supplier/campaignMetricsEmail')(Self); require('../methods/supplier/newSupplier')(Self);