+//------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
+
+function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, $adj_only)
+{
+ $currency = get_supplier_currency($supplier);
+ if ($currency != null)
+ $price_in_home_currency = to_home_currency($price, $currency, $date);
+ else
+ $price_in_home_currency = $price;
+ $sql = "SELECT material_cost FROM ".TB_PREF."stock_master WHERE stock_id='$stock_id'";
+ $result = db_query($sql);
+ $myrow = db_fetch($result);
+ $material_cost = $myrow['material_cost'];
+ $qoh = get_qoh_on_date($stock_id, null, $date);
+ if ($qoh + $qty <= 0)
+ $material_cost = 0;
+ else
+ {
+ if ($adj_only)
+ {
+ if ($qoh <= 0)
+ $material_cost = 0;
+ else
+ $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) / $qoh;
+ }
+ else
+ $material_cost = ($qoh * $material_cost + $qty * $price_in_home_currency) / ($qoh + $qty);
+ }
+ $sql = "UPDATE ".TB_PREF."stock_master SET material_cost=".db_escape($material_cost)."
+ WHERE stock_id='$stock_id'";
+ db_query($sql,"The cost details for the inventory item could not be updated");
+ return $material_cost;
+}
+