shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

tax.php (5662B)


      1 <?php
      2 namespace Cart;
      3 final class Tax {
      4 	private $tax_rates = array();
      5 
      6 	public function __construct($registry) {
      7 		$this->config = $registry->get('config');
      8 		$this->db = $registry->get('db');
      9 	}
     10 
     11 	public function unsetRates() {
     12 		$this->tax_rates = array();
     13 	}
     14 
     15 	public function setShippingAddress($country_id, $zone_id) {
     16 		$tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'shipping' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC");
     17 
     18 		foreach ($tax_query->rows as $result) {
     19 			$this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array(
     20 				'tax_rate_id' => $result['tax_rate_id'],
     21 				'name'        => $result['name'],
     22 				'rate'        => $result['rate'],
     23 				'type'        => $result['type'],
     24 				'priority'    => $result['priority']
     25 			);
     26 		}
     27 	}
     28 
     29 	public function setPaymentAddress($country_id, $zone_id) {
     30 		$tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'payment' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC");
     31 
     32 		foreach ($tax_query->rows as $result) {
     33 			$this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array(
     34 				'tax_rate_id' => $result['tax_rate_id'],
     35 				'name'        => $result['name'],
     36 				'rate'        => $result['rate'],
     37 				'type'        => $result['type'],
     38 				'priority'    => $result['priority']
     39 			);
     40 		}
     41 	}
     42 
     43 	public function setStoreAddress($country_id, $zone_id) {
     44 		$tax_query = $this->db->query("SELECT tr1.tax_class_id, tr2.tax_rate_id, tr2.name, tr2.rate, tr2.type, tr1.priority FROM " . DB_PREFIX . "tax_rule tr1 LEFT JOIN " . DB_PREFIX . "tax_rate tr2 ON (tr1.tax_rate_id = tr2.tax_rate_id) INNER JOIN " . DB_PREFIX . "tax_rate_to_customer_group tr2cg ON (tr2.tax_rate_id = tr2cg.tax_rate_id) LEFT JOIN " . DB_PREFIX . "zone_to_geo_zone z2gz ON (tr2.geo_zone_id = z2gz.geo_zone_id) LEFT JOIN " . DB_PREFIX . "geo_zone gz ON (tr2.geo_zone_id = gz.geo_zone_id) WHERE tr1.based = 'store' AND tr2cg.customer_group_id = '" . (int)$this->config->get('config_customer_group_id') . "' AND z2gz.country_id = '" . (int)$country_id . "' AND (z2gz.zone_id = '0' OR z2gz.zone_id = '" . (int)$zone_id . "') ORDER BY tr1.priority ASC");
     45 
     46 		foreach ($tax_query->rows as $result) {
     47 			$this->tax_rates[$result['tax_class_id']][$result['tax_rate_id']] = array(
     48 				'tax_rate_id' => $result['tax_rate_id'],
     49 				'name'        => $result['name'],
     50 				'rate'        => $result['rate'],
     51 				'type'        => $result['type'],
     52 				'priority'    => $result['priority']
     53 			);
     54 		}
     55 	}
     56 
     57 	public function calculate($value, $tax_class_id, $calculate = true) {
     58 		if ($tax_class_id && $calculate) {
     59 			$amount = 0;
     60 
     61 			$tax_rates = $this->getRates($value, $tax_class_id);
     62 
     63 			foreach ($tax_rates as $tax_rate) {
     64 				if ($calculate != 'P' && $calculate != 'F') {
     65 					$amount += $tax_rate['amount'];
     66 				} elseif ($tax_rate['type'] == $calculate) {
     67 					$amount += $tax_rate['amount'];
     68 				}
     69 			}
     70 
     71 			return $value + $amount;
     72 		} else {
     73 			return $value;
     74 		}
     75 	}
     76 
     77 	public function getTax($value, $tax_class_id) {
     78 		$amount = 0;
     79 
     80 		$tax_rates = $this->getRates($value, $tax_class_id);
     81 
     82 		foreach ($tax_rates as $tax_rate) {
     83 			$amount += $tax_rate['amount'];
     84 		}
     85 
     86 		return $amount;
     87 	}
     88 
     89 	public function getRateName($tax_rate_id) {
     90 		$tax_query = $this->db->query("SELECT name FROM " . DB_PREFIX . "tax_rate WHERE tax_rate_id = '" . (int)$tax_rate_id . "'");
     91 
     92 		if ($tax_query->num_rows) {
     93 			return $tax_query->row['name'];
     94 		} else {
     95 			return false;
     96 		}
     97 	}
     98 
     99 	public function getRates($value, $tax_class_id) {
    100 		$tax_rate_data = array();
    101 
    102 		if (isset($this->tax_rates[$tax_class_id])) {
    103 			foreach ($this->tax_rates[$tax_class_id] as $tax_rate) {
    104 				if (isset($tax_rate_data[$tax_rate['tax_rate_id']])) {
    105 					$amount = $tax_rate_data[$tax_rate['tax_rate_id']]['amount'];
    106 				} else {
    107 					$amount = 0;
    108 				}
    109 
    110 				if ($tax_rate['type'] == 'F') {
    111 					$amount += $tax_rate['rate'];
    112 				} elseif ($tax_rate['type'] == 'P') {
    113 					$amount += ($value / 100 * $tax_rate['rate']);
    114 				}
    115 
    116 				$tax_rate_data[$tax_rate['tax_rate_id']] = array(
    117 					'tax_rate_id' => $tax_rate['tax_rate_id'],
    118 					'name'        => $tax_rate['name'],
    119 					'rate'        => $tax_rate['rate'],
    120 					'type'        => $tax_rate['type'],
    121 					'amount'      => $amount
    122 				);
    123 			}
    124 		}
    125 
    126 		return $tax_rate_data;
    127 	}
    128 }