aef3ad25ae08772cd46f3bdb4c10b8ca8c025036
[fa-stable.git] / sales / manage / sales_people.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 3;
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         unset($_POST);
109 }
110 //------------------------------------------------------------------------------------------------
111
112 $sql = "SELECT * FROM ".TB_PREF."salesman";
113 $result = db_query($sql,"could not get sales persons");
114
115 start_form();
116 start_table("$table_style width=60%");
117 $th = array(_("Name"), _("Phone"), _("Fax"), _("Email"), _("Provision"), _("Break Pt."), _("Provision")." 2", "", "");
118 table_header($th);
119
120 $k = 0;
121
122 while ($myrow = db_fetch($result))
123 {
124
125         alt_table_row_color($k);
126
127     label_cell($myrow["salesman_name"]);
128         label_cell($myrow["salesman_phone"]);
129         label_cell($myrow["salesman_fax"]);
130         email_cell($myrow["salesman_email"]);
131         label_cell(percent_format($myrow["provision"])." %", "nowrap align=right");
132         amount_cell($myrow["break_pt"]);
133         label_cell(percent_format($myrow["provision2"])." %", "nowrap align=right");
134         edit_button_cell("Edit".$myrow["salesman_code"], _("Edit"));
135         delete_button_cell("Delete".$myrow["salesman_code"], _("Delete"));
136         end_row();
137
138 } //END WHILE LIST LOOP
139
140 end_table();
141 end_form();
142 echo '<br>';
143
144 //------------------------------------------------------------------------------------------------
145
146 start_form();
147
148
149 $_POST['salesman_email'] = "";
150 if ($selected_id != -1) 
151 {
152         if ($Mode == 'Edit') {
153                 //editing an existing Sales-person
154                 $sql = "SELECT *  FROM ".TB_PREF."salesman WHERE salesman_code='$selected_id'";
155
156                 $result = db_query($sql,"could not get sales person");
157                 $myrow = db_fetch($result);
158
159                 $_POST['salesman_name'] = $myrow["salesman_name"];
160                 $_POST['salesman_phone'] = $myrow["salesman_phone"];
161                 $_POST['salesman_fax'] = $myrow["salesman_fax"];
162                 $_POST['salesman_email'] = $myrow["salesman_email"];
163                 $_POST['provision'] = percent_format($myrow["provision"]);
164                 $_POST['break_pt'] = price_format($myrow["break_pt"]);
165                 $_POST['provision2'] = percent_format($myrow["provision2"]);
166         }
167         hidden('selected_id', $selected_id);
168 } elseif ($Mode != 'ADD_ITEM') {
169                 $_POST['provision'] = percent_format(0);
170                 $_POST['break_pt'] = price_format(0);
171                 $_POST['provision2'] = percent_format(0);       
172 }
173
174 start_table("$table_style2 width=60%");
175
176 text_row_ex(_("Sales person name:"), 'salesman_name', 30);
177 text_row_ex(_("Telephone number:"), 'salesman_phone', 20);
178 text_row_ex(_("Fax number:"), 'salesman_fax', 20);
179 email_row_ex(_("E-mail:"), 'salesman_email', 40);
180 percent_row(_("Provision").':', 'provision');
181 amount_row(_("Break Pt.:"), 'break_pt');
182 percent_row(_("Provision")." 2:", 'provision2');
183 end_table(1);
184
185 submit_add_or_update_center($selected_id == -1, '', true);
186
187 end_form();
188
189 end_page();
190
191 ?>