bacfa377d9c0ffd2666c4e885a089084add3e0c2
[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 ($SysPrefs->use_popup_windows)
25         $js .= get_js_open_window(800, 500);
26 if (user_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 //-----------------------------------------------------------------------------------------------
35
36 if (isset($_GET['AddedID'])) 
37 {
38         $trans_no = $_GET['AddedID'];
39         $trans_type = ST_INVADJUST;
40
41         display_notification_centered(_("Items adjustment has been processed"));
42         display_note(get_trans_view_str($trans_type, $trans_no, _("&View this adjustment")));
43
44         display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Adjustment")), 1, 0);
45
46         hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Adjustment"));
47
48         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
49
50         display_footer_exit();
51 }
52 //--------------------------------------------------------------------------------------------------
53
54 function line_start_focus() {
55   global        $Ajax;
56
57   $Ajax->activate('items_table');
58   set_focus('_stock_id_edit');
59 }
60 //-----------------------------------------------------------------------------------------------
61
62 function handle_new_order()
63 {
64         if (isset($_SESSION['adj_items']))
65         {
66                 $_SESSION['adj_items']->clear_items();
67                 unset ($_SESSION['adj_items']);
68         }
69
70     $_SESSION['adj_items'] = new items_cart(ST_INVADJUST);
71         $_POST['AdjDate'] = new_doc_date();
72         if (!is_date_in_fiscalyear($_POST['AdjDate']))
73                 $_POST['AdjDate'] = end_fiscalyear();
74         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
75 }
76
77 //-----------------------------------------------------------------------------------------------
78
79 function can_process()
80 {
81         global $Refs, $SysPrefs;
82
83         $adj = &$_SESSION['adj_items'];
84
85         if (count($adj->line_items) == 0)       {
86                 display_error(_("You must enter at least one non empty item line."));
87                 set_focus('stock_id');
88                 return false;
89         }
90         if (!$Refs->is_valid($_POST['ref'])) 
91         {
92                 display_error( _("You must enter a reference."));
93                 set_focus('ref');
94                 return false;
95         }
96
97         if (!is_new_reference($_POST['ref'], ST_INVADJUST)) 
98         {
99                 display_error( _("The entered reference is already in use."));
100                 set_focus('ref');
101                 return false;
102         }
103
104         if (!is_date($_POST['AdjDate'])) 
105         {
106                 display_error(_("The entered date for the adjustment is invalid."));
107                 set_focus('AdjDate');
108                 return false;
109         } 
110         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
111         {
112                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
113                 set_focus('AdjDate');
114                 return false;
115         }
116         elseif (!$SysPrefs->allow_negative_stock())
117         {
118                 $low_stock = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate'], !$_POST['Increase']);
119
120                 if ($low_stock)
121                 {
122                 display_error(_("The adjustment cannot be processed because it would cause negative inventory balance for marked items as of document date or later."));
123                         unset($_POST['Process']);
124                         return false;
125                 }
126         }
127         return true;
128 }
129
130 //-------------------------------------------------------------------------------
131
132 if (isset($_POST['Process']) && can_process()){
133
134         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
135                 $_POST['StockLocation'], $_POST['AdjDate'], $_POST['Increase'],
136                 $_POST['ref'], $_POST['memo_']);
137         new_doc_date($_POST['AdjDate']);
138         $_SESSION['adj_items']->clear_items();
139         unset($_SESSION['adj_items']);
140
141         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
142
143 } /*end of process credit note */
144
145 //-----------------------------------------------------------------------------------------------
146
147 function check_item_data()
148 {
149         if (!check_num('qty',0) || input_num('qty') == 0)
150         {
151                 display_error(_("The quantity entered is negative or invalid."));
152                 set_focus('qty');
153                 return false;
154         }
155
156         if (!check_num('std_cost', 0))
157         {
158                 display_error(_("The entered standard cost is negative or invalid."));
159                 set_focus('std_cost');
160                 return false;
161         }
162
163         return true;
164 }
165
166 //-----------------------------------------------------------------------------------------------
167
168 function handle_update_item()
169 {
170         $id = $_POST['LineNo'];
171         $_SESSION['adj_items']->update_cart_item($id, input_num('qty'), 
172                 input_num('std_cost'));
173         line_start_focus();
174 }
175
176 //-----------------------------------------------------------------------------------------------
177
178 function handle_delete_item($id)
179 {
180         $_SESSION['adj_items']->remove_from_cart($id);
181         line_start_focus();
182 }
183
184 //-----------------------------------------------------------------------------------------------
185
186 function handle_new_item()
187 {
188         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], 
189           input_num('qty'), input_num('std_cost'));
190         line_start_focus();
191 }
192
193 //-----------------------------------------------------------------------------------------------
194 $id = find_submit('Delete');
195 if ($id != -1)
196         handle_delete_item($id);
197
198 if (isset($_POST['AddItem']) && check_item_data())
199         handle_new_item();
200
201 if (isset($_POST['UpdateItem']) && check_item_data())
202         handle_update_item();
203
204 if (isset($_POST['CancelItemChanges'])) {
205         line_start_focus();
206 }
207 //-----------------------------------------------------------------------------------------------
208
209 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
210 {
211         handle_new_order();
212 }
213
214 //-----------------------------------------------------------------------------------------------
215 start_form();
216
217 display_order_header($_SESSION['adj_items']);
218
219 start_outer_table(TABLESTYLE, "width='70%'", 10);
220
221 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
222 adjustment_options_controls();
223
224 end_outer_table(1, false);
225
226 submit_center_first('Update', _("Update"), '', null);
227 submit_center_last('Process', _("Process Adjustment"), '', 'default');
228
229 end_form();
230 end_page();
231