1 <?php
2 3 4 5 6 7 8 9 10 11 12 13 14
15
16
17 if ( !defined( 'ABSPATH' ) ) exit;
18
19 20 21 22 23 24 25 26
27 function edd_use_taxes() {
28 global $edd_options;
29
30 return apply_filters( 'edd_use_taxes', isset( $edd_options['enable_taxes'] ) );
31 }
32
33 34 35 36 37 38 39 40
41 function edd_local_taxes_only() {
42 global $edd_options;
43
44 $local_only = isset( $edd_options['tax_condition'] ) && $edd_options['tax_condition'] == 'local';
45
46 return apply_filters( 'edd_local_taxes_only', $local_only );
47 }
48
49 50 51 52 53 54 55
56 function edd_local_tax_opted_in() {
57 $opted_in = EDD()->session->get( 'edd_local_tax_opt_in' );
58 return ! empty( $opted_in );
59 }
60
61 62 63 64 65 66 67
68 function edd_opt_into_local_taxes() {
69 EDD()->session->set( 'edd_local_tax_opt_in', '1' );
70 }
71
72 73 74 75 76 77 78
79 function edd_opt_out_local_taxes() {
80 EDD()->session->set( 'edd_local_tax_opt_in', null);
81 }
82
83 84 85 86 87 88 89
90 function edd_taxes_on_prices() {
91 global $edd_options;
92 return apply_filters( 'edd_taxes_on_prices', isset( $edd_options['taxes_on_prices'] ) );
93 }
94
95 96 97 98 99 100 101 102
103 function edd_taxes_after_discounts() {
104 global $edd_options;
105 return apply_filters( 'edd_taxes_after_discounts', isset( $edd_options['taxes_after_discounts'] ) );
106 }
107
108 109 110 111 112 113 114
115 function edd_get_tax_rate() {
116 global $edd_options;
117
118 $rate = isset( $edd_options['tax_rate'] ) ? (float) $edd_options['tax_rate'] : 0;
119
120 if( $rate > 1 ) {
121
122 $rate = $rate / 100;
123 }
124 return apply_filters( 'edd_tax_rate', $rate );
125 }
126
127 128 129 130 131 132 133
134 function edd_calculate_tax( $amount, $sum = true ) {
135 global $edd_options;
136
137
138 if ( !edd_use_taxes() ) return $amount;
139
140 $rate = edd_get_tax_rate();
141 $tax = 0;
142 $prices_include_tax = isset( $edd_options['prices_include_tax'] ) ? $edd_options['prices_include_tax'] : 'no';
143
144 if ( $prices_include_tax == 'yes' ) {
145 $tax = $amount - ( $amount / ( $rate + 1 ) );
146 }
147
148 if ( $prices_include_tax == 'no' ) {
149 $tax = $amount * $rate;
150 }
151
152 if ( $sum ) {
153
154 if ( $prices_include_tax == 'yes' ) {
155 $tax = $amount - $tax;
156 } else {
157 $tax = $amount + $tax;
158 }
159
160 }
161
162 $tax = round( $tax, 2 );
163 return apply_filters( 'edd_taxed_amount', $tax, $rate );
164 }
165
166 167 168 169 170 171 172 173
174 function edd_sales_tax_for_year( $year = null ) {
175 echo edd_currency_filter( edd_format_amount( edd_get_sales_tax_for_year( $year ) ) );
176 }
177
178 179 180 181 182 183 184 185
186 function edd_get_sales_tax_for_year( $year = null ) {
187 if ( empty( $year ) )
188 return 0;
189
190
191 $tax = 0;
192
193 $args = array(
194 'post_type' => 'edd_payment',
195 'posts_per_page' => -1,
196 'year' => $year,
197 'meta_key' => '_edd_payment_mode',
198 'meta_value' => edd_is_test_mode() ? 'test' : 'live',
199 'fields' => 'ids'
200 );
201
202 $payments = get_posts( $args );
203
204 if( $payments ) :
205
206 foreach( $payments as $payment ) :
207 $tax += edd_get_payment_tax( $payment );
208 endforeach;
209
210 endif;
211
212 return apply_filters( 'edd_get_sales_tax_for_year', $tax, $year );
213 }
214
215
216 217 218 219 220 221 222
223 function edd_prices_show_tax_on_checkout() {
224 global $edd_options;
225
226 $include_tax = isset( $edd_options['checkout_include_tax'] ) ? $edd_options['checkout_include_tax'] : 'no';
227
228 return ( $include_tax == 'yes' );
229 }
230
231 232 233 234 235 236 237
238 function edd_prices_include_tax() {
239 global $edd_options;
240
241 $include_tax = isset( $edd_options['prices_include_tax'] ) ? $edd_options['prices_include_tax'] : 'no';
242
243 return ( $include_tax == 'yes' );
244 }
245
246 247 248 249 250 251
252 function edd_is_cart_taxed() {
253 return edd_use_taxes() && ( ( edd_local_tax_opted_in() && edd_local_taxes_only() ) || ! edd_local_taxes_only() );
254 }