2017-01-12 10:02:22 +00:00
|
|
|
module.exports = function(Client) {
|
2017-05-17 06:19:03 +00:00
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
// Methods
|
|
|
|
|
|
|
|
require('../scopes/client/card.js')(Client);
|
|
|
|
require('../scopes/client/activate.js')(Client);
|
|
|
|
require('../scopes/client/addresses.js')(Client);
|
2017-05-31 08:58:48 +00:00
|
|
|
require('../scopes/client/filter.js')(Client);
|
|
|
|
require('../scopes/client/before-save.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'
|
|
|
|
});
|
|
|
|
Client.validatesFormatOf('postcode', {
|
|
|
|
message: 'El código postal solo debe contener números',
|
|
|
|
allowNull: true,
|
|
|
|
with: /^\d+$/
|
|
|
|
});
|
|
|
|
Client.validatesFormatOf('email', {
|
|
|
|
message: 'Correo electrónico inválido',
|
|
|
|
allowNull: true,
|
|
|
|
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w$/
|
|
|
|
});
|
|
|
|
Client.validatesLengthOf('postcode', {
|
|
|
|
allowNull: true,
|
|
|
|
min: 3, max: 10
|
|
|
|
});
|
|
|
|
Client.validatesLengthOf('iban', {
|
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
|
|
|
max: 23
|
|
|
|
});
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
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) {
|
|
|
|
let iban = this.iban;
|
|
|
|
let PayMethod = Client.app.models.PayMethod;
|
2017-05-26 15:05:33 +00:00
|
|
|
PayMethod.findById(this.payMethodFk, function (_, instance) {
|
|
|
|
if (instance && instance.ibanRequired && !iban)
|
2017-05-26 12:39:36 +00:00
|
|
|
err();
|
|
|
|
done();
|
|
|
|
});
|
2017-05-26 15:05:33 +00:00
|
|
|
}
|
2017-05-26 12:39:36 +00:00
|
|
|
|
2017-01-12 10:02:22 +00:00
|
|
|
};
|