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