Added trans_type parameter to items_cart()
[fa-stable.git] / inventory / transfers.php
1 <?php
2
3 $page_security = 3;
4 $path_to_root="..";
5 include_once($path_to_root . "/includes/ui/items_cart.inc");
6
7 include_once($path_to_root . "/includes/session.inc");
8
9 include_once($path_to_root . "/includes/date_functions.inc");
10 include_once($path_to_root . "/includes/data_checks.inc");
11
12 include_once($path_to_root . "/inventory/includes/stock_transfers_ui.inc");
13 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
14 $js = "";
15 if ($use_popup_windows)
16         $js .= get_js_open_window(800, 500);
17 if ($use_date_picker)
18         $js .= get_js_date_picker();
19 page(_("Inventory Location Transfers"), false, false, "", $js);
20
21 //-----------------------------------------------------------------------------------------------
22
23 check_db_has_costable_items(_("There are no inventory items defined in the system (Purchased or manufactured items)."));
24
25 check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));
26
27 //-----------------------------------------------------------------------------------------------
28
29 if (isset($_GET['AddedID'])) 
30 {
31         $trans_no = $_GET['AddedID'];
32         $trans_type = systypes::location_transfer();
33
34         display_notification_centered(_("Inventory transfer has been processed"));
35         display_note(get_trans_view_str($trans_type, $trans_no, _("View this transfer")));
36
37         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Inventory Transfer"));
38
39         display_footer_exit();
40 }
41 //--------------------------------------------------------------------------------------------------
42
43 function line_start_focus() {
44   global        $Ajax;
45
46   $Ajax->activate('items_table');
47   set_focus('_stock_id_edit');
48 }
49 //--------------------------------------------------------------------------------------------------
50
51 function copy_to_st()
52 {
53         $_SESSION['transfer_items']->from_loc = $_POST['FromStockLocation'];
54         $_SESSION['transfer_items']->to_loc = $_POST['ToStockLocation'];
55         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];
56         $_SESSION['transfer_items']->transfer_type = $_POST['type'];
57         $_SESSION['transfer_items']->memo_ = $_POST['memo_'];
58 }
59 //--------------------------------------------------------------------------------------------------
60
61 function copy_from_st()
62 {
63         $_POST['FromStockLocation'] = $_SESSION['transfer_items']->from_loc;
64         $_POST['ToStockLocation'] = $_SESSION['transfer_items']->to_loc;
65         $_POST['AdjDate'] = $_SESSION['transfer_items']->tran_date;
66         $_POST['type'] = $_SESSION['transfer_items']->transfer_type;
67         $_POST['memo_'] = $_SESSION['transfer_items']->memo_;
68 }
69
70 //-----------------------------------------------------------------------------------------------
71
72 function handle_new_order()
73 {
74         if (isset($_SESSION['transfer_items']))
75         {
76                 $_SESSION['transfer_items']->clear_items();
77                 unset ($_SESSION['transfer_items']);
78         }
79
80     session_register("transfer_items");
81
82         $_SESSION['transfer_items'] = new items_cart(systypes::location_transfer());
83         $_POST['AdjDate'] = Today();
84         if (!is_date_in_fiscalyear($_POST['AdjDate']))
85                 $_POST['AdjDate'] = end_fiscalyear();
86         $_SESSION['transfer_items']->tran_date = $_POST['AdjDate'];     
87 }
88
89 //-----------------------------------------------------------------------------------------------
90
91 if (isset($_POST['Process']))
92 {
93
94         $tr = &$_SESSION['transfer_items'];
95         $input_error = 0;
96
97         if (count($tr->line_items) == 0)        {
98                 display_error(_("You must enter at least one non empty item line."));
99                 set_focus('stock_id');
100                 return false;
101         }
102         if (!references::is_valid($_POST['ref'])) 
103         {
104                 display_error(_("You must enter a reference."));
105                 set_focus('ref');
106                 $input_error = 1;
107         } 
108         elseif (!is_new_reference($_POST['ref'], systypes::location_transfer())) 
109         {
110                 display_error(_("The entered reference is already in use."));
111                 set_focus('ref');
112                 $input_error = 1;
113         } 
114         elseif (!is_date($_POST['AdjDate'])) 
115         {
116                 display_error(_("The entered date for the adjustment is invalid."));
117                 set_focus('AdjDate');
118                 $input_error = 1;
119         } 
120         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
121         {
122                 display_error(_("The entered date is not in fiscal year."));
123                 set_focus('AdjDate');
124                 $input_error = 1;
125         } 
126         elseif ($_POST['FromStockLocation'] == $_POST['ToStockLocation'])
127         {
128                 display_error(_("The locations to transfer from and to must be different."));
129                 set_focus('FromStockLocation');
130                 $input_error = 1;
131         } 
132         else 
133         {
134                 $failed_item = $tr->check_qoh($_POST['FromStockLocation'], $_POST['AdjDate'], true);
135                 if ($failed_item >= 0) 
136                 {
137                         $line = $tr->line_items[$failed_item];
138                 display_error(_("The quantity entered is greater than the available quantity for this item at the source location :") .
139                         " " . $line->stock_id . " - " .  $line->item_description);
140                 echo "<br>";
141                         $_POST['Edit'.$failed_item] = 1; // enter edit mode
142                         $input_error = 1;
143                 }
144         }
145
146         if ($input_error == 1)
147                 unset($_POST['Process']);
148 }
149
150 //-------------------------------------------------------------------------------
151
152 if (isset($_POST['Process']))
153 {
154
155         $trans_no = add_stock_transfer($_SESSION['transfer_items']->line_items,
156                 $_POST['FromStockLocation'], $_POST['ToStockLocation'],
157                 $_POST['AdjDate'], $_POST['type'], $_POST['ref'], $_POST['memo_']);
158
159         $_SESSION['transfer_items']->clear_items();
160         unset($_SESSION['transfer_items']);
161
162         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
163 } /*end of process credit note */
164
165 //-----------------------------------------------------------------------------------------------
166
167 function check_item_data()
168 {
169         if (!check_num('qty', 0))
170         {
171                 display_error(_("The quantity entered must be a positive number."));
172                 set_focus('qty');
173                 return false;
174         }
175         return true;
176 }
177
178 //-----------------------------------------------------------------------------------------------
179
180 function handle_update_item()
181 {
182     if($_POST['UpdateItem'] != "" && check_item_data())
183     {
184                 $id = $_POST['LineNo'];
185         if (!isset($_POST['std_cost']))
186                 $_POST['std_cost'] = $_SESSION['transfer_items']->line_items[$id]->standard_cost;
187         $_SESSION['transfer_items']->update_cart_item($id, input_num('qty'), $_POST['std_cost']);
188     }
189         line_start_focus();
190 }
191
192 //-----------------------------------------------------------------------------------------------
193
194 function handle_delete_item($id)
195 {
196         $_SESSION['transfer_items']->remove_from_cart($id);
197         line_start_focus();
198 }
199
200 //-----------------------------------------------------------------------------------------------
201
202 function handle_new_item()
203 {
204         if (!check_item_data())
205                 return;
206         if (!isset($_POST['std_cost']))
207                 $_POST['std_cost'] = 0;
208         add_to_order($_SESSION['transfer_items'], $_POST['stock_id'], input_num('qty'), $_POST['std_cost']);
209         line_start_focus();
210 }
211
212 //-----------------------------------------------------------------------------------------------
213 $id = find_submit('Delete');
214 if ($id != -1)
215         handle_delete_item($id);
216
217 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem'])) {
218         copy_to_st();
219         line_start_focus();
220 }
221         
222 if (isset($_POST['AddItem']))
223         handle_new_item();
224
225 if (isset($_POST['UpdateItem']))
226         handle_update_item();
227
228 if (isset($_POST['CancelItemChanges'])) {
229         line_start_focus();
230 }
231 //-----------------------------------------------------------------------------------------------
232
233 if (isset($_GET['NewTransfer']) || !isset($_SESSION['transfer_items']))
234 {
235         handle_new_order();
236 }
237
238 //-----------------------------------------------------------------------------------------------
239 start_form(false, true);
240
241 display_order_header($_SESSION['transfer_items']);
242
243 start_table("$table_style width=70%", 10);
244 start_row();
245 echo "<td>";
246 display_transfer_items(_("Items"), $_SESSION['transfer_items']);
247 transfer_options_controls();
248 echo "</td>";
249 end_row();
250 end_table(1);
251
252 submit_center_first('Update', _("Update"), '', null);
253 submit_center_last('Process', _("Process Transfer"), '', true);
254
255 end_form();
256 end_page();
257
258 ?>