8546ba7e38d1f7d3578a3e33e5b02b3ea0ffadd4
[fa-stable.git] / sales / manage / sales_people.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_SALESMAN';
13 $path_to_root = "../..";
14 include($path_to_root . "/includes/session.inc");
15
16 page(_("Sales Persons"));
17
18 include($path_to_root . "/includes/ui.inc");
19
20 simple_page_mode(true);
21 //------------------------------------------------------------------------------------------------
22
23 if ($Mode=='ADD_ITEM' || $Mode=='UPDATE_ITEM') 
24 {
25
26         //initialise no input errors assumed initially before we test
27         $input_error = 0;
28
29         if (strlen($_POST['salesman_name']) == 0)
30         {
31                 $input_error = 1;
32                 display_error(_("The sales person name cannot be empty."));
33                 set_focus('salesman_name');
34         }
35         $pr1 = check_num('provision', 0,100);
36         if (!$pr1 || !check_num('provision2', 0, 100)) {
37                 $input_error = 1;
38                 display_error( _("Salesman provision cannot be less than 0 or more than 100%."));
39                 set_focus(!$pr1 ? 'provision' : 'provision2');
40         }
41         if (!check_num('break_pt', 0)) {
42                 $input_error = 1;
43                 display_error( _("Salesman provision breakpoint must be numeric and not less than 0."));
44                 set_focus('break_pt');
45         }
46         if ($input_error != 1)
47         {
48         if ($selected_id != -1) 
49         {
50                 /*selected_id could also exist if submit had not been clicked this code would not run in this case cos submit is false of course  see the delete code below*/
51
52                 $sql = "UPDATE ".TB_PREF."salesman SET salesman_name=".db_escape($_POST['salesman_name']) . ",
53                         salesman_phone=".db_escape($_POST['salesman_phone']) . ",
54                         salesman_fax=".db_escape($_POST['salesman_fax']) . ",
55                         salesman_email=".db_escape($_POST['salesman_email']) . ",
56                         provision=".input_num('provision').",
57                         break_pt=".input_num('break_pt').",
58                         provision2=".input_num('provision2')."
59                         WHERE salesman_code = '$selected_id'";
60         }
61         else
62         {
63                 /*Selected group is null cos no item selected on first time round so must be adding a record must be submitting new entries in the new Sales-person form */
64                 $sql = "INSERT INTO ".TB_PREF."salesman (salesman_name, salesman_phone, salesman_fax, salesman_email,
65                         provision, break_pt, provision2)
66                         VALUES (".db_escape($_POST['salesman_name']) . ", "
67                                   .db_escape($_POST['salesman_phone']) . ", "
68                                   .db_escape($_POST['salesman_fax']) . ", "
69                                   .db_escape($_POST['salesman_email']) . ", ".
70                         input_num('provision').", ".input_num('break_pt').", "
71                                 .input_num('provision2').")";
72         }
73
74         //run the sql from either of the above possibilites
75         db_query($sql,"The insert or update of the sales person failed");
76         if ($selected_id != -1) 
77                         display_notification(_('Selected sales person data have been updated'));
78                 else
79                         display_notification(_('New sales person data have been added'));
80                 $Mode = 'RESET';
81         }
82 }
83 if ($Mode == 'Delete')
84 {
85         //the link to delete a selected record was clicked instead of the submit button
86
87         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'
88
89         $sql= "SELECT COUNT(*) FROM ".TB_PREF."cust_branch WHERE salesman='$selected_id'";
90         $result = db_query($sql,"check failed");
91         $myrow = db_fetch_row($result);
92         if ($myrow[0] > 0)
93         {
94                 display_error("Cannot delete this sales-person because branches are set up referring to this sales-person - first alter the branches concerned.");
95         }
96         else
97         {
98                 $sql="DELETE FROM ".TB_PREF."salesman WHERE salesman_code='$selected_id'";
99                 db_query($sql,"The sales-person could not be deleted");
100                 display_notification(_('Selected sales person data have been deleted'));
101         }
102         $Mode = 'RESET';
103 }
104
105 if ($Mode == 'RESET')
106 {
107         $selected_id = -1;
108         $sav = get_post('show_inactive');
109         unset($_POST);
110         $_POST['show_inactive'] = $sav;
111 }
112 //------------------------------------------------------------------------------------------------
113
114 $sql = "SELECT * FROM ".TB_PREF."salesman";
115 if (!check_value('show_inactive')) $sql .= " WHERE !inactive";
116 $result = db_query($sql,"could not get sales persons");
117
118 start_form();
119 start_table("$table_style width=60%");
120 $th = array(_("Name"), _("Phone"), _("Fax"), _("Email"), _("Provision"), _("Break Pt."), _("Provision")." 2", "", "");
121 inactive_control_column($th);
122 table_header($th);
123
124 $k = 0;
125
126 while ($myrow = db_fetch($result))
127 {
128
129         alt_table_row_color($k);
130
131     label_cell($myrow["salesman_name"]);
132         label_cell($myrow["salesman_phone"]);
133         label_cell($myrow["salesman_fax"]);
134         email_cell($myrow["salesman_email"]);
135         label_cell(percent_format($myrow["provision"])." %", "nowrap align=right");
136         amount_cell($myrow["break_pt"]);
137         label_cell(percent_format($myrow["provision2"])." %", "nowrap align=right");
138         inactive_control_cell($myrow["salesman_code"], $myrow["inactive"],
139                 'salesman', 'salesman_code');
140         edit_button_cell("Edit".$myrow["salesman_code"], _("Edit"));
141         delete_button_cell("Delete".$myrow["salesman_code"], _("Delete"));
142         end_row();
143
144 } //END WHILE LIST LOOP
145
146 inactive_control_row($th);
147 end_table();
148 echo '<br>';
149
150 //------------------------------------------------------------------------------------------------
151
152 $_POST['salesman_email'] = "";
153 if ($selected_id != -1) 
154 {
155         if ($Mode == 'Edit') {
156                 //editing an existing Sales-person
157                 $sql = "SELECT *  FROM ".TB_PREF."salesman WHERE salesman_code='$selected_id'";
158
159                 $result = db_query($sql,"could not get sales person");
160                 $myrow = db_fetch($result);
161
162                 $_POST['salesman_name'] = $myrow["salesman_name"];
163                 $_POST['salesman_phone'] = $myrow["salesman_phone"];
164                 $_POST['salesman_fax'] = $myrow["salesman_fax"];
165                 $_POST['salesman_email'] = $myrow["salesman_email"];
166                 $_POST['provision'] = percent_format($myrow["provision"]);
167                 $_POST['break_pt'] = price_format($myrow["break_pt"]);
168                 $_POST['provision2'] = percent_format($myrow["provision2"]);
169         }
170         hidden('selected_id', $selected_id);
171 } elseif ($Mode != 'ADD_ITEM') {
172                 $_POST['provision'] = percent_format(0);
173                 $_POST['break_pt'] = price_format(0);
174                 $_POST['provision2'] = percent_format(0);       
175 }
176
177 start_table($table_style2);
178
179 text_row_ex(_("Sales person name:"), 'salesman_name', 30);
180 text_row_ex(_("Telephone number:"), 'salesman_phone', 20);
181 text_row_ex(_("Fax number:"), 'salesman_fax', 20);
182 email_row_ex(_("E-mail:"), 'salesman_email', 40);
183 percent_row(_("Provision").':', 'provision');
184 amount_row(_("Break Pt.:"), 'break_pt');
185 percent_row(_("Provision")." 2:", 'provision2');
186 end_table(1);
187
188 submit_add_or_update_center($selected_id == -1, '', 'both');
189
190 end_form();
191
192 end_page();
193
194 ?>