예제 #1
0
 function _tax_based_on_vendor_address($ship_to_info_id = '')
 {
     global $auth;
     global $vmLogger;
     switch (TAX_MODE) {
         case '0':
             return false;
         case '1':
             return true;
         case '17749':
             $ship_to_info_id = !empty($ship_to_info_id) ? $ship_to_info_id : vmGet($_REQUEST, 'ship_to_info_id');
             $db = new ps_DB();
             $q = "SELECT country FROM #__{vm}_user_info WHERE user_info_id='" . $ship_to_info_id . "'";
             $db->query($q);
             $db->next_record();
             $ship_country = $db->f("country");
             if (!array_key_exists('country', $auth) || empty($ship_country)) {
                 $vmLogger->debug('shopper\'s country is not known; defaulting to vendor-based tax');
                 return true;
             }
             if ($ship_to_info_id) {
                 $vmLogger->debug('shopper shipping in ' . $ship_country);
                 $auth_country = $ship_country;
             } else {
                 $vmLogger->debug('shopper is in ' . $auth['country']);
                 $auth_country = $auth['country'];
             }
             return ps_checkout::country_in_eu_common_vat_zone($auth_country);
         default:
             $vmLogger->warning('unknown TAX_MODE "' . TAX_MODE . '"');
             return true;
     }
 }
예제 #2
0
 function _tax_based_on_vendor_address()
 {
     global $auth;
     global $vmLogger;
     switch (TAX_MODE) {
         case '0':
             return false;
         case '1':
             return true;
         case '17749':
             if (!array_key_exists('country', $auth)) {
                 $vmLogger->debug('shopper\'s country is not known; defaulting to vendor-based tax');
                 return true;
             }
             $vmLogger->debug('shopper is in ' . $auth['country']);
             return ps_checkout::country_in_eu_common_vat_zone($auth['country']);
         default:
             $vmLogger->warning('unknown TAX_MODE "' . TAX_MODE . '"');
             return true;
     }
 }
예제 #3
0
 /**
  * Function to get the tax rate of product $product_id
  * If not found, it uses get_taxrate()
  *
  * @param int $product_id
  * @param int $weight_subtotal (tax virtual/zero-weight items?)
  * @return int The tax rate for the product
  */
 function get_product_taxrate($product_id, $weight_subtotal = 0, $ship_to_info_id = '')
 {
     global $vendor_country_3_code;
     $db = new ps_DB();
     // Product's weight_subtotal, if weight_subtotal is 0!
     $product_weight = ps_product::get_field($product_id, 'product_weight');
     if ($weight_subtotal == 0 && $product_weight > 0) {
         $weight_subtotal = $product_weight;
     }
     require_once CLASSPATH . 'ps_checkout.php';
     // EU TAX_MODE
     $auth = $_SESSION['auth'];
     $userid = $auth["user_id"];
     if ($userid > 0 && TAX_MODE == '17749' && ps_checkout::country_in_eu_common_vat_zone($vendor_country_3_code)) {
         $ship_country = '';
         $user_info_id = '';
         if (vmGet($_REQUEST, 'ship_to_info_id') || $ship_to_info_id) {
             if (!$ship_to_info_id) {
                 $ship_to_info_id = vmGet($_REQUEST, 'ship_to_info_id');
             }
             $q = "SELECT country FROM #__{vm}_user_info WHERE user_info_id='" . $ship_to_info_id . "'";
             $db->query($q);
             $db->next_record();
             $ship_country = $db->f("country");
         }
         if ($ship_to_info_id == '') {
             $ship_country = $auth["country"];
             $q = "SELECT user_info_id FROM #__{vm}_user_info WHERE user_id = '" . (int) $userid . "' AND address_type='BT'";
             $db->query($q);
             $user_info_id = $db->f("user_info_id");
         }
         // Check if user country is outside EU.
         $eu_vat = '';
         if (ps_checkout::country_in_eu_common_vat_zone($ship_country)) {
             $eu_vat = 'yes';
         }
         if ($eu_vat == 'yes') {
             $q = "SELECT name FROM #__{vm}_userfield WHERE type='euvatid' AND published=1";
             $db->query($q);
             $vatid_fieldname = $db->f('name');
         }
         //???
         if (!$eu_vat && ps_checkout::country_in_eu_common_vat_zone($vendor_country_3_code)) {
             $_SESSION['product_sess'][$product_id]['tax_rate'] = 0;
             return $_SESSION['product_sess'][$product_id]['tax_rate'];
         }
         // Handle TAX if EU VAT ID
         if ($eu_vat == 'yes' && $vendor_country_3_code != $ship_country) {
             $q = "SELECT country_2_code FROM #__{vm}_country WHERE country_3_code='" . $ship_country . "'";
             $db->query($q);
             $db->next_record();
             $ship_country_2_code = $db->f("country_2_code");
             if ($vatid_fieldname) {
                 $q = "SELECT `{$vatid_fieldname}` FROM #__{vm}_user_info WHERE user_info_id='" . $ship_to_info_id . "' OR user_info_id='" . $user_info_id . "'";
                 $db->query($q);
                 while ($db->next_record()) {
                     $vat_id = $db->f($vatid_fieldname);
                     if ($ship_country_2_code == substr($vat_id, 0, 2)) {
                         $_SESSION['product_sess'][$product_id]['tax_rate'] = 0;
                         return $_SESSION['product_sess'][$product_id]['tax_rate'];
                     }
                 }
             }
         }
     }
     if (($weight_subtotal != 0 or TAX_VIRTUAL == '1') && !ps_checkout::tax_based_on_vendor_address($ship_to_info_id)) {
         $tax_rate_id = ps_product::get_field($product_id, 'product_tax_id');
         if ($tax_rate_id == 0) {
             // if the tax rate was explicitely set to "0 (none)", the product should not be taxed, no matter what else
             $_SESSION['product_sess'][$product_id]['tax_rate'] = 0;
         } else {
             $_SESSION['product_sess'][$product_id]['tax_rate'] = $this->get_taxrate($ship_to_info_id);
         }
         return $_SESSION['product_sess'][$product_id]['tax_rate'];
     } elseif (($weight_subtotal == 0 or TAX_VIRTUAL != '1') && !ps_checkout::tax_based_on_vendor_address($ship_to_info_id)) {
         $_SESSION['product_sess'][$product_id]['tax_rate'] = 0;
         return $_SESSION['product_sess'][$product_id]['tax_rate'];
     } elseif (ps_checkout::tax_based_on_vendor_address($ship_to_info_id)) {
         //			if( empty( $_SESSION['product_sess'][$product_id]['tax_rate'] ) ) {
         $db = new ps_DB();
         // Product's tax rate id has priority!
         $q = 'SELECT tax_rate FROM #__{vm}_product, #__{vm}_tax_rate ';
         $q .= 'WHERE product_tax_id=tax_rate_id AND product_id=' . (int) $product_id;
         $db->query($q);
         if ($db->next_record()) {
             $rate = $db->f("tax_rate");
         } else {
             // if we didn't find a product tax rate id, let's get the store's tax rate
             $rate = $this->get_taxrate($ship_to_info_id);
         }
         if ($weight_subtotal != 0 or TAX_VIRTUAL == '1') {
             $_SESSION['product_sess'][$product_id]['tax_rate'] = $rate;
             return $rate;
         } else {
             $_SESSION['product_sess'][$product_id]['tax_rate'] = 0;
             return 0;
         }
         //			}
         //			else {
         //				return $_SESSION['product_sess'][$product_id]['tax_rate'];
         //			}
     }
     return 0;
 }
