73b4467a3d5382c3efec42d88beafbf2a8b0ec77
[fa-stable.git] / sales / manage / sales_groups.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(_("Sales Groups"));
17
18 include($path_to_root . "/includes/ui.inc");
19
20 simple_page_mode(true);
21
22 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
23 {
24
25         $input_error = 0;
26
27         if (strlen($_POST['description']) == 0) 
28         {
29                 $input_error = 1;
30                 display_error(_("The area description cannot be empty."));
31                 set_focus('description');
32         }
33
34         if ($input_error != 1)
35         {
36         if ($selected_id != -1) 
37         {
38                 $sql = "UPDATE ".TB_PREF."groups SET description=".db_escape($_POST['description'])." WHERE id = '$selected_id'";
39                         $note = _('Selected sales group has been updated');
40         } 
41         else 
42         {
43                 $sql = "INSERT INTO ".TB_PREF."groups (description) VALUES (".db_escape($_POST['description']) . ")";
44                         $note = _('New sales group has been added');
45         }
46     
47         db_query($sql,"The sales group could not be updated or added");
48                 display_notification($note);            
49                 $Mode = 'RESET';
50         }
51
52
53 if ($Mode == 'Delete')
54 {
55
56         $cancel_delete = 0;
57
58         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'
59
60         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE group_no='$selected_id'";
61         $result = db_query($sql,"check failed");
62         $myrow = db_fetch_row($result);
63         if ($myrow[0] > 0) 
64         {
65                 $cancel_delete = 1;
66                 display_error(_("Cannot delete this group because customers have been created using this group."));
67         } 
68         if ($cancel_delete == 0) 
69         {
70                 $sql="DELETE FROM ".TB_PREF."groups WHERE id='" . $selected_id . "'";
71                 db_query($sql,"could not delete sales group");
72
73                 display_notification(_('Selected sales group has been deleted'));
74         } //end if Delete area
75         $Mode = 'RESET';
76
77
78 if ($Mode == 'RESET')
79 {
80         $selected_id = -1;
81         $sav = get_post('show_inactive');
82         unset($_POST);
83         if ($sav) $_POST['show_inactive'] = 1;
84 }
85 //-------------------------------------------------------------------------------------------------
86
87 $sql = "SELECT * FROM ".TB_PREF."groups";
88 if (!check_value('show_inactive')) $sql .= " WHERE !inactive";
89 $sql .= " ORDER BY description";
90 $result = db_query($sql,"could not get groups");
91
92 start_form();
93 start_table("$table_style width=30%");
94 $th = array(_("Group Name"), "", "");
95 inactive_control_column($th);
96
97 table_header($th);
98 $k = 0; 
99
100 while ($myrow = db_fetch($result)) 
101 {
102         
103         alt_table_row_color($k);
104                 
105         label_cell($myrow["description"]);
106         inactive_control_cell($myrow["id"], $myrow["inactive"], 'groups', 'id');
107         edit_button_cell("Edit".$myrow["id"], _("Edit"));
108         delete_button_cell("Delete".$myrow["id"], _("Delete"));
109         end_row();
110 }
111
112 inactive_control_row($th);
113 end_table();
114
115 echo '<br>';
116
117 //-------------------------------------------------------------------------------------------------
118
119 start_table($table_style2);
120
121 if ($selected_id != -1) 
122 {
123         if ($Mode == 'Edit') {
124                 //editing an existing area
125                 $sql = "SELECT * FROM ".TB_PREF."groups WHERE id='$selected_id'";
126
127                 $result = db_query($sql,"could not get group");
128                 $myrow = db_fetch($result);
129
130                 $_POST['description']  = $myrow["description"];
131         }
132         hidden("selected_id", $selected_id);
133
134
135 text_row_ex(_("Group Name:"), 'description', 30); 
136
137 end_table(1);
138
139 submit_add_or_update_center($selected_id == -1, '', 'both');
140
141 end_form();
142
143 end_page();
144 ?>