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); }, 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({ const newSale = await models.Sale.create({
ticketFk: id, ticketFk: id,
itemFk: item.id, itemFk: item.id,

View File

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