Add a field in portal table (not need to be in portal)
4. stockCount (sorry I don't know best English word for this. Choose any of you want.)
Case ( Sale Or Purchase = "Sale" ; - Quantity ; Quantity )
then you can get Sum ( portal::stockCount )
But I think this is not for real business. You would need to limit data of summing with term of Date.
I use a sock movement table that sits between the order and stock item. This simply holds key fields pointing to order & stock and the quantity. You can then either have a calc field in stock table that sums up all movements, or I tend to run a script to calculate the stock level as a number.
The Case calculation, if I had a sale or purchase option, would I put the same calculation for if I selected purchase
This way either option will remove quantity if sold or add quantity if purchased?
Agreeing with user19752 (and expanding), with any kind of inventory system, it's usually better to have a table of "transactions" for events where the stock total is changed. Each time a record is created in this table, a script updates the quantities. Otherwise, as you continue adding data to the system, your calculations will become progressively slower until the system isn't usable.