Workaround for segfaults in some buggy php encoding library versions.
[fa-stable.git] / inventory / manage / item_units.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_UOM';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Units of Measure"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19
20 include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
21
22 simple_page_mode(false);
23 //----------------------------------------------------------------------------------
24
25 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
26 {
27
28         //initialise no input errors assumed initially before we test
29         $input_error = 0;
30
31         if (strlen($_POST['abbr']) == 0)
32         {
33                 $input_error = 1;
34                 display_error(_("The unit of measure code cannot be empty."));
35                 set_focus('abbr');
36         }
37         if (strlen(db_escape($_POST['abbr']))>(20+2))
38         {
39                 $input_error = 1;
40                 display_error(_("The unit of measure code is too long."));
41                 set_focus('abbr');
42         }
43         if (strlen($_POST['description']) == 0)
44         {
45                 $input_error = 1;
46                 display_error(_("The unit of measure description cannot be empty."));
47                 set_focus('description');
48         }
49
50         if ($input_error !=1) {
51         write_item_unit($selected_id, $_POST['abbr'], $_POST['description'], $_POST['decimals'] );
52                 if($selected_id != '')
53                         display_notification(_('Selected unit has been updated'));
54                 else
55                         display_notification(_('New unit has been added'));
56                 $Mode = 'RESET';
57         }
58 }
59
60 //----------------------------------------------------------------------------------
61
62 if ($Mode == 'Delete')
63 {
64
65         // PREVENT DELETES IF DEPENDENT RECORDS IN 'stock_master'
66
67         if (item_unit_used($selected_id))
68         {
69                 display_error(_("Cannot delete this unit of measure because items have been created using this unit."));
70
71         }
72         else
73         {
74                 delete_item_unit($selected_id);
75                 display_notification(_('Selected unit has been deleted'));
76         }
77         $Mode = 'RESET';
78 }
79
80 if ($Mode == 'RESET')
81 {
82         $selected_id = '';
83         $sav = get_post('show_inactive');
84         unset($_POST);
85         $_POST['show_inactive'] = $sav;
86 }
87
88 //----------------------------------------------------------------------------------
89
90 $result = get_all_item_units(check_value('show_inactive'));
91
92 start_form();
93 start_table(TABLESTYLE, "width='40%'");
94 $th = array(_('Unit'), _('Description'), _('Decimals'), "", "");
95 inactive_control_column($th);
96
97 table_header($th);
98 $k = 0; //row colour counter
99
100 while ($myrow = db_fetch($result))
101 {
102
103         alt_table_row_color($k);
104
105         label_cell($myrow["abbr"]);
106         label_cell($myrow["name"]);
107         label_cell(($myrow["decimals"]==-1?_("User Quantity Decimals"):$myrow["decimals"]));
108         $id = html_specials_encode($myrow["abbr"]);
109         inactive_control_cell($id, $myrow["inactive"], 'item_units', 'abbr');
110         edit_button_cell("Edit".$id, _("Edit"));
111         delete_button_cell("Delete".$id, _("Delete"));
112         end_row();
113 }
114
115 inactive_control_row($th);
116 end_table(1);
117
118 //----------------------------------------------------------------------------------
119
120 start_table(TABLESTYLE2);
121
122 if ($selected_id != '') 
123 {
124         if ($Mode == 'Edit') {
125                 //editing an existing item category
126
127                 $myrow = get_item_unit($selected_id);
128
129                 $_POST['abbr'] = $myrow["abbr"];
130                 $_POST['description']  = $myrow["name"];
131                 $_POST['decimals']  = $myrow["decimals"];
132         }
133         hidden('selected_id', $myrow["abbr"]);
134 }
135 if ($selected_id != '' && item_unit_used($selected_id)) {
136     label_row(_("Unit Abbreviation:"), $_POST['abbr']);
137     hidden('abbr', $_POST['abbr']);
138 } else
139     text_row(_("Unit Abbreviation:"), 'abbr', null, 20, 20);
140 text_row(_("Descriptive Name:"), 'description', null, 40, 40);
141
142 number_list_row(_("Decimal Places:"), 'decimals', null, 0, 6, _("User Quantity Decimals"));
143
144 end_table(1);
145
146 submit_add_or_update_center($selected_id == '', '', 'both');
147
148 end_form();
149
150 end_page();
151