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-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', hasCC, {
|
|
|
|
message: 'El método de pago seleccionado requiere que se especifique IBAN del cliente'
|
|
|
|
});
|
2017-01-12 10:02:22 +00:00
|
|
|
function hasCC(err) {
|
2017-02-08 11:45:55 +00:00
|
|
|
if (this.payMethod == 2 && !this.iban) err();
|
2017-01-25 10:46:15 +00:00
|
|
|
};
|
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-01-31 13:13:06 +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, {
|
|
|
|
message: 'Tiene que rellenar el IBAN'
|
|
|
|
});
|
|
|
|
function hasIban(err, done) {
|
|
|
|
let iban = this.iban;
|
|
|
|
let PayMethod = Client.app.models.PayMethod;
|
|
|
|
PayMethod.findById(this.payMethodFk, function (_, instance){
|
|
|
|
if (instance.ibanRequired && !iban)
|
|
|
|
err();
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-02-08 11:45:55 +00:00
|
|
|
// Hooks
|
|
|
|
|
|
|
|
Client.observe('before save', function(ctx, next) {
|
|
|
|
if(ctx.instance) {
|
|
|
|
if (!ctx.instance.dueDay){
|
|
|
|
ctx.instance.dueDay = 5;
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Client.findById(ctx.where.id,
|
|
|
|
function(err, item) {
|
|
|
|
if (!err) {
|
|
|
|
if (item.payMethod != ctx.data.payMethod && item.dueDay == ctx.data.dueDay) {
|
|
|
|
ctx.data.dueDay = 5;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
next();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-02-21 10:36:43 +00:00
|
|
|
// Basic filter
|
2017-02-08 11:45:55 +00:00
|
|
|
|
2017-02-28 17:10:11 +00:00
|
|
|
Client.installMethod('filter', filterClients);
|
2017-02-28 10:46:18 +00:00
|
|
|
function filterClients(p) {
|
2017-02-21 10:36:43 +00:00
|
|
|
return {
|
|
|
|
where: {
|
|
|
|
id: p.id,
|
2017-05-05 10:54:47 +00:00
|
|
|
name: {regexp: p.name},
|
2017-02-21 10:36:43 +00:00
|
|
|
cif: p.cif,
|
2017-05-05 10:54:47 +00:00
|
|
|
socialName: {regexp: p.socialName},
|
|
|
|
city: {regexp: p.city},
|
2017-02-21 10:36:43 +00:00
|
|
|
postcode: p.postcode,
|
2017-05-05 10:54:47 +00:00
|
|
|
email: {regexp: p.email},
|
2017-02-21 10:36:43 +00:00
|
|
|
phone: p.phone
|
|
|
|
},
|
|
|
|
skip: (p.page - 1) * p.size,
|
|
|
|
limit: p.size
|
|
|
|
};
|
2017-02-08 11:45:55 +00:00
|
|
|
}
|
2017-01-12 10:02:22 +00:00
|
|
|
};
|