Merging latest changes from stable branch up to 2.3.24
[fa-stable.git] / inventory / transfers.php
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 $page_security = 'SA_LOCATIONTRANSFER';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/inventory/includes/stock_transfers_ui.inc");
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
23 $js = "";
24 if ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if (user_use_date_picker())
27         $js .= get_js_date_picker();
28 page(_($help_context = "Inventory Location Transfers"), false, false, "", $js);
29
30 //-----------------------------------------------------------------------------------------------
31
32 check_db_has_costable_items(_("There are no inventory items defined in the system (Purchased or manufactured items)."));
33
34 //-----------------------------------------------------------------------------------------------
35
36 if (isset($_GET['AddedID'])) 
37 {
38         $trans_no = $_GET['AddedID'];
39         $trans_type = ST_LOCTRANSFER;
40
41         display_notification_centered(_("Inventory transfer has been processed"));
42         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this transfer")));
43
44         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Inventory Transfer"));
45
46         display_footer_exit();
47 }
48 //--------------------------------------------------------------------------------------------------
49
50 function line_start_focus() {
51   global        $Ajax;
52
53   $Ajax->activate('items_table');
54   set_focus('_stock_id_edit');
55 }
56 //-----------------------------------------------------------------------------------------------
57
58 function handle_new_order()
59 {
60         if (isset($_SESSION['transfer_items']))
61         {
62                 $_SESSION['transfer_items']->clear_items();
63                 unset ($_SESSION['transfer_items']);
64         }
65
66         $_SESSION['transfer_items'] = new items_cart(ST_LOCTRANSFER);
67         $_POST['AdjDate'] = new_doc_date();
68         if (!is_date_in_fiscalyear($_POST['AdjDate']))
69                 $_POST['AdjDate'] = end_fiscalyear();
70         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];     
71 }
72
73 //-----------------------------------------------------------------------------------------------
74
75 if (isset($_POST['Process']))
76 {
77
78         $tr = &$_SESSION['transfer_items'];
79         $input_error = 0;
80
81         if (count($tr->line_items) == 0)        {
82                 display_error(_("You must enter at least one non empty item line."));
83                 set_focus('stock_id');
84                 $input_error = 1;
85         }
86         if (!$Refs->is_valid($_POST['ref'])) 
87         {
88                 display_error(_("You must enter a reference."));
89                 set_focus('ref');
90                 $input_error = 1;
91         } 
92         elseif (!is_new_reference($_POST['ref'], ST_LOCTRANSFER)) 
93         {
94                 display_error(_("The entered reference is already in use."));
95                 set_focus('ref');
96                 $input_error = 1;
97         } 
98         elseif (!is_date($_POST['AdjDate'])) 
99         {
100                 display_error(_("The entered transfer date is invalid."));
101                 set_focus('AdjDate');
102                 $input_error = 1;
103         } 
104         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
105         {
106                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
107                 set_focus('AdjDate');
108                 $input_error = 1;
109         } 
110         elseif ($_POST['FromStockLocation'] == $_POST['ToStockLocation'])
111         {
112                 display_error(_("The locations to transfer from and to must be different."));
113                 set_focus('FromStockLocation');
114                 $input_error = 1;
115         }
116         elseif (!$SysPrefs->allow_negative_stock())
117         {
118                 $low_stock = $tr->check_qoh($_POST['FromStockLocation'], $_POST['AdjDate'], true);
119
120                 if ($low_stock)
121                 {
122                 display_error(_("The transfer cannot be processed because it would cause negative inventory balance in source location for marked items as of document date or later."));
123                         $input_error = 1;
124                 }
125         }
126
127         if ($input_error == 1)
128                 unset($_POST['Process']);
129 }
130
131 //-------------------------------------------------------------------------------
132
133 if (isset($_POST['Process']))
134 {
135
136         $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items,
137                 $_POST['FromStockLocation'], $_POST['ToStockLocation'],
138                 $_POST['AdjDate'], $_POST['ref'], $_POST['memo_']);
139         new_doc_date($_POST['AdjDate']);
140         $_SESSION['transfer_items']->clear_items();
141         unset($_SESSION['transfer_items']);
142
143         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
144 } /*end of process credit note */
145
146 //-----------------------------------------------------------------------------------------------
147
148 function check_item_data()
149 {
150         if (!check_num('qty', 0) || input_num('qty') == 0)
151         {
152                 display_error(_("The quantity entered must be a positive number."));
153                 set_focus('qty');
154                 return false;
155         }
156         return true;
157 }
158
159 //-----------------------------------------------------------------------------------------------
160
161 function handle_update_item()
162 {
163         $id = $_POST['LineNo'];
164         if (!isset($_POST['std_cost']))
165                 $_POST['std_cost'] = $_SESSION['transfer_items']->line_items[$id]->standard_cost;
166         $_SESSION['transfer_items']->update_cart_item($id, input_num('qty'), $_POST['std_cost']);
167         line_start_focus();
168 }
169
170 //-----------------------------------------------------------------------------------------------
171
172 function handle_delete_item($id)
173 {
174         $_SESSION['transfer_items']->remove_from_cart($id);
175         line_start_focus();
176 }
177
178 //-----------------------------------------------------------------------------------------------
179
180 function handle_new_item()
181 {
182         if (!isset($_POST['std_cost']))
183                 $_POST['std_cost'] = 0;
184         add_to_order($_SESSION['transfer_items'], $_POST['stock_id'], input_num('qty'), $_POST['std_cost']);
185         line_start_focus();
186 }
187
188 //-----------------------------------------------------------------------------------------------
189 $id = find_submit('Delete');
190 if ($id != -1)
191         handle_delete_item($id);
192         
193 if (isset($_POST['AddItem']) && check_item_data())
194         handle_new_item();
195
196 if (isset($_POST['UpdateItem']) && check_item_data())
197         handle_update_item();
198
199 if (isset($_POST['CancelItemChanges'])) {
200         line_start_focus();
201 }
202 //-----------------------------------------------------------------------------------------------
203
204 if (isset($_GET['NewTransfer']) || !isset($_SESSION['transfer_items']))
205 {
206         handle_new_order();
207 }
208
209 //-----------------------------------------------------------------------------------------------
210 start_form();
211
212 display_order_header($_SESSION['transfer_items']);
213
214 start_table(TABLESTYLE, "width='70%'", 10);
215 start_row();
216 echo "<td>";
217 display_transfer_items(_("Items"), $_SESSION['transfer_items']);
218 transfer_options_controls();
219 echo "</td>";
220 end_row();
221 end_table(1);
222
223 submit_center_first('Update', _("Update"), '', null);
224 submit_center_last('Process', _("Process Transfer"), '',  'default');
225
226 end_form();
227 end_page();
228