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;
|
2017-10-18 10:51:33 +00:00
|
|
|
|
2017-05-26 12:39:36 +00:00
|
|
|
// 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-11-16 11:35:07 +00:00
|
|
|
|
2017-02-08 11:45:55 +00:00
|
|
|
// Validations
|
2017-05-26 12:06:21 +00:00
|
|
|
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validatesUniquenessOf('fi', {
|
2017-05-26 12:39:36 +00:00
|
|
|
message: 'El NIF/CIF debe ser único'
|
|
|
|
});
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validatesUniquenessOf('socialName', {
|
2017-06-01 12:39:22 +00:00
|
|
|
message: 'La razón social debe ser única'
|
|
|
|
});
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validatesFormatOf('postcode', {
|
2017-05-26 12:39:36 +00:00
|
|
|
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+$/
|
|
|
|
});
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validatesFormatOf('email', {
|
2017-05-26 12:39:36 +00:00
|
|
|
message: 'Correo electrónico inválido',
|
|
|
|
allowNull: true,
|
2017-07-03 07:42:51 +00:00
|
|
|
allowBlank: true,
|
2017-11-16 11:35:07 +00:00
|
|
|
with: /^[\w|.|-]+@\w[\w|.|-]*\w(,[\w|.|-]+@\w[\w|.|-]*\w)*$/
|
2017-05-26 12:39:36 +00:00
|
|
|
});
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validatesLengthOf('postcode', {
|
2017-05-26 12:39:36 +00:00
|
|
|
allowNull: true,
|
2017-07-03 07:42:51 +00:00
|
|
|
allowBlank: true,
|
2017-05-26 12:39:36 +00:00
|
|
|
min: 3, max: 10
|
|
|
|
});
|
2017-09-25 15:40:02 +00:00
|
|
|
|
|
|
|
var validateIban = require('../validations/validateIban');
|
2017-11-16 11:35:07 +00:00
|
|
|
Self.validateBinded('iban', validateIban, {
|
|
|
|
message: 'El iban no tiene el formato correcto'
|
2017-05-26 12:39:36 +00:00
|
|
|
});
|
2017-09-25 15:40:02 +00:00
|
|
|
|
2017-11-16 11:02:45 +00:00
|
|
|
let validateDni = require('../validations/validateDni');
|
|
|
|
Self.validateBinded('fi', validateDni, {
|
2017-11-16 12:46:56 +00:00
|
|
|
message: 'DNI Incorrecto'
|
2017-11-16 11:02:45 +00:00
|
|
|
});
|
|
|
|
|
2017-10-11 13:36:47 +00:00
|
|
|
Self.validate('payMethod', hasSalesMan, {
|
2017-05-26 12:39:36 +00:00
|
|
|
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-11-16 11:35:07 +00:00
|
|
|
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'
|
2017-05-26 12:39:36 +00:00
|
|
|
});
|
2017-10-18 10:51:33 +00:00
|
|
|
|
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-11-16 11:35:07 +00:00
|
|
|
};
|