Focus set to invalid field after submit check fail
[fa-stable.git] / inventory / manage / item_units.php
1 <?php
2
3 $page_security = 11;
4 $path_to_root="../..";
5 include($path_to_root . "/includes/session.inc");
6
7 page(_("Units of Measure"));
8
9 include_once($path_to_root . "/includes/ui.inc");
10
11 include_once($path_to_root . "/inventory/includes/db/items_units_db.inc");
12
13 if (isset($_GET['selected_id']))
14 {
15         $selected_id = $_GET['selected_id'];
16
17 else if (isset($_POST['selected_id']))
18 {
19         $selected_id = $_POST['selected_id'];
20 }
21
22 //----------------------------------------------------------------------------------
23
24 if (isset($_POST['ADD_ITEM']) || isset($_POST['UPDATE_ITEM'])) 
25 {
26
27         //initialise no input errors assumed initially before we test
28         $input_error = 0;
29
30         if (strlen($_POST['abbr']) == 0) 
31         {
32                 $input_error = 1;
33                 display_error(_("The unit of measure code cannot be empty."));
34                 set_focus('abbr');
35         }
36         if (strlen($_POST['description']) == 0) 
37         {
38                 $input_error = 1;
39                 display_error(_("The unit of measure description cannot be empty."));
40                 set_focus('description');
41         }
42         if (!is_numeric($_POST['decimals'])) 
43         {
44                 $input_error = 1;
45                 display_error(_("The number of decimal places must be integer."));
46                 set_focus('decimals');
47         }
48
49
50         if ($input_error !=1) {
51         write_item_unit(isset($selected_id) ? $selected_id : '', $_POST['abbr'], $_POST['description'], $_POST['decimals'] );
52                 meta_forward($_SERVER['PHP_SELF']); 
53         }
54 }
55
56 //---------------------------------------------------------------------------------- 
57
58 if (isset($_GET['delete'])) 
59 {
60
61         // PREVENT DELETES IF DEPENDENT RECORDS IN 'stock_master'
62     
63         if (item_unit_used($selected_id))
64         {
65                 display_error(_("Cannot delete this unit of measure because items have been created using this units."));
66
67         } 
68         else 
69         {
70                 delete_item_unit($selected_id);
71                 meta_forward($_SERVER['PHP_SELF']);             
72         }
73 }
74
75 //----------------------------------------------------------------------------------
76
77 $result = get_all_item_units();
78 start_table("$table_style width=50%");
79 $th = array(_('Unit'), _('Description'), _('Decimals'), "", "");
80
81 table_header($th);
82 $k = 0; //row colour counter
83
84 while ($myrow = db_fetch($result)) 
85 {
86         
87         alt_table_row_color($k);
88
89         label_cell($myrow["abbr"]);
90         label_cell($myrow["name"]);
91         label_cell($myrow["decimals"]);
92
93         edit_link_cell(SID."selected_id=$myrow[0]");
94         delete_link_cell(SID."selected_id=$myrow[0]&delete=yes");
95         end_row();
96 }
97
98 end_table();
99
100 //----------------------------------------------------------------------------------
101
102 hyperlink_no_params($_SERVER['PHP_SELF'], _("New Unit of Measure"));
103
104 start_form();
105
106 start_table("class='tablestyle_noborder'");
107
108 if (isset($selected_id)) 
109 {
110         //editing an existing item category
111
112         $myrow = get_item_unit($selected_id);
113
114         $_POST['abbr'] = $myrow["abbr"];
115         $_POST['description']  = $myrow["name"];
116         $_POST['decimals']  = $myrow["decimals"];
117
118         hidden('selected_id', $selected_id);
119 }
120
121 if (isset($selected_id) && item_unit_used($selected_id)) {
122     label_row(_("Unit Abbreviation:"), $_POST['abbr']);
123     hidden('abbr', $_POST['abbr']);
124 } else
125     text_row(_("Unit Abbreviation:"), 'abbr', null, 20, 20);
126 text_row(_("Descriptive Name:"), 'description', null, 40, 40);  
127 text_row(_("Decimal Places:"), 'decimals', null, 3, 3);  
128
129 end_table(1);
130
131 submit_add_or_update_center(!isset($selected_id));
132
133 end_form();
134
135 end_page();
136
137 ?>