81c0dc268bb2c37eff755696131385536c9c2dec
[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(_($help_context = "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                         update_salesman($selected_id, $_POST['salesman_name'], $_POST['salesman_phone'], $_POST['salesman_fax'],
52                                 $_POST['salesman_email'], input_num('provision'), input_num('break_pt'), input_num('provision2'));
53         }
54         else
55         {
56                 /*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 */
57                         add_salesman($_POST['salesman_name'], $_POST['salesman_phone'], $_POST['salesman_fax'],
58                                 $_POST['salesman_email'], input_num('provision'), input_num('break_pt'), input_num('provision2'));
59         }
60
61         if ($selected_id != -1) 
62                         display_notification(_('Selected sales person data have been updated'));
63                 else
64                         display_notification(_('New sales person data have been added'));
65                 $Mode = 'RESET';
66         }
67 }
68 if ($Mode == 'Delete')
69 {
70         //the link to delete a selected record was clicked instead of the submit button
71
72         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtors_master'
73
74         if (key_in_foreign_table($selected_id, 'cust_branch', 'salesman'))
75         {
76                 display_error(_("Cannot delete this sales-person because branches are set up referring to this sales-person - first alter the branches concerned."));
77         }
78         else
79         {
80                 delete_salesman($selected_id);
81                 display_notification(_('Selected sales person data have been deleted'));
82         }
83         $Mode = 'RESET';
84 }
85
86 if ($Mode == 'RESET')
87 {
88         $selected_id = -1;
89         $sav = get_post('show_inactive');
90         unset($_POST);
91         $_POST['show_inactive'] = $sav;
92 }
93 //------------------------------------------------------------------------------------------------
94
95 $result = get_salesmen(check_value('show_inactive'));
96
97 start_form();
98 start_table(TABLESTYLE, "width=60%");
99 $th = array(_("Name"), _("Phone"), _("Fax"), _("Email"), _("Provision"), _("Break Pt."), _("Provision")." 2", "", "");
100 inactive_control_column($th);
101 table_header($th);
102
103 $k = 0;
104
105 while ($myrow = db_fetch($result))
106 {
107
108         alt_table_row_color($k);
109
110     label_cell($myrow["salesman_name"]);
111         label_cell($myrow["salesman_phone"]);
112         label_cell($myrow["salesman_fax"]);
113         email_cell($myrow["salesman_email"]);
114         label_cell(percent_format($myrow["provision"])." %", "nowrap align=right");
115         amount_cell($myrow["break_pt"]);
116         label_cell(percent_format($myrow["provision2"])." %", "nowrap align=right");
117         inactive_control_cell($myrow["salesman_code"], $myrow["inactive"],
118                 'salesman', 'salesman_code');
119         edit_button_cell("Edit".$myrow["salesman_code"], _("Edit"));
120         delete_button_cell("Delete".$myrow["salesman_code"], _("Delete"));
121         end_row();
122
123 } //END WHILE LIST LOOP
124
125 inactive_control_row($th);
126 end_table();
127 echo '<br>';
128
129 //------------------------------------------------------------------------------------------------
130
131 $_POST['salesman_email'] = "";
132 if ($selected_id != -1) 
133 {
134         if ($Mode == 'Edit') {
135                 //editing an existing Sales-person
136                 $myrow = get_salesman($selected_id);
137
138                 $_POST['salesman_name'] = $myrow["salesman_name"];
139                 $_POST['salesman_phone'] = $myrow["salesman_phone"];
140                 $_POST['salesman_fax'] = $myrow["salesman_fax"];
141                 $_POST['salesman_email'] = $myrow["salesman_email"];
142                 $_POST['provision'] = percent_format($myrow["provision"]);
143                 $_POST['break_pt'] = price_format($myrow["break_pt"]);
144                 $_POST['provision2'] = percent_format($myrow["provision2"]);
145         }
146         hidden('selected_id', $selected_id);
147 } elseif ($Mode != 'ADD_ITEM') {
148                 $_POST['provision'] = percent_format(0);
149                 $_POST['break_pt'] = price_format(0);
150                 $_POST['provision2'] = percent_format(0);       
151 }
152
153 start_table(TABLESTYLE2);
154
155 text_row_ex(_("Sales person name:"), 'salesman_name', 30);
156 text_row_ex(_("Telephone number:"), 'salesman_phone', 20);
157 text_row_ex(_("Fax number:"), 'salesman_fax', 20);
158 email_row_ex(_("E-mail:"), 'salesman_email', 40);
159 percent_row(_("Provision").':', 'provision');
160 amount_row(_("Break Pt.:"), 'break_pt');
161 percent_row(_("Provision")." 2:", 'provision2');
162 end_table(1);
163
164 submit_add_or_update_center($selected_id == -1, '', 'both');
165
166 end_form();
167
168 end_page();
169
170 ?>