2020-11-02 13:30:33 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
|
|
|
const validateTin = require('vn-loopback/util/validateTin');
|
2021-12-14 07:23:11 +00:00
|
|
|
const LoopBackContext = require('loopback-context');
|
2020-11-02 13:30:33 +00:00
|
|
|
|
2020-10-15 06:15:04 +00:00
|
|
|
module.exports = Self => {
|
|
|
|
require('../methods/supplier/filter')(Self);
|
2020-10-20 07:44:44 +00:00
|
|
|
require('../methods/supplier/getSummary')(Self);
|
2020-10-30 14:11:45 +00:00
|
|
|
require('../methods/supplier/updateFiscalData')(Self);
|
2020-12-16 07:05:41 +00:00
|
|
|
require('../methods/supplier/consumption')(Self);
|
2022-03-17 13:27:55 +00:00
|
|
|
require('../methods/supplier/freeAgencies')(Self);
|
2022-09-26 11:33:27 +00:00
|
|
|
require('../methods/supplier/campaignMetricsPdf')(Self);
|
|
|
|
require('../methods/supplier/campaignMetricsEmail')(Self);
|
2022-10-17 05:25:25 +00:00
|
|
|
require('../methods/supplier/newSupplier')(Self);
|
2023-05-04 10:03:47 +00:00
|
|
|
require('../methods/supplier/getItemsPackaging')(Self);
|
2020-11-02 13:30:33 +00:00
|
|
|
|
|
|
|
Self.validatesPresenceOf('name', {
|
|
|
|
message: 'The social name cannot be empty'
|
|
|
|
});
|
|
|
|
|
2022-10-17 05:35:02 +00:00
|
|
|
if (this.city) {
|
|
|
|
Self.validatesPresenceOf('city', {
|
|
|
|
message: 'City cannot be empty'
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (this.nif) {
|
|
|
|
Self.validatesPresenceOf('nif', {
|
|
|
|
message: 'The nif cannot be empty'
|
|
|
|
});
|
|
|
|
}
|
2020-11-02 13:30:33 +00:00
|
|
|
|
|
|
|
Self.validatesUniquenessOf('nif', {
|
|
|
|
message: 'TIN must be unique'
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.validateAsync('nif', tinIsValid, {
|
|
|
|
message: 'Invalid TIN'
|
|
|
|
});
|
|
|
|
|
2020-11-04 10:37:54 +00:00
|
|
|
Self.validatesLengthOf('postCode', {
|
|
|
|
allowNull: true,
|
|
|
|
allowBlank: true,
|
|
|
|
min: 3, max: 10
|
|
|
|
});
|
|
|
|
|
|
|
|
Self.validateAsync('postCode', hasValidPostcode, {
|
|
|
|
message: `The postcode doesn't exist. Please enter a correct one`
|
|
|
|
});
|
|
|
|
|
|
|
|
async function hasValidPostcode(err, done) {
|
|
|
|
if (!this.postcode)
|
|
|
|
return done();
|
|
|
|
|
|
|
|
const models = Self.app.models;
|
|
|
|
const postcode = await models.Postcode.findById(this.postcode);
|
|
|
|
|
|
|
|
if (!postcode) err();
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2023-04-27 09:31:26 +00:00
|
|
|
async function tinIsValid(err, done) {
|
|
|
|
if (!this.countryFk)
|
|
|
|
return done();
|
|
|
|
|
|
|
|
const filter = {
|
|
|
|
fields: ['code'],
|
|
|
|
where: {id: this.countryFk}
|
|
|
|
};
|
|
|
|
const country = await Self.app.models.Country.findOne(filter);
|
|
|
|
const code = country ? country.code.toLowerCase() : null;
|
2023-07-13 11:45:07 +00:00
|
|
|
const countryCode = this.nif?.toLowerCase().substring(0, 2);
|
2023-04-27 09:31:26 +00:00
|
|
|
|
2023-07-13 11:45:07 +00:00
|
|
|
if (!validateTin(this.nif, code) || (this.isVies && countryCode == code))
|
2023-04-12 10:03:55 +00:00
|
|
|
err();
|
2023-04-12 12:39:40 +00:00
|
|
|
done();
|
2023-04-12 10:03:55 +00:00
|
|
|
}
|
|
|
|
|
2020-11-02 13:30:33 +00:00
|
|
|
function isAlpha(value) {
|
|
|
|
const regexp = new RegExp(/^[ñça-zA-Z0-9\s]*$/i);
|
|
|
|
|
|
|
|
return regexp.test(value);
|
|
|
|
}
|
|
|
|
|
2020-12-14 15:09:17 +00:00
|
|
|
Self.validateAsync('payMethodFk', hasSupplierAccount, {
|
|
|
|
message: 'You can not select this payment method without a registered bankery account'
|
|
|
|
});
|
|
|
|
|
|
|
|
async function hasSupplierAccount(err, done) {
|
2022-10-17 05:25:25 +00:00
|
|
|
if (!this.payMethodFk) return done();
|
2020-12-14 15:09:17 +00:00
|
|
|
const payMethod = await Self.app.models.PayMethod.findById(this.payMethodFk);
|
|
|
|
const supplierAccount = await Self.app.models.SupplierAccount.findOne({where: {supplierFk: this.id}});
|
|
|
|
const hasIban = supplierAccount && supplierAccount.iban;
|
2021-11-22 12:44:22 +00:00
|
|
|
const isMissingIban = payMethod && payMethod.isIbanRequiredForSuppliers && !hasIban;
|
2020-12-14 15:09:17 +00:00
|
|
|
|
2021-11-22 12:44:22 +00:00
|
|
|
if (isMissingIban)
|
2020-12-14 15:09:17 +00:00
|
|
|
err();
|
2020-12-15 08:59:53 +00:00
|
|
|
|
2020-12-14 15:09:17 +00:00
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2020-11-02 13:30:33 +00:00
|
|
|
Self.observe('before save', async function(ctx) {
|
2022-10-17 05:25:25 +00:00
|
|
|
if (ctx.isNewInstance) return;
|
2021-12-14 07:23:11 +00:00
|
|
|
const changes = ctx.data || ctx.instance;
|
|
|
|
const orgData = ctx.currentInstance;
|
2023-04-25 07:15:05 +00:00
|
|
|
const loopBackContext = LoopBackContext.getCurrentContext();
|
2023-07-12 11:02:27 +00:00
|
|
|
const accessToken = {req: loopBackContext.active};
|
2023-04-25 07:15:05 +00:00
|
|
|
|
|
|
|
const editPayMethodCheck =
|
|
|
|
await Self.app.models.ACL.checkAccessAcl(accessToken, 'Supplier', 'editPayMethodCheck', 'WRITE');
|
2021-12-14 07:23:11 +00:00
|
|
|
|
|
|
|
const isPayMethodChecked = changes.isPayMethodChecked || orgData.isPayMethodChecked;
|
|
|
|
const hasChanges = orgData && changes;
|
|
|
|
const isPayMethodCheckedChanged = hasChanges
|
2022-10-17 05:25:25 +00:00
|
|
|
&& orgData.isPayMethodChecked != isPayMethodChecked;
|
2021-12-14 07:23:11 +00:00
|
|
|
|
2023-04-25 07:15:05 +00:00
|
|
|
if (!editPayMethodCheck && isPayMethodCheckedChanged)
|
2021-12-14 07:23:11 +00:00
|
|
|
throw new UserError('You can not modify is pay method checked');
|
|
|
|
});
|
|
|
|
|
2023-01-20 09:03:00 +00:00
|
|
|
Self.validateAsync('name', 'countryFk', hasSupplierSameName, {
|
|
|
|
message: 'A supplier with the same name already exists. Change the country.'
|
|
|
|
});
|
|
|
|
|
|
|
|
async function hasSupplierSameName(err, done) {
|
2023-07-13 11:45:07 +00:00
|
|
|
if (!this.name || !this.countryFk) return done();
|
2023-01-20 09:03:00 +00:00
|
|
|
const supplier = await Self.app.models.Supplier.findOne(
|
|
|
|
{
|
|
|
|
where: {
|
|
|
|
name: this.name,
|
|
|
|
countryFk: this.countryFk
|
|
|
|
},
|
|
|
|
fields: ['id']
|
|
|
|
});
|
|
|
|
|
|
|
|
if (supplier && supplier.id != this.id)
|
|
|
|
err();
|
|
|
|
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
|
2021-12-14 07:23:11 +00:00
|
|
|
Self.observe('before save', async function(ctx) {
|
|
|
|
const changes = ctx.data || ctx.instance;
|
|
|
|
const orgData = ctx.currentInstance;
|
2020-11-02 13:30:33 +00:00
|
|
|
|
2023-01-20 09:03:00 +00:00
|
|
|
const socialName = changes.name || orgData.name;
|
|
|
|
const hasChanges = orgData && changes;
|
|
|
|
const socialNameChanged = hasChanges
|
|
|
|
&& orgData.socialName != socialName;
|
2023-01-13 07:38:29 +00:00
|
|
|
|
2023-01-20 09:03:00 +00:00
|
|
|
if ((socialNameChanged) && !isAlpha(socialName))
|
|
|
|
throw new UserError('The social name has an invalid format');
|
2020-11-02 13:30:33 +00:00
|
|
|
});
|
2020-10-15 06:15:04 +00:00
|
|
|
};
|