Merging version 2.1 RC to main trunk.
[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 = 3;
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(_("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 = systypes::location_transfer();
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_register("transfer_items");
69
70         $_SESSION['transfer_items'] = new items_cart(systypes::location_transfer());
71         $_POST['AdjDate'] = Today();
72         if (!is_date_in_fiscalyear($_POST['AdjDate']))
73                 $_POST['AdjDate'] = end_fiscalyear();
74         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];     
75 }
76
77 //-----------------------------------------------------------------------------------------------
78
79 if (isset($_POST['Process']))
80 {
81
82         $tr = &$_SESSION['transfer_items'];
83         $input_error = 0;
84
85         if (count($tr->line_items) == 0)        {
86                 display_error(_("You must enter at least one non empty item line."));
87                 set_focus('stock_id');
88                 return false;
89         }
90         if (!references::is_valid($_POST['ref'])) 
91         {
92                 display_error(_("You must enter a reference."));
93                 set_focus('ref');
94                 $input_error = 1;
95         } 
96         elseif (!is_new_reference($_POST['ref'], systypes::location_transfer())) 
97         {
98                 display_error(_("The entered reference is already in use."));
99                 set_focus('ref');
100                 $input_error = 1;
101         } 
102         elseif (!is_date($_POST['AdjDate'])) 
103         {
104                 display_error(_("The entered date for the adjustment is invalid."));
105                 set_focus('AdjDate');
106                 $input_error = 1;
107         } 
108         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
109         {
110                 display_error(_("The entered date is not in fiscal year."));
111                 set_focus('AdjDate');
112                 $input_error = 1;
113         } 
114         elseif ($_POST['FromStockLocation'] == $_POST['ToStockLocation'])
115         {
116                 display_error(_("The locations to transfer from and to must be different."));
117                 set_focus('FromStockLocation');
118                 $input_error = 1;
119         } 
120         else 
121         {
122                 $failed_item = $tr->check_qoh($_POST['FromStockLocation'], $_POST['AdjDate'], true);
123                 if ($failed_item >= 0) 
124                 {
125                         $line = $tr->line_items[$failed_item];
126                 display_error(_("The quantity entered is greater than the available quantity for this item at the source location :") .
127                         " " . $line->stock_id . " - " .  $line->item_description);
128                 echo "<br>";
129                         $_POST['Edit'.$failed_item] = 1; // enter edit mode
130                         $input_error = 1;
131                 }
132         }
133
134         if ($input_error == 1)
135                 unset($_POST['Process']);
136 }
137
138 //-------------------------------------------------------------------------------
139
140 if (isset($_POST['Process']))
141 {
142
143         $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items,
144                 $_POST['FromStockLocation'], $_POST['ToStockLocation'],
145                 $_POST['AdjDate'], $_POST['type'], $_POST['ref'], $_POST['memo_']);
146
147         $_SESSION['transfer_items']->clear_items();
148         unset($_SESSION['transfer_items']);
149
150         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
151 } /*end of process credit note */
152
153 //-----------------------------------------------------------------------------------------------
154
155 function check_item_data()
156 {
157         if (!check_num('qty', 0))
158         {
159                 display_error(_("The quantity entered must be a positive number."));
160                 set_focus('qty');
161                 return false;
162         }
163         return true;
164 }
165
166 //-----------------------------------------------------------------------------------------------
167
168 function handle_update_item()
169 {
170     if($_POST['UpdateItem'] != "" && check_item_data())
171     {
172                 $id = $_POST['LineNo'];
173         if (!isset($_POST['std_cost']))
174                 $_POST['std_cost'] = $_SESSION['transfer_items']->line_items[$id]->standard_cost;
175         $_SESSION['transfer_items']->update_cart_item($id, input_num('qty'), $_POST['std_cost']);
176     }
177         line_start_focus();
178 }
179
180 //-----------------------------------------------------------------------------------------------
181
182 function handle_delete_item($id)
183 {
184         $_SESSION['transfer_items']->remove_from_cart($id);
185         line_start_focus();
186 }
187
188 //-----------------------------------------------------------------------------------------------
189
190 function handle_new_item()
191 {
192         if (!check_item_data())
193                 return;
194         if (!isset($_POST['std_cost']))
195                 $_POST['std_cost'] = 0;
196         add_to_order($_SESSION['transfer_items'], $_POST['stock_id'], input_num('qty'), $_POST['std_cost']);
197         line_start_focus();
198 }
199
200 //-----------------------------------------------------------------------------------------------
201 $id = find_submit('Delete');
202 if ($id != -1)
203         handle_delete_item($id);
204         
205 if (isset($_POST['AddItem']))
206         handle_new_item();
207
208 if (isset($_POST['UpdateItem']))
209         handle_update_item();
210
211 if (isset($_POST['CancelItemChanges'])) {
212         line_start_focus();
213 }
214 //-----------------------------------------------------------------------------------------------
215
216 if (isset($_GET['NewTransfer']) || !isset($_SESSION['transfer_items']))
217 {
218         handle_new_order();
219 }
220
221 //-----------------------------------------------------------------------------------------------
222 start_form(false, true);
223
224 display_order_header($_SESSION['transfer_items']);
225
226 start_table("$table_style width=70%", 10);
227 start_row();
228 echo "<td>";
229 display_transfer_items(_("Items"), $_SESSION['transfer_items']);
230 transfer_options_controls();
231 echo "</td>";
232 end_row();
233 end_table(1);
234
235 submit_center_first('Update', _("Update"), '', null);
236 submit_center_last('Process', _("Process Transfer"), '', true);
237
238 end_form();
239 end_page();
240
241 ?>