*** empty log message ***
[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 page(_("Item Adjustments Note"), false, false, "", $js);
18
19 //-----------------------------------------------------------------------------------------------
20
21 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
22
23 check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));
24
25 //-----------------------------------------------------------------------------------------------
26
27 if (isset($_GET['AddedID'])) 
28 {
29         $trans_no = $_GET['AddedID'];
30         $trans_type = systypes::inventory_adjustment();
31
32         display_notification_centered(_("Items adjustment has been processed"));
33         display_note(get_trans_view_str($trans_type, $trans_no, _("View this adjustment")));
34
35         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL Postings for this Adjustment")), 1, 0);
36
37         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter Another Adjustment"));
38
39         display_footer_exit();
40 }
41
42 //--------------------------------------------------------------------------------------------------
43
44 function copy_to_st()
45 {
46         $_SESSION['adj_items']->from_loc = $_POST['StockLocation'];
47         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];
48         $_SESSION['adj_items']->transfer_type = $_POST['type'];
49         $_SESSION['adj_items']->increase = $_POST['Increase'];
50         $_SESSION['adj_items']->memo_ = $_POST['memo_'];
51 }
52
53 //--------------------------------------------------------------------------------------------------
54
55 function copy_from_st()
56 {
57         $_POST['StockLocation'] = $_SESSION['adj_items']->from_loc;
58         $_POST['AdjDate'] = $_SESSION['adj_items']->tran_date;
59         $_POST['type'] = $_SESSION['adj_items']->transfer_type;
60         $_POST['Increase'] = $_SESSION['adj_items']->increase;
61         $_POST['memo_'] = $_SESSION['adj_items']->memo_;
62 }
63
64 //-----------------------------------------------------------------------------------------------
65
66 function handle_new_order()
67 {
68         if (isset($_SESSION['adj_items']))
69         {
70                 $_SESSION['adj_items']->clear_items();
71                 unset ($_SESSION['adj_items']);
72         }
73
74     session_register("adj_items");
75
76     $_SESSION['adj_items'] = new items_cart;
77         $_POST['AdjDate'] = Today();
78         if (!is_date_in_fiscalyear($_POST['AdjDate']))
79                 $_POST['AdjDate'] = end_fiscalyear();
80         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
81 }
82
83 //-----------------------------------------------------------------------------------------------
84
85 function can_process()
86 {
87         if (!references::is_valid($_POST['ref'])) 
88         {
89                 display_error( _("You must enter a reference."));
90                 return false;
91         }
92
93         if (!is_new_reference($_POST['ref'], systypes::inventory_adjustment())) 
94         {
95                 display_error( _("The entered reference is already in use."));
96                 return false;
97         }
98
99         if (!is_date($_POST['AdjDate'])) 
100         {
101                 display_error(_("The entered date for the adjustment is invalid."));
102                 return false;
103         } 
104         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
105         {
106                 display_error(_("The entered date is not in fiscal year."));
107                 return false;
108         }
109         $failed_item = $_SESSION['adj_items']->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
110         if ($failed_item != null) 
111         {
112         display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
113                 " " . $failed_item->stock_id . " - " .  $failed_item->item_description);
114                 return false;
115         }
116
117         return true;
118 }
119
120 //-------------------------------------------------------------------------------
121
122 if (isset($_POST['Process']) && can_process()){
123
124         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
125                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['type'], $_POST['Increase'],
126                 $_POST['ref'], $_POST['memo_']);
127
128         $_SESSION['adj_items']->clear_items();
129         unset($_SESSION['adj_items']);
130         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
131 } /*end of process credit note */
132
133 //-----------------------------------------------------------------------------------------------
134
135 function check_item_data()
136 {
137         if (!is_numeric($_POST['qty']))
138         {
139                 display_error(_("The quantity entered is not a valid number."));
140                 return false;
141         }
142
143         if ($_POST['qty'] <= 0)
144         {
145                 display_error(_("The quantity entered must be greater than zero."));
146                 return false;
147         }
148
149         if (!is_numeric($_POST['std_cost']) || $_POST['std_cost'] < 0)
150         {
151                 display_error(_("The entered standard cost is negative or invalid."));
152                 return false;
153         }
154
155         return true;
156 }
157
158 //-----------------------------------------------------------------------------------------------
159
160 function handle_update_item()
161 {
162     if($_POST['UpdateItem'] != "" && check_item_data())
163     {
164         $_SESSION['adj_items']->update_cart_item($_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
165     }
166 }
167
168 //-----------------------------------------------------------------------------------------------
169
170 function handle_delete_item()
171 {
172         $_SESSION['adj_items']->remove_from_cart($_GET['Delete']);
173 }
174
175 //-----------------------------------------------------------------------------------------------
176
177 function handle_new_item()
178 {
179         if (!check_item_data())
180                 return;
181
182         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], $_POST['qty'], $_POST['std_cost']);
183 }
184
185 //-----------------------------------------------------------------------------------------------
186
187 if (isset($_GET['Delete']) || isset($_GET['Edit']))
188         copy_from_st();
189
190 if (isset($_GET['Delete']))
191         handle_delete_item();
192
193 if (isset($_POST['AddItem']) || isset($_POST['UpdateItem']))
194         copy_to_st();
195         
196 if (isset($_POST['AddItem']))
197         handle_new_item();
198
199 if (isset($_POST['UpdateItem']))
200         handle_update_item();
201
202 //-----------------------------------------------------------------------------------------------
203
204 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
205 {
206         handle_new_order();
207 }
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 start_table();
225 start_row();
226 submit_cells('Update', _("Update"));
227 if ($_SESSION['adj_items']->count_items() >= 1)
228 {
229     submit_cells('Process', _("Process Adjustment"));
230 }
231 end_row();
232 end_table();
233
234 end_form();
235 end_page();
236
237 ?>