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