refs #6119 fix(sale_hook): fix quantity case
gitea/salix/pipeline/head There was a failure building this commit Details

This commit is contained in:
Alex Moreno 2023-10-16 15:27:10 +02:00
parent a6437f8024
commit 093431ecdf
2 changed files with 28 additions and 27 deletions

View File

@ -63,17 +63,6 @@ module.exports = Self => {
}
}, myOptions);
const itemInfo = await models.Item.getVisibleAvailable(
itemId,
ticket.warehouseFk,
ticket.shipped,
myOptions
);
const isPackaging = item.family == 'EMB';
if (!isPackaging && itemInfo.available < quantity)
throw new UserError(`This item is not available`);
const newSale = await models.Sale.create({
ticketFk: id,
itemFk: item.id,

View File

@ -27,8 +27,7 @@ module.exports = Self => {
const loopBackContext = LoopBackContext.getCurrentContext();
ctx.req = loopBackContext.active;
const canEditQuantity = await models.ACL.checkAccessAcl(ctx, 'Sale', 'canForceQuantity', 'WRITE');
if (canEditQuantity) return;
if (await models.ACL.checkAccessAcl(ctx, 'Sale', 'canForceQuantity', 'WRITE')) return;
const ticketId = changes?.ticketFk || instance?.ticketFk;
const itemId = changes?.itemFk || instance?.itemFk;
@ -36,7 +35,7 @@ module.exports = Self => {
const ticket = await models.Ticket.findById(
ticketId,
{
fields: ['id', 'clientFk'],
fields: ['id', 'clientFk', 'warehouseFk', 'shipped'],
include: {
relation: 'client',
scope: {
@ -51,29 +50,42 @@ module.exports = Self => {
}
},
ctx.options);
if (ticket.client().type().code === 'loses') return;
if (ticket?.client()?.type()?.code === 'loses') return;
const ticketRefund = await models.TicketRefund.findOne({
where: {refundTicketFk: ticketId},
fields: ['id']}
, ctx.options);
const isRefund = await models.TicketRefund.findOne({
fields: ['id'],
where: {refundTicketFk: ticketId}
}, ctx.options);
if (isRefund) return;
if (newQuantity < 0 && !ticketRefund)
if (newQuantity < 0)
throw new UserError('You can only add negative amounts in refund tickets');
const item = await models.Item.findOne({
fields: ['family', 'minQuantity'],
where: {id: itemId},
fields: ['minQuantity']}
, ctx.options);
}, ctx.options);
if (newQuantity < item.minQuantity && !ticketRefund)
throw new UserError('The amount cannot be less than the minimum');
if (item.family == 'EMB') return;
const itemInfo = await models.Item.getVisibleAvailable(
itemId,
ticket.warehouseFk,
ticket.shipped,
ctx.options
);
const oldQuantity = instance?.quantity;
if (oldQuantity === null) return;
const quantityAdded = newQuantity - oldQuantity;
if (itemInfo.available < quantityAdded)
throw new UserError(`This item is not available`);
const isRoleAdvanced = await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*');
if (newQuantity > oldQuantity && !isRoleAdvanced)
if (await models.ACL.checkAccessAcl(ctx, 'Ticket', 'isRoleAdvanced', '*')) return;
if (newQuantity < item.minQuantity && itemInfo.available != newQuantity)
throw new UserError('The amount cannot be less than the minimum');
if (!ctx.isNewInstance && newQuantity > oldQuantity)
throw new UserError('The new quantity should be smaller than the old one');
});
};