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';
14 include_once($path_to_root . "/includes/ui/items_cart.inc");
16 include_once($path_to_root . "/includes/session.inc");
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/includes/data_checks.inc");
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");
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");
34 $_SESSION['page_title'] = _($help_context = "Item Adjustments Note");
37 page($_SESSION['page_title'], false, false, "", $js);
39 //-----------------------------------------------------------------------------------------------
41 if (isset($_GET['AddedID']))
43 $trans_no = $_GET['AddedID'];
44 $trans_type = ST_INVADJUST;
46 $result = get_stock_adjustment_items($trans_no);
47 $row = db_fetch($result);
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")));
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");
57 display_notification_centered(_("Items adjustment has been processed"));
58 display_note(get_trans_view_str($trans_type, $trans_no, _("&View this adjustment")));
60 display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Adjustment")), 1, 0);
62 hyperlink_params($_SERVER['PHP_SELF'], _("Enter &Another Adjustment"), "NewAdjustment=1");
65 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
67 display_footer_exit();
69 //--------------------------------------------------------------------------------------------------
71 function line_start_focus() {
74 $Ajax->activate('items_table');
75 set_focus('_stock_id_edit');
77 //-----------------------------------------------------------------------------------------------
79 function handle_new_order()
81 if (isset($_SESSION['adj_items']))
83 $_SESSION['adj_items']->clear_items();
84 unset ($_SESSION['adj_items']);
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'];
95 //-----------------------------------------------------------------------------------------------
97 function can_process()
101 $adj = &$_SESSION['adj_items'];
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');
109 if (!check_reference($_POST['ref'], ST_INVADJUST))
115 if (!is_date($_POST['AdjDate']))
117 display_error(_("The entered date for the adjustment is invalid."));
118 set_focus('AdjDate');
121 elseif (!is_date_in_fiscalyear($_POST['AdjDate']))
123 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
124 set_focus('AdjDate');
127 elseif (!$SysPrefs->allow_negative_stock())
129 $low_stock = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate']);
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']);
141 //-------------------------------------------------------------------------------
143 if (isset($_POST['Process']) && can_process()){
145 $fixed_asset = $_SESSION['adj_items']->fixed_asset;
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']);
154 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no&FixedAsset=1");
156 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
158 } /*end of process credit note */
160 //-----------------------------------------------------------------------------------------------
162 function check_item_data()
164 if (input_num('qty') == 0)
166 display_error(_("The quantity entered is invalid."));
171 if (!check_num('std_cost', 0))
173 display_error(_("The entered standard cost is negative or invalid."));
174 set_focus('std_cost');
181 //-----------------------------------------------------------------------------------------------
183 function handle_update_item()
185 $id = $_POST['LineNo'];
186 $_SESSION['adj_items']->update_cart_item($id, input_num('qty'),
187 input_num('std_cost'));
191 //-----------------------------------------------------------------------------------------------
193 function handle_delete_item($id)
195 $_SESSION['adj_items']->remove_from_cart($id);
199 //-----------------------------------------------------------------------------------------------
201 function handle_new_item()
203 add_to_order($_SESSION['adj_items'], $_POST['stock_id'],
204 input_num('qty'), input_num('std_cost'));
208 //-----------------------------------------------------------------------------------------------
209 $id = find_submit('Delete');
211 handle_delete_item($id);
213 if (isset($_POST['AddItem']) && check_item_data())
216 if (isset($_POST['UpdateItem']) && check_item_data())
217 handle_update_item();
219 if (isset($_POST['CancelItemChanges'])) {
222 //-----------------------------------------------------------------------------------------------
224 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
227 if (isset($_GET['FixedAsset']))
228 check_db_has_disposable_fixed_assets(_("There are no fixed assets defined in the system."));
230 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
235 //-----------------------------------------------------------------------------------------------
238 if ($_SESSION['adj_items']->fixed_asset) {
239 $items_title = _("Disposal Items");
240 $button_title = _("Process Disposal");
242 $items_title = _("Adjustment Items");
243 $button_title = _("Process Adjustment");
246 display_order_header($_SESSION['adj_items']);
248 start_outer_table(TABLESTYLE, "width='70%'", 10);
250 display_adjustment_items($items_title, $_SESSION['adj_items']);
251 adjustment_options_controls();
253 end_outer_table(1, false);
255 submit_center_first('Update', _("Update"), '', null);
256 submit_center_last('Process', $button_title, '', 'default');