5d5181f78ce89d7660123c76565e46086f107590
[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_, $type, $reference, $memo_)
15 {
16         global $Refs;
17
18         begin_transaction();
19         $args = func_get_args();
20         $args = (object)array_combine(array('Items', 'location_from', 'location_to', 
21         'date_', 'type', '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         foreach ($Items as $line_item)
28         {
29                 add_stock_transfer_item($transfer_id, $line_item->stock_id, $location_from,
30                         $location_to, $date_, $type, $reference, $line_item->quantity);
31         }
32
33         add_comments(ST_LOCTRANSFER, $transfer_id, $date_, $memo_);
34
35         $Refs->save(ST_LOCTRANSFER, $transfer_id, $reference);
36         add_audit_trail(ST_LOCTRANSFER, $transfer_id, $date_);
37
38         $args->trans_no = $transfer_id;
39         hook_db_postwrite($args, ST_LOCTRANSFER);
40
41         commit_transaction();
42
43         return $transfer_id;
44 }
45
46 //-------------------------------------------------------------------------------------------------------------
47
48 // add 2 stock_moves entries for a stock transfer
49 // $date_ is display date (not sql)
50 // std_cost is in HOME currency
51 // it seems the standard_cost field is not used at all
52
53 function add_stock_transfer_item($transfer_id, $stock_id, $location_from, $location_to,
54         $date_, $type, $reference, $quantity)
55 {
56         add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_from,
57         $date_, $reference, -$quantity, 0, $type);
58
59         add_stock_move(ST_LOCTRANSFER, $stock_id, $transfer_id, $location_to,
60                 $date_, $reference, $quantity, 0, $type);
61
62 }
63
64 //-------------------------------------------------------------------------------------------------------------
65
66 function get_stock_transfer($trans_no)
67 {
68         $result = get_stock_transfer_items($trans_no);
69         if (db_num_rows($result) < 2)
70         {
71                 display_db_error("transfer with less than 2 items : $trans_no", "");
72         }
73
74         // this function is very bad that it assumes that 1st record and 2nd record contain the
75         // from and to locations - if get_stock_moves uses a different ordering than trans_no then
76         // it will bomb
77         $move1 = db_fetch($result);
78         $move2 = db_fetch($result);
79
80         // return an array of (From, To)
81         if ($move1['qty'] < 0)
82                 return array($move1, $move2);
83         else
84                 return array($move2, $move1);
85 }
86
87 //-------------------------------------------------------------------------------------------------------------
88
89 function get_stock_transfer_items($trans_no)
90 {
91         $result = get_stock_moves(ST_LOCTRANSFER, $trans_no);
92
93         if (db_num_rows($result) == 0)
94         {
95                 return null;
96         }
97
98         return $result;
99 }
100
101 //-------------------------------------------------------------------------------------------------------------
102
103 function void_stock_transfer($type_no)
104 {
105         hook_db_prevoid(ST_LOCTRANSFER, $type_no);
106         void_stock_move(ST_LOCTRANSFER, $type_no);
107 }
108
109 ?>