2019-02-28 13:30:29 +00:00
|
|
|
let request = require('request-promise-native');
|
2018-12-27 11:54:16 +00:00
|
|
|
let UserError = require('vn-loopback/util/user-error');
|
|
|
|
let getFinalState = require('vn-loopback/util/hook').getFinalState;
|
|
|
|
let isMultiple = require('vn-loopback/util/hook').isMultiple;
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-05-04 09:46:03 +00:00
|
|
|
module.exports = Self => {
|
2018-01-29 11:37:54 +00:00
|
|
|
// Methods
|
2018-11-27 07:50:13 +00:00
|
|
|
require('../methods/client/activeWorkersWithRole')(Self);
|
2018-09-14 11:43:51 +00:00
|
|
|
require('../methods/client/getCard')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
require('../methods/client/createWithUser')(Self);
|
|
|
|
require('../methods/client/listWorkers')(Self);
|
|
|
|
require('../methods/client/hasCustomerRole')(Self);
|
2018-03-07 11:24:47 +00:00
|
|
|
require('../methods/client/isValidClient')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
require('../methods/client/addressesPropagateRe')(Self);
|
2018-03-09 15:44:18 +00:00
|
|
|
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);
|
2018-09-28 13:00:12 +00:00
|
|
|
require('../methods/client/getTransactions')(Self);
|
|
|
|
require('../methods/client/confirmTransaction')(Self);
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
// Validations
|
|
|
|
|
2019-02-28 07:55:34 +00:00
|
|
|
Self.validatesPresenceOf('street', {
|
|
|
|
message: 'Street cannot be empty'
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.validatesPresenceOf('city', {
|
|
|
|
message: 'City cannot be empty'
|
|
|
|
});
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesUniquenessOf('fi', {
|
2018-04-05 11:08:10 +00:00
|
|
|
message: 'TIN must be unique'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesUniquenessOf('socialName', {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'The company name must be unique'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesFormatOf('email', {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'Invalid email',
|
2018-01-29 11:37:54 +00:00
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
2018-03-12 11:08:34 +00:00
|
|
|
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-23 12:56:38 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.validatesLengthOf('postcode', {
|
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
|
|
|
min: 3, max: 10
|
|
|
|
});
|
|
|
|
|
2018-07-27 13:13:01 +00:00
|
|
|
Self.validateAsync('iban', ibanNeedsValidation, {
|
|
|
|
message: 'The IBAN does not have the correct format'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-11-08 14:55:48 +00:00
|
|
|
let validateIban = require('../validations/validateIban');
|
2018-07-27 13:13:01 +00:00
|
|
|
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;
|
2018-11-23 07:21:21 +00:00
|
|
|
if (code != 'es')
|
2018-07-27 13:13:01 +00:00
|
|
|
return done();
|
|
|
|
|
|
|
|
if (!validateIban(this.iban))
|
|
|
|
err();
|
|
|
|
done();
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
2018-03-21 11:57:23 +00:00
|
|
|
Self.validateAsync('fi', tinIsValid, {
|
2018-04-05 11:08:10 +00:00
|
|
|
message: 'Invalid TIN'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-03-21 11:57:23 +00:00
|
|
|
let validateTin = require('../validations/validateTin');
|
|
|
|
async function tinIsValid(err, done) {
|
2018-05-22 10:09:53 +00:00
|
|
|
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();
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
Self.validate('payMethod', hasSalesMan, {
|
2018-04-05 11:20:27 +00:00
|
|
|
message: 'Cannot change the payment method if no salesperson'
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
2018-10-16 11:06:58 +00:00
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
function hasSalesMan(err) {
|
|
|
|
if (this.payMethod && !this.salesPerson)
|
|
|
|
err();
|
|
|
|
}
|
|
|
|
|
2018-11-20 10:02:57 +00:00
|
|
|
Self.validate('isEqualizated', cannotHaveET, {
|
|
|
|
message: 'Cannot check Equalization Tax in this NIF/CIF'
|
|
|
|
});
|
|
|
|
|
|
|
|
function cannotHaveET(err) {
|
|
|
|
let tin = this.fi.toUpperCase();
|
|
|
|
let cannotHaveET = /^[A-B]/.test(tin);
|
|
|
|
|
|
|
|
if (cannotHaveET && this.isEqualizated)
|
|
|
|
err();
|
|
|
|
}
|
|
|
|
|
2018-11-07 08:17:07 +00:00
|
|
|
Self.validateAsync('payMethodFk', hasIban, {
|
|
|
|
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();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
Self.validateAsync('bankEntityFk', hasBic, {
|
|
|
|
message: 'That payment method requires a BIC'
|
|
|
|
});
|
|
|
|
|
|
|
|
function hasBic(err, done) {
|
2018-11-22 14:17:35 +00:00
|
|
|
if (this.iban && !this.bankEntityFk)
|
|
|
|
err();
|
|
|
|
done();
|
2018-11-07 08:17:07 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 07:55:34 +00:00
|
|
|
Self.validateAsync('defaultAddressFk', isActive,
|
|
|
|
{message: 'Unable to default a disabled consignee'}
|
|
|
|
);
|
|
|
|
|
|
|
|
async function isActive(err, done) {
|
|
|
|
if (!this.defaultAddressFk)
|
|
|
|
return done();
|
|
|
|
|
|
|
|
const address = await Self.app.models.Address.findById(this.defaultAddressFk);
|
|
|
|
if (address && !address.isActive) err();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
Self.observe('before save', async function(ctx) {
|
|
|
|
let changes = ctx.data || ctx.instance;
|
2018-07-10 12:13:47 +00:00
|
|
|
let orgData = ctx.currentInstance;
|
2018-01-29 11:37:54 +00:00
|
|
|
let finalState = getFinalState(ctx);
|
2018-07-10 12:13:47 +00:00
|
|
|
let payMethodWithIban = 4;
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
if (changes.salesPerson === null) {
|
|
|
|
changes.credit = 0;
|
|
|
|
changes.discount = 0;
|
|
|
|
changes.payMethodFk = 5; // Credit card
|
|
|
|
}
|
|
|
|
|
2018-07-10 12:13:47 +00:00
|
|
|
let payMethodFk = changes.payMethodFk || (orgData && orgData.payMethodFk);
|
|
|
|
let dueDay = changes.dueDay || (orgData && orgData.dueDay);
|
|
|
|
|
|
|
|
if (payMethodFk == payMethodWithIban && dueDay == 0)
|
2018-01-29 11:37:54 +00:00
|
|
|
changes.dueDay = 5;
|
|
|
|
|
|
|
|
if (isMultiple(ctx)) return;
|
|
|
|
|
2018-03-06 14:14:49 +00:00
|
|
|
if (changes.credit !== undefined) {
|
2018-06-18 11:41:49 +00:00
|
|
|
await validateCreditChange(ctx, finalState);
|
2018-03-06 14:14:49 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
});
|
|
|
|
|
2019-02-28 13:30:29 +00:00
|
|
|
Self.observe('after save', async ctx => {
|
|
|
|
if (ctx.isNewInstance) return;
|
|
|
|
|
|
|
|
const hookState = ctx.hookState;
|
|
|
|
const newInstance = hookState.newInstance;
|
|
|
|
const oldInstance = hookState.oldInstance;
|
|
|
|
const instance = ctx.instance;
|
|
|
|
|
|
|
|
const payMethodChanged = oldInstance.payMethodFk != newInstance.payMethodFk;
|
|
|
|
const ibanChanged = oldInstance.iban != newInstance.iban;
|
|
|
|
const dueDayChanged = oldInstance.dueDay != newInstance.dueDay;
|
|
|
|
|
|
|
|
if (payMethodChanged || ibanChanged || dueDayChanged) {
|
|
|
|
const message = `La forma de pago del client con id ${instance.id} ha cambiado`;
|
|
|
|
const salesPersonFk = instance.salesPersonFk;
|
|
|
|
|
|
|
|
if (salesPersonFk) {
|
|
|
|
await Self.app.models.Message.send(ctx, {
|
|
|
|
recipientFk: salesPersonFk,
|
|
|
|
message: message
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const options = {
|
|
|
|
method: 'POST',
|
|
|
|
uri: 'http://127.0.0.1:3000/api/email/payment-update',
|
|
|
|
body: {
|
|
|
|
clientFk: instance.id
|
|
|
|
},
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
await request(options);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
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;
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
let filter = {
|
|
|
|
fields: ['roleFk'],
|
|
|
|
where: {
|
|
|
|
maxAmount: {gt: ctx.data.credit}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let limits = await models.ClientCreditLimit.find(filter);
|
|
|
|
|
|
|
|
if (limits.length == 0)
|
2018-06-18 11:41:49 +00:00
|
|
|
throw new UserError('Credit limits not found');
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
// 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)
|
2018-06-18 11:41:49 +00:00
|
|
|
throw new UserError('The role cannot set this credit amount');
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
// 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)
|
2018-01-29 11:37:54 +00:00
|
|
|
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'`;
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
let instance = await Self.rawSql(query, [instances[0].workerFk]);
|
|
|
|
|
2018-05-17 11:18:16 +00:00
|
|
|
if (instance[0].hasRoleManager)
|
2018-06-18 11:41:49 +00:00
|
|
|
throw new UserError('Only manager can change the credit');
|
2018-01-29 11:37:54 +00:00
|
|
|
}
|
|
|
|
};
|