Sales type bug in tax_included (persistent on)
[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 = 'SA_SALESTYPES';
13 $path_to_root = "../..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_($help_context = "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'], check_value('tax_included'),
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'], check_value('tax_included'),
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         if (key_in_foreign_table($selected_id, 'debtor_trans', 'tpe'))
70         {
71                 display_error(_("Cannot delete this sale type because customer transactions have been created using this sales type."));
72
73         }
74         else
75         {
76                 if (key_in_foreign_table($selected_id, 'debtors_master', 'sales_type'))
77                 {
78                         display_error(_("Cannot delete this sale type because customers are currently set up to use this sales type."));
79                 }
80                 else
81                 {
82                         delete_sales_type($selected_id);
83                         display_notification(_('Selected sales type has been deleted'));
84                 }
85         } //end if sales type used in debtor transactions or in customers set up
86         $Mode = 'RESET';
87 }
88
89 if ($Mode == 'RESET')
90 {
91         $selected_id = -1;
92         $sav = get_post('show_inactive');
93         unset($_POST);
94         $_POST['show_inactive'] = $sav;
95 }
96 //----------------------------------------------------------------------------------------------------
97
98 $result = get_all_sales_types(check_value('show_inactive'));
99
100 start_form();
101 start_table(TABLESTYLE, "width='30%'");
102
103 $th = array (_('Type Name'), _('Factor'), _('Tax Incl'), '','');
104 inactive_control_column($th);
105 table_header($th);
106 $k = 0;
107 $base_sales = get_base_sales_type();
108
109 while ($myrow = db_fetch($result))
110 {
111         if ($myrow["id"] == $base_sales)
112             start_row("class='overduebg'");
113         else
114             alt_table_row_color($k);
115         label_cell($myrow["sales_type"]);
116         $f = number_format2($myrow["factor"],4);
117         if($myrow["id"] == $base_sales) $f = "<I>"._('Base')."</I>";
118         label_cell($f);
119         label_cell($myrow["tax_included"] ? _('Yes'):_('No'), 'align=center');
120         inactive_control_cell($myrow["id"], $myrow["inactive"], 'sales_types', 'id');
121         edit_button_cell("Edit".$myrow['id'], _("Edit"));
122         delete_button_cell("Delete".$myrow['id'], _("Delete"));
123         end_row();
124 }
125 inactive_control_row($th);
126 end_table();
127
128 display_note(_("Marked sales type is the company base pricelist for prices calculations."), 0, 0, "class='overduefg'");
129
130 //----------------------------------------------------------------------------------------------------
131
132  if (!isset($_POST['tax_included']))
133         $_POST['tax_included'] = 0;
134  if (!isset($_POST['base']))
135         $_POST['base'] = 0;
136
137 start_table(TABLESTYLE2);
138
139 if ($selected_id != -1)
140 {
141
142         if ($Mode == 'Edit') {
143                 $myrow = get_sales_type($selected_id);
144
145                 $_POST['sales_type']  = $myrow["sales_type"];
146                 $_POST['tax_included']  = $myrow["tax_included"];
147                 $_POST['factor']  = number_format2($myrow["factor"],4);
148         }
149         hidden('selected_id', $selected_id);
150 } else {
151                 $_POST['factor']  = number_format2(1,4);
152 }
153
154 text_row_ex(_("Sales Type Name").':', 'sales_type', 20);
155 amount_row(_("Calculation factor").':', 'factor', null, null, null, 4);
156 check_row(_("Tax included").':', 'tax_included', $_POST['tax_included']);
157
158 end_table(1);
159
160 submit_add_or_update_center($selected_id == -1, '', 'both');
161
162 end_form();
163
164 end_page();
165