[0004212] Work Order Entry: fixed error when voided WO refence is reused.
[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 . "/fixed_assets/includes/fixed_assets_db.inc");
22 include_once($path_to_root . "/inventory/includes/item_adjustments_ui.inc");
23 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
24 $js = "";
25 if ($SysPrefs->use_popup_windows)
26         $js .= get_js_open_window(800, 500);
27 if (user_use_date_picker())
28         $js .= get_js_date_picker();
29 if (isset($_GET['NewAdjustment'])) {
30         if (isset($_GET['FixedAsset'])) {
31                 $page_security = 'SA_ASSETDISPOSAL';
32                 $_SESSION['page_title'] = _($help_context = "Fixed Assets Disposal");
33         } else {
34                 $_SESSION['page_title'] = _($help_context = "Item Adjustments Note");
35         }
36 }
37 page($_SESSION['page_title'], false, false, "", $js);
38
39 //-----------------------------------------------------------------------------------------------
40
41 if (isset($_GET['AddedID'])) 
42 {
43         $trans_no = $_GET['AddedID'];
44         $trans_type = ST_INVADJUST;
45
46   $result = get_stock_adjustment_items($trans_no);
47   $row = db_fetch($result);
48
49   if (is_fixed_asset($row['mb_flag'])) {
50     display_notification_centered(_("Fixed Assets disposal has been processed"));
51     display_note(get_trans_view_str($trans_type, $trans_no, _("&View this disposal")));
52
53     display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Disposal")), 1, 0);
54           hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Disposal"), "NewAdjustment=1&FixedAsset=1");
55   }
56   else {
57     display_notification_centered(_("Items adjustment has been processed"));
58     display_note(get_trans_view_str($trans_type, $trans_no, _("&View this adjustment")));
59
60     display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Adjustment")), 1, 0);
61
62           hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Adjustment"), "NewAdjustment=1");
63   }
64
65         hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
66
67         display_footer_exit();
68 }
69 //--------------------------------------------------------------------------------------------------
70
71 function line_start_focus() {
72   global        $Ajax;
73
74   $Ajax->activate('items_table');
75   set_focus('_stock_id_edit');
76 }
77 //-----------------------------------------------------------------------------------------------
78
79 function handle_new_order()
80 {
81         if (isset($_SESSION['adj_items']))
82         {
83                 $_SESSION['adj_items']->clear_items();
84                 unset ($_SESSION['adj_items']);
85         }
86
87     $_SESSION['adj_items'] = new items_cart(ST_INVADJUST);
88     $_SESSION['adj_items']->fixed_asset = isset($_GET['FixedAsset']);
89         $_POST['AdjDate'] = new_doc_date();
90         if (!is_date_in_fiscalyear($_POST['AdjDate']))
91                 $_POST['AdjDate'] = end_fiscalyear();
92         $_SESSION['adj_items']->tran_date = $_POST['AdjDate'];  
93 }
94
95 //-----------------------------------------------------------------------------------------------
96
97 function can_process()
98 {
99         global $SysPrefs;
100
101         $adj = &$_SESSION['adj_items'];
102
103         if (count($adj->line_items) == 0)       {
104                 display_error(_("You must enter at least one non empty item line."));
105                 set_focus('stock_id');
106                 return false;
107         }
108
109         if (!check_reference($_POST['ref'], ST_INVADJUST))
110         {
111                 set_focus('ref');
112                 return false;
113         }
114
115         if (!is_date($_POST['AdjDate'])) 
116         {
117                 display_error(_("The entered date for the adjustment is invalid."));
118                 set_focus('AdjDate');
119                 return false;
120         } 
121         elseif (!is_date_in_fiscalyear($_POST['AdjDate'])) 
122         {
123                 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
124                 set_focus('AdjDate');
125                 return false;
126         }
127         elseif (!$SysPrefs->allow_negative_stock())
128         {
129                 $low_stock = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate']);
130
131                 if ($low_stock)
132                 {
133                 display_error(_("The adjustment cannot be processed because it would cause negative inventory balance for marked items as of document date or later."));
134                         unset($_POST['Process']);
135                         return false;
136                 }
137         }
138         return true;
139 }
140
141 //-------------------------------------------------------------------------------
142
143 if (isset($_POST['Process']) && can_process()){
144
145   $fixed_asset = $_SESSION['adj_items']->fixed_asset; 
146
147         $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
148                 $_POST['StockLocation'], $_POST['AdjDate'],     $_POST['ref'], $_POST['memo_']);
149         new_doc_date($_POST['AdjDate']);
150         $_SESSION['adj_items']->clear_items();
151         unset($_SESSION['adj_items']);
152
153   if ($fixed_asset)
154         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no&FixedAsset=1");
155   else
156         meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
157
158 } /*end of process credit note */
159
160 //-----------------------------------------------------------------------------------------------
161
162 function check_item_data()
163 {
164         if (input_num('qty') == 0)
165         {
166                 display_error(_("The quantity entered is invalid."));
167                 set_focus('qty');
168                 return false;
169         }
170
171         if (!check_num('std_cost', 0))
172         {
173                 display_error(_("The entered standard cost is negative or invalid."));
174                 set_focus('std_cost');
175                 return false;
176         }
177
178         return true;
179 }
180
181 //-----------------------------------------------------------------------------------------------
182
183 function handle_update_item()
184 {
185         $id = $_POST['LineNo'];
186         $_SESSION['adj_items']->update_cart_item($id, input_num('qty'), 
187                 input_num('std_cost'));
188         line_start_focus();
189 }
190
191 //-----------------------------------------------------------------------------------------------
192
193 function handle_delete_item($id)
194 {
195         $_SESSION['adj_items']->remove_from_cart($id);
196         line_start_focus();
197 }
198
199 //-----------------------------------------------------------------------------------------------
200
201 function handle_new_item()
202 {
203         add_to_order($_SESSION['adj_items'], $_POST['stock_id'], 
204         input_num('qty'), input_num('std_cost'));
205         line_start_focus();
206 }
207
208 //-----------------------------------------------------------------------------------------------
209 $id = find_submit('Delete');
210 if ($id != -1)
211         handle_delete_item($id);
212
213 if (isset($_POST['AddItem']) && check_item_data())
214         handle_new_item();
215
216 if (isset($_POST['UpdateItem']) && check_item_data())
217         handle_update_item();
218
219 if (isset($_POST['CancelItemChanges'])) {
220         line_start_focus();
221 }
222 //-----------------------------------------------------------------------------------------------
223
224 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
225 {
226
227         if (isset($_GET['FixedAsset']))
228                 check_db_has_disposable_fixed_assets(_("There are no fixed assets defined in the system."));
229         else
230                 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
231
232         handle_new_order();
233 }
234
235 //-----------------------------------------------------------------------------------------------
236 start_form();
237
238 if ($_SESSION['adj_items']->fixed_asset) {
239         $items_title = _("Disposal Items");
240         $button_title = _("Process Disposal");
241 } else {
242         $items_title = _("Adjustment Items");
243         $button_title = _("Process Adjustment");
244 }
245
246 display_order_header($_SESSION['adj_items']);
247
248 start_outer_table(TABLESTYLE, "width='70%'", 10);
249
250 display_adjustment_items($items_title, $_SESSION['adj_items']);
251 adjustment_options_controls();
252
253 end_outer_table(1, false);
254
255 submit_center_first('Update', _("Update"), '', null);
256 submit_center_last('Process', $button_title, '', 'default');
257
258 end_form();
259 end_page();
260