Fix COGS computation with negative quantity

issue3287
review976002
Add test for COGS computation with negative quantity

issue3287
review976002