Update from usntable branch.
[fa-stable.git] / inventory / manage / movement_types.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_INVENTORYMOVETYPE';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "Inventory Movement Types"));
17
18 include_once($path_to_root . "/inventory/includes/inventory_db.inc");
19
20 include_once($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 inventory movement type name cannot be empty."));
35                 set_focus('name');
36         }
37
38         if ($input_error != 1) 
39         {
40         if ($selected_id != -1) 
41         {
42                 update_movement_type($selected_id, $_POST['name']);
43                         display_notification(_('Selected movement type has been updated'));
44         } 
45         else 
46         {
47                 add_movement_type($_POST['name']);
48                         display_notification(_('New movement type has been added'));
49         }
50         
51                 $Mode = 'RESET';
52         }
53
54
55 //-----------------------------------------------------------------------------------
56
57 function can_delete($selected_id)
58 {
59         $sql= "SELECT COUNT(*) FROM ".TB_PREF."stock_moves 
60                 WHERE type=" . ST_INVADJUST. " AND person_id=".db_escape($selected_id);
61
62         $result = db_query($sql, "could not query stock moves");
63         $myrow = db_fetch_row($result);
64         if ($myrow[0] > 0) 
65         {
66                 display_error(_("Cannot delete this inventory movement type because item transactions have been created referring to it."));
67                 return false;
68         }
69         
70         return true;
71 }
72
73
74 //-----------------------------------------------------------------------------------
75
76 if ($Mode == 'Delete')
77 {
78         if (can_delete($selected_id))
79         {
80                 delete_movement_type($selected_id);
81                 display_notification(_('Selected movement type has been deleted'));
82         }
83         $Mode = 'RESET';
84 }
85
86 if ($Mode == 'RESET')
87 {
88         $selected_id = -1;
89         $sav = get_post('show_inactive');
90         unset($_POST);
91         $_POST['show_inactive'] = $sav;
92 }
93 //-----------------------------------------------------------------------------------
94
95 $result = get_all_movement_type(check_value('show_inactive'));
96
97 start_form();
98 start_table("$table_style width=30%");
99
100 $th = array(_("Description"), "", "");
101 inactive_control_column($th);
102 table_header($th);
103 $k = 0;
104 while ($myrow = db_fetch($result)) 
105 {
106         
107         alt_table_row_color($k);        
108
109         label_cell($myrow["name"]);
110         inactive_control_cell($myrow["id"], $myrow["inactive"], 'movement_types', 'id');
111         edit_button_cell("Edit".$myrow['id'], _("Edit"));
112         delete_button_cell("Delete".$myrow['id'], _("Delete"));
113         end_row();
114 }
115 inactive_control_row($th);
116 end_table(1);
117
118 //-----------------------------------------------------------------------------------
119
120 start_table($table_style2);
121
122 if ($selected_id != -1) 
123 {
124         if ($Mode == 'Edit') {
125                 //editing an existing status code
126
127                 $myrow = get_movement_type($selected_id);
128
129                 $_POST['name']  = $myrow["name"];
130         }
131         hidden('selected_id', $selected_id);
132
133
134 text_row(_("Description:"), 'name', null, 50, 50);
135
136 end_table(1);
137
138 submit_add_or_update_center($selected_id == -1, '', 'both');
139
140 end_form();
141
142 //------------------------------------------------------------------------------------
143
144 end_page();
145
146 ?>