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

38 lines
1.3 KiB
JavaScript
Raw Normal View History

2017-01-12 10:02:22 +00:00
module.exports = function(Client) {
2017-01-16 08:57:06 +00:00
//validations
Client.validatesUniquenessOf('name', {message: 'el nombre debe ser unico'});
Client.validatesUniquenessOf('fi', {message: 'el nif/cif debe ser unico'});
2017-01-12 10:02:22 +00:00
2017-01-16 08:57: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-12 10:02:22 +00:00
2017-01-16 08:57:06 +00:00
Client.validate('payMethod',hasSalesMan,{message: 'No se puede cambiar la forma de pago si no hay comercial asignado'});
function hasSalesMan(err) {
if (this.payMethod && !this.salesPerson) err();
2017-01-12 10:02:22 +00:00
}
2017-01-16 08:57:06 +00:00
// Hooks
Client.observe('before save', function (ctx, next) {
2017-01-16 08:57:06 +00:00
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;
}
if(!ctx.data.name)
ctx.data.name = item.name;
2017-01-16 08:57:06 +00:00
}
next();
}
);
}
});
2017-01-12 10:02:22 +00:00
};