+ // -------------- if price changed since po received.
+ $iv_act = is_inventory_item($entered_grn->item_code) ? ($clearing_act ? $clearing_act : $stock_gl_code["inventory_account"]) :
+ ($supplier["purchase_account"] ? $supplier["purchase_account"] : $stock_gl_code["cogs_account"]);
+ $old = update_supp_received_items_for_invoice($entered_grn->id, $entered_grn->po_detail_item,
+ $entered_grn->this_quantity_inv, $entered_grn->chg_price);
+
+ // Since the standard cost is always calculated on basis of the po unit_price,
+ // this is also the price that should be the base of calculating the price diff.
+ // In cases where there is two different po invoices on the same delivery with different unit prices this will not work either
+
+ $old_price = $old[2];
+
+ $old_date = sql2date($old[1]);
+ if (!is_inventory_item($entered_grn->item_code))
+ $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+ $dim, $dim2, $taxfree_line, $supp_trans->supplier_id);
+ else
+ {
+ $ex_rate = get_exchange_rate_from_home_currency($supplier['curr_code'], $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);
+ }
+ }
+ }
+ add_or_update_purchase_data($supp_trans->supplier_id, $entered_grn->item_code, $entered_grn->chg_price);
+ }
+ update_purchase_value($entered_grn->item_code, $entered_grn->chg_price * $ex_rate);
+ add_supp_invoice_item($trans_type, $invoice_id, $entered_grn->item_code,
+ $entered_grn->item_description, 0, $entered_grn->chg_price, $line_tax/$entered_grn->this_quantity_inv,
+ $entered_grn->this_quantity_inv, $entered_grn->id, $entered_grn->po_detail_item, "");
+ } /* end of GRN postings */
+
+ /*Post a balance post if $total != 0 */
+ add_gl_balance($trans_type, $invoice_id, $date_, -$total, PT_SUPPLIER, $supp_trans->supplier_id); // ??
+