- $line_tax = $entered_grn->full_charge_price() - $line_taxfree;
-
- update_supp_received_items_for_invoice($entered_grn->id, $entered_grn->po_detail_item,
- $entered_grn->this_quantity_inv, $entered_grn->chg_price);
-
- $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
- $stock_entry_account = $stock_gl_code["inventory_account"];
-
- add_gl_trans_supplier($trans_type, $invoice_id, $date_, $stock_entry_account,
- $stock_gl_code['dimension_id'], $stock_gl_code['dimension2_id'],
- $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
-
-/*
- $supplierCurrency = get_supplier_currency($supp_trans->supplier_id);
- $localChgPrice = to_home_currency($entered_grn->chg_price, $supplierCurrency, $date_);
- $PurchPriceVar = $entered_grn->this_quantity_inv * ($localChgPrice - $entered_grn->std_cost_unit);
-
- echo "purchase price variance is $PurchPriceVar";
-
- // Yes but where to post this difference to - if its an inventory item the variance account
- // must be retreived from the stock category record
-
- if ($PurchPriceVar !=0){ // don't bother with this lot if there is no difference !
- // need to get the stock category record for this inventory item -
- $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
-
-
- add_gl_trans_supplier($trans_type, $invoice_id, $date_, $stock_gl_code["purch_price_var_act"],
- $PurchPriceVar, $supp_trans->supplier_id,
- "The general ledger transaction could not be added for the price variance of the inventory item");
- }*/
-
- add_supp_invoice_item($trans_type, $invoice_id, $entered_grn->item_code,
- $entered_grn->item_description, 0, $line_taxfree, $line_tax,
- $entered_grn->this_quantity_inv, $entered_grn->id, $entered_grn->po_detail_item, "");
+ $line_tax = $entered_grn->full_charge_price($supp_trans->tax_group_id) - $line_taxfree;
+ $stock_gl_code = get_stock_gl_code($entered_grn->item_code);
+ $dim = $supplier['dimension_id'] ? $supplier['dimension_id'] : $stock_gl_code['dimension_id'];
+ $dim2 = $supplier['dimension2_id'] ? $supplier['dimension2_id'] : $stock_gl_code['dimension2_id'];
+ if ($trans_type == ST_SUPPCREDIT)
+ {
+ $iv_act = (is_inventory_item($entered_grn->item_code) ? $stock_gl_code["inventory_account"] :
+ ($supplier["purchase_account"] ? $supplier["purchase_account"] : $stock_gl_code["cogs_account"]));
+ $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+ $dim, $dim2, $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
+ }
+ else
+ {
+ // -------------- if price changed since po received. 16 Aug 2008 Joe Hunt
+ $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[0];
+
+ $old_price = $old[2];
+ // adjust for tax included.
+ $old_price = get_tax_free_price_for_item($entered_grn->item_code, $old_price,
+ $supp_trans->tax_group_id, $entered_grn->tax_included);
+
+ /*
+ If statement is removed. Should always check for deliveries nomatter if there has been a price change.
+ */
+ //if ($old_price != $line_taxfree) // price-change, so update
+ //{
+ //$diff = $line_taxfree - $old_price;
+ $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, $entered_grn->this_quantity_inv * $line_taxfree, $supp_trans->supplier_id);
+ else
+ {
+ $currency = get_supplier_currency($supp_trans->supplier_id);
+ $ex_rate = get_exchange_rate_from_home_currency($currency, $old_date);
+ $total += add_gl_trans_supplier($trans_type, $invoice_id, $date_, $iv_act,
+ $dim, $dim2, $entered_grn->this_quantity_inv * $old_price, $supp_trans->supplier_id, "", $ex_rate);
+ $diff = get_diff_in_home_currency($supp_trans->supplier_id, $old_date, $date_, $old_price,
+ $line_taxfree);
+
+ // always return due to change in currency.
+ /*$mat_cost = update_average_material_cost(null, $entered_grn->item_code,
+ $diff, $entered_grn->this_quantity_inv, $old_date, true); */
+ $mat_cost = update_average_material_cost(null, $entered_grn->item_code,
+ $diff, $entered_grn->this_quantity_inv, null, true);
+
+ //Add GL transaction for GRN Provision in case of difference
+ if (($diff * $entered_grn->this_quantity_inv) != 0 )
+ {
+ $diff_amt = $diff * $entered_grn->this_quantity_inv;
+ $total += add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["inventory_account"],
+ $dim, $dim2, 'GRN Provision', $diff_amt, null, null, null,
+ "The general ledger transaction could not be added for the GRN of the inventory item");
+
+ //Chaitanya
+ //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;
+
+ //Chaitanya : Post a journal entry
+ $id = get_next_trans_no(ST_JOURNAL);
+ $ref = $Refs->get_next(ST_JOURNAL);
+ $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, -$entered_grn->this_quantity_inv * $diff);
+ //GL Posting to inventory adjustment account
+ add_gl_trans_std_cost(ST_JOURNAL, $id, $date_,
+ $stock_gl_code["adjustment_account"],
+ $dim, $dim2, $memo, $entered_grn->this_quantity_inv * $diff);
+
+ add_audit_trail(ST_JOURNAL, $id, $date_);
+ add_comments(ST_JOURNAL, $id, $date_, $memo);
+ $Refs->save(ST_JOURNAL, $id, $ref);
+ }
+
+ /*$qoh = get_qoh_on_date($entered_grn->item_code);
+ if ($qoh <= 0)
+ {
+ $memo = "Diff. in cost: ".$diff;
+ //Reverse the inventory effect if $qoh <=0
+ add_gl_trans_supplier($trans_type, $invoice_id, $date_,
+ $stock_gl_code["inventory_account"],
+ $dim, $dim2, -$entered_grn->this_quantity_inv * $diff, $supp_trans->supplier_id, "", null, $memo);
+ //GL Posting to inventory adjustment account
+ add_gl_trans_supplier($trans_type, $invoice_id, $date_,
+ $stock_gl_code["adjustment_account"],
+ $dim, $dim2, $entered_grn->this_quantity_inv * $diff, $supp_trans->supplier_id, "", null, $memo);
+ }*/
+
+ //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");
+ }
+ }
+ // added 2008-12-08 Joe Hunt. Update the purchase data table
+ add_or_update_purchase_data($supp_trans->supplier_id, $entered_grn->item_code, $entered_grn->chg_price);
+ /*$deliveries = get_deliveries_between($entered_grn->item_code, $old_date, Today()); // extend the period, if invoice is before any deliveries.
+ if ($deliveries[0] != 0) // have deliveries been done during the period?
+ {
+ $deliveries[1] /= $deliveries[0];
+ $amt = ($mat_cost - $deliveries[1]) * $deliveries[0]; // $amt in home currency
+ if ($amt != 0.0)
+ {
+ $_s = $entered_grn->item_code;
+ $_d = $deliveries[0];
+ $_od = $old_date;
+ $_cd = $mat_cost - $deliveries[1];
+ $memo = _("Cost difference adjustment for $_s. $_d items delivered since $_od. The cost difference is $_cd");
+ add_gl_trans($trans_type, $invoice_id, $date_, $stock_gl_code["cogs_account"],
+ $dim, $dim2, $memo, $amt, null, null, null,
+ "The general ledger transaction could not be added for the price variance of the inventory item");
+ add_gl_trans($trans_type, $invoice_id, $date_, $iv_act,
+ 0, 0, $memo, -$amt, null, null, null,
+ "The general ledger transaction could not be added for the price variance of the inventory item");
+ }
+ update_stock_move_pid(ST_CUSTDELIVERY, $entered_grn->item_code, $old_date, $date_, 0, $mat_cost);
+ }
+ if (is_inventory_item($entered_grn->item_code))
+ update_stock_move_pid(ST_SUPPRECEIVE, $entered_grn->item_code, $old_date, $old_date, $supp_trans->supplier_id, $mat_cost);
+ */
+ //}
+ }
+ // ----------------------------------------------------------------------
+
+ 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->id, $entered_grn->po_detail_item, "");