Use new code style
This commit is contained in:
@ -9,7 +9,7 @@
|
||||
// Exit if accessed directly
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
use \WP_Ultimo\Tax\Tax;
|
||||
use WP_Ultimo\Tax\Tax;
|
||||
|
||||
/**
|
||||
* Checks if WP Multisite WaaS should collect taxes.
|
||||
@ -20,8 +20,7 @@ use \WP_Ultimo\Tax\Tax;
|
||||
function wu_should_collect_taxes() {
|
||||
|
||||
return (bool) wu_get_setting('enable_taxes', false);
|
||||
|
||||
} // end wu_should_collect_taxes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tax categories.
|
||||
@ -32,8 +31,7 @@ function wu_should_collect_taxes() {
|
||||
function wu_get_tax_categories() {
|
||||
|
||||
return Tax::get_instance()->get_tax_rates();
|
||||
|
||||
} // end wu_get_tax_categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a given tax category
|
||||
@ -46,11 +44,14 @@ function wu_get_tax_category($tax_category = 'default') {
|
||||
|
||||
$tax_categories = wu_get_tax_categories();
|
||||
|
||||
return wu_get_isset($tax_categories, $tax_category, array(
|
||||
'rates' => array(),
|
||||
));
|
||||
|
||||
} // end wu_get_tax_category;
|
||||
return wu_get_isset(
|
||||
$tax_categories,
|
||||
$tax_category,
|
||||
array(
|
||||
'rates' => array(),
|
||||
)
|
||||
);
|
||||
}
|
||||
/**
|
||||
* Returns the tax categories as a slug => name array.
|
||||
*
|
||||
@ -59,8 +60,7 @@ function wu_get_tax_category($tax_category = 'default') {
|
||||
function wu_get_tax_categories_as_options(): array {
|
||||
|
||||
return array_map(fn($item) => $item['name'], wu_get_tax_categories());
|
||||
|
||||
} // end wu_get_tax_categories_as_options;
|
||||
}
|
||||
/**
|
||||
* Calculates the tax value.
|
||||
*
|
||||
@ -81,8 +81,7 @@ function wu_get_tax_amount($base_price, $amount, $type, $format = true, $inclusi
|
||||
$tax_total = $amount;
|
||||
|
||||
if ($type === 'percentage') {
|
||||
|
||||
if (!$inclusive) {
|
||||
if ( ! $inclusive) {
|
||||
|
||||
/**
|
||||
* Exclusive tax
|
||||
@ -90,7 +89,6 @@ function wu_get_tax_amount($base_price, $amount, $type, $format = true, $inclusi
|
||||
* Calculates tax to be ADDED to the order.
|
||||
*/
|
||||
$tax_total = $base_price * ($amount / 100);
|
||||
|
||||
} else {
|
||||
|
||||
/**
|
||||
@ -99,24 +97,19 @@ function wu_get_tax_amount($base_price, $amount, $type, $format = true, $inclusi
|
||||
* Calculates the tax value inside the total price.
|
||||
*/
|
||||
$tax_total = $base_price - ($base_price / (1 + ($amount / 100)));
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Return results
|
||||
*/
|
||||
|
||||
if (!$format) {
|
||||
|
||||
if ( ! $format) {
|
||||
return round($tax_total, 2);
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
return number_format((float) $tax_total, 2);
|
||||
|
||||
} // end wu_get_tax_amount;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for applicable tax rates based on the country.
|
||||
@ -134,11 +127,9 @@ function wu_get_tax_amount($base_price, $amount, $type, $format = true, $inclusi
|
||||
*/
|
||||
function wu_get_applicable_tax_rates($country, $tax_category = 'default', $state = '*', $city = '*') {
|
||||
|
||||
if (!$country) {
|
||||
|
||||
if ( ! $country) {
|
||||
return array();
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
$tax_category = wu_get_tax_category($tax_category);
|
||||
|
||||
@ -146,84 +137,69 @@ function wu_get_applicable_tax_rates($country, $tax_category = 'default', $state
|
||||
|
||||
foreach ($tax_category['rates'] as &$rate) {
|
||||
/*
|
||||
* Step 0: Prepare.
|
||||
* Step 0: Prepare.
|
||||
*/
|
||||
$rate['state'] = explode(',', (string) $rate['state']);
|
||||
$rate['city'] = explode(',', (string) $rate['city']);
|
||||
|
||||
$keys_of_interest = array_intersect_key($rate, array(
|
||||
'country' => 1,
|
||||
'state' => 1,
|
||||
'city' => 1,
|
||||
));
|
||||
$keys_of_interest = array_intersect_key(
|
||||
$rate,
|
||||
array(
|
||||
'country' => 1,
|
||||
'state' => 1,
|
||||
'city' => 1,
|
||||
)
|
||||
);
|
||||
|
||||
$priority = 0;
|
||||
|
||||
foreach ($keys_of_interest as $key => $value) {
|
||||
|
||||
$value = is_array($value) ? array_filter($value) : trim((string) $value);
|
||||
|
||||
/*
|
||||
* Step 1: The country.
|
||||
*/
|
||||
if ($key === 'country' && $rate['country'] === $country) {
|
||||
|
||||
$priority += 10;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 2: The state / province
|
||||
*/
|
||||
if ($key === 'state' && $state !== '*') {
|
||||
|
||||
if (in_array($state, $value, true)) {
|
||||
|
||||
$priority += 1;
|
||||
|
||||
} elseif (empty($value) || in_array('*', $value, true)) {
|
||||
|
||||
$priority += 0.5;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Step 3: The city
|
||||
*/
|
||||
if ($key === 'city' && $city !== '*') {
|
||||
|
||||
if (in_array($city, $value, true)) {
|
||||
/*
|
||||
* If it's a full match, gives 1 point.
|
||||
*/
|
||||
$priority += 1;
|
||||
|
||||
} elseif (empty($value) || in_array('*', $value, true)) {
|
||||
/*
|
||||
* If it is a wildcard, award half a point.
|
||||
*/
|
||||
$priority += 0.5;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ($priority >= 10) {
|
||||
|
||||
$rate['order'] = $priority;
|
||||
|
||||
$results[$rate['id']] = $rate;
|
||||
|
||||
} // end if;
|
||||
|
||||
} // end foreach;
|
||||
$results[ $rate['id'] ] = $rate;
|
||||
}
|
||||
}
|
||||
|
||||
uasort($results, 'wu_sort_by_order');
|
||||
|
||||
return array_values($results);
|
||||
|
||||
} // end wu_get_applicable_tax_rates;
|
||||
}
|
||||
|
Reference in New Issue
Block a user