When average cost is reset to latest cost
Get Unit Cost Price of transactions of type receipt or manufacture for a product
Get the current quantity in stock
Working backwards, see which of those transactions make up the current quantity in stock. Once you have reach BOF or quantity in stock <= 0, you have reached the transactions which make up the current quantity in stock held
Of those transactions, get the minimum value and the maximum value
Calculate the new average
If the transaction quantity of the transaction is less than zero and ((new average < minimum) or (new average > maximum)) then reset to latest cost