Release 1.0.1 established on SourceForge, fixing the bugs and including a Date Picker...
[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
46 function copy_to_st()
47 {
48         $_SESSION['adj_items']->from_loc = $_POST['StockLocation'];
49         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];
50         $_SESSION['adj_items']->transfer_type = $_POST['type'];
51         $_SESSION['adj_items']->increase = $_POST['Increase'];
52         $_SESSION['adj_items']->memo_ = $_POST['memo_'];
53 }
54
55 //--------------------------------------------------------------------------------------------------
56
57 function copy_from_st()
58 {
59         $_POST['StockLocation'] = $_SESSION['adj_items']->from_loc;
60         $_POST['AdjDate'] = $_SESSION['adj_items']->tran_date;
61         $_POST['type'] = $_SESSION['adj_items']->transfer_type;
62         $_POST['Increase'] = $_SESSION['adj_items']->increase;
63         $_POST['memo_'] = $_SESSION['adj_items']->memo_;
64 }
65
66 //-----------------------------------------------------------------------------------------------
67
68 function handle_new_order()
69 {
70         if (isset($_SESSION['adj_items']))
71         {
72                 $_SESSION['adj_items']->clear_items();
73                 unset ($_SESSION['adj_items']);
74         }
75
76     session_register("adj_items");
77
78     $_SESSION['adj_items'] = new items_cart;
79         $_POST['AdjDate'] = Today();
80         if (!is_date_in_fiscalyear($_POST['AdjDate']))
81                 $_POST['AdjDate'] = end_fiscalyear();
82         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
83 }
84
85 //-----------------------------------------------------------------------------------------------
86
87 function can_process()
88 {
89         if (!references::is_valid($_POST['ref'])) 
90         {
91                 display_error( _("You must enter a reference."));
92                 return false;
93         }
94
95         if (!is_new_reference($_POST['ref'], systypes::inventory_adjustment())) 
96         {
97                 display_error( _("The entered reference is already in use."));
98                 return false;
99         }
100
101         if (!is_date($_POST['AdjDate'])) 
102         {
103                 display_error(_("The entered date for the adjustment is invalid."));
104                 return false;
105         } 
106         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
107         {
108                 display_error(_("The entered date is not in fiscal year."));
109                 return false;
110         }
111         $failed_item = $_SESSION['adj_items']->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
112         if ($failed_item != null) 
113         {
114         display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
115                 " " . $failed_item->stock_id . " - " .  $failed_item->item_description);
116                 return false;
117         }
118
119         return true;
120 }
121
122 //-------------------------------------------------------------------------------
123
124 if (isset($_POST['Process']) && can_process()){
125
126         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
127                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['type'], $_POST['Increase'],
128                 $_POST['ref'], $_POST['memo_']);
129
130         $_SESSION['adj_items']->clear_items();
131         unset($_SESSION['adj_items']);
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 (!is_numeric($_POST['qty']))
140         {
141                 display_error(_("The quantity entered is not a valid number."));
142                 return false;
143         }
144
145         if ($_POST['qty'] <= 0)
146         {
147                 display_error(_("The quantity entered must be greater than zero."));
148                 return false;
149         }
150
151         if (!is_numeric($_POST['std_cost']) || $_POST['std_cost'] < 0)
152         {
153                 display_error(_("The entered standard cost is negative or invalid."));
154                 return false;
155         }
156
157         return true;
158 }
159
160 //-----------------------------------------------------------------------------------------------
161
162 function handle_update_item()
163 {
164     if($_POST['UpdateItem'] != "" && check_item_data())
165     {
166         $_SESSION['adj_items']->update_cart_item($_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
167     }
168 }
169
170 //-----------------------------------------------------------------------------------------------
171
172 function handle_delete_item()
173 {
174         $_SESSION['adj_items']->remove_from_cart($_GET['Delete']);
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'], $_POST['qty'], $_POST['std_cost']);
185 }
186
187 //-----------------------------------------------------------------------------------------------
188
189 if (isset($_GET['Delete']) || isset($_GET['Edit']))
190         copy_from_st();
191
192 if (isset($_GET['Delete']))
193         handle_delete_item();
194
195 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
196         copy_to_st();
197         
198 if (isset($_POST['AddItem']))
199         handle_new_item();
200
201 if (isset($_POST['UpdateItem']))
202         handle_update_item();
203
204 //-----------------------------------------------------------------------------------------------
205
206 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
207 {
208         handle_new_order();
209 }
210
211 //-----------------------------------------------------------------------------------------------
212
213 start_form(false, true);
214
215 display_order_header($_SESSION['adj_items']);
216
217 start_table("$table_style width=70%", 10);
218 start_row();
219 echo "<TD>";
220 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
221 adjustment_options_controls();
222 echo "</TD>";
223 end_row();
224 end_table(1);
225
226 start_table();
227 start_row();
228 submit_cells('Update', _("Update"));
229 if ($_SESSION['adj_items']->count_items() >= 1)
230 {
231     submit_cells('Process', _("Process Adjustment"));
232 }
233 end_row();
234 end_table();
235
236 end_form();
237 end_page();
238
239 ?>