salix/services/loopback/common/models/client.js

205 lines
6.8 KiB
JavaScript
Raw Normal View History

var UserError = require('../helpers').UserError;
var getFinalState = require('../helpers').getFinalState;
var isMultiple = require('../helpers').isMultiple;
module.exports = Self => {
// Methods
require('../methods/client/activate')(Self);
require('../methods/client/card')(Self);
require('../methods/client/createWithUser')(Self);
require('../methods/client/listWorkers')(Self);
require('../methods/client/hasCustomerRole')(Self);
require('../methods/client/isValidClient')(Self);
require('../methods/client/activeSalesPerson')(Self);
require('../methods/client/addressesPropagateRe')(Self);
require('../methods/client/getDebt')(Self);
2018-03-27 13:06:22 +00:00
require('../methods/client/getMana')(Self);
require('../methods/client/getAverageInvoiced')(Self);
require('../methods/client/summary')(Self);
2018-07-13 10:37:58 +00:00
require('../methods/client/updateFiscalData')(Self);
require('../methods/client/updateBillingData')(Self);
require('../methods/client/updateBasicData')(Self);
// Validations
Self.validatesUniquenessOf('fi', {
2018-04-05 11:08:10 +00:00
message: 'TIN must be unique'
});
Self.validatesUniquenessOf('socialName', {
2018-04-05 11:20:27 +00:00
message: 'The company name must be unique'
});
Self.validatesFormatOf('email', {
2018-04-05 11:20:27 +00:00
message: 'Invalid email',
allowNull: true,
allowBlank: true,
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
});
Self.validatesLengthOf('postcode', {
allowNull: true,
allowBlank: true,
min: 3, max: 10
});
Self.validateAsync('iban', ibanNeedsValidation, {
message: 'The IBAN does not have the correct format'
});
var validateIban = require('../validations/validateIban');
async function ibanNeedsValidation(err, done) {
let filter = {
fields: ['code'],
where: {id: this.countryFk}
};
let country = await Self.app.models.Country.findOne(filter);
let code = country ? country.code.toLowerCase() : null;
if (code === 'fr')
return done();
if (!validateIban(this.iban))
err();
done();
}
2018-03-21 11:57:23 +00:00
Self.validateAsync('fi', tinIsValid, {
2018-04-05 11:08:10 +00:00
message: 'Invalid TIN'
});
2018-03-21 11:57:23 +00:00
let validateTin = require('../validations/validateTin');
async function tinIsValid(err, done) {
if (!this.isTaxDataChecked)
return done();
2018-03-12 13:13:36 +00:00
let filter = {
fields: ['code'],
where: {id: this.countryFk}
};
let country = await Self.app.models.Country.findOne(filter);
let code = country ? country.code.toLowerCase() : null;
2018-03-21 11:57:23 +00:00
if (!validateTin(this.fi, code))
2018-03-12 13:13:36 +00:00
err();
done();
}
Self.validate('payMethod', hasSalesMan, {
2018-04-05 11:20:27 +00:00
message: 'Cannot change the payment method if no salesperson'
});
function hasSalesMan(err) {
if (this.payMethod && !this.salesPerson)
err();
}
Self.validateAsync('payMethodFk', hasIban, {
2018-04-05 11:20:27 +00:00
message: 'That payment method requires an IBAN'
});
function hasIban(err, done) {
Self.app.models.PayMethod.findById(this.payMethodFk, (_, instance) => {
if (instance && instance.ibanRequired && !this.iban)
err();
done();
});
}
// Hooks
Self.observe('before save', async function(ctx) {
let changes = ctx.data || ctx.instance;
let orgData = ctx.currentInstance;
let finalState = getFinalState(ctx);
let payMethodWithIban = 4;
if (changes.salesPerson === null) {
changes.credit = 0;
changes.discount = 0;
changes.payMethodFk = 5; // Credit card
}
let payMethodFk = changes.payMethodFk || (orgData && orgData.payMethodFk);
let dueDay = changes.dueDay || (orgData && orgData.dueDay);
if (payMethodFk == payMethodWithIban && dueDay == 0)
changes.dueDay = 5;
if (isMultiple(ctx)) return;
if (changes.isEqualizated || changes.fi !== undefined) {
let fiLetter = finalState.fi && finalState.fi.toUpperCase().charAt(0);
let canMarkEqualizationTax = fiLetter != 'A' && fiLetter != 'B';
if (finalState.isEqualizated && !canMarkEqualizationTax)
throw new UserError('Unable to mark the equivalence surcharge');
}
if (changes.credit !== undefined) {
await validateCreditChange(ctx, finalState);
let filter = {fields: ['id'], where: {userFk: ctx.options.accessToken.userId}};
let worker = await Self.app.models.Worker.findOne(filter);
let newCredit = {
amount: changes.credit,
clientFk: finalState.id,
workerFk: worker ? worker.id : null
};
await Self.app.models.ClientCredit.create(newCredit);
}
});
async function validateCreditChange(ctx, finalState) {
let models = Self.app.models;
let userId = ctx.options.accessToken.userId;
2018-05-17 11:18:16 +00:00
let currentUserIsManager = await models.Account.hasRole(userId, 'manager');
if (currentUserIsManager)
return;
let filter = {
fields: ['roleFk'],
where: {
maxAmount: {gt: ctx.data.credit}
}
};
let limits = await models.ClientCreditLimit.find(filter);
if (limits.length == 0)
throw new UserError('Credit limits not found');
// Si el usuario no tiene alguno de los roles no continua
let requiredRoles = [];
for (limit of limits)
requiredRoles.push(limit.roleFk);
let where = {
roleId: {inq: requiredRoles},
principalType: 'USER',
principalId: userId
};
let count = await models.RoleMapping.count(where);
if (count <= 0)
throw new UserError('The role cannot set this credit amount');
// Si se puso a 0 por gerencia, solo gerencia puede aumentarlo
let query = 'SELECT * FROM clientCredit WHERE clientFk = ? ORDER BY created DESC LIMIT 1';
let instances = await Self.rawSql(query, [finalState.id]);
2018-05-17 11:18:16 +00:00
if (instances.length !== 1 || instances[0].amount > 0)
return;
2018-05-17 11:18:16 +00:00
query = `
SELECT COUNT(*) AS hasRoleManager
FROM worker em
JOIN account.user ac ON ac.id = em.userFk
JOIN salix.RoleMapping rm ON rm.principalId = ac.id
JOIN account.role r on r.id = rm.roleId
WHERE em.id = ?
AND rm.principalType = 'USER'
AND r.name = 'manager'`;
let instance = await Self.rawSql(query, [instances[0].workerFk]);
2018-05-17 11:18:16 +00:00
if (instance[0].hasRoleManager)
throw new UserError('Only manager can change the credit');
}
};