I have a shopping cart inventory table (product sale).
With each sale I must decrease the amount of product in my stock. In this case I use the inventory table update

N to N

How to do this double loop?

The ideal scenario?

For each product in the cart, you check if stock is enough.
If not, ask the user to decrease the quantity. Change nothing in DB.

If all products are ok:
While you loop cart products, you add to order product+quantity and reduce from stock product+quantity.
One loop, two updates.

At the end of the process, commit so it is saved simultaneously. If anything fails, rollback. Maybe someone bought simultaneously the last item.

I check this quantity before placing it in the cart. Write the desired item in the Cart table without lowering the stock. When the user clicks the "Checkout" button, I loop the cart table and decrease the stock. However, the way I did it, it only decreases the first item in the cart.

The completed sale will be recorded in a new table.

And the updated inventory in the inventory table.

It would be a loop to read the cart and at the same time a loop to search the stock for the product id to check it out.

Sorry but I didn't understand your solution well.
Please send a screen print;



Hi José,

Please show us what you did exactly, as your code seems to have a bug. Can you share the Module?

Unfortunately I can't because it is an application for a public entity.

I can send you the screens.

Well, please do so :)

follows the file with maximum explanation.

Thank you in advance.

This is not the oml. We need more information to understand.

You said "this assign does everything" but don't show what it does. We can't guess. Or maybe...

My guess is that the Assign can't do it all. It is not pretty,  or easy to do,or easy to maintain. Use a Query to search for the cart item in stock and update quantity.

Do it in debug to confirm that the id is right. The way you wrote I fear you may be mixing Cart.Id and Cart.Productid.

Hi José,

Like Nuno, I assume there's something going wrong with the assign and/or the query. Without a Module to look at, it's difficult to trace where the error is. I would advise you to debug it, and step by step look whether the expected values are filled etc. It must be a logic error somewhere.

When you say a double loop you mean a nested loop, one inside other?

If so it is not the best approach because the cyclomatic complexity would increase and it impairs performance.

I think the best approach would be check if the required SKU is available before and then decrement the available quantity.