Global change in naming convention from std_cost to unit_cost.
[fa-stable.git] / inventory / includes / db / items_transfer_db.inc
index c1eac28b8538f06064d905963b1c684cf71a0f9c..f35bf3696c43439ca0bfcb6c26ba574a960e2a04 100644 (file)
 ***********************************************************************/
 //-------------------------------------------------------------------------------------------------------------
 
-function add_stock_transfer($Items, $location_from, $location_to, $date_, $type, $reference, $memo_)
+function add_stock_transfer($Items, $location_from, $location_to, $date_, $reference, $memo_)
 {
+       global $SysPrefs, $path_to_root, $Refs;
+
        begin_transaction();
+       $args = func_get_args();
+       $args = (object)array_combine(array('Items', 'location_from', 'location_to', 
+       'date_', 'reference', 'memo_'), $args);
+       $args->trans_no = 0;
+       hook_db_prewrite($args, ST_LOCTRANSFER);
 
-       $transfer_id = get_next_trans_no(systypes::location_transfer());
+       $transfer_id = get_next_trans_no(ST_LOCTRANSFER);
 
+       if ($SysPrefs->loc_notification() == 1)
+       {
+               include_once($path_to_root . "/inventory/includes/inventory_db.inc");
+               $st_ids = array();
+               $st_names = array();
+               $st_num = array();
+               $st_reorder = array();
+       }
        foreach ($Items as $line_item)
        {
+               if ($SysPrefs->loc_notification() == 1)
+                       $loc = calculate_reorder_level($location_from, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
                add_stock_transfer_item($transfer_id, $line_item->stock_id, $location_from,
-                       $location_to, $date_, $type, $reference, $line_item->quantity);
+                       $location_to, $date_, $reference, $line_item->quantity);
        }
 
-       add_comments(systypes::location_transfer(), $transfer_id, $date_, $memo_);
+       add_comments(ST_LOCTRANSFER, $transfer_id, $date_, $memo_);
 
-       references::save_last($reference, systypes::location_transfer());
-       add_audit_trail(systypes::location_transfer(), $transfer_id, $date_);
+       $Refs->save(ST_LOCTRANSFER, $transfer_id, $reference);
+       add_audit_trail(ST_LOCTRANSFER, $transfer_id, $date_);
+
+       $args->trans_no = $transfer_id;
+       hook_db_postwrite($args, ST_LOCTRANSFER);
 
        commit_transaction();
+       
+       if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
+               send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
 
        return $transfer_id;
 }
@@ -38,47 +61,50 @@ function add_stock_transfer($Items, $location_from, $location_to, $date_, $type,
 // add 2 stock_moves entries for a stock transfer
 // $date_ is display date (not sql)
 // std_cost is in HOME currency
-// it seems the standard_cost field is not used at all
+// it seems the unit_cost field is not used at all
 
 function add_stock_transfer_item($transfer_id, $stock_id, $location_from, $location_to,
-       $date_, $type, $reference, $quantity)
+       $date_, $reference, $quantity)
 {
-       add_stock_move(systypes::location_transfer(), $stock_id, $transfer_id, $location_from,
-               $date_, $reference, -$quantity, 0, $type);
+       add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_from,
+               $date_, $reference, -$quantity, 0);
 
-       add_stock_move(systypes::location_transfer(), $stock_id, $transfer_id, $location_to,
-               $date_, $reference, $quantity, 0, $type);
+       add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_to,
+               $date_, $reference, $quantity, 0);
 
 }
 
 //-------------------------------------------------------------------------------------------------------------
-
+//
+//     Get inventory transfer common data (currently this is still stored in stock_moves table).
+//
 function get_stock_transfer($trans_no)
 {
-       $result = get_stock_transfer_items($trans_no);
-       if (db_num_rows($result) < 2)
-       {
-               display_db_error("transfer with less than 2 items : $trans_no", "");
-       }
+       // retrieve common data from any two from/to move records
+       $sql = "SELECT loc_from.*, loc_to.*
+               FROM
+                       (SELECT trans_no, type, tran_date, reference, move.loc_code as from_loc, loc.location_name as from_name
+                       FROM ".TB_PREF."stock_moves move
+                               LEFT JOIN ".TB_PREF."locations loc ON loc.loc_code=move.loc_code
+                       WHERE type=".ST_LOCTRANSFER." AND trans_no=".db_escape($trans_no). " AND qty<0 LIMIT 1) loc_from,
+
+                       (SELECT move.loc_code as to_loc, loc.location_name as to_name
+                       FROM ".TB_PREF."stock_moves move
+                               LEFT JOIN ".TB_PREF."locations loc ON loc.loc_code=move.loc_code
+                       WHERE type=".ST_LOCTRANSFER." AND trans_no=".db_escape($trans_no). " AND qty>0 LIMIT 1) loc_to";
+
+       $result = db_query($sql, "Could not get transfer common data");
+
+       $data = db_fetch($result);
 
-       // this function is very bad that it assumes that 1st record and 2nd record contain the
-       // from and to locations - if get_stock_moves uses a different ordering than trans_no then
-       // it will bomb
-       $move1 = db_fetch($result);
-       $move2 = db_fetch($result);
-
-       // return an array of (From, To)
-       if ($move1['qty'] < 0)
-               return array($move1, $move2);
-       else
-               return array($move2, $move1);
+       return $data;
 }
 
 //-------------------------------------------------------------------------------------------------------------
 
 function get_stock_transfer_items($trans_no)
 {
-       $result = get_stock_moves(systypes::location_transfer(), $trans_no);
+       $result = get_stock_moves(ST_LOCTRANSFER, $trans_no);
 
        if (db_num_rows($result) == 0)
        {
@@ -92,7 +118,7 @@ function get_stock_transfer_items($trans_no)
 
 function void_stock_transfer($type_no)
 {
-       void_stock_move(systypes::location_transfer(), $type_no);
+       hook_db_prevoid(ST_LOCTRANSFER, $type_no);
+       void_stock_move(ST_LOCTRANSFER, $type_no);
 }
 
-?>
\ No newline at end of file