I'd suggest using the same script you use to reduce the stock but multiply your quantities by -1.
I'm personally not so keen on deleting invoices. But it will depend on requirements, I guess.
I mark wrong invoices as VOID, once they have been issued and goods are not physically shipped / delivered yet.
Just so there are no weird gaps for the tax man, they can see it as a page torn out of a book .
In other cases a Return / Credit invoice takes care of that. With negative quantities.
Are you unable to create a script that reduces the stock?
In simplest form if go to related record of ITEM PORTAL and loop through all records:
// default multiplier
Set Variable ( $multiplier = 1 )
If ( $delete = True )
Set Variable ( $multiplier = -1 )
// !!! you could set the $delete variable from a script parameter
GoToRecord ( First )
SetField ( Stock::Quantity In Stock ; Stock::Quantity In Stock - ( Item Portal::Quantity * $multiplier ) )
GoToRecordNext ( ExitAfterLast )
GoToLayout ( OriginalLayout )
I concur that deleting invoices is a practice you should avoid in most cases. Mark them as void or cancelled.
I do this with a layout based on invoices that has a portal with the line items and the total stock qty for the product. The script loops through the portal rows grabs the line item count and adjusts the stock qty. It is fast and I have it run on server as that seems to keep the data updated faster.
There are other ways to get it done, but I think with the basic ideas you have here you should be able to get it working. depending on how you handle your invoices this can also keep the same invoice and just update stock if a line item qty goes up or down after the original data was entered.
I have an issue regarding the calculation, actually everything is working well with the solution and the only problem may be happened if more items are added the system will get slower and slower.
If there is a way to avoid the system to become slower by using the actual calculation?
It should not be slow at all. If it is there is a problem somewhere.