Fixed exchange rate storage in GRN transactions.
[fa-stable.git] / purchasing / includes / db / grn_db.inc
index 94853b469f52bde3ddff3c66d3c5cdee5a279828..09090c302fc6a2bf08b3afbab50f94bfd2b0653b 100644 (file)
 //------------------- update average material cost ------------------------------------------ Joe Hunt Mar-03-2008
 function update_average_material_cost($supplier, $stock_id, $price, $qty, $date, $adj_only=false)
 {
-       //Handle if inventory will become negative
-    //8-OCT-2011 : Skip negative inventory adjustment for case of adjust_only
-    if (is_inventory_item($stock_id) && !$adj_only)
-        handle_negative_inventory($stock_id, $qty, $price, $date);     
-
        // probably this function should be optimized
        // passing transaction cart as argument. This would
        // save a couple of db calls like get_supplier()
@@ -40,6 +35,11 @@ function update_average_material_cost($supplier, $stock_id, $price, $qty, $date,
                $price_in_home_currency = $price;
 
        $price_in_home_currency_ = $price_in_home_currency;
+
+       //Handle if inventory will become negative
+    //8-OCT-2011 : Skip negative inventory adjustment for case of adjust_only
+    if (is_inventory_item($stock_id) && !$adj_only) 
+        handle_negative_inventory($stock_id, $qty, $price_in_home_currency, $date);
        
        $sql = "SELECT mb_flag, material_cost, labour_cost, overhead_cost FROM ".TB_PREF."stock_master WHERE stock_id=".db_escape($stock_id);
        $result = db_query($sql);
@@ -99,9 +99,16 @@ function add_grn(&$po)
        begin_transaction();
        hook_db_prewrite($po, ST_SUPPRECEIVE);
 
-       add_new_exchange_rate(get_supplier_currency($po->supplier_id), $date_, $po->ex_rate);
+       if (!is_company_currency($po->curr_code))
+       {
+               if (!$po->ex_rate)
+                       $po->ex_rate = get_date_exchange_rate($po->curr_code, $date_);
+               else
+                       add_new_exchange_rate($po->curr_code, $date_, $po->ex_rate);
+       } else
+               $po->ex_rate = 1;
 
-       $grn = add_grn_batch($po->order_no, $po->supplier_id, $po->reference, $po->Location, $date_);
+       $grn = add_grn_batch($po->order_no, $po->supplier_id, $po->reference, $po->Location, $date_, $po->ex_rate);
 
     $clearing_act = get_company_pref('grn_clearing_act');
        if ($clearing_act) {    // otherwise GRN clearing account is not used
@@ -168,13 +175,13 @@ function add_grn(&$po)
 
 //----------------------------------------------------------------------------------------
 
-function add_grn_batch($po_number, $supplier_id, $reference, $location, $date_)
+function add_grn_batch($po_number, $supplier_id, $reference, $location, $date_, $rate)
 {
        $date = date2sql($date_);
 
-       $sql = "INSERT INTO ".TB_PREF."grn_batch (purch_order_no, delivery_date, supplier_id, reference, loc_code)
+       $sql = "INSERT INTO ".TB_PREF."grn_batch (purch_order_no, delivery_date, supplier_id, reference, loc_code, rate)
                        VALUES (".db_escape($po_number).", ".db_escape($date).", "
-                       .db_escape($supplier_id).", ".db_escape($reference).", ".db_escape($location).")";
+                       .db_escape($supplier_id).", ".db_escape($reference).", ".db_escape($location).", ".db_escape($rate).")";
 
        db_query($sql, "A grn batch record could not be inserted.");
 
@@ -368,6 +375,7 @@ function read_grn($grn_batch, &$order)
 
        $row = db_fetch($result);
        $po_number = $row["purch_order_no"];
+       $order->ex_rate = $row["rate"];
 
        $result = read_po_header($po_number, $order);
 
@@ -465,4 +473,3 @@ function void_grn($grn_batch)
 
 //----------------------------------------------------------------------------------------------------------
 
-?>
\ No newline at end of file