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 . "/inventory/includes/item_adjustments_ui.inc");
22 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
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);
30 //-----------------------------------------------------------------------------------------------
32 check_db_has_costable_items(_("There are no inventory items defined in the system which can be adjusted (Purchased or Manufactured)."));
34 //-----------------------------------------------------------------------------------------------
36 if (isset($_GET['AddedID']))
38 $trans_no = $_GET['AddedID'];
39 $trans_type = ST_INVADJUST;
41 display_notification_centered(_("Items adjustment has been processed"));
42 display_note(get_trans_view_str($trans_type, $trans_no, _("&View this adjustment")));
44 display_note(get_gl_view_str($trans_type, $trans_no, _("View the GL &Postings for this Adjustment")), 1, 0);
46 hyperlink_no_params($_SERVER['PHP_SELF'], _("Enter &Another Adjustment"));
48 hyperlink_params("$path_to_root/admin/attachments.php", _("Add an Attachment"), "filterType=$trans_type&trans_no=$trans_no");
50 display_footer_exit();
52 //--------------------------------------------------------------------------------------------------
54 function line_start_focus() {
57 $Ajax->activate('items_table');
58 set_focus('_stock_id_edit');
60 //-----------------------------------------------------------------------------------------------
62 function handle_new_order()
64 if (isset($_SESSION['adj_items']))
66 $_SESSION['adj_items']->clear_items();
67 unset ($_SESSION['adj_items']);
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'];
77 //-----------------------------------------------------------------------------------------------
79 function can_process()
83 $adj = &$_SESSION['adj_items'];
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');
91 if (!check_reference($_POST['ref'], ST_INVADJUST))
97 if (!is_date($_POST['AdjDate']))
99 display_error(_("The entered date for the adjustment is invalid."));
100 set_focus('AdjDate');
103 elseif (!is_date_in_fiscalyear($_POST['AdjDate']))
105 display_error(_("The entered date is out of fiscal year or is closed for further data entry."));
106 set_focus('AdjDate');
109 elseif (!$SysPrefs->allow_negative_stock())
111 $low_stock = $adj->check_qoh($_POST['StockLocation'], $_POST['AdjDate']);
115 display_error(_("The adjustment cannot be processed because it would cause negative inventory balance for marked items as of document date or later."));
116 unset($_POST['Process']);
123 //-------------------------------------------------------------------------------
125 if (isset($_POST['Process']) && can_process()){
127 $trans_no = add_stock_adjustment($_SESSION['adj_items']->line_items,
128 $_POST['StockLocation'], $_POST['AdjDate'], $_POST['ref'], $_POST['memo_']);
129 new_doc_date($_POST['AdjDate']);
130 $_SESSION['adj_items']->clear_items();
131 unset($_SESSION['adj_items']);
133 meta_forward($_SERVER['PHP_SELF'], "AddedID=$trans_no");
135 } /*end of process credit note */
137 //-----------------------------------------------------------------------------------------------
139 function check_item_data()
141 if (input_num('qty') == 0)
143 display_error(_("The quantity entered is invalid."));
148 if (!check_num('std_cost', 0))
150 display_error(_("The entered standard cost is negative or invalid."));
151 set_focus('std_cost');
158 //-----------------------------------------------------------------------------------------------
160 function handle_update_item()
162 $id = $_POST['LineNo'];
163 $_SESSION['adj_items']->update_cart_item($id, input_num('qty'),
164 input_num('std_cost'));
168 //-----------------------------------------------------------------------------------------------
170 function handle_delete_item($id)
172 $_SESSION['adj_items']->remove_from_cart($id);
176 //-----------------------------------------------------------------------------------------------
178 function handle_new_item()
180 add_to_order($_SESSION['adj_items'], $_POST['stock_id'],
181 input_num('qty'), input_num('std_cost'));
185 //-----------------------------------------------------------------------------------------------
186 $id = find_submit('Delete');
188 handle_delete_item($id);
190 if (isset($_POST['AddItem']) && check_item_data())
193 if (isset($_POST['UpdateItem']) && check_item_data())
194 handle_update_item();
196 if (isset($_POST['CancelItemChanges'])) {
199 //-----------------------------------------------------------------------------------------------
201 if (isset($_GET['NewAdjustment']) || !isset($_SESSION['adj_items']))
206 //-----------------------------------------------------------------------------------------------
209 display_order_header($_SESSION['adj_items']);
211 start_outer_table(TABLESTYLE, "width='70%'", 10);
213 display_adjustment_items(_("Adjustment Items"), $_SESSION['adj_items']);
214 adjustment_options_controls();
216 end_outer_table(1, false);
218 submit_center_first('Update', _("Update"), '', null);
219 submit_center_last('Process', _("Process Adjustment"), '', 'default');