3fd37dd23c1aed87d1965463e9faf8f18b16ddbf
[fa-stable.git] / sales / manage / sales_areas.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_SALESAREA';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Sales Areas"));
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."areas SET description=".db_escape($_POST['description'])." WHERE area_code = '$selected_id'";
39                         $note = _('Selected sales area has been updated');
40         } 
41         else 
42         {
43                 $sql = "INSERT INTO ".TB_PREF."areas (description) VALUES (".db_escape($_POST['description']) . ")";
44                         $note = _('New sales area has been added');
45         }
46     
47         db_query($sql,"The sales area 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 area='$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 area because customer branches have been created using this area."));
67         } 
68         if ($cancel_delete == 0) 
69         {
70                 $sql="DELETE FROM ".TB_PREF."areas WHERE area_code='" . $selected_id . "'";
71                 db_query($sql,"could not delete sales area");
72
73                 display_notification(_('Selected sales area 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         $_POST['show_inactive'] = $sav;
84 }
85
86 //-------------------------------------------------------------------------------------------------
87
88 $sql = "SELECT * FROM ".TB_PREF."areas";
89 if (!check_value('show_inactive')) $sql .= " WHERE !inactive";
90 $result = db_query($sql,"could not get areas");
91
92 start_form();
93 start_table("$table_style width=30%");
94
95 $th = array(_("Area Name"), "", "");
96 inactive_control_column($th);
97
98 table_header($th);
99 $k = 0; 
100
101 while ($myrow = db_fetch($result)) 
102 {
103         
104         alt_table_row_color($k);
105                 
106         label_cell($myrow["description"]);
107         
108         inactive_control_cell($myrow["area_code"], $myrow["inactive"], 'areas', 'area_code');
109
110         edit_button_cell("Edit".$myrow["area_code"], _("Edit"));
111         delete_button_cell("Delete".$myrow["area_code"], _("Delete"));
112         end_row();
113 }
114         
115 inactive_control_row($th);
116 end_table();
117 echo '<br>';
118
119 //-------------------------------------------------------------------------------------------------
120
121 start_table($table_style2);
122
123 if ($selected_id != -1) 
124 {
125         if ($Mode == 'Edit') {
126                 //editing an existing area
127                 $sql = "SELECT * FROM ".TB_PREF."areas WHERE area_code='$selected_id'";
128
129                 $result = db_query($sql,"could not get area");
130                 $myrow = db_fetch($result);
131
132                 $_POST['description']  = $myrow["description"];
133         }
134         hidden("selected_id", $selected_id);
135
136
137 text_row_ex(_("Area Name:"), 'description', 30); 
138
139 end_table(1);
140
141 submit_add_or_update_center($selected_id == -1, '', 'both');
142
143 end_form();
144
145 end_page();
146 ?>