tax_class.php (3711B)
1 <?php 2 class ModelLocalisationTaxClass extends Model { 3 public function addTaxClass($data) { 4 $this->db->query("INSERT INTO " . DB_PREFIX . "tax_class SET title = '" . $this->db->escape($data['title']) . "', description = '" . $this->db->escape($data['description']) . "', date_added = NOW()"); 5 6 $tax_class_id = $this->db->getLastId(); 7 8 if (isset($data['tax_rule'])) { 9 foreach ($data['tax_rule'] as $tax_rule) { 10 $this->db->query("INSERT INTO " . DB_PREFIX . "tax_rule SET tax_class_id = '" . (int)$tax_class_id . "', tax_rate_id = '" . (int)$tax_rule['tax_rate_id'] . "', based = '" . $this->db->escape($tax_rule['based']) . "', priority = '" . (int)$tax_rule['priority'] . "'"); 11 } 12 } 13 14 $this->cache->delete('tax_class'); 15 16 return $tax_class_id; 17 } 18 19 public function editTaxClass($tax_class_id, $data) { 20 $this->db->query("UPDATE " . DB_PREFIX . "tax_class SET title = '" . $this->db->escape($data['title']) . "', description = '" . $this->db->escape($data['description']) . "', date_modified = NOW() WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 21 22 $this->db->query("DELETE FROM " . DB_PREFIX . "tax_rule WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 23 24 if (isset($data['tax_rule'])) { 25 foreach ($data['tax_rule'] as $tax_rule) { 26 $this->db->query("INSERT INTO " . DB_PREFIX . "tax_rule SET tax_class_id = '" . (int)$tax_class_id . "', tax_rate_id = '" . (int)$tax_rule['tax_rate_id'] . "', based = '" . $this->db->escape($tax_rule['based']) . "', priority = '" . (int)$tax_rule['priority'] . "'"); 27 } 28 } 29 30 $this->cache->delete('tax_class'); 31 } 32 33 public function deleteTaxClass($tax_class_id) { 34 $this->db->query("DELETE FROM " . DB_PREFIX . "tax_class WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 35 $this->db->query("DELETE FROM " . DB_PREFIX . "tax_rule WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 36 37 $this->cache->delete('tax_class'); 38 } 39 40 public function getTaxClass($tax_class_id) { 41 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "tax_class WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 42 43 return $query->row; 44 } 45 46 public function getTaxClasses($data = array()) { 47 if ($data) { 48 $sql = "SELECT * FROM " . DB_PREFIX . "tax_class"; 49 50 $sql .= " ORDER BY title"; 51 52 if (isset($data['order']) && ($data['order'] == 'DESC')) { 53 $sql .= " DESC"; 54 } else { 55 $sql .= " ASC"; 56 } 57 58 if (isset($data['start']) || isset($data['limit'])) { 59 if ($data['start'] < 0) { 60 $data['start'] = 0; 61 } 62 63 if ($data['limit'] < 1) { 64 $data['limit'] = 20; 65 } 66 67 $sql .= " LIMIT " . (int)$data['start'] . "," . (int)$data['limit']; 68 } 69 70 $query = $this->db->query($sql); 71 72 return $query->rows; 73 } else { 74 $tax_class_data = $this->cache->get('tax_class'); 75 76 if (!$tax_class_data) { 77 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "tax_class"); 78 79 $tax_class_data = $query->rows; 80 81 $this->cache->set('tax_class', $tax_class_data); 82 } 83 84 return $tax_class_data; 85 } 86 } 87 88 public function getTotalTaxClasses() { 89 $query = $this->db->query("SELECT COUNT(*) AS total FROM " . DB_PREFIX . "tax_class"); 90 91 return $query->row['total']; 92 } 93 94 public function getTaxRules($tax_class_id) { 95 $query = $this->db->query("SELECT * FROM " . DB_PREFIX . "tax_rule WHERE tax_class_id = '" . (int)$tax_class_id . "'"); 96 97 return $query->rows; 98 } 99 100 public function getTotalTaxRulesByTaxRateId($tax_rate_id) { 101 $query = $this->db->query("SELECT COUNT(DISTINCT tax_class_id) AS total FROM " . DB_PREFIX . "tax_rule WHERE tax_rate_id = '" . (int)$tax_rate_id . "'"); 102 103 return $query->row['total']; 104 } 105 }