Changed license type to GPLv3 in top of files
[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 = 3;
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         unset($_POST);
82 }
83 //-------------------------------------------------------------------------------------------------
84
85 $sql = "SELECT * FROM ".TB_PREF."areas";
86 $result = db_query($sql,"could not get areas");
87
88 start_form();
89 start_table("$table_style width=30%");
90 $th = array(_("Area Name"), "", "");
91 table_header($th);
92 $k = 0; 
93
94 while ($myrow = db_fetch($result)) 
95 {
96         
97         alt_table_row_color($k);
98                 
99         label_cell($myrow["description"]);
100         edit_button_cell("Edit".$myrow["area_code"], _("Edit"));
101         delete_button_cell("Delete".$myrow["area_code"], _("Delete"));
102         end_row();
103 }
104
105
106 end_table();
107 end_form();
108 echo '<br>';
109
110 //-------------------------------------------------------------------------------------------------
111
112 start_form();
113
114 start_table($table_style2);
115
116 if ($selected_id != -1) 
117 {
118         if ($Mode == 'Edit') {
119                 //editing an existing area
120                 $sql = "SELECT * FROM ".TB_PREF."areas WHERE area_code='$selected_id'";
121
122                 $result = db_query($sql,"could not get area");
123                 $myrow = db_fetch($result);
124
125                 $_POST['description']  = $myrow["description"];
126         }
127         hidden("selected_id", $selected_id);
128
129
130 text_row_ex(_("Area Name:"), 'description', 30); 
131
132 end_table(1);
133
134 submit_add_or_update_center($selected_id == -1, '', true);
135
136 end_form();
137
138 end_page();
139 ?>