Moving 2.0 development version to main trunk.
[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 handle_new_order()
54 {
55         if (isset($_SESSION['adj_items']))
56         {
57                 $_SESSION['adj_items']->clear_items();
58                 unset ($_SESSION['adj_items']);
59         }
60
61     session_register("adj_items");
62
63     $_SESSION['adj_items'] = new items_cart(systypes::inventory_adjustment());
64         $_POST['AdjDate'] = Today();
65         if (!is_date_in_fiscalyear($_POST['AdjDate']))
66                 $_POST['AdjDate'] = end_fiscalyear();
67         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
68 }
69
70 //-----------------------------------------------------------------------------------------------
71
72 function can_process()
73 {
74         $adj = &$_SESSION['adj_items'];
75
76         if (count($adj->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                 return false;
86         }
87
88         if (!is_new_reference($_POST['ref'], systypes::inventory_adjustment())) 
89         {
90                 display_error( _("The entered reference is already in use."));
91                 set_focus('ref');
92                 return false;
93         }
94
95         if (!is_date($_POST['AdjDate'])) 
96         {
97                 display_error(_("The entered date for the adjustment is invalid."));
98                 set_focus('AdjDate');
99                 return false;
100         } 
101         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
102         {
103                 display_error(_("The entered date is not in fiscal year."));
104                 set_focus('AdjDate');
105                 return false;
106         } else {
107                 $failed_item = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
108                 if ($failed_item >= 0) 
109                 {
110                         $line = $adj->line_items[$failed_item];
111                 display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
112                         " " . $line->stock_id . " - " .  $line->item_description);
113                         $_POST['Edit'.$failed_item] = 1; // enter edit mode
114                         unset($_POST['Process']);
115                 return false;
116                 }
117         }
118         return true;
119 }
120
121 //-------------------------------------------------------------------------------
122
123 if (isset($_POST['Process']) && can_process()){
124
125         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
126                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['type'], $_POST['Increase'],
127                 $_POST['ref'], $_POST['memo_']);
128
129         $_SESSION['adj_items']->clear_items();
130         unset($_SESSION['adj_items']);
131
132         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
133 } /*end of process credit note */
134
135 //-----------------------------------------------------------------------------------------------
136
137 function check_item_data()
138 {
139         if (!check_num('qty',0))
140         {
141                 display_error(_("The quantity entered is negative or invalid."));
142                 set_focus('qty');
143                 return false;
144         }
145
146         if (!check_num('std_cost', 0))
147         {
148                 display_error(_("The entered standard cost is negative or invalid."));
149                 set_focus('std_cost');
150                 return false;
151         }
152
153         return true;
154 }
155
156 //-----------------------------------------------------------------------------------------------
157
158 function handle_update_item()
159 {
160     if($_POST['UpdateItem'] != "" && check_item_data())
161     {
162                 $id = $_POST['LineNo'];
163         $_SESSION['adj_items']->update_cart_item($id, input_num('qty'), 
164                         input_num('std_cost'));
165     }
166         line_start_focus();
167 }
168
169 //-----------------------------------------------------------------------------------------------
170
171 function handle_delete_item($id)
172 {
173         $_SESSION['adj_items']->remove_from_cart($id);
174         line_start_focus();
175 }
176
177 //-----------------------------------------------------------------------------------------------
178
179 function handle_new_item()
180 {
181         if (!check_item_data())
182                 return;
183
184         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], 
185           input_num('qty'), input_num('std_cost'));
186         line_start_focus();
187 }
188
189 //-----------------------------------------------------------------------------------------------
190 $id = find_submit('Delete');
191 if ($id != -1)
192         handle_delete_item($id);
193
194 if (isset($_POST['AddItem']))
195         handle_new_item();
196
197 if (isset($_POST['UpdateItem']))
198         handle_update_item();
199
200 if (isset($_POST['CancelItemChanges'])) {
201         line_start_focus();
202 }
203 //-----------------------------------------------------------------------------------------------
204
205 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
206 {
207         handle_new_order();
208 }
209
210 //-----------------------------------------------------------------------------------------------
211 start_form(false, true);
212
213 display_order_header($_SESSION['adj_items']);
214
215 start_table("$table_style width=70%", 10);
216 start_row();
217 echo "<TD>";
218 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
219 adjustment_options_controls();
220 echo "</TD>";
221 end_row();
222 end_table(1);
223
224 submit_center_first('Update', _("Update"), '', null);
225 submit_center_last('Process', _("Process Adjustment"), '', true);
226
227 end_form();
228 end_page();
229
230 ?>