Changed license type to GPLv3 in top of files
[fa-stable.git] / sales / manage / sales_types.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 = 14;
13 $path_to_root="../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Sales Types"));
17
18 include_once($path_to_root . "/includes/ui.inc");
19 include_once($path_to_root . "/sales/includes/db/sales_types_db.inc");
20
21 simple_page_mode(true);
22 //----------------------------------------------------------------------------------------------------
23
24 function can_process()
25 {
26         if (strlen($_POST['sales_type']) == 0)
27         {
28                 display_error(_("The sales type description cannot be empty."));
29                 set_focus('sales_type');
30                 return false;
31         }
32
33         if (!check_num('factor', 0))
34         {
35                 display_error(_("Calculation factor must be valid positive number."));
36                 set_focus('factor');
37                 return false;
38         }
39         return true;
40 }
41
42 //----------------------------------------------------------------------------------------------------
43
44 if ($Mode=='ADD_ITEM' && can_process())
45 {
46         add_sales_type($_POST['sales_type'], isset($_POST['tax_included']) ? 1:0,
47             input_num('factor'));
48         display_notification(_('New sales type has been added'));
49         $Mode = 'RESET';
50 }
51
52 //----------------------------------------------------------------------------------------------------
53
54 if ($Mode=='UPDATE_ITEM' && can_process())
55 {
56
57         update_sales_type($selected_id, $_POST['sales_type'], isset($_POST['tax_included']) ? 1:0,
58              input_num('factor'));
59         display_notification(_('Selected sales type has been updated'));
60         $Mode = 'RESET';
61 }
62
63 //----------------------------------------------------------------------------------------------------
64
65 if ($Mode == 'Delete')
66 {
67         // PREVENT DELETES IF DEPENDENT RECORDS IN 'debtor_trans'
68
69         $sql= "SELECT COUNT(*) FROM ".TB_PREF."debtor_trans WHERE tpe='$selected_id'";
70         $result = db_query($sql,"check failed");
71         check_db_error("The number of transactions using this Sales type record could not be retrieved", $sql);
72
73         $myrow = db_fetch_row($result);
74         if ($myrow[0] > 0)
75         {
76                 display_error(_("Cannot delete this sale type because customer transactions have been created using this sales type."));
77
78         }
79         else
80         {
81
82                 $sql = "SELECT COUNT(*) FROM ".TB_PREF."debtors_master WHERE sales_type='$selected_id'";
83                 $result = db_query($sql,"check failed");
84                 check_db_error("The number of customers using this Sales type record could not be retrieved", $sql);
85
86                 $myrow = db_fetch_row($result);
87                 if ($myrow[0] > 0)
88                 {
89                         display_error(_("Cannot delete this sale type because customers are currently set up to use this sales type."));
90                 }
91                 else
92                 {
93                         delete_sales_type($selected_id);
94                         display_notification(_('Selected sales type has been deleted'));
95                 }
96         } //end if sales type used in debtor transactions or in customers set up
97         $Mode = 'RESET';
98 }
99
100 if ($Mode == 'RESET')
101 {
102         $selected_id = -1;
103         unset($_POST);
104 }
105 //----------------------------------------------------------------------------------------------------
106
107 $result = get_all_sales_types();
108
109 start_form();
110 start_table("$table_style width=30%");
111
112 $th = array (_('Type Name'), _('Factor'), _('Tax Incl'), '','');
113 table_header($th);
114 $k = 0;
115 $base_sales = get_base_sales_type();
116
117 while ($myrow = db_fetch($result))
118 {
119         if ($myrow["id"] == $base_sales)
120             start_row("class='overduebg'");
121         else
122             alt_table_row_color($k);
123         label_cell($myrow["sales_type"]);
124         $f = number_format2($myrow["factor"],4);
125         if($myrow["id"] == $base_sales) $f = "<I>"._('Base')."</I>";
126         label_cell($f);
127         label_cell($myrow["tax_included"] ? _('Yes'):_('No'), 'align=center');
128         edit_button_cell("Edit".$myrow['id'], _("Edit"));
129         delete_button_cell("Delete".$myrow['id'], _("Delete"));
130         end_row();
131 }
132
133 end_table();
134 end_form();
135 display_note(_("Marked sales type is the company base pricelist for prices calculations."), 0, 0, "class='overduefg'");
136
137 //----------------------------------------------------------------------------------------------------
138
139 start_form();
140  if (!isset($_POST['tax_included']))
141         $_POST['tax_included'] = 0;
142  if (!isset($_POST['base']))
143         $_POST['base'] = 0;
144
145 start_table($table_style2);
146
147 if ($selected_id != -1)
148 {
149
150         if ($Mode == 'Edit') {
151                 $myrow = get_sales_type($selected_id);
152
153                 $_POST['sales_type']  = $myrow["sales_type"];
154                 $_POST['tax_included']  = $myrow["tax_included"];
155                 $_POST['factor']  = number_format2($myrow["factor"],4);
156         }
157         hidden('selected_id', $selected_id);
158 } else {
159                 $_POST['factor']  = number_format2(1,4);
160 }
161
162 text_row_ex(_("Sales Type Name").':', 'sales_type', 20);
163 amount_row(_("Calculation factor").':', 'factor', null, null, null, 4);
164 check_row(_("Tax included").':', 'tax_included', $_POST['tax_included']);
165
166 end_table(1);
167
168 submit_add_or_update_center($selected_id == -1, '', true);
169
170 end_form();
171
172 end_page();
173
174 ?>