| 1 |
<?php |
| 2 |
/** |
| 3 |
* Welcart_Tax class. |
| 4 |
* |
| 5 |
* @package Welcart |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! defined( 'ABSPATH' ) ) { |
| 9 |
exit; |
| 10 |
} |
| 11 |
|
| 12 |
/** |
| 13 |
* Welcart_Tax class |
| 14 |
*/ |
| 15 |
class Welcart_Tax { |
| 16 |
|
| 17 |
/** |
| 18 |
* Instance of this class. |
| 19 |
* |
| 20 |
* @var object |
| 21 |
*/ |
| 22 |
protected static $instance = null; |
| 23 |
|
| 24 |
/** |
| 25 |
* Standard tax rate. |
| 26 |
* Set by the user in the basic settings. |
| 27 |
* |
| 28 |
* @var int |
| 29 |
*/ |
| 30 |
public $tax_rate_standard; |
| 31 |
|
| 32 |
/** |
| 33 |
* Reduced tax rate. |
| 34 |
* Set by the user in the basic settings. |
| 35 |
* |
| 36 |
* @var int |
| 37 |
*/ |
| 38 |
public $tax_rate_reduced; |
| 39 |
|
| 40 |
/** |
| 41 |
* Total amount of items. |
| 42 |
* |
| 43 |
* @var float |
| 44 |
*/ |
| 45 |
public $item_total_price; |
| 46 |
|
| 47 |
/** |
| 48 |
* Subtotal of standard tax rate. |
| 49 |
* |
| 50 |
* @var float |
| 51 |
*/ |
| 52 |
public $subtotal_standard; |
| 53 |
|
| 54 |
/** |
| 55 |
* Subtotal of reduced tax rate. |
| 56 |
* |
| 57 |
* @var float |
| 58 |
*/ |
| 59 |
public $subtotal_reduced; |
| 60 |
|
| 61 |
/** |
| 62 |
* Discount of standard tax rate. |
| 63 |
* |
| 64 |
* @var float |
| 65 |
*/ |
| 66 |
public $discount_standard; |
| 67 |
|
| 68 |
/** |
| 69 |
* Discount of reduced tax rate. |
| 70 |
* |
| 71 |
* @var float |
| 72 |
*/ |
| 73 |
public $discount_reduced; |
| 74 |
|
| 75 |
/** |
| 76 |
* Discount. |
| 77 |
* |
| 78 |
* @var float |
| 79 |
*/ |
| 80 |
public $discount; |
| 81 |
|
| 82 |
/** |
| 83 |
* Instance of this class. |
| 84 |
* |
| 85 |
* @var float |
| 86 |
*/ |
| 87 |
public $tax_standard; |
| 88 |
|
| 89 |
/** |
| 90 |
* Instance of this class. |
| 91 |
* |
| 92 |
* @var float |
| 93 |
*/ |
| 94 |
public $tax_reduced; |
| 95 |
|
| 96 |
/** |
| 97 |
* Tax amount. |
| 98 |
* |
| 99 |
* @var float |
| 100 |
*/ |
| 101 |
public $tax; |
| 102 |
|
| 103 |
/** |
| 104 |
* Instance of this class. |
| 105 |
* |
| 106 |
* @var array |
| 107 |
*/ |
| 108 |
public $cart_standard; |
| 109 |
|
| 110 |
/** |
| 111 |
* Instance of this class. |
| 112 |
* |
| 113 |
* @var array |
| 114 |
*/ |
| 115 |
public $cart_reduced; |
| 116 |
|
| 117 |
/** |
| 118 |
* Reduced tax rate mark. |
| 119 |
* |
| 120 |
* @var string |
| 121 |
*/ |
| 122 |
public $reduced_taxrate_mark; |
| 123 |
|
| 124 |
/** |
| 125 |
* Constructor. |
| 126 |
*/ |
| 127 |
public function __construct() { |
| 128 |
$this->initialize_data(); |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Return an instance of this class. |
| 133 |
*/ |
| 134 |
public static function get_instance() { |
| 135 |
if ( is_null( self::$instance ) ) { |
| 136 |
self::$instance = new self(); |
| 137 |
} |
| 138 |
return self::$instance; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Initialize. |
| 143 |
*/ |
| 144 |
public function initialize_data() { |
| 145 |
$options = get_option( 'usces', array() ); |
| 146 |
$this->tax_rate_standard = ( isset( $options['tax_rate'] ) ) ? (int) $options['tax_rate'] : 0; |
| 147 |
$this->tax_rate_reduced = ( isset( $options['tax_rate_reduced'] ) ) ? (int) $options['tax_rate_reduced'] : 0; |
| 148 |
$this->item_total_price = 0; |
| 149 |
$this->subtotal_standard = 0; |
| 150 |
$this->subtotal_reduced = 0; |
| 151 |
$this->discount_standard = 0; |
| 152 |
$this->discount_reduced = 0; |
| 153 |
$this->discount = 0; |
| 154 |
$this->tax_standard = 0; |
| 155 |
$this->tax_reduced = 0; |
| 156 |
$this->tax = 0; |
| 157 |
$this->cart_standard = array(); |
| 158 |
$this->cart_reduced = array(); |
| 159 |
} |
| 160 |
|
| 161 |
/** |
| 162 |
* Load textdomain. |
| 163 |
*/ |
| 164 |
public function load_textdomain() { |
| 165 |
$this->reduced_taxrate_mark = apply_filters( 'usces_filter_reduced_taxrate_mark', __( '(*)', 'usces' ) ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Get sku applicable taxrate. |
| 170 |
* |
| 171 |
* @param int $post_id Post ID. |
| 172 |
* @param array $sku SKU data. |
| 173 |
* @return string |
| 174 |
*/ |
| 175 |
public function get_sku_applicable_taxrate( $post_id, $sku ) { |
| 176 |
global $usces; |
| 177 |
|
| 178 |
$skus = $usces->get_skus( $post_id, 'code' ); |
| 179 |
if ( isset( $skus[ $sku ]['taxrate'] ) ) { |
| 180 |
$applicable_taxrate = $skus[ $sku ]['taxrate']; |
| 181 |
} else { |
| 182 |
$applicable_taxrate = 'standard'; |
| 183 |
} |
| 184 |
return $applicable_taxrate; |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Set sku tax rate. |
| 189 |
* |
| 190 |
* @param int $post_id Post ID. |
| 191 |
* @param array $sku SKU data. |
| 192 |
* @return string |
| 193 |
*/ |
| 194 |
public function get_sku_tax_rate( $post_id, $sku ) { |
| 195 |
global $usces; |
| 196 |
|
| 197 |
$applicable_taxrate = $this->get_sku_applicable_taxrate( $post_id, $sku ); |
| 198 |
if ( 'reduced' === $applicable_taxrate ) { |
| 199 |
$tax_rate = $usces->options['tax_rate_reduced']; |
| 200 |
} else { |
| 201 |
$tax_rate = $usces->options['tax_rate']; |
| 202 |
} |
| 203 |
return $tax_rate; |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Set ordercart applicable taxrate. |
| 208 |
* |
| 209 |
* @param string $ordercart_id Order cart ID. |
| 210 |
* @param array $sku SKU data. |
| 211 |
*/ |
| 212 |
public function set_ordercart_applicable_taxrate( $ordercart_id, $sku ) { |
| 213 |
global $usces, $wpdb; |
| 214 |
|
| 215 |
if ( isset( $sku['taxrate'] ) && 'reduced' === $sku['taxrate'] ) { |
| 216 |
$tkey = 'reduced'; |
| 217 |
$tvalue = $usces->options['tax_rate_reduced']; |
| 218 |
} else { |
| 219 |
$tkey = 'standard'; |
| 220 |
$tvalue = $usces->options['tax_rate']; |
| 221 |
} |
| 222 |
$query = $wpdb->prepare( |
| 223 |
"INSERT INTO {$wpdb->prefix}usces_ordercart_meta ( cart_id, meta_type, meta_key, meta_value ) VALUES ( %d, 'taxrate', %s, %s )", |
| 224 |
$ordercart_id, |
| 225 |
$tkey, |
| 226 |
$tvalue |
| 227 |
); |
| 228 |
$wpdb->query( $query ); |
| 229 |
} |
| 230 |
|
| 231 |
/** |
| 232 |
* Get ordercart applicable taxrate. |
| 233 |
* |
| 234 |
* @param string $ordercart_id Order cart ID. |
| 235 |
* @param int $post_id Post ID. |
| 236 |
* @param array $sku SKU data. |
| 237 |
* @return string |
| 238 |
*/ |
| 239 |
public function get_ordercart_applicable_taxrate( $ordercart_id, $post_id = 0, $sku = array() ) { |
| 240 |
$taxrate = usces_get_ordercart_meta( 'taxrate', $ordercart_id ); |
| 241 |
if ( $taxrate && isset( $taxrate[0]['meta_key'] ) ) { |
| 242 |
$applicable_taxrate = $taxrate[0]['meta_key']; |
| 243 |
} elseif ( ! empty( $post_id ) && ! empty( $sku ) ) { |
| 244 |
$applicable_taxrate = $this->get_sku_applicable_taxrate( $post_id, $sku ); |
| 245 |
} else { |
| 246 |
$applicable_taxrate = 'standard'; |
| 247 |
} |
| 248 |
return $applicable_taxrate; |
| 249 |
} |
| 250 |
|
| 251 |
/** |
| 252 |
* Set cart. |
| 253 |
* |
| 254 |
* @param array $cart Cart data. |
| 255 |
*/ |
| 256 |
public function set_cart( $cart = array() ) { |
| 257 |
global $usces; |
| 258 |
|
| 259 |
if ( empty( $cart ) ) { |
| 260 |
$cart = $usces->cart->get_cart(); |
| 261 |
} |
| 262 |
|
| 263 |
$this->subtotal_standard = 0; |
| 264 |
$this->subtotal_reduced = 0; |
| 265 |
$this->cart_standard = array(); |
| 266 |
$this->cart_reduced = array(); |
| 267 |
|
| 268 |
foreach ( (array) $cart as $cart_row ) { |
| 269 |
$items_price = (float) $cart_row['price'] * (float) $cart_row['quantity']; |
| 270 |
if ( isset( $cart_row['cart_id'] ) ) { |
| 271 |
$taxrate = usces_get_ordercart_meta( 'taxrate', $cart_row['cart_id'] ); |
| 272 |
if ( $taxrate && isset( $taxrate[0]['meta_key'] ) ) { |
| 273 |
if ( 'reduced' === $taxrate[0]['meta_key'] ) { |
| 274 |
$this->subtotal_reduced += (float) $items_price; |
| 275 |
$this->cart_reduced[] = $cart_row; |
| 276 |
} else { |
| 277 |
$this->subtotal_standard += (float) $items_price; |
| 278 |
$this->cart_standard[] = $cart_row; |
| 279 |
} |
| 280 |
} else { |
| 281 |
$sku = ( isset( $cart_row['sku_code'] ) ) ? $cart_row['sku_code'] : urldecode( $cart_row['sku'] ); |
| 282 |
$skus = $usces->get_skus( $cart_row['post_id'], 'code' ); |
| 283 |
if ( isset( $skus[ $sku ]['taxrate'] ) && 'reduced' === $skus[ $sku ]['taxrate'] ) { |
| 284 |
$this->subtotal_reduced += (float) $items_price; |
| 285 |
$this->cart_reduced[] = $cart_row; |
| 286 |
} else { |
| 287 |
$this->subtotal_standard += (float) $items_price; |
| 288 |
$this->cart_standard[] = $cart_row; |
| 289 |
} |
| 290 |
} |
| 291 |
} else { |
| 292 |
$sku = urldecode( $cart_row['sku'] ); |
| 293 |
$skus = $usces->get_skus( $cart_row['post_id'], 'code' ); |
| 294 |
if ( isset( $skus[ $sku ]['taxrate'] ) && 'reduced' === $skus[ $sku ]['taxrate'] ) { |
| 295 |
$this->subtotal_reduced += (float) $items_price; |
| 296 |
$this->cart_reduced[] = $cart_row; |
| 297 |
} else { |
| 298 |
$this->subtotal_standard += (float) $items_price; |
| 299 |
$this->cart_standard[] = $cart_row; |
| 300 |
} |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
/** |
| 306 |
* Get order discount. |
| 307 |
* |
| 308 |
* @param array $cart Cart data. |
| 309 |
* @param array $condition Condition data. |
| 310 |
* @return float |
| 311 |
*/ |
| 312 |
public function get_order_discount( $cart = array(), $condition = array() ) { |
| 313 |
global $usces; |
| 314 |
|
| 315 |
if ( empty( $cart ) ) { |
| 316 |
$cart = $usces->cart->get_cart(); |
| 317 |
} |
| 318 |
if ( empty( $condition ) ) { |
| 319 |
$condition = $usces->get_condition(); |
| 320 |
} |
| 321 |
|
| 322 |
$this->set_cart( $cart ); |
| 323 |
$this->discount_standard = 0; |
| 324 |
$this->discount_reduced = 0; |
| 325 |
$this->discount = 0; |
| 326 |
|
| 327 |
if ( 'Promotionsale' === $condition['display_mode'] ) { |
| 328 |
if ( 'discount' === $condition['campaign_privilege'] ) { |
| 329 |
if ( 0 === (int) $condition['campaign_category'] ) { |
| 330 |
foreach ( (array) $this->cart_standard as $cart_row ) { |
| 331 |
$items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 ); |
| 332 |
$this->discount_standard += $items_discount; |
| 333 |
} |
| 334 |
foreach ( (array) $this->cart_reduced as $cart_row ) { |
| 335 |
$items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 ); |
| 336 |
$this->discount_reduced += $items_discount; |
| 337 |
} |
| 338 |
} else { |
| 339 |
foreach ( (array) $this->cart_standard as $cart_row ) { |
| 340 |
if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) { |
| 341 |
$items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 ); |
| 342 |
$this->discount_standard += $items_discount; |
| 343 |
} |
| 344 |
} |
| 345 |
foreach ( (array) $this->cart_reduced as $cart_row ) { |
| 346 |
if ( in_category( (int) $condition['campaign_category'], $cart_row['post_id'] ) ) { |
| 347 |
$items_discount = (float) sprintf( '%.3f', (float) $cart_row['price'] * (float) $cart_row['quantity'] * (float) $condition['privilege_discount'] / 100 ); |
| 348 |
$this->discount_reduced += $items_discount; |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
if ( 0.0 !== (float) $this->discount_standard || 0.0 !== (float) $this->discount_reduced ) { |
| 353 |
$decimal = $usces->get_currency_decimal(); |
| 354 |
if ( 0 === (int) $decimal ) { |
| 355 |
$this->discount_standard = ceil( $this->discount_standard ); |
| 356 |
$this->discount_reduced = ceil( $this->discount_reduced ); |
| 357 |
} else { |
| 358 |
$decipad = (int) str_pad( '1', (int) $decimal + 1, '0', STR_PAD_RIGHT ); |
| 359 |
$this->discount_standard = ceil( $this->discount_standard * $decipad ) / $decipad; |
| 360 |
$this->discount_reduced = ceil( $this->discount_reduced * $decipad ) / $decipad; |
| 361 |
} |
| 362 |
$this->discount_standard *= -1; |
| 363 |
$this->discount_reduced *= -1; |
| 364 |
$this->discount = $this->discount_standard + $this->discount_reduced; |
| 365 |
} |
| 366 |
} |
| 367 |
} |
| 368 |
$this->discount = apply_filters( 'usces_order_discount', $this->discount, $cart ); |
| 369 |
return $this->discount; |
| 370 |
} |
| 371 |
|
| 372 |
/** |
| 373 |
* Get order tax amount. |
| 374 |
* |
| 375 |
* @param array $materials Compact array( $total_items_price $shipping_charge $discount $cod_fee $use_point $carts $condition ). |
| 376 |
*/ |
| 377 |
public function get_order_tax( $materials = array() ) { |
| 378 |
global $usces; |
| 379 |
|
| 380 |
$this->initialize_data(); |
| 381 |
if ( ! empty( $materials ) ) { |
| 382 |
extract( $materials ); |
| 383 |
} |
| 384 |
if ( empty( $carts ) ) { |
| 385 |
$cart = $usces->cart->get_cart(); |
| 386 |
} else { |
| 387 |
$cart = $carts; |
| 388 |
} |
| 389 |
if ( empty( $condition ) ) { |
| 390 |
$condition = $usces->get_condition(); |
| 391 |
} |
| 392 |
|
| 393 |
$this->tax_rate_standard = ( isset( $condition['tax_rate'] ) ) ? (int) $condition['tax_rate'] : (int) $usces->options['tax_rate']; |
| 394 |
$this->tax_rate_reduced = ( isset( $condition['tax_rate_reduced'] ) ) ? (int) $condition['tax_rate_reduced'] : (int) $usces->options['tax_rate_reduced']; |
| 395 |
|
| 396 |
$reduced_taxrate_before = ( ! empty( $order_id ) ) ? $this->was_reduced_taxrate_before( $order_id ) : false; |
| 397 |
if ( $reduced_taxrate_before ) { |
| 398 |
$this->set_cart( $cart ); |
| 399 |
$this->subtotal_standard = (float) $usces->get_order_meta_value( 'subtotal_standard', $order_id ); |
| 400 |
$this->subtotal_reduced = (float) $usces->get_order_meta_value( 'subtotal_reduced', $order_id ); |
| 401 |
$this->discount_standard = (float) $usces->get_order_meta_value( 'discount_standard', $order_id ); |
| 402 |
$this->discount_reduced = (float) $usces->get_order_meta_value( 'discount_reduced', $order_id ); |
| 403 |
$this->discount = $this->discount_standard + $this->discount_reduced; |
| 404 |
$this->item_total_price = (float) $total_items_price; |
| 405 |
} else { |
| 406 |
$this->get_order_discount( $cart, $condition ); |
| 407 |
$this->item_total_price = $this->subtotal_standard + $this->subtotal_reduced; |
| 408 |
if ( 'all' === $condition['tax_target'] ) { |
| 409 |
if ( ! empty( $shipping_charge ) ) { |
| 410 |
$this->subtotal_standard += (float) $shipping_charge; |
| 411 |
} |
| 412 |
if ( ! empty( $cod_fee ) ) { |
| 413 |
$this->subtotal_standard += (float) $cod_fee; |
| 414 |
} |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
if ( 'include' === $condition['tax_mode'] ) { |
| 419 |
if ( 0 < $this->subtotal_standard ) { |
| 420 |
$this->tax_standard = (float) sprintf( '%.3f', ( (float) $this->subtotal_standard + (float) $this->discount_standard ) * $this->tax_rate_standard / ( 100 + $this->tax_rate_standard ) ); |
| 421 |
} |
| 422 |
if ( 0 < $this->subtotal_reduced ) { |
| 423 |
$this->tax_reduced = (float) sprintf( '%.3f', ( (float) $this->subtotal_reduced + (float) $this->discount_reduced ) * $this->tax_rate_reduced / ( 100 + $this->tax_rate_reduced ) ); |
| 424 |
} |
| 425 |
} else { |
| 426 |
if ( 0 < $this->subtotal_standard ) { |
| 427 |
$this->tax_standard = (float) sprintf( '%.3f', ( (float) $this->subtotal_standard + (float) $this->discount_standard ) * $this->tax_rate_standard / 100 ); |
| 428 |
} |
| 429 |
if ( 0 < $this->subtotal_reduced ) { |
| 430 |
$this->tax_reduced = (float) sprintf( '%.3f', ( (float) $this->subtotal_reduced + (float) $this->discount_reduced ) * $this->tax_rate_reduced / 100 ); |
| 431 |
} |
| 432 |
} |
| 433 |
$tax_method = ( isset( $condition['tax_method'] ) ) ? $condition['tax_method'] : ''; |
| 434 |
$this->tax_standard = usces_tax_rounding_off( $this->tax_standard, $tax_method ); |
| 435 |
$this->tax_reduced = usces_tax_rounding_off( $this->tax_reduced, $tax_method ); |
| 436 |
$this->tax = $this->tax_standard + $this->tax_reduced; |
| 437 |
} |
| 438 |
|
| 439 |
/** |
| 440 |
* Set order condition reduced taxrate. |
| 441 |
* |
| 442 |
* @param int $order_id Order ID. |
| 443 |
*/ |
| 444 |
public function set_order_condition_reduced_taxrate( $order_id ) { |
| 445 |
global $usces, $wpdb; |
| 446 |
|
| 447 |
$condition = usces_get_order_condition( $order_id ); |
| 448 |
$condition['tax_mode'] = $usces->options['tax_mode']; |
| 449 |
$condition['tax_target'] = $usces->options['tax_target']; |
| 450 |
$condition['tax_rate'] = $usces->options['tax_rate']; |
| 451 |
$condition['applicable_taxrate'] = ( isset( $usces->options['applicable_taxrate'] ) ) ? $usces->options['applicable_taxrate'] : 'standard'; |
| 452 |
$condition['tax_rate_reduced'] = ( isset( $usces->options['tax_rate_reduced'] ) ) ? $usces->options['tax_rate_reduced'] : $usces->options['tax_rate']; |
| 453 |
$query = $wpdb->prepare( "UPDATE {$wpdb->prefix}usces_order SET order_condition = %s WHERE ID = %d", serialize( $condition ), $order_id ); |
| 454 |
$wpdb->query( $query ); |
| 455 |
} |
| 456 |
|
| 457 |
/** |
| 458 |
* Was reduced taxrate before. |
| 459 |
* |
| 460 |
* @param int $order_id Order ID. |
| 461 |
* @return string |
| 462 |
*/ |
| 463 |
public function was_reduced_taxrate_before( $order_id ) { |
| 464 |
global $usces; |
| 465 |
|
| 466 |
$subtotal_standard = $usces->get_order_meta_value( 'subtotal_standard', $order_id ); |
| 467 |
$subtotal_reduced = $usces->get_order_meta_value( 'subtotal_reduced', $order_id ); |
| 468 |
return ( $subtotal_standard || $subtotal_reduced ); |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
new Welcart_Tax(); |
| 473 |
|