145 lines
4.4 KiB
JavaScript
145 lines
4.4 KiB
JavaScript
var app = require('../../server/server');
|
|
|
|
module.exports = function(Client) {
|
|
var models = app.models;
|
|
|
|
// Methods
|
|
|
|
require('../methods/client/activate.js')(Client);
|
|
require('../methods/client/addresses.js')(Client);
|
|
require('../methods/client/before-save.js')(Client);
|
|
require('../methods/client/card.js')(Client);
|
|
require('../methods/client/create.js')(Client);
|
|
require('../methods/client/employee.js')(Client);
|
|
require('../methods/client/filter.js')(Client);
|
|
|
|
// Validations
|
|
|
|
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.validatesUniquenessOf('socialName', {
|
|
message: 'La razón social debe ser única'
|
|
});
|
|
Client.validatesFormatOf('postcode', {
|
|
message: 'El código postal solo debe contener números',
|
|
allowNull: true,
|
|
allowBlank: true,
|
|
with: /^\d+$/
|
|
});
|
|
Client.validatesFormatOf('email', {
|
|
message: 'Correo electrónico inválido',
|
|
allowNull: true,
|
|
allowBlank: true,
|
|
with: /^[\w|\.|\-]+@\w[\w|\.|\-]*\w(,[\w|\.|\-]+@\w[\w|\.|\-]*\w)*$/
|
|
});
|
|
Client.validatesLengthOf('postcode', {
|
|
allowNull: true,
|
|
allowBlank: true,
|
|
min: 3, max: 10
|
|
});
|
|
Client.validatesLengthOf('iban', {
|
|
allowNull: true,
|
|
allowBlank: true,
|
|
max: 23
|
|
});
|
|
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();
|
|
}
|
|
Client.validateAsync('payMethodFk', hasIban, {
|
|
message: 'El método de pago seleccionado requiere que se especifique el IBAN'
|
|
});
|
|
function hasIban(err, done) {
|
|
models.PayMethod.findById(this.payMethodFk, (_, instance) => {
|
|
if (instance && instance.ibanRequired && !this.iban)
|
|
err();
|
|
done();
|
|
});
|
|
}
|
|
/*
|
|
Client.validateAsync('credit', validateCredit, {
|
|
message: 'No tienes privilegios para modificar el crédito'
|
|
});
|
|
function validateCredit(err, done) {
|
|
// FIXME: Id del usuario actual
|
|
let userId = 1;
|
|
|
|
// Comprueba si el rol del usuario puede asignar esa cantidad
|
|
|
|
let filter = {
|
|
fields: ['roleFk'],
|
|
where: {
|
|
maxAmount: {gt: this.credit}
|
|
}
|
|
};
|
|
models.ClientCreditLimit.find(filter,
|
|
(_, res) => limitCb(_, res));
|
|
|
|
function limitCb(_, instances) {
|
|
let requiredRoles = [];
|
|
for (instance of instances)
|
|
requiredRoles.push (instance.roleFk);
|
|
|
|
let where = {
|
|
roleId: {inq: requiredRoles},
|
|
principalType: 'USER',
|
|
principalId: userId
|
|
};
|
|
models.RoleMapping.count(where,
|
|
(_, res) => roleCb(_, res));
|
|
}
|
|
function roleCb(_, count) {
|
|
if (!(count > 0)) {
|
|
err();
|
|
done();
|
|
} else
|
|
validate();
|
|
}
|
|
|
|
// Si se puso a 0 por gerencia, solo gerencia puede aumentarlo
|
|
|
|
function validate() {
|
|
let query = 'SELECT MAX(created) created FROM ClientCredit WHERE clientFk = ?';
|
|
Client.dataSource.connector.execute (query, [this.id],
|
|
(_, res) => maxCb(_, res));
|
|
}
|
|
function maxCb(_, instances) {
|
|
if (instances.length !== 1) {
|
|
done();
|
|
return;
|
|
}
|
|
|
|
let filter = {
|
|
fields: ['amount', 'employeeFk', 'employee'],
|
|
where: {
|
|
clientFk: this.id,
|
|
created: instances[0].created
|
|
},
|
|
include: {
|
|
relation: 'employee',
|
|
scope: {
|
|
fields: ['userFk']
|
|
}
|
|
}
|
|
};
|
|
models.ClientCredit.findOne(filter,
|
|
(_, res) => clientCreditCb(_, res));
|
|
}
|
|
function clientCreditCb(_, instance) {
|
|
if (instance.amount == 0 && instance.employee.userFk != userId)
|
|
err();
|
|
done();
|
|
}
|
|
}
|
|
*/
|
|
}; |