]> gitweb.factorcode.org Git - factor.git/blob - extra/taxes/taxes.factor
Fixing everything for mandatory stack effects
[factor.git] / extra / taxes / taxes.factor
1 USING: arrays assocs kernel math math.intervals namespaces
2 sequences combinators.lib money math.order ;
3 IN: taxes
4
5 : monthly ( x -- y ) 12 / ;
6 : semimonthly ( x -- y ) 24 / ;
7 : biweekly ( x -- y ) 26 / ;
8 : weekly ( x -- y ) 52 / ;
9 : daily ( x -- y ) 360 / ;
10
11 ! Each employee fills out a w4
12 TUPLE: w4 year allowances married? ;
13 C: <w4> w4
14
15 : allowance ( -- x ) 3500 ; inline
16
17 : calculate-w4-allowances ( w4 -- x )
18     w4-allowances allowance * ;
19
20 ! Withhold: FICA, Medicare, Federal (FICA is social security)
21 : fica-tax-rate ( -- x ) DECIMAL: .062 ; inline
22
23 ! Base rate -- income over this rate is not taxed
24 TUPLE: fica-base-unknown ;
25 : fica-base-rate ( year -- x )
26     H{
27         { 2008 102000 }
28         { 2007  97500 }
29     } at* [ T{ fica-base-unknown } throw ] unless ;
30
31 : fica-tax ( salary w4 -- x )
32     w4-year fica-base-rate min fica-tax-rate * ;
33
34 ! Employer tax only, not withheld
35 : futa-tax-rate ( -- x ) DECIMAL: .062 ; inline
36 : futa-base-rate ( -- x ) 7000 ; inline
37 : futa-tax-offset-credit ( -- x ) DECIMAL: .054 ; inline
38
39 : futa-tax ( salary w4 -- x )
40     drop futa-base-rate min
41     futa-tax-rate futa-tax-offset-credit -
42     * ;
43
44 ! No base rate for medicare; all wages subject
45 : medicare-tax-rate ( -- x ) DECIMAL: .0145 ; inline
46 : medicare-tax ( salary w4 -- x ) drop medicare-tax-rate * ;
47
48 MIXIN: collector
49 GENERIC: adjust-allowances ( salary w4 collector -- newsalary )
50 GENERIC: withholding ( salary w4 collector -- x )
51
52 TUPLE: tax-table single married ;
53
54 : <tax-table> ( single married class -- obj )
55     >r tax-table boa r> construct-delegate ;
56
57 : tax-bracket-range ( pair -- n ) dup second swap first - ;
58
59 : tax-bracket ( tax salary triples -- tax salary )
60     [ [ tax-bracket-range min ] keep third * + ] 2keep
61     tax-bracket-range [-] ;
62
63 : tax ( salary triples -- x )
64     0 -rot [ tax-bracket ] each drop ;
65
66 : marriage-table ( w4 tax-table -- triples )
67     swap w4-married?
68     [ tax-table-married ] [ tax-table-single ] if ;
69
70 : federal-tax ( salary w4 tax-table -- n )
71     [ adjust-allowances ] 2keep marriage-table tax ;
72
73 ! http://www.irs.gov/pub/irs-pdf/p15.pdf
74 ! Table 7 ANNUAL Payroll Period 
75
76 : federal-single ( -- triples )
77     {
78         {      0   2650 DECIMAL: 0   }
79         {   2650  10300 DECIMAL: .10 }
80         {  10300  33960 DECIMAL: .15 }
81         {  33960  79725 DECIMAL: .25 }
82         {  79725 166500 DECIMAL: .28 }
83         { 166500 359650 DECIMAL: .33 }
84         { 359650   1/0. DECIMAL: .35 }
85     } ;
86
87 : federal-married ( -- triples )
88     {
89         {      0   8000 DECIMAL: 0   }
90         {   8000  23550 DECIMAL: .10 }
91         {  23550  72150 DECIMAL: .15 }
92         {  72150 137850 DECIMAL: .25 }
93         { 137850 207700 DECIMAL: .28 }
94         { 207700 365100 DECIMAL: .33 }
95         { 365100   1/0. DECIMAL: .35 }
96     } ;
97
98 TUPLE: federal ;
99 INSTANCE: federal collector
100 : <federal> ( -- obj )
101     federal-single federal-married federal <tax-table> ;
102
103 M: federal adjust-allowances ( salary w4 collector -- newsalary )
104     drop calculate-w4-allowances - ;
105
106 M: federal withholding ( salary w4 tax-table -- x )
107     [ federal-tax ] 3keep drop
108     [ fica-tax ] 2keep
109     medicare-tax + + ;
110
111
112 ! Minnesota
113 : minnesota-single ( -- triples )
114     {
115         {     0  1950  DECIMAL: 0     }
116         {  1950 23750  DECIMAL: .0535 }
117         { 23750 73540  DECIMAL: .0705 }
118         { 73540 1/0.   DECIMAL: .0785 }
119     } ;
120
121 : minnesota-married ( -- triples )
122     {
123         {      0   7400 DECIMAL: 0     }
124         {   7400  39260 DECIMAL: .0535 }
125         {  39260 133980 DECIMAL: .0705 }
126         { 133980   1/0. DECIMAL: .0785 }
127     } ;
128
129 TUPLE: minnesota ;
130 INSTANCE: minnesota collector
131 : <minnesota> ( -- obj )
132     minnesota-single minnesota-married minnesota <tax-table> ;
133
134 M: minnesota adjust-allowances ( salary w4 collector -- newsalary )
135     drop calculate-w4-allowances - ;
136
137 M: minnesota withholding ( salary w4 collector -- x )
138     [ adjust-allowances ] 2keep marriage-table tax ;
139
140 : employer-withhold ( salary w4 collector -- x )
141     [ withholding ] 3keep
142     dup federal? [ 3drop ] [ drop <federal> withholding + ] if ;
143
144 : net ( salary w4 collector -- x )
145     >r dupd r> employer-withhold - ;