Merged latest changes from stable branch.
[fa-stable.git] / inventory / adjustments.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_INVENTORYADJUSTMENT';
13 $path_to_root = "..";
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
15
16 include_once($path_to_root . "/includes/session.inc");
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
20
21 include_once($path_to_root . "/inventory/includes/item_adjustments_ui.inc");
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
23 $js = "";
24 if ($use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if ($use_date_picker)
27         $js .= get_js_date_picker();
28 page(_($help_context = "Item Adjustments Note"), false, false, "", $js);
29
30 //-----------------------------------------------------------------------------------------------
31
32 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
33
34 check_db_has_movement_types(_("There are no inventory movement types defined in the system. Please define at least one inventory adjustment type."));
35
36 //-----------------------------------------------------------------------------------------------
37
38 if (isset($_GET['AddedID'])) 
39 {
40         $trans_no = $_GET['AddedID'];
41         $trans_type = ST_INVADJUST;
42
43         display_notification_centered(_("Items adjustment has been processed"));
44         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this adjustment")));
45
46         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Adjustment")), 1, 0);
47
48         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Adjustment"));
49
50         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
51
52         display_footer_exit();
53 }
54 //--------------------------------------------------------------------------------------------------
55
56 function line_start_focus() {
57   global        $Ajax;
58
59   $Ajax->activate('items_table');
60   set_focus('_stock_id_edit');
61 }
62 //-----------------------------------------------------------------------------------------------
63
64 function handle_new_order()
65 {
66         if (isset($_SESSION['adj_items']))
67         {
68                 $_SESSION['adj_items']->clear_items();
69                 unset ($_SESSION['adj_items']);
70         }
71
72     //session_register("adj_items");
73
74     $_SESSION['adj_items'] = new items_cart(ST_INVADJUST);
75         $_POST['AdjDate'] = new_doc_date();
76         if (!is_date_in_fiscalyear($_POST['AdjDate']))
77                 $_POST['AdjDate'] = end_fiscalyear();
78         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
79 }
80
81 //-----------------------------------------------------------------------------------------------
82
83 function can_process()
84 {
85         global $Refs;
86
87         $adj = &$_SESSION['adj_items'];
88
89         if (count($adj->line_items) == 0)       {
90                 display_error(_("You must enter at least one non empty item line."));
91                 set_focus('stock_id');
92                 return false;
93         }
94         if (!$Refs->is_valid($_POST['ref'])) 
95         {
96                 display_error( _("You must enter a reference."));
97                 set_focus('ref');
98                 return false;
99         }
100
101         if (!is_new_reference($_POST['ref'], ST_INVADJUST)) 
102         {
103                 display_error( _("The entered reference is already in use."));
104                 set_focus('ref');
105                 return false;
106         }
107
108         if (!is_date($_POST['AdjDate'])) 
109         {
110                 display_error(_("The entered date for the adjustment is invalid."));
111                 set_focus('AdjDate');
112                 return false;
113         } 
114         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
115         {
116                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
117                 set_focus('AdjDate');
118                 return false;
119         } else {
120                 $failed_item = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
121                 if ($failed_item >= 0) 
122                 {
123                         $line = $adj->line_items[$failed_item];
124                 display_error(_("The adjustment cannot be processed because an adjustment item would cause a negative inventory balance :") .
125                         " " . $line->stock_id . " - " .  $line->item_description);
126                         $_POST['Edit'.$failed_item] = 1; // enter edit mode
127                         unset($_POST['Process']);
128                 return false;
129                 }
130         }
131         return true;
132 }
133
134 //-------------------------------------------------------------------------------
135
136 if (isset($_POST['Process']) && can_process()){
137
138         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
139                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['type'], $_POST['Increase'],
140                 $_POST['ref'], $_POST['memo_']);
141         new_doc_date($_POST['AdjDate']);
142         $_SESSION['adj_items']->clear_items();
143         unset($_SESSION['adj_items']);
144
145         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
146 } /*end of process credit note */
147
148 //-----------------------------------------------------------------------------------------------
149
150 function check_item_data()
151 {
152         if (!check_num('qty',0))
153         {
154                 display_error(_("The quantity entered is negative or invalid."));
155                 set_focus('qty');
156                 return false;
157         }
158
159         if (!check_num('std_cost', 0))
160         {
161                 display_error(_("The entered standard cost is negative or invalid."));
162                 set_focus('std_cost');
163                 return false;
164         }
165
166         return true;
167 }
168
169 //-----------------------------------------------------------------------------------------------
170
171 function handle_update_item()
172 {
173     if($_POST['UpdateItem'] != "" && check_item_data())
174     {
175                 $id = $_POST['LineNo'];
176         $_SESSION['adj_items']->update_cart_item($id, input_num('qty'), 
177                         input_num('std_cost'));
178     }
179         line_start_focus();
180 }
181
182 //-----------------------------------------------------------------------------------------------
183
184 function handle_delete_item($id)
185 {
186         $_SESSION['adj_items']->remove_from_cart($id);
187         line_start_focus();
188 }
189
190 //-----------------------------------------------------------------------------------------------
191
192 function handle_new_item()
193 {
194         if (!check_item_data())
195                 return;
196
197         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], 
198           input_num('qty'), input_num('std_cost'));
199         line_start_focus();
200 }
201
202 //-----------------------------------------------------------------------------------------------
203 $id = find_submit('Delete');
204 if ($id != -1)
205         handle_delete_item($id);
206
207 if (isset($_POST['AddItem']))
208         handle_new_item();
209
210 if (isset($_POST['UpdateItem']))
211         handle_update_item();
212
213 if (isset($_POST['CancelItemChanges'])) {
214         line_start_focus();
215 }
216 //-----------------------------------------------------------------------------------------------
217
218 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
219 {
220         handle_new_order();
221 }
222
223 //-----------------------------------------------------------------------------------------------
224 start_form();
225
226 display_order_header($_SESSION['adj_items']);
227
228 start_outer_table(TABLESTYLE, "width=70%", 10);
229
230 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
231 adjustment_options_controls();
232
233 end_outer_table(1, false);
234
235 submit_center_first('Update', _("Update"), '', null);
236 submit_center_last('Process', _("Process Adjustment"), '', 'default');
237
238 end_form();
239 end_page();
240
241 ?>