public function __construct($taxable) { $Tax = ShoppOrder()->Tax; $taxes = array(); $Tax->rates($taxes); $firstrate = reset($taxes); if ($firstrate) { $this->rate = $firstrate->rate; } $this->id = 'shipping'; $this->amount = ShoppTax::calculate($taxes, $taxable); $this->label = Shopp::__('Shipping Tax'); }
public function prepare_items() { $this->id = 'taxrates'; $defaults = array('paged' => 1, 'per_page' => 25, 'action' => false); $args = array_merge($defaults, $_GET); extract($args, EXTR_SKIP); $rates = (array) shopp_setting('taxrates'); $this->items = array(); foreach ($rates as $index => $taxrate) { $this->items[$index] = array_merge(self::$template, array('id' => $index), $taxrate); } $specials = array(ShoppTax::ALL => Shopp::__('All Markets')); if (ShoppTax::euvat(false, ShoppBaseLocale()->country(), ShoppTax::EUVAT)) { $specials[ShoppTax::EUVAT] = Shopp::__('European Union'); } $this->countries = array_filter(array_merge($specials, (array) shopp_setting('target_markets'))); $this->zones = ShoppLookup::country_zones(); $total = count($this->items); $this->set_pagination_args(array('total_items' => $total, 'total_pages' => $total / $per_page, 'per_page' => $per_page)); shopp_custom_script('taxrates', ' var suggurl = "' . wp_nonce_url(admin_url('admin-ajax.php'), 'wp_ajax_shopp_suggestions') . '", rates = ' . json_encode($this->items) . ', zones = ' . json_encode($this->zones) . ', lookup = ' . json_encode(ShoppLookup::localities()) . ', taxrates = []; '); }
/** * Calculate taxes that apply to the item * * @author Jonathan Davis * @since 1.3 * * @param integer $quantity The taxable quantity of items * @return void **/ public function taxes($quantity = 1) { if (!$this->istaxed) { return do_action('shopp_cart_item_taxes', $this); } $Tax = ShoppOrder()->Tax; if (empty($Tax)) { $Tax = new ShoppTax(); } // ShoppTax support for Dev API calls // For all the price units (base product and any addons), // distribute discounts across taxable amounts using weighted averages $_ = array(); if ($this->unitprice > 0) { $taxable = 0; foreach ($this->taxable as $amount) { $_[] = $amount - $amount / $this->unitprice * $this->discount; } } $taxable = (double) array_sum($_); $taxableqty = $this->bogof && $this->bogof != $this->quantity ? $this->quantity - $this->bogof : $this->quantity; $Tax->rates($this->taxes, $Tax->item($this)); $this->unittax = ShoppTax::calculate($this->taxes, $taxable); $this->tax = $Tax->total($this->taxes, (int) $taxableqty); // Handle inclusive tax price adjustments for non-EU markets or alternate tax rate markets $adjustment = ShoppTax::adjustment($this->taxes); if (1 != $adjustment) { if (!isset($this->taxprice)) { $this->taxprice = $this->unitprice; } // Modify the unitprice from the original tax inclusive price and update the discounted price $this->unitprice = $this->taxprice / $adjustment; $this->priced = $this->unitprice - $this->discount; } elseif (isset($this->taxprice)) { // Undo tax price adjustments $this->unitprice = $this->taxprice; unset($this->taxprice); } do_action('shopp_cart_item_taxes', $this); }
public function taxrates() { if (!current_user_can('shopp_settings_taxes')) { wp_die(__('You do not have sufficient permissions to access this page.')); } $edit = false; if (isset($_REQUEST['id'])) { $edit = (int) $_REQUEST['id']; } $localerror = false; $rates = shopp_setting('taxrates'); if (!is_array($rates)) { $rates = array(); } if (isset($_GET['delete'])) { check_admin_referer('shopp_delete_taxrate'); $delete = (int) $_GET['delete']; if (isset($rates[$delete])) { array_splice($rates, $delete, 1); } shopp_set_setting('taxrates', $rates); } if (isset($_POST['editing'])) { $rates[$edit] = $_POST['settings']['taxrates'][$edit]; } if (isset($_POST['addrule'])) { $rates[$edit]['rules'][] = array('p' => '', 'v' => ''); } if (isset($_POST['deleterule'])) { check_admin_referer('shopp-settings-taxrates'); list($rateid, $row) = explode(',', $_POST['deleterule']); if (isset($rates[$rateid]) && isset($rates[$rateid]['rules'])) { array_splice($rates[$rateid]['rules'], $row, 1); shopp_set_setting('taxrates', $rates); } } if (isset($rates[$edit]['haslocals'])) { $rates[$edit]['haslocals'] = $rates[$edit]['haslocals'] == 'true' || $rates[$edit]['haslocals'] == '1'; } if (isset($_POST['add-locals'])) { $rates[$edit]['haslocals'] = true; } if (isset($_POST['remove-locals'])) { $rates[$edit]['haslocals'] = false; $rates[$edit]['locals'] = array(); } $upload = $this->taxrate_upload(); if ($upload !== false) { if (isset($upload['error'])) { $localerror = $upload['error']; } else { $rates[$edit]['locals'] = $upload; } } if (isset($_POST['editing'])) { // Re-sort taxes from generic to most specific usort($rates, array($this, 'taxrates_sorting')); $rates = stripslashes_deep($rates); shopp_set_setting('taxrates', $rates); } if (isset($_POST['addrate'])) { $edit = count($rates); } if (isset($_POST['submit'])) { $edit = false; } $base = shopp_setting('base_operations'); $specials = array(ShoppTax::ALL => Shopp::__('All Markets')); if (ShoppTax::euvat(false, $base['country'], ShoppTax::EUVAT)) { $specials[ShoppTax::EUVAT] = Shopp::__('European Union'); } $countries = array_merge($specials, (array) shopp_setting('target_markets')); $zones = Lookup::country_zones(); include $this->ui('taxrates.php'); }
/** * Helper to apply or exclude taxes from a single amount based on inclusive tax settings and the tax option * * @author Jonathan Davis * @since 1.3 * * @param float $amount The amount to add taxes to, or exclude taxes from * @param ShoppProduct $O The product to get properties from * @param boolean $istaxed Whether the amount can be taxed * @param boolean $taxoption The Theme API tax option given the the tag * @param array $taxrates A list of taxrates that apply to the product and amount * @return float The amount with tax added or tax excluded **/ private static function _taxed($amount, ShoppProduct $O, $istaxed, $taxoption = null, array $taxrates = array()) { if (!$istaxed) { return $amount; } if (empty($taxrates)) { $taxrates = Shopp::taxrates($O); } if (isset($taxoption)) { $taxoption = Shopp::str_true($taxoption); } $inclusivetax = self::_inclusive_taxes($O); if ($inclusivetax) { $adjustment = ShoppTax::adjustment($taxrates); if (1 != $adjustment && false !== $taxoption) { // Only adjust when taxes are not excluded @see #3041 return (double) ($amount / $adjustment); } } // Handle inclusive/exclusive tax presentation options (product editor setting or api option) // If the 'taxes' option is specified and the item either has inclusive taxes that apply, // or the 'taxes' option is forced on (but not both) then handle taxes by either adding or excluding taxes // This is an exclusive or known as XOR, the lesser known brother of Thor that gets left out of the family get togethers if (isset($taxoption) && $inclusivetax ^ $taxoption) { if ($taxoption) { return ShoppTax::calculate($taxrates, (double) $amount); } else { return ShoppTax::exclusive($taxrates, (double) $amount); } } return $amount; }
/** * Determines all applicable tax rates for the store or an item * * @author Jonathan Davis * @since 1.0 * @version 1.3 * * @param Object $Item (optional) The ShoppProduct, ShoppCartItem or ShoppPurchased object to find tax rates for * @return float The determined tax rate **/ public static function taxrates($Item = null) { $Tax = new ShoppTax(); $Order = ShoppOrder(); // Setup taxable address $Tax->address($Order->Billing, $Order->Shipping, $Order->Cart->shipped()); $taxes = array(); if (is_null($Item)) { $Tax->rates($taxes); } else { $Tax->rates($taxes, $Tax->item($Item)); } return apply_filters('shopp_taxrates', $taxes); }
public static function adjustment($rates) { if (!shopp_setting_enabled('tax_inclusive')) { return 1; } $baserates = ShoppTax::baserates(); $baserate = reset($baserates); $appliedrate = reset($rates); $baserate = isset($baserate->rate) ? $baserate->rate : 0; $appliedrate = isset($appliedrate->rate) ? $appliedrate->rate : 0; return 1 + ($baserate - $appliedrate); }