2017-06-01 16:23:41 +00:00
|
|
|
var app = require('../../server/server');
|
|
|
|
|
2017-01-12 10:02:22 +00:00
|
|
|
module.exports = function(Client) {
|
2017-06-01 16:23:41 +00:00
|
|
|
var models = app.models;
|
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
// Methods
|
|
|
|
|
2017-06-03 11:01:47 +00:00
|
|
|
require('../methods/client/card.js')(Client);
|
|
|
|
require('../methods/client/activate.js')(Client);
|
|
|
|
require('../methods/client/addresses.js')(Client);
|
|
|
|
require('../methods/client/filter.js')(Client);
|
|
|
|
require('../methods/client/before-save.js')(Client);
|
2017-06-30 07:26:55 +00:00
|
|
|
require('../methods/client/employee.js')(Client);
|
2017-05-17 06:19:03 +00:00
|
|
|
|
2017-02-08 11:45:55 +00:00
|
|
|
// Validations
|
2017-05-26 12:06:21 +00:00
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
Client.validatesUniquenessOf('name', {
|
|
|
|
message: 'El nombre debe ser único'
|
|
|
|
});
|
|
|
|
Client.validatesUniquenessOf('fi', {
|
|
|
|
message: 'El NIF/CIF debe ser único'
|
|
|
|
});
|
|
|
|
Client.validatesPresenceOf('socialName', {
|
|
|
|
message: 'Debe especificarse la razón social'
|
|
|
|
});
|
2017-06-01 12:39:22 +00:00
|
|
|
Client.validatesUniquenessOf('socialName', {
|
|
|
|
message: 'La razón social debe ser única'
|
|
|
|
});
|
2017-05-26 12:39:36 +00:00
|
|
|
Client.validatesFormatOf('postcode', {
|
|
|
|
message: 'El código postal solo debe contener números',
|
|
|
|
allowNull: true,
|
2017-07-03 07:42:51 +00:00
|
|
|
allowBlank: true,
|
2017-05-26 12:39:36 +00:00
|
|
|
with: /^\d+$/
|
|
|
|
});
|
|
|
|
Client.validatesFormatOf('email', {
|
|
|
|
message: 'Correo electrónico inválido',
|
|
|
|
allowNull: true,
|
2017-07-03 07:42:51 +00:00
|
|
|
allowBlank: true,
|
2017-06-01 12:05:23 +00:00
|
|
|
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w(,[\w|\.|\-]+@\w[\w|\.|\-]*\w)*$/
|
2017-05-26 12:39:36 +00:00
|
|
|
});
|
|
|
|
Client.validatesLengthOf('postcode', {
|
|
|
|
allowNull: true,
|
2017-07-03 07:42:51 +00:00
|
|
|
allowBlank: true,
|
2017-05-26 12:39:36 +00:00
|
|
|
min: 3, max: 10
|
|
|
|
});
|
|
|
|
Client.validatesLengthOf('iban', {
|
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
|
|
|
max: 23
|
|
|
|
});
|
|
|
|
Client.validate('payMethod', hasSalesMan, {
|
|
|
|
message: 'No se puede cambiar la forma de pago si no hay comercial asignado'
|
|
|
|
});
|
2017-01-16 08:57:06 +00:00
|
|
|
function hasSalesMan(err) {
|
2017-05-26 15:05:33 +00:00
|
|
|
if(this.payMethod && !this.salesPerson)
|
|
|
|
err();
|
|
|
|
}
|
2017-01-25 10:46:15 +00:00
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
Client.validateAsync('payMethodFk', hasIban, {
|
2017-05-26 12:44:56 +00:00
|
|
|
message: 'El método de pago seleccionado requiere que se especifique el IBAN'
|
2017-05-26 12:39:36 +00:00
|
|
|
});
|
|
|
|
function hasIban(err, done) {
|
2017-06-01 16:23:41 +00:00
|
|
|
models.PayMethod.findById(this.payMethodFk, (_, instance) => {
|
|
|
|
if (instance && instance.ibanRequired && !this.iban)
|
2017-05-26 12:39:36 +00:00
|
|
|
err();
|
|
|
|
done();
|
|
|
|
});
|
2017-05-26 15:05:33 +00:00
|
|
|
}
|
2017-06-03 11:01:47 +00:00
|
|
|
/*
|
2017-06-01 16:23:41 +00:00
|
|
|
Client.validateAsync('credit', validateCredit, {
|
|
|
|
message: 'No tienes privilegios para modificar el crédito'
|
|
|
|
});
|
|
|
|
function validateCredit(err, done) {
|
|
|
|
// FIXME: Id del usuario actual
|
|
|
|
let userId = 1;
|
|
|
|
|
|
|
|
// Comprueba si el rol del usuario puede asignar esa cantidad
|
|
|
|
|
|
|
|
let filter = {
|
|
|
|
fields: ['roleFk'],
|
|
|
|
where: {
|
|
|
|
maxAmount: {gt: this.credit}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
models.ClientCreditLimit.find(filter,
|
|
|
|
(_, res) => limitCb(_, res));
|
|
|
|
|
|
|
|
function limitCb(_, instances) {
|
|
|
|
let requiredRoles = [];
|
|
|
|
for (instance of instances)
|
|
|
|
requiredRoles.push (instance.roleFk);
|
|
|
|
|
|
|
|
let where = {
|
|
|
|
roleId: {inq: requiredRoles},
|
|
|
|
principalType: 'USER',
|
|
|
|
principalId: userId
|
|
|
|
};
|
|
|
|
models.RoleMapping.count(where,
|
|
|
|
(_, res) => roleCb(_, res));
|
|
|
|
}
|
|
|
|
function roleCb(_, count) {
|
|
|
|
if (!(count > 0)) {
|
|
|
|
err();
|
|
|
|
done();
|
|
|
|
} else
|
|
|
|
validate();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Si se puso a 0 por gerencia, solo gerencia puede aumentarlo
|
|
|
|
|
|
|
|
function validate() {
|
|
|
|
let query = 'SELECT MAX(created) created FROM ClientCredit WHERE clientFk = ?';
|
|
|
|
Client.dataSource.connector.execute (query, [this.id],
|
|
|
|
(_, res) => maxCb(_, res));
|
|
|
|
}
|
|
|
|
function maxCb(_, instances) {
|
|
|
|
if (instances.length !== 1) {
|
|
|
|
done();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let filter = {
|
|
|
|
fields: ['amount', 'employeeFk', 'employee'],
|
|
|
|
where: {
|
|
|
|
clientFk: this.id,
|
|
|
|
created: instances[0].created
|
|
|
|
},
|
|
|
|
include: {
|
|
|
|
relation: 'employee',
|
|
|
|
scope: {
|
|
|
|
fields: ['userFk']
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
models.ClientCredit.findOne(filter,
|
|
|
|
(_, res) => clientCreditCb(_, res));
|
|
|
|
}
|
|
|
|
function clientCreditCb(_, instance) {
|
|
|
|
if (instance.amount == 0 && instance.employee.userFk != userId)
|
|
|
|
err();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
}
|
2017-06-03 11:01:47 +00:00
|
|
|
*/
|
2017-01-12 10:02:22 +00:00
|
|
|
};
|