2017-01-12 10:02:22 +00:00
|
|
|
module.exports = function(Client) {
|
2017-01-16 08:57:06 +00:00
|
|
|
//validations
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.validatesUniquenessOf('name', {message: 'El nombre debe ser unico'});
|
|
|
|
Client.validatesUniquenessOf('fi', {message: 'El nif/cif debe ser unico'});
|
|
|
|
Client.validatesPresenceOf('socialName', {message: 'Debe especificarse la razón social'});
|
|
|
|
Client.validatesFormatOf('postcode', {with: /^\d+$/, message: 'El código postal solo debe contener números'});
|
|
|
|
Client.validatesLengthOf('postcode', {min: 3, max: 10});
|
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-01-16 08:57:06 +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
|
|
|
};
|
|
|
|
|
|
|
|
Client.remoteMethod('activate',
|
|
|
|
{
|
2017-01-26 14:00:13 +00:00
|
|
|
description: 'Activate or deactive client',
|
|
|
|
accepts: {arg: 'id', type: 'number', required: true, http: function(ctx) {
|
|
|
|
var id = ctx && ctx.req && ctx.req.params.id
|
|
|
|
return id;
|
|
|
|
}
|
|
|
|
},
|
2017-01-25 10:46:15 +00:00
|
|
|
returns: {
|
|
|
|
arg: 'active',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: '/:id/activate', verb: 'put'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-01-26 14:00:13 +00:00
|
|
|
Client.activate = function(id, cb){
|
2017-01-25 10:46:15 +00:00
|
|
|
Client.findById(id, function(err, client) {
|
2017-01-31 13:13:06 +00:00
|
|
|
if(!err) {
|
2017-01-25 10:46:15 +00:00
|
|
|
Client.update({id: client.id}, {active: !client.active});
|
|
|
|
cb(null, !client.active);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
};
|
2017-01-12 10:02:22 +00:00
|
|
|
|
2017-01-16 08:57:06 +00:00
|
|
|
// Hooks
|
2017-01-31 13:13:06 +00:00
|
|
|
Client.observe('before save', function(ctx, next) {
|
|
|
|
if(ctx.instance) {
|
2017-01-16 08:57:06 +00:00
|
|
|
if (!ctx.instance.dueDay){
|
|
|
|
ctx.instance.dueDay = 5;
|
|
|
|
}
|
|
|
|
next();
|
2017-01-31 13:13:06 +00:00
|
|
|
}
|
|
|
|
else {
|
2017-01-16 08:57:06 +00:00
|
|
|
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
|
|
|
};
|