Fix COGS computation with negative quantity

issue3287
review976002
(transplanted from b041a5be1444d626bd9105f28f6861c7695ffaa9)

--HG--
extra : transplant_source : %B0A%A5%BE%14D%D6%26%BD%91%05%F2%8Fha%C7i_%FA%A9