- $diff_amt = $diff * $entered_grn->this_quantity_inv;
- add_gl_trans($trans_type, $invoice_id, $date_, $iv_act,
- $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'], 'GRN Provision',
- $diff_amt, null, null, null,
- "The general ledger transaction could not be added for the GRN of the inventory item");
-
- add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["cogs_account"],
- 0, 0, 'GRN Provision', -$diff_amt, null, null, null,
- "The general ledger transaction could not be added for the GRN of the inventory item");
+ $total += add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["inventory_account"],
+ $dim, $dim2, 'GRN Provision', $diff, null, null, null,
+ "The general ledger transaction could not be added for the GRN of the inventory item");
+
+ //If QOH is 0 or negative then update_average_material_cost will be skipped
+ //Thus difference in PO and Supp Invoice should be handled separately
+ $qoh = get_qoh_on_date($entered_grn->item_code);
+ if ($qoh <= 0)
+ {
+ global $Refs;
+
+ $id = get_next_trans_no(ST_JOURNAL);
+ $ref = $Refs->get_next(ST_JOURNAL, null, $date_);
+ add_journal(ST_JOURNAL, $id, $diff, $date_, get_company_currency(), $ref);
+ $stock_id = $entered_grn->item_code;
+ $stock_gl_code = get_stock_gl_code($stock_id);
+ $memo = _("Supplier invoice adjustment for zero inventory of ").$stock_id." "._("Invoice")." ".$supp_trans->reference;
+ //Reverse the inventory effect if $qoh <=0
+ add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
+ $stock_gl_code["inventory_account"],
+ $dim, $dim2, $memo, -$diff);
+ //GL Posting to inventory adjustment account
+ add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
+ $stock_gl_code["adjustment_account"],
+ $dim, $dim2, $memo, $diff);
+
+ add_audit_trail(ST_JOURNAL, $id, $date_);
+ add_comments(ST_JOURNAL, $id, $date_, $memo);
+ $Refs->save(ST_JOURNAL, $id, $ref);
+ }