2017-01-12 10:02:22 +00:00
|
|
|
module.exports = function(Client) {
|
2017-05-17 06:19:03 +00:00
|
|
|
|
2017-05-17 07:45:34 +00:00
|
|
|
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-04-28 13:04:29 +00:00
|
|
|
Client.validatesUniquenessOf('name', {message: 'El nombre debe ser único'});
|
|
|
|
Client.validatesUniquenessOf('fi', {message: 'El NIF/CIF debe ser único'});
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validatesPresenceOf('socialName', {message: 'Debe especificarse la razón social'});
|
2017-02-07 11:58:25 +00:00
|
|
|
Client.validatesFormatOf('postcode', {allowNull: true, with: /^\d+$/, message: 'El código postal solo debe contener números'});
|
|
|
|
Client.validatesLengthOf('postcode', {allowNull: true, min: 3, max: 10});
|
|
|
|
Client.validatesFormatOf('email', {allowNull: true, with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w$/, message: 'Correo electrónico inválido'});
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validate('payMethod', hasCC, {message: 'Introduzca el 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-01-31 13:13:06 +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-17 12:04:02 +00:00
|
|
|
Client.validatesLengthOf('iban', {allowNull: true, min: 23, max: 23});
|
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-01-12 10:02:22 +00:00
|
|
|
|
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
|
|
|
};
|