예제 #1
0
		/**
		 * Add the taxation information to the google object representation of the customers cart
		 *
		 * @return void
		 **/
		private function AddTaxInformationToCart()
		{
			require_once dirname(__FILE__).'/library/googletax.php';

			// Fetch available tax classes
			$taxClasses = getClass('ISC_TAX')->getTaxClasses();
			foreach($taxClasses as $id => $name) {
				$taxClasses[$id] = array(
					'name' => $name,
					'rules' => array()
				);
			}

			$taxClassIds = array_keys($taxClasses);

			// Fetch available tax zones
			$taxZones = array();
			$query = "
				SELECT id, type, `default`
				FROM [|PREFIX|]tax_zones
				WHERE enabled=1 OR `default`=1
			";
			$result = $GLOBALS['ISC_CLASS_DB']->query($query);
			while($taxZone = $GLOBALS['ISC_CLASS_DB']->fetch($result)) {
				$taxClasses[$id]['rules'] = array();

				$shippingTaxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], getConfig('taxShippingTaxClass'));
				$shippingTaxed = 'false';
				if($shippingTaxRate > 0) {
					$shippingTaxed = 'true';
				}

				$defaultTaxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], 0) / 100;
				$defaultTaxRule = new GoogleDefaultTaxRule($defaultTaxRate, $shippingTaxed);

				foreach($taxClassIds as $id) {
					$taxRate = getClass('ISC_TAX')->getEffectiveClassRate($taxZone['id'], $id) / 100;
					$taxClasses[$id]['rules'][$taxZone['id']] = new GoogleAlternateTaxRule($taxRate);
				}

				// Everywhere else tax zone - allow everywhere
				if($taxZone['default']) {
					$defaultTaxRule->setWorldArea(true);
					foreach($taxClassIds as $id) {
						$taxClasses[$id]['rules'][$taxZone['id']]->setWorldArea(true);
					}
				}
				// Location specific zone, so the tax becomes dependant on configured locations
				else {
					$zipPatterns = array();
					$stateCodes = array();
					$query = "
						SELECT *
						FROM [|PREFIX|]tax_zone_locations
						WHERE tax_zone_id='".$taxZone['id']."'
					";
					$locationResult = $GLOBALS['ISC_CLASS_DB']->query($query);
					while($location = $GLOBALS['ISC_CLASS_DB']->fetch($locationResult)) {
						if($location['type'] == 'country') {
							$countryIso = getCountryISO2ById($location['value_id']);
							$defaultTaxRule->addPostalArea($countryIso);
							foreach($taxClassIds as $id) {
								$taxClasses[$id]['rules'][$taxZone['id']]->addPostalArea($countryIso);
							}
						}
						else if($location['type'] == 'zip') {
							$countryIso = getCountryISO2ById($location['country_id']);

							// US zip codes are handled with setZipPatterns below
							if($countryIso == 'US') {
								$zipPatterns[] = $location['value'];
							}
							else {
								$defaultTaxRule->addPostalArea($countryIso, $location['value']);
								foreach($taxClassIds as $id) {
									$taxClasses[$id]['rules'][$taxZone['id']]->addPostalArea($countryIso, $location['value']);
								}
							}
						}
						else if($location['type'] == 'state') {
							$countryIso = getCountryISO2ById($location['country_id']);

							// Google Checkout only supports US based states
							// Ref: http://code.google.com/apis/checkout/developer/Google_Checkout_XML_API_Understanding_Areas.html
							if($countryIso != 'US') {
								continue;
							}

							$stateCodes[] = getStateISO2ById($location['value_id']);
						}
					}
				}

				$this->cart->addDefaultTaxRules($defaultTaxRule);

				// Add in the US zip codes if there are any
				if(!empty($zipPatterns)) {
					$defaultTaxRule->setZipPatterns($zipPatterns);
					foreach($taxClassIds as $id) {
						$taxClasses[$id]['rules'][$taxZone['id']]->setZipPatterns($zipPatterns);
					}
				}

				// Add in US states if there are any
				if(!empty($stateCodes)) {
					$defaultTaxRule->setStateAreas($stateCodes);
					foreach($taxClassIds as $id) {
						$taxClasses[$id]['rules'][$taxZone['id']]->setStateAreas($stateCodes);
					}
				}
			}

			foreach($taxClasses as $id => $taxClass) {
				$table = new GoogleAlternateTaxTable($id);
				foreach($taxClass['rules'] as $rule) {
					$table->addAlternateTaxRules($rule);
				}

				$this->cart->addAlternateTaxTables($table);
			}
		}