New tax system - sales side
[fa-stable.git] / taxes / tax_rules.inc
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 /*
13         FA tax basic rules.
14 */
15 define('TAX_NONE', 0); // none option
16 define('TQ_NONE', 0); // none option
17
18 // implemented taxation options
19 // basic:
20 define('TAX_PAYABLE', 1);
21 define('TAX_DEDUCTIBLE', 2);
22 define('TAX_CHARGED', 4);
23 // category depenedent:
24 define('TAX_DUEDATE', 8);
25 define('TAX_PARTIAL', 16);
26
27 // quirks dependent on vat_categories
28 define('TQ_NONDEDUCT', 1);      // never deductible
29 define('TQ_PARTIAL', 2);        // partially deductible (using global factor)
30 define('TQ_DUEDATE', 4);        // taxable on due date instead of transaction date
31 define('TQ_REVERSE', 8);        // tax is reverse charged
32 define('TQ_IMPDEDUCTIBLE', 16); // import tax is deductible
33
34 class tax_system
35 {
36         //-------------------------- entity taxation rules ----------------------------
37         // general rules: who and when pays taxes
38         //
39         // FIXME: credit notes
40         //
41         var     $description;
42         var $ts_basic_rules;
43         var $ts_category_quirks;
44
45         function __construct()
46         {
47                 $this->ts_basic_rules = array(
48                         TA_DOMESTIC => array(
49                                 ST_SALESQUOTE => TAX_CHARGED,
50                                 ST_SALESORDER => TAX_CHARGED,
51                                 ST_CUSTDELIVERY => TAX_CHARGED,
52                                 ST_SALESINVOICE => TAX_PAYABLE | TAX_CHARGED,
53                                 ST_CUSTCREDIT => TAX_PAYABLE | TAX_CHARGED,
54                                 ST_PURCHORDER => TAX_CHARGED,
55                                 ST_SUPPRECEIVE => TAX_CHARGED,
56                                 ST_SUPPINVOICE => TAX_DEDUCTIBLE | TAX_CHARGED,
57                                 ST_SUPPCREDIT => TAX_DEDUCTIBLE | TAX_CHARGED,
58                         ),
59                         TA_EXPORT => array(
60                                 ST_SALESQUOTE => TAX_NONE,
61                                 ST_SALESORDER => TAX_NONE,
62                                 ST_CUSTDELIVERY => TAX_NONE,
63                                 ST_SALESINVOICE => TAX_NONE,
64                                 ST_CUSTCREDIT => TAX_NONE,
65                                 ST_PURCHORDER => TAX_NONE,
66                                 ST_SUPPRECEIVE => TAX_NONE,
67                                 ST_SUPPINVOICE => TAX_NONE,
68                                 ST_SUPPCREDIT => TAX_NONE,
69                         ),
70                         TA_EU => array(
71                                 ST_SALESQUOTE => TAX_NONE,
72                                 ST_SALESORDER => TAX_NONE,
73                                 ST_CUSTDELIVERY => TAX_NONE,
74                                 ST_SALESINVOICE => TAX_NONE,
75                                 ST_CUSTCREDIT => TAX_NONE,
76                                 ST_PURCHORDER => TAX_NONE,
77                                 ST_SUPPRECEIVE => TAX_NONE,
78                                 ST_SUPPINVOICE => TAX_DEDUCTIBLE | TAX_PAYABLE,
79                                 ST_SUPPCREDIT => TAX_DEDUCTIBLE | TAX_PAYABLE,
80                         ),
81                 );
82
83                 //-------------------------- special goods dependent rules  ----------------------------
84                 //
85                 $this->ts_category_quirks = array(
86                         VC_OTHER => TQ_NONE,
87                         VC_MEDIA => TQ_DUEDATE,                 // is no longer used ?
88                         VC_ASSETS => TQ_NONE,                   // just separate category in tax reg
89                         VC_NONDEDUCT => TQ_NONDEDUCT,
90                         VC_SERVICES => TQ_IMPDEDUCTIBLE,
91                         VC_PARTIAL => TQ_PARTIAL,
92                         VC_REVERSE => TQ_REVERSE,
93                 );
94         }
95         /*
96                 Returns tax options applicable for the arguments set
97
98                 $allow_reverse - decides whether reverse charging option is honoured for sales invoice
99                 (this depends on 'continue transaction' value as defined by law, so have to be set by case).
100         */
101         function options($trans_type, $tax_area, $vat_category, $allow_reverse=true)
102         {
103                 if (!isset($vat_category)) // exempt goods has really no category
104                         return TAX_NONE;
105
106                 $options = $this->ts_basic_rules[$tax_area][$trans_type];
107
108                 // per vat category quirks
109                 $quirks = $this->ts_category_quirks[$vat_category];
110
111                 if ($quirks & TQ_DUEDATE)
112                         $options |= TAX_DUEDATE;
113
114                 if ($quirks & TQ_NONDEDUCT)
115                         $options &= ~TAX_DEDUCTIBLE;
116
117                 if ($quirks & TQ_PARTIAL)
118                         if ($options & TAX_DEDUCTIBLE)
119                                 $options |= TAX_PARTIAL;
120
121                 if ($quirks & TQ_IMPDEDUCTIBLE)
122                         if ($tax_area == TA_EXPORT && in_array($trans_type, array(ST_SUPPINVOICE, ST_SUPPCREDIT)))
123                         {
124                                 $options |= TAX_DEDUCTIBLE | TAX_PAYABLE;
125                         }
126
127                 if (($quirks & TQ_REVERSE) && (!in_array($trans_type,array(ST_SALESINVOICE, ST_CUSTCREDIT)) || $allow_reverse))
128                         if ($tax_area == TA_DOMESTIC)
129                         {
130                                 $options ^= TAX_PAYABLE;
131                                 $options &= ~TAX_CHARGED;
132                         }
133
134                 return $options;
135         }
136 };
137
138 function dbg_tax($options)
139 {
140         $dbg_rules = array(
141                 TAX_PAYABLE => 'payable',
142                 TAX_DEDUCTIBLE => 'deductible',
143                 TAX_CHARGED => 'charged',
144                 TAX_DUEDATE => 'due_date',
145                 TAX_PARTIAL => 'partial'
146         );
147
148         $opts = array();
149         foreach($dbg_rules as $key => $name)
150                 if ($options & $key)
151                         $opts[] = $name;
152
153         _vd(implode($opts, ','));
154 }