Beispiel #1
0
	public function commitTaxZoneLocations($taxZoneId, $data)
	{
		// Delete old zone locations first
		$this->db->deleteQuery('tax_zone_locations', "WHERE tax_zone_id='".(int)$taxZoneId."'");

		// Handle a country based tax zone
		if($data['type'] == 'country') {
			$countryList = getCountryListAsIdValuePairs();
			foreach($data['countries'] as $countryId) {
				// Ignore unknown countreis
				if(empty($countryList[$countryId])) {
					continue;
				}

				$newLocation = array(
					'tax_zone_id' => $taxZoneId,
					'type' => 'country',
					'value' => $countryList[$countryId],
					'value_id' => $countryId,
				);
				if(!$this->db->insertQuery('tax_zone_locations', $newLocation)) {
					return false;
				}
			}
		}
		// Handle state based tax zones
		else if($data['type'] == 'state') {
			$countryList = getCountryListAsIdValuePairs();
			$stateList = array();

			foreach($data['states'] as $stateRecord) {
				$state = explode('-', $stateRecord, 2);

				// Caching - if we haven't loaded the list of states for this country do so now
				if(empty($stateList[$state[0]])) {
					$stateList[$state[0]] = array();
					$query = "
						SELECT *
						FROM [|PREFIX|]country_states
						WHERE statecountry='".(int)$state[0]."'
					";
					$result = $this->db->query($query);
					while($stateResult = $this->db->fetch($result)) {
						$stateList[$stateResult['statecountry']][$stateResult['stateid']] = $stateResult['statename'];
					}
				}

				if(isset($stateList[$state[0]][$state[1]])) {
					$stateName = $stateList[$state[0]][$state[1]];
				}
				else {
					$stateName = '';
				}

				$newLocation = array(
					'tax_zone_id' => $taxZoneId,
					'type' => 'state',
					'value' => $stateName,
					'value_id' => (int)$state[1],
					'country_id' => (int)$state[0]
				);
				if(!$this->db->insertQuery('tax_zone_locations', $newLocation)) {
					return false;
				}
			}
		}
		// Zip code based zone
		else if($data['type'] == 'zip') {
			$countryId = $data['country'];
			$countryName = getCountryById($countryId);
			if(!$countryName) {
				return false;
			}

			// Now save all of the codes that were entered
			foreach($data['zip_codes'] as $zipCode) {
				$zipCode = trim($zipCode);
				if(!$zipCode) {
					continue;
				}

				$newLocation = array(
					'tax_zone_id' => $taxZoneId,
					'type' => 'zip',
					'value' => $zipCode,
					'value_id' => '0',
					'country_id' => $countryId,
				);
				if(!$this->db->insertQuery('tax_zone_locations', $newLocation)) {
					return false;
				}
			}
		}

		return true;
	}
Beispiel #2
0
	public function convertOldTaxRatesToNewTaxRates()
	{
		// Already done this. Don't do it again.
		if($this->columnExists('[|PREFIX|]tax_rates', 'tax_zone_id')) {
			return true;
		}

		$query = "TRUNCATE [|PREFIX|]tax_rates_new";
		if(!$GLOBALS['ISC_CLASS_DB']->query($query)) {
			$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
			return false;
		}

		$query = "
			SELECT *
			FROM [|PREFIX|]tax_rates
		";
		$result = $GLOBALS['ISC_CLASS_DB']->query($query);
		if(!$result) {
			$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
			return false;
		}

		while($rate = $GLOBALS['ISC_CLASS_DB']->fetch($result)) {
			$states = explode(',', trim($rate['taxratestates'], ','));
			if(!$rate['taxratecountry']) {
				// All countries - this is the default zone
				$taxZoneId = 1;
			}
			else {
				if(in_array(0, $states) || empty($states)) {
					$type = 'country';
					// All states in country
				}
				else {
					$type = 'state';
				}

				$newTaxZone = array(
					'name' => $rate['taxratename'],
					'type' => $type,
					'enabled' => $rate['taxratestatus'],
				);
				$taxZoneId = $GLOBALS['ISC_CLASS_DB']->insertQuery('tax_zones', $newTaxZone);
				if(!$taxZoneId) {
					$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
					return false;
				}

				// Insert locations
				if($type == 'country') {
					$newLocation = array(
						'tax_zone_id' => $taxZoneId,
						'type' => 'country',
						'value_id' => $rate['taxratecountry'],
						'value' => getCountryById($rate['taxratecountry']),
					);
					if(!$GLOBALS['ISC_CLASS_DB']->insertQuery('tax_zone_locations', $newLocation)) {
						$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
						return false;
					}
				}
				else if($type == 'state') {
					foreach($states as $state) {
						$newLocation = array(
							'tax_zone_id' => $taxZoneId,
							'type' => 'state',
							'value_id' => $state,
							'value' => getStateById($state),
							'country_id' => $rate['taxratecountry']
						);
						if(!$GLOBALS['ISC_CLASS_DB']->insertQuery('tax_zone_locations', $newLocation)) {
							$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
							return false;
						}
					}
				}
			}

			$newTaxRate = array(
				'tax_zone_id' => $taxZoneId,
				'name' => $rate['taxratename'],
				'default_rate' => $rate['taxratepercent'],
			);
			$taxRateId = $GLOBALS['ISC_CLASS_DB']->insertQuery('tax_rates_new', $newTaxRate);
			if(!$taxRateId) {
				$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
				return false;
			}

			// Insert the tax class rates
			$shippingRate = 0;
			if($rate['taxratebasedon'] == 'subtotal_and_shipping') {
				$shippingRate = $rate['taxratepercent'];
			}

			// Non-taxable
			$newTaxClassRate = array(
				'tax_rate_id' => $taxRateId,
				'tax_class_id' => 1,
				'rate' => 0
			);
			if(!$GLOBALS['ISC_CLASS_DB']->insertQuery('tax_rate_class_rates', $newTaxClassRate)) {
				$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
				return false;
			}

			// Shipping
			$newTaxClassRate = array(
				'tax_rate_id' => $taxRateId,
				'tax_class_id' => 2,
				'rate' => $shippingRate
			);
			if(!$GLOBALS['ISC_CLASS_DB']->insertQuery('tax_rate_class_rates', $newTaxClassRate)) {
				$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
				return false;
			}

			// Wrapping
			$newTaxClassRate = array(
				'tax_rate_id' => $taxRateId,
				'tax_class_id' => 3,
				'rate' => $rate['taxratepercent']
			);
			if(!$GLOBALS['ISC_CLASS_DB']->insertQuery('tax_rate_class_rates', $newTaxClassRate)) {
				$this->setError($GLOBALS['ISC_CLASS_DB']->getErrorMsg());
				return false;
			}
		}

		return true;
	}