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