- $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");
+ $ex_rate = get_exchange_rate_from_home_currency($supp_trans->currency, $old_date);
+ $old_value = get_tax_free_price_for_item($entered_grn->item_code, $entered_grn->this_quantity_inv * $old_price,
+ $supp_trans->tax_group_id, $supp_trans->tax_included);
+
+ $currency = get_supplier_currency($supp_trans->supplier_id);
+
+ $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+ $dim, $dim2, $old_value, $supp_trans->supplier_id, "", $ex_rate);
+ $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_value,
+ $taxfree_line);
+
+ $mat_cost = update_average_material_cost(null, $entered_grn->item_code,
+ $diff/$entered_grn->this_quantity_inv, $entered_grn->this_quantity_inv, null, true);
+
+ //Add GL transaction for GRN Provision in case of difference
+ if ($diff != 0)
+ {
+ $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);
+ }
+ }