Ajax additions
[fa-stable.git] / inventory / adjustments.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/item_adjustments_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(_("Item Adjustments Note"), false, false, "", $js);
20
21 //-----------------------------------------------------------------------------------------------
22
23 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
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::inventory_adjustment();
33
34         display_notification_centered(_("Items adjustment has been processed"));
35         display_note(get_trans_view_str($trans_type, $trans_no, _("View this adjustment")));
36
37         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Adjustment")), 1, 0);
38
39         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Adjustment"));
40
41         display_footer_exit();
42 }
43 //--------------------------------------------------------------------------------------------------
44
45 function line_start_focus() {
46   global        $Ajax;
47
48   $Ajax->activate('items_table');
49   set_focus('_stock_id_edit');
50 }
51 //--------------------------------------------------------------------------------------------------
52
53 function copy_to_st()
54 {
55         $_SESSION['adj_items']->from_loc = $_POST['StockLocation'];
56         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];
57         $_SESSION['adj_items']->transfer_type = $_POST['type'];
58         $_SESSION['adj_items']->increase = $_POST['Increase'];
59         $_SESSION['adj_items']->memo_ = $_POST['memo_'];
60 }
61 //--------------------------------------------------------------------------------------------------
62
63 function copy_from_st()
64 {
65         $_POST['StockLocation'] = $_SESSION['adj_items']->from_loc;
66         $_POST['AdjDate'] = $_SESSION['adj_items']->tran_date;
67         $_POST['type'] = $_SESSION['adj_items']->transfer_type;
68         $_POST['Increase'] = $_SESSION['adj_items']->increase;
69         $_POST['memo_'] = $_SESSION['adj_items']->memo_;
70 }
71
72 //-----------------------------------------------------------------------------------------------
73
74 function handle_new_order()
75 {
76         if (isset($_SESSION['adj_items']))
77         {
78                 $_SESSION['adj_items']->clear_items();
79                 unset ($_SESSION['adj_items']);
80         }
81
82     session_register("adj_items");
83
84     $_SESSION['adj_items'] = new items_cart;
85         $_POST['AdjDate'] = Today();
86         if (!is_date_in_fiscalyear($_POST['AdjDate']))
87                 $_POST['AdjDate'] = end_fiscalyear();
88         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
89 }
90
91 //-----------------------------------------------------------------------------------------------
92
93 function can_process()
94 {
95         $adj = &$_SESSION['adj_items'];
96
97         if (count($adj->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                 return false;
107         }
108
109         if (!is_new_reference($_POST['ref'], systypes::inventory_adjustment())) 
110         {
111                 display_error( _("The entered reference is already in use."));
112                 set_focus('ref');
113                 return false;
114         }
115
116         if (!is_date($_POST['AdjDate'])) 
117         {
118                 display_error(_("The entered date for the adjustment is invalid."));
119                 set_focus('AdjDate');
120                 return false;
121         } 
122         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
123         {
124                 display_error(_("The entered date is not in fiscal year."));
125                 set_focus('AdjDate');
126                 return false;
127         } else {
128                 $failed_item = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
129                 if ($failed_item >= 0) 
130                 {
131                         $line = $adj->line_items[$failed_item];
132                 display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
133                         " " . $line->stock_id . " - " .  $line->item_description);
134                         $_POST['Edit'.$failed_item] = 1; // enter edit mode
135                         unset($_POST['Process']);
136                 return false;
137                 }
138         }
139         return true;
140 }
141
142 //-------------------------------------------------------------------------------
143
144 if (isset($_POST['Process']) && can_process()){
145
146         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
147                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['type'], $_POST['Increase'],
148                 $_POST['ref'], $_POST['memo_']);
149
150         $_SESSION['adj_items']->clear_items();
151         unset($_SESSION['adj_items']);
152
153         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
154 } /*end of process credit note */
155
156 //-----------------------------------------------------------------------------------------------
157
158 function check_item_data()
159 {
160         if (!check_num('qty',0))
161         {
162                 display_error(_("The quantity entered is negative or invalid."));
163                 set_focus('qty');
164                 return false;
165         }
166
167         if (!check_num('std_cost', 0))
168         {
169                 display_error(_("The entered standard cost is negative or invalid."));
170                 set_focus('std_cost');
171                 return false;
172         }
173
174         return true;
175 }
176
177 //-----------------------------------------------------------------------------------------------
178
179 function handle_update_item()
180 {
181     if($_POST['UpdateItem'] != "" && check_item_data())
182     {
183                 $id = $_POST['LineNo'];
184         $_SESSION['adj_items']->update_cart_item($id, input_num('qty'), 
185                         input_num('std_cost'));
186     }
187         line_start_focus();
188 }
189
190 //-----------------------------------------------------------------------------------------------
191
192 function handle_delete_item($id)
193 {
194         $_SESSION['adj_items']->remove_from_cart($id);
195         line_start_focus();
196 }
197
198 //-----------------------------------------------------------------------------------------------
199
200 function handle_new_item()
201 {
202         if (!check_item_data())
203                 return;
204
205         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], 
206           input_num('qty'), input_num('std_cost'));
207         line_start_focus();
208 }
209
210 //-----------------------------------------------------------------------------------------------
211 $id = find_submit('Delete');
212 if ($id != -1)
213         handle_delete_item($id);
214
215 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem'])) {
216         copy_to_st();
217         line_start_focus();
218 }
219
220 if (isset($_POST['AddItem']))
221         handle_new_item();
222
223 if (isset($_POST['UpdateItem']))
224         handle_update_item();
225
226 if (isset($_POST['CancelItemChanges'])) {
227         line_start_focus();
228 }
229 //-----------------------------------------------------------------------------------------------
230
231 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
232 {
233         handle_new_order();
234 }
235
236 //-----------------------------------------------------------------------------------------------
237 start_form(false, true);
238
239 display_order_header($_SESSION['adj_items']);
240
241 start_table("$table_style width=70%", 10);
242 start_row();
243 echo "<TD>";
244 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
245 adjustment_options_controls();
246 echo "</TD>";
247 end_row();
248 end_table(1);
249
250 submit_center_first('Update', _("Update"), '', true);
251 submit_center_last('Process', _("Process Transfer"), '', true);
252
253 end_form();
254 end_page();
255
256 ?>