Fixed many issues in output HTML code according to HTML 4.01 Transitional format.
[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 ($use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if ($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 check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));
35
36 //-----------------------------------------------------------------------------------------------
37
38 if (isset($_GET['AddedID'])) 
39 {
40         $trans_no = $_GET['AddedID'];
41         $trans_type = ST_LOCTRANSFER;
42
43         display_notification_centered(_("Inventory transfer has been processed"));
44         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this transfer")));
45
46         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Inventory Transfer"));
47
48         display_footer_exit();
49 }
50 //--------------------------------------------------------------------------------------------------
51
52 function line_start_focus() {
53   global        $Ajax;
54
55   $Ajax->activate('items_table');
56   set_focus('_stock_id_edit');
57 }
58 //-----------------------------------------------------------------------------------------------
59
60 function handle_new_order()
61 {
62         if (isset($_SESSION['transfer_items']))
63         {
64                 $_SESSION['transfer_items']->clear_items();
65                 unset ($_SESSION['transfer_items']);
66         }
67
68         $_SESSION['transfer_items'] = new items_cart(ST_LOCTRANSFER);
69         $_POST['AdjDate'] = new_doc_date();
70         if (!is_date_in_fiscalyear($_POST['AdjDate']))
71                 $_POST['AdjDate'] = end_fiscalyear();
72         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];     
73 }
74
75 //-----------------------------------------------------------------------------------------------
76
77 if (isset($_POST['Process']))
78 {
79
80         $tr = &$_SESSION['transfer_items'];
81         $input_error = 0;
82
83         if (count($tr->line_items) == 0)        {
84                 display_error(_("You must enter at least one non empty item line."));
85                 set_focus('stock_id');
86                 $input_error = 1;
87         }
88         if (!$Refs->is_valid($_POST['ref'])) 
89         {
90                 display_error(_("You must enter a reference."));
91                 set_focus('ref');
92                 $input_error = 1;
93         } 
94         elseif (!is_new_reference($_POST['ref'], ST_LOCTRANSFER)) 
95         {
96                 display_error(_("The entered reference is already in use."));
97                 set_focus('ref');
98                 $input_error = 1;
99         } 
100         elseif (!is_date($_POST['AdjDate'])) 
101         {
102                 display_error(_("The entered transfer date is invalid."));
103                 set_focus('AdjDate');
104                 $input_error = 1;
105         } 
106         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
107         {
108                 display_error(_("The entered date is not in fiscal year."));
109                 set_focus('AdjDate');
110                 $input_error = 1;
111         } 
112         elseif ($_POST['FromStockLocation'] == $_POST['ToStockLocation'])
113         {
114                 display_error(_("The locations to transfer from and to must be different."));
115                 set_focus('FromStockLocation');
116                 $input_error = 1;
117         }
118         elseif (!$SysPrefs->allow_negative_stock())
119         {
120                 $low_stock = $tr->check_qoh($_POST['FromStockLocation'], $_POST['AdjDate'], true);
121
122                 if ($low_stock)
123                 {
124                 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."));
125                         $input_error = 1;
126                 }
127         }
128
129         if ($input_error == 1)
130                 unset($_POST['Process']);
131 }
132
133 //-------------------------------------------------------------------------------
134
135 if (isset($_POST['Process']))
136 {
137
138         $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items,
139                 $_POST['FromStockLocation'], $_POST['ToStockLocation'],
140                 $_POST['AdjDate'], $_POST['type'], $_POST['ref'], $_POST['memo_']);
141         new_doc_date($_POST['AdjDate']);
142         $_SESSION['transfer_items']->clear_items();
143         unset($_SESSION['transfer_items']);
144
145         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
146 } /*end of process credit note */
147
148 //-----------------------------------------------------------------------------------------------
149
150 function check_item_data()
151 {
152         if (!check_num('qty', 0))
153         {
154                 display_error(_("The quantity entered must be a positive number."));
155                 set_focus('qty');
156                 return false;
157         }
158         return true;
159 }
160
161 //-----------------------------------------------------------------------------------------------
162
163 function handle_update_item()
164 {
165     if($_POST['UpdateItem'] != "" && check_item_data())
166     {
167                 $id = $_POST['LineNo'];
168         if (!isset($_POST['std_cost']))
169                 $_POST['std_cost'] = $_SESSION['transfer_items']->line_items[$id]->standard_cost;
170         $_SESSION['transfer_items']->update_cart_item($id, input_num('qty'), $_POST['std_cost']);
171     }
172         line_start_focus();
173 }
174
175 //-----------------------------------------------------------------------------------------------
176
177 function handle_delete_item($id)
178 {
179         $_SESSION['transfer_items']->remove_from_cart($id);
180         line_start_focus();
181 }
182
183 //-----------------------------------------------------------------------------------------------
184
185 function handle_new_item()
186 {
187         if (!check_item_data())
188                 return;
189         if (!isset($_POST['std_cost']))
190                 $_POST['std_cost'] = 0;
191         add_to_order($_SESSION['transfer_items'], $_POST['stock_id'], input_num('qty'), $_POST['std_cost']);
192         line_start_focus();
193 }
194
195 //-----------------------------------------------------------------------------------------------
196 $id = find_submit('Delete');
197 if ($id != -1)
198         handle_delete_item($id);
199         
200 if (isset($_POST['AddItem']))
201         handle_new_item();
202
203 if (isset($_POST['UpdateItem']))
204         handle_update_item();
205
206 if (isset($_POST['CancelItemChanges'])) {
207         line_start_focus();
208 }
209 //-----------------------------------------------------------------------------------------------
210
211 if (isset($_GET['NewTransfer']) || !isset($_SESSION['transfer_items']))
212 {
213         handle_new_order();
214 }
215
216 //-----------------------------------------------------------------------------------------------
217 start_form();
218
219 display_order_header($_SESSION['transfer_items']);
220
221 start_table(TABLESTYLE, "width='70%'", 10);
222 start_row();
223 echo "<td>";
224 display_transfer_items(_("Items"), $_SESSION['transfer_items']);
225 transfer_options_controls();
226 echo "</td>";
227 end_row();
228 end_table(1);
229
230 submit_center_first('Update', _("Update"), '', null);
231 submit_center_last('Process', _("Process Transfer"), '',  'default');
232
233 end_form();
234 end_page();
235
236 ?>