Merged last changes from stable.
[fa-stable.git] / includes / db / inventory_db.inc
index fa465ba7b62f9560f77e9cac2b66410275c33386..c65cacb2c665a93a2072ecf23e98b921f71449c4 100644 (file)
@@ -45,7 +45,8 @@ function get_qoh_on_date($stock_id, $location=null, $date_=null, $exclude=0)
             $myrow[0] -= $myrow2[0];
     }
 
-    return $myrow[0];
+    $qoh =  $myrow[0];
+               return $qoh ? $qoh : 0;
 }
 
 //--------------------------------------------------------------------------------------
@@ -140,9 +141,9 @@ function last_negative_stock_trans_id($stock_id, $to)
                        $negative_trans_id = $myrow['trans_id'];
                }
                if ($qty >= 0)
-                       $flag = 0;      
+                       $flag = 0;
        }
-               
+
        if ($flag == 1)
                return $negative_trans_id;
        else 
@@ -180,14 +181,14 @@ function get_deliveries_from_trans($stock_id, $from)
             ." AND trans_id ='$from'";
     $result = db_query($sql, "The deliveries could not be updated");
     $cost = db_fetch_row($result);
-       
+
        // Adjusting QOH valuation 
        $sql = "SELECT SUM(qty) FROM ".TB_PREF."stock_moves
                WHERE stock_id=".db_escape($stock_id)." AND
                        trans_id<'$from' GROUP BY stock_id";
        $result = db_query($sql, "The deliveries could not be updated");
        $qoh = db_fetch_row($result);
-       
+
        $qty = $row[0] - $qoh[0]; //QOH prior to -ve stock is subtracted
        $final_cost = $row[1] - $qoh[0]*$cost[0];
        
@@ -234,7 +235,7 @@ function adjust_deliveries($stock_id, $material_cost, $to)
                $new_purchase_cost = $row[0] * $material_cost;
                $purchase_diff = $new_purchase_cost - $old_purchase_cost;
        }
-       
+
        $diff =  $sales_diff - $purchase_diff;
        
        if ($diff != 0)
@@ -383,6 +384,7 @@ function get_stock_moves($type, $type_no, $visible=false)
 function void_stock_move($type, $type_no)
 {
     $sql = "SELECT * from ".TB_PREF."stock_moves WHERE type=".db_escape($type)." AND trans_no=".db_escape($type_no);
+
     $result = db_query($sql, "Could not void stock moves");
     while ($row = db_fetch($result))
     {
@@ -392,13 +394,13 @@ function void_stock_move($type, $type_no)
                        // The cost has to be adjusted.
                        // Transaction rates are stored either as price or standard_cost depending on types
                        $types = array(ST_SUPPCREDIT, ST_SUPPRECEIVE);
-                       if (in_array($type,$types))
-                               $trans_rate = $row["price"];
+                       if (in_array($type, $types))
+                               $unit_cost = $row["price"];
                        else
-                               $trans_rate = $row["standard_cost"];
+                               $unit_cost = $row["standard_cost"];
 
-                       update_average_material_cost(0, $row["stock_id"],
-                               $trans_rate, -$row["qty"], sql2date($row["tran_date"]));
+                       update_average_material_cost($row["person_id"], $row["stock_id"],
+                               $unit_cost, -$row["qty"], sql2date($row["tran_date"]));
                }
 
     }
@@ -424,8 +426,3 @@ function get_location_name($loc_code)
 
        display_db_error("could not retreive the location name for $loc_code", $sql, true);
 }
-
-//--------------------------------------------------------------------------------------------------
-
-
-?>
\ No newline at end of file