Security statements update against sql injection attacks.
[fa-stable.git] / manufacturing / manage / work_centres.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 = 3;
13 $path_to_root="../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Work Centres"));
17
18 include($path_to_root . "/manufacturing/includes/manufacturing_db.inc");
19
20 include($path_to_root . "/includes/ui.inc");
21
22 simple_page_mode(true);
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['name']) == 0) 
32         {
33                 $input_error = 1;
34                 display_error(_("The work centre name cannot be empty."));
35                 set_focus('name');
36         }
37
38         if ($input_error != 1) 
39         {
40                 
41         if ($selected_id != -1) 
42         {
43                 update_work_centre($selected_id, $_POST['name'], $_POST['description']);
44                         display_notification(_('Selected work center has been updated'));
45         } 
46         else 
47         {
48                 add_work_centre($_POST['name'], $_POST['description']);
49                         display_notification(_('New work center has been added'));
50         }
51                 $Mode = 'RESET';
52         }
53
54
55 //-----------------------------------------------------------------------------------
56
57 function can_delete($selected_id)
58 {
59         $sql= "SELECT COUNT(*) FROM ".TB_PREF."bom WHERE workcentre_added=".db_escape($selected_id);
60         $result = db_query($sql, "check can delete work centre");
61         $myrow = db_fetch_row($result);
62         if ($myrow[0] > 0) 
63         {
64                 display_error(_("Cannot delete this work centre because BOMs have been created referring to it."));
65                 return false;
66         }
67         
68         $sql= "SELECT COUNT(*) FROM ".TB_PREF."wo_requirements WHERE workcentre=".db_escape($selected_id);
69         $result = db_query($sql, "check can delete work centre");
70         $myrow = db_fetch_row($result);
71         if ($myrow[0] > 0) 
72         {
73                 display_error(_("Cannot delete this work centre because work order requirements have been created referring to it."));
74                 return false;
75         }               
76         
77         return true;
78 }
79
80
81 //-----------------------------------------------------------------------------------
82
83 if ($Mode == 'Delete')
84 {
85
86         if (can_delete($selected_id))
87         {
88                 delete_work_centre($selected_id);
89                 display_notification(_('Selected work center has been deleted'));
90         }
91         $Mode = 'RESET';
92 }
93
94 if ($Mode == 'RESET')
95 {
96         $selected_id = -1;
97         unset($_POST);
98 }
99 //-----------------------------------------------------------------------------------
100
101 $result = get_all_work_centres();
102
103 start_form();
104 start_table("$table_style width=50%");
105 $th = array(_("Name"), _("description"), "", "");
106 table_header($th);
107
108 $k = 0;
109 while ($myrow = db_fetch($result)) 
110 {
111         
112         alt_table_row_color($k);        
113
114         label_cell($myrow["name"]);
115         label_cell($myrow["description"]);
116         edit_button_cell("Edit".$myrow['id'], _("Edit"));
117         delete_button_cell("Delete".$myrow['id'], _("Delete"));
118         end_row();
119 }
120
121 end_table();
122 end_form();
123 echo '<br>';
124 //-----------------------------------------------------------------------------------
125
126 start_form();
127
128 start_table($table_style2);
129
130 if ($selected_id != -1) 
131 {
132         if ($Mode == 'Edit') {
133                 //editing an existing status code
134                 $myrow = get_work_centre($selected_id);
135                 
136                 $_POST['name']  = $myrow["name"];
137                 $_POST['description']  = $myrow["description"];
138         }
139         hidden('selected_id', $selected_id);
140
141
142 text_row_ex(_("Name:"), 'name', 40);
143 text_row_ex(_("Description:"), 'description', 50);
144
145 end_table(1);
146
147 submit_add_or_update_center($selected_id == -1, '', true);
148
149 end_form();
150
151 //------------------------------------------------------------------------------------
152
153 end_page();
154
155 ?>