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

75 lines
2.4 KiB
JavaScript
Raw Normal View History

2017-06-01 16:23:41 +00:00
var app = require('../../server/server');
2017-10-11 13:36:47 +00:00
module.exports = function(Self) {
2017-06-01 16:23:41 +00:00
var models = app.models;
// Methods
2017-10-11 13:36:47 +00:00
require('../methods/client/activate.js')(Self);
require('../methods/client/addresses.js')(Self);
require('../methods/client/before-save.js')(Self);
require('../methods/client/card.js')(Self);
require('../methods/client/create.js')(Self);
require('../methods/client/employee.js')(Self);
require('../methods/client/filter.js')(Self);
require('../methods/client/roles.js')(Self);
require('../methods/client/salesperson.js')(Self);
require('../methods/client/addressesPropagateRe.js')(Self);
2017-02-08 11:45:55 +00:00
// Validations
2017-10-11 13:36:47 +00:00
Self.validatesUniquenessOf('name', {
message: 'El nombre debe ser único'
});
2017-10-11 13:36:47 +00:00
Self.validatesUniquenessOf('fi', {
message: 'El NIF/CIF debe ser único'
});
2017-10-11 13:36:47 +00:00
Self.validatesPresenceOf('socialName', {
message: 'Debe especificarse la razón social'
});
2017-10-11 13:36:47 +00:00
Self.validatesUniquenessOf('socialName', {
message: 'La razón social debe ser única'
});
2017-10-11 13:36:47 +00:00
Self.validatesFormatOf('postcode', {
message: 'El código postal solo debe contener números',
allowNull: true,
2017-07-03 07:42:51 +00:00
allowBlank: true,
with: /^\d+$/
});
2017-10-11 13:36:47 +00:00
Self.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-10-11 13:36:47 +00:00
Self.validatesLengthOf('postcode', {
allowNull: true,
2017-07-03 07:42:51 +00:00
allowBlank: true,
min: 3, max: 10
});
2017-09-25 15:40:02 +00:00
var validateIban = require('../validations/validateIban');
Self.validateBinded('iban', validateIban, {
message: 'El iban no tiene el formato correcto'
});
2017-09-25 15:40:02 +00:00
2017-10-11 13:36:47 +00:00
Self.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) {
if (this.payMethod && !this.salesPerson)
2017-05-26 15:05:33 +00:00
err();
}
2017-10-11 13:36:47 +00:00
Self.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) {
2017-06-01 16:23:41 +00:00
models.PayMethod.findById(this.payMethodFk, (_, instance) => {
if (instance && instance.ibanRequired && !this.iban)
err();
done();
});
2017-05-26 15:05:33 +00:00
}
};