Merge branch 'dev' of https://gitea.verdnatura.es/verdnatura/salix into 3472-supplier_summary

This commit is contained in:
Alex Moreno 2022-01-07 07:43:14 +01:00
commit 19a6b55a72
6 changed files with 70 additions and 22 deletions

View File

@ -102,7 +102,7 @@
"Weekday cannot be blank": "El día de la semana no puede quedar en blanco", "Weekday cannot be blank": "El día de la semana no puede quedar en blanco",
"You can't delete a confirmed order": "No puedes borrar un pedido confirmado", "You can't delete a confirmed order": "No puedes borrar un pedido confirmado",
"Can't create stowaway for this ticket": "No se puede crear un polizon para este ticket", "Can't create stowaway for this ticket": "No se puede crear un polizon para este ticket",
"The socialName has an invalid format": "El nombre fiscal tiene un formato incorrecto", "The social name has an invalid format": "El nombre fiscal tiene un formato incorrecto",
"Invalid quantity": "Cantidad invalida", "Invalid quantity": "Cantidad invalida",
"This postal code is not valid": "This postal code is not valid", "This postal code is not valid": "This postal code is not valid",
"is invalid": "is invalid", "is invalid": "is invalid",

View File

@ -59,11 +59,12 @@ module.exports = Self => {
const landedPlusWeek = new Date(ticket.landed); const landedPlusWeek = new Date(ticket.landed);
landedPlusWeek.setDate(landedPlusWeek.getDate() + 7); landedPlusWeek.setDate(landedPlusWeek.getDate() + 7);
const hasClaimManagerRole = await models.Account.hasRole(userId, 'claimManager', myOptions);
const isClaimable = landedPlusWeek >= new Date(); const isClaimable = landedPlusWeek >= new Date();
if (ticket.isDeleted) if (ticket.isDeleted)
throw new UserError(`You can't create a claim for a removed ticket`); throw new UserError(`You can't create a claim for a removed ticket`);
if (!isClaimable) if (!isClaimable && !hasClaimManagerRole)
throw new UserError(`You can't create a claim from a ticket delivered more than seven days ago`); throw new UserError(`You can't create a claim from a ticket delivered more than seven days ago`);
const newClaim = await Self.create({ const newClaim = await Self.create({

View File

@ -46,9 +46,40 @@ describe('Claim createFromSales()', () => {
} }
}); });
it('should be able to create a claim for a ticket delivered more than seven days ago as claimManager', async() => {
const tx = await models.Claim.beginTransaction({});
const claimManagerId = 72;
activeCtx.accessToken.userId = claimManagerId;
try {
const options = {transaction: tx};
const todayMinusEightDays = new Date();
todayMinusEightDays.setDate(todayMinusEightDays.getDate() - 8);
const ticket = await models.Ticket.findById(ticketId, options);
await ticket.updateAttribute('landed', todayMinusEightDays, options);
const claim = await models.Claim.createFromSales(ctx, ticketId, newSale, options);
expect(claim.ticketFk).toEqual(ticketId);
const claimBeginning = await models.ClaimBeginning.findOne({where: {claimFk: claim.id}}, options);
expect(claimBeginning.saleFk).toEqual(newSale[0].id);
expect(claimBeginning.quantity).toEqual(newSale[0].quantity);
await tx.rollback();
} catch (e) {
await tx.rollback();
throw e;
}
});
it('should not be able to create a claim for a ticket delivered more than seven days ago', async() => { it('should not be able to create a claim for a ticket delivered more than seven days ago', async() => {
const tx = await models.Claim.beginTransaction({}); const tx = await models.Claim.beginTransaction({});
activeCtx.accessToken.userId = 1;
let error; let error;
try { try {

View File

@ -55,15 +55,6 @@ module.exports = Self => {
with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/ with: /^[\w|.|-]+@[\w|-]+(\.[\w|-]+)*(,[\w|.|-]+@[\w|-]+(\.[\w|-]+)*)*$/
}); });
Self.validate('businessTypeFk', hasBusinessType, {
message: `The type of business must be filled in basic data`
});
function hasBusinessType(err) {
if (!this.businessTypeFk)
err();
}
Self.validatesLengthOf('postcode', { Self.validatesLengthOf('postcode', {
allowNull: true, allowNull: true,
allowBlank: true, allowBlank: true,
@ -189,6 +180,32 @@ module.exports = Self => {
return regexp.test(value); return regexp.test(value);
} }
Self.observe('before save', async ctx => {
const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance;
const businessTypeFk = changes && changes.businessTypeFk || orgData && orgData.businessTypeFk;
const isTaxDataChecked = changes && changes.isTaxDataChecked || orgData && orgData.isTaxDataChecked;
let invalidBusinessType = false;
if (!ctx.isNewInstance) {
const isWorker = await Self.app.models.UserAccount.findById(orgData.id);
const changedFields = Object.keys(changes);
const hasChangedOtherFields = changedFields.some(key => key !== 'businessTypeFk');
if (!businessTypeFk && !isTaxDataChecked && !isWorker && !hasChangedOtherFields)
invalidBusinessType = true;
}
if (ctx.isNewInstance) {
if (!businessTypeFk && !isTaxDataChecked)
invalidBusinessType = true;
}
if (invalidBusinessType)
throw new UserError(`The type of business must be filled in basic data`);
});
Self.observe('before save', async function(ctx) { Self.observe('before save', async function(ctx) {
const changes = ctx.data || ctx.instance; const changes = ctx.data || ctx.instance;
const orgData = ctx.currentInstance; const orgData = ctx.currentInstance;
@ -206,7 +223,7 @@ module.exports = Self => {
&& orgData.isTaxDataChecked != isTaxDataChecked; && orgData.isTaxDataChecked != isTaxDataChecked;
if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName)) if ((socialNameChanged || dataCheckedChanged) && !isAlpha(socialName))
throw new UserError('The socialName has an invalid format'); throw new UserError(`The social name has an invalid format`);
if (changes.salesPerson === null) { if (changes.salesPerson === null) {
changes.credit = 0; changes.credit = 0;
@ -374,14 +391,11 @@ module.exports = Self => {
throw new UserError(`You don't have enough privileges to set this credit amount`); throw new UserError(`You don't have enough privileges to set this credit amount`);
} }
const client = await models.Client.findById(finalState.id, null, ctx.options); await models.ClientCredit.create({
if (client.businessTypeFk) { amount: changes.credit,
await models.ClientCredit.create({ clientFk: finalState.id,
amount: changes.credit, workerFk: userId
clientFk: finalState.id, }, ctx.options);
workerFk: userId
}, ctx.options);
}
}; };
const app = require('vn-loopback/server/server'); const app = require('vn-loopback/server/server');

View File

@ -114,6 +114,6 @@ module.exports = Self => {
&& orgData.socialName != socialName; && orgData.socialName != socialName;
if ((socialNameChanged) && !isAlpha(socialName)) if ((socialNameChanged) && !isAlpha(socialName))
throw new UserError('The socialName has an invalid format'); throw new UserError('The social name has an invalid format');
}); });
}; };

View File

@ -40,7 +40,9 @@ class Controller extends Section {
const landedPlusWeek = new Date(this.ticket.landed); const landedPlusWeek = new Date(this.ticket.landed);
landedPlusWeek.setDate(landedPlusWeek.getDate() + 7); landedPlusWeek.setDate(landedPlusWeek.getDate() + 7);
return landedPlusWeek >= new Date(); const hasClaimManagerRole = this.aclService.hasAny(['claimManager']);
return landedPlusWeek >= new Date() || hasClaimManagerRole;
} }
return false; return false;
} }