- $gl_cart->add_gl_item($supplier["purchase_account"] ? $supplier["purchase_account"] : $stock_gl_code["cogs_account"], $dimension, $dimension2, $taxfree_line);
-
+ $gl_cart->add_gl_item($supplier["purchase_account"] ? $supplier["purchase_account"] : $stock_gl_code["cogs_account"], $dimension, $dimension2, $taxfree_line);
+
+ } elseif ($trans_type != ST_SUPPCREDIT) {
+ $gl_cart->add_gl_item($stock_gl_code["inventory_account"], $dimension, $dimension2, $taxfree_line, _('Return to supplier'));
+ // we must use invoice value here to preserve proper inventory valuation,
+ // but if the purchase changed average cost, and any item was sold between invoice and credit,
+ // the average will not return to the previous one. To fix this additional cost update should be made here to compensate cogs difference on items sold.
+ update_average_material_cost(null, $item->item_code, $item_gl['Cost']/$item->this_quantity_inv, -$item->this_quantity_inv, $date_);