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

63 lines
1.9 KiB
JavaScript
Raw Normal View History

2017-01-12 10:02:22 +00:00
module.exports = function(Client) {
// Methods
require('../scopes/client/card.js')(Client);
require('../scopes/client/activate.js')(Client);
require('../scopes/client/addresses.js')(Client);
require('../scopes/client/filter.js')(Client);
require('../scopes/client/before-save.js')(Client);
2017-02-08 11:45:55 +00:00
// Validations
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
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
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'
});
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)
err();
done();
});
2017-05-26 15:05:33 +00:00
}
2017-01-12 10:02:22 +00:00
};