예제 #4
0
 /**
  * Retrieves the tax rate to apply to a shipping rate
  *
  * @param int $shipping_rate_id
  * @return float
  */
 function get_tax_rate($shipping_rate_id = 0)
 {
     global $vendor_country_3_code;
     $db = new ps_DB();
     // added by sobers_2002 to fix the issue with shipping tax being calculated for non-state orders
     $ship_to_info_id = vmGet($_REQUEST, 'ship_to_info_id');
     $q = "SELECT state, country FROM #__{vm}_user_info ";
     $q .= "WHERE user_info_id='" . $ship_to_info_id . "'";
     $db->query($q);
     $db->next_record();
     $state = $db->f("state");
     // EU VAT check
     $auth = $_SESSION['auth'];
     $userid = $auth["user_id"];
     if ($userid > 0 && TAX_MODE == '17749' && ps_checkout::country_in_eu_common_vat_zone($vendor_country_3_code)) {
         $ship_country = '';
         $user_info_id = '';
         if (vmGet($_REQUEST, 'ship_to_info_id') || $ship_to_info_id) {
             if (!$ship_to_info_id) {
                 $ship_to_info_id = vmGet($_REQUEST, 'ship_to_info_id');
             }
             $q = "SELECT country FROM #__{vm}_user_info WHERE user_info_id='" . $ship_to_info_id . "'";
             $db->query($q);
             $db->next_record();
             $ship_country = $db->f("country");
         }
         if ($ship_to_info_id == '') {
             $ship_country = $auth["country"];
             $q = "SELECT user_info_id FROM #__{vm}_user_info WHERE user_id = '" . (int) $userid . "' AND address_type='BT'";
             $db->query($q);
             $user_info_id = $db->f("user_info_id");
         }
         // Check if user country is inside EU.
         $eu_vat = '';
         if (ps_checkout::country_in_eu_common_vat_zone($ship_country)) {
             $eu_vat = 'yes';
             $q = "SELECT name FROM #__{vm}_userfield WHERE type='euvatid' AND published=1";
             $db->query($q);
             $vatid_fieldname = $db->f('name');
         }
         // Handle TAX if EU VAT ID
         if ($eu_vat == 'yes' && $vendor_country_3_code != $ship_country) {
             $q = "SELECT country_2_code FROM #__{vm}_country WHERE country_3_code='" . $ship_country . "'";
             $db->query($q);
             $db->next_record();
             $ship_country_2_code = $db->f("country_2_code");
             if ($vatid_fieldname) {
                 $q = "SELECT `{$vatid_fieldname}` FROM #__{vm}_user_info WHERE user_info_id='" . $ship_to_info_id . "' OR user_info_id='" . $user_info_id . "'";
                 $db->query($q);
                 while ($db->next_record()) {
                     $vat_id = $db->f($vatid_fieldname);
                     if ($ship_country_2_code == substr($vat_id, 0, 2)) {
                         return 0.0;
                     }
                 }
             }
         }
     }
     if ($shipping_rate_id == 0) {
         $shipping_rate_id = vmGet($_REQUEST, "shipping_rate_id");
         $ship_arr = explode("|", urldecode(urldecode($shipping_rate_id)));
         $shipping_rate_id = (int) $ship_arr[4];
     }
     $q = "SELECT tax_rate FROM #__{vm}_shipping_rate,#__{vm}_tax_rate WHERE shipping_rate_id='{$shipping_rate_id}' AND shipping_rate_vat_id=tax_rate_id";
     // check if state tax is applicable to the user
     if ($state) {
         $q .= " AND (tax_state='{$state}' OR tax_state=' {$state} ' OR rtrim(ltrim(tax_state))='-' OR tax_state is null)";
     }
     $db->query($q);
     $db->next_record();
     if ($db->f('tax_rate')) {
         return $db->f('tax_rate');
     } else {
         return 0.0;
     }
 }