Copyright notes at top op every source file
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 //-------------------------------------------------------------------------------------------------------------
13
14 function add_stock_transfer($Items, $location_from, $location_to, $date_, $type, $reference, $memo_)
15 {
16         begin_transaction();
17
18         $transfer_id = get_next_trans_no(systypes::location_transfer());
19
20         foreach ($Items as $line_item)
21         {
22                 add_stock_transfer_item($transfer_id, $line_item->stock_id, $location_from,
23                         $location_to, $date_, $type, $reference, $line_item->quantity);
24         }
25
26         add_comments(systypes::location_transfer(), $transfer_id, $date_, $memo_);
27
28         references::save_last($reference, systypes::location_transfer());
29
30         commit_transaction();
31
32         return $transfer_id;
33 }
34
35 //-------------------------------------------------------------------------------------------------------------
36
37 // add 2 stock_moves entries for a stock transfer
38 // $date_ is display date (not sql)
39 // std_cost is in HOME currency
40 // it seems the standard_cost field is not used at all
41
42 function add_stock_transfer_item($transfer_id, $stock_id, $location_from, $location_to,
43         $date_, $type, $reference, $quantity)
44 {
45         add_stock_move(systypes::location_transfer(), $stock_id, $transfer_id, $location_from,
46         $date_, $reference, -$quantity, 0, $type);
47
48         add_stock_move(systypes::location_transfer(), $stock_id, $transfer_id, $location_to,
49                 $date_, $reference, $quantity, 0, $type);
50
51 }
52
53 //-------------------------------------------------------------------------------------------------------------
54
55 function get_stock_transfer($trans_no)
56 {
57         $result = get_stock_transfer_items($trans_no);
58         if (db_num_rows($result) < 2)
59         {
60                 display_db_error("transfer with less than 2 items : $trans_no", "");
61         }
62
63         // this function is very bad that it assumes that 1st record and 2nd record contain the
64         // from and to locations - if get_stock_moves uses a different ordering than trans_no then
65         // it will bomb
66         $move1 = db_fetch($result);
67         $move2 = db_fetch($result);
68
69         // return an array of (From, To)
70         if ($move1['qty'] < 0)
71                 return array($move1, $move2);
72         else
73                 return array($move2, $move1);
74 }
75
76 //-------------------------------------------------------------------------------------------------------------
77
78 function get_stock_transfer_items($trans_no)
79 {
80         $result = get_stock_moves(systypes::location_transfer(), $trans_no);
81
82         if (db_num_rows($result) == 0)
83         {
84                 return null;
85         }
86
87         return $result;
88 }
89
90 //-------------------------------------------------------------------------------------------------------------
91
92 function void_stock_transfer($type_no)
93 {
94         void_stock_move(systypes::location_transfer(), $type_no);
95 }
96
97 ?>