Old ineffective sql_trail superseded by new improved db_trail logging only calls...
[fa-stable.git] / inventory / includes / db / items_transfer_db.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //-------------------------------------------------------------------------------------------------------------
13
14 function add_stock_transfer($Items, $location_from, $location_to, $date_, $reference, $memo_)
15 {
16         global $SysPrefs, $path_to_root, $Refs;
17
18         begin_transaction(__FUNCTION__, func_get_args());
19         $args = func_get_args();
20         $args = (object)array_combine(array('Items', 'location_from', 'location_to', 
21         'date_', 'reference', 'memo_'), $args);
22         $args->trans_no = 0;
23         hook_db_prewrite($args, ST_LOCTRANSFER);
24
25         $transfer_id = get_next_trans_no(ST_LOCTRANSFER);
26
27         if ($SysPrefs->loc_notification() == 1)
28         {
29                 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
30                 $st_ids = array();
31                 $st_names = array();
32                 $st_num = array();
33                 $st_reorder = array();
34         }
35         foreach ($Items as $line_item)
36         {
37                 if ($SysPrefs->loc_notification() == 1)
38                         $loc = calculate_reorder_level($location_from, $line_item, $st_ids, $st_names, $st_num, $st_reorder); 
39                 add_stock_transfer_item($transfer_id, $line_item->stock_id, $location_from,
40                         $location_to, $date_, $reference, $line_item->quantity);
41         }
42
43         add_comments(ST_LOCTRANSFER, $transfer_id, $date_, $memo_);
44
45         $Refs->save(ST_LOCTRANSFER, $transfer_id, $reference);
46         add_audit_trail(ST_LOCTRANSFER, $transfer_id, $date_);
47
48         $args->trans_no = $transfer_id;
49         hook_db_postwrite($args, ST_LOCTRANSFER);
50
51         commit_transaction();
52         
53         if ($SysPrefs->loc_notification() == 1 && count($st_ids) > 0)
54                 send_reorder_email($loc, $st_ids, $st_names, $st_num, $st_reorder);
55
56         return $transfer_id;
57 }
58
59 //-------------------------------------------------------------------------------------------------------------
60
61 // add 2 stock_moves entries for a stock transfer
62 // $date_ is display date (not sql)
63 // std_cost is in HOME currency
64 // it seems the unit_cost field is not used at all
65
66 function add_stock_transfer_item($transfer_id, $stock_id, $location_from, $location_to,
67         $date_, $reference, $quantity)
68 {
69         add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_from,
70         $date_, $reference, -$quantity, 0);
71
72         add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_to,
73                 $date_, $reference, $quantity, 0);
74
75 }
76
77 //-------------------------------------------------------------------------------------------------------------
78 //
79 //      Get inventory transfer common data (currently this is still stored in stock_moves table).
80 //
81 function get_stock_transfer($trans_no)
82 {
83         // retrieve common data from any two from/to move records
84         $sql = "SELECT loc_from.*, loc_to.*
85                 FROM
86                         (SELECT trans_no, type, tran_date, reference, move.loc_code as from_loc, loc.location_name as from_name
87                         FROM ".TB_PREF."stock_moves move
88                                 LEFT JOIN ".TB_PREF."locations loc ON loc.loc_code=move.loc_code
89                         WHERE type=".ST_LOCTRANSFER." AND trans_no=".db_escape($trans_no). " AND qty<0 LIMIT 1) loc_from,
90
91                         (SELECT move.loc_code as to_loc, loc.location_name as to_name
92                         FROM ".TB_PREF."stock_moves move
93                                 LEFT JOIN ".TB_PREF."locations loc ON loc.loc_code=move.loc_code
94                         WHERE type=".ST_LOCTRANSFER." AND trans_no=".db_escape($trans_no). " AND qty>0 LIMIT 1) loc_to";
95
96         $result = db_query($sql, "Could not get transfer common data");
97
98         $data = db_fetch($result);
99
100         return $data;
101 }
102
103 //-------------------------------------------------------------------------------------------------------------
104
105 function get_stock_transfer_items($trans_no)
106 {
107         $result = get_stock_moves(ST_LOCTRANSFER, $trans_no);
108
109         if (db_num_rows($result) == 0)
110         {
111                 return null;
112         }
113
114         return $result;
115 }
116
117 //-------------------------------------------------------------------------------------------------------------
118
119 function void_stock_transfer($type_no)
120 {
121         begin_transaction(__FUNCTION__, func_get_args());
122         hook_db_prevoid(ST_LOCTRANSFER, $type_no);
123         void_stock_move(ST_LOCTRANSFER, $type_no);
124         commit_transaction();
125 }
126