1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387:
<?php
class Tax extends CI_Model
{
public function exists($tax_code)
{
$this->db->from('tax_codes');
$this->db->where('tax_code', $tax_code);
return ($this->db->get()->num_rows() == 1);
}
public function tax_rate_exists($tax_code, $tax_category_id)
{
$this->db->from('tax_code_rates');
$this->db->where('rate_tax_code', $tax_code);
$this->db->where('rate_tax_category_id', $tax_category_id);
return ($this->db->get()->num_rows() == 1);
}
public function get_total_rows()
{
$this->db->from('tax_codes');
return $this->db->count_all_results();
}
public function get_info($tax_code)
{
$this->db->from('tax_codes');
$this->db->join('tax_code_rates',
'tax_code = rate_tax_code and rate_tax_category_id = 0', 'LEFT');
$this->db->join('tax_categories',
'rate_tax_category_id = tax_category_id');
$this->db->where('tax_code', $tax_code);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row();
}
else
{
$tax_code_obj = new stdClass();
foreach($this->db->list_fields('tax_codes') as $field)
{
$tax_code_obj->$field = '';
}
foreach($this->db->list_fields('tax_code_rates') as $field)
{
$tax_code_obj->$field = '';
}
return $tax_code_obj;
}
}
public function get_rate_info($tax_code, $tax_category_id)
{
$this->db->from('tax_code_rates');
$this->db->join('tax_categories',
'rate_tax_category_id = tax_category_id');
$this->db->where('rate_tax_code', $tax_code);
$this->db->where('rate_tax_category_id', $tax_category_id);
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row();
}
else
{
$tax_rate_obj = new stdClass();
foreach($this->db->list_fields('tax_code_rates') as $field)
{
$tax_rate_obj->$field = '';
}
foreach($this->db->list_fields('tax_categories') as $field)
{
$tax_rate_obj->$field = '';
}
return $tax_rate_obj;
}
}
public function get_sales_tax_code($city = '', $state = '')
{
$this->db->from('tax_codes');
$this->db->where('city', $city);
$this->db->where('state', $state);
$this->db->where('tax_code_type', '0');
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row()->tax_code;
}
else
{
$this->db->from('tax_codes');
$this->db->where('city', '');
$this->db->where('state', $state);
$this->db->where('tax_code_type', '0');
$query = $this->db->get();
if($query->num_rows() == 1)
{
return $query->row()->tax_code;
}
else
{
return $this->config->item('default_origin_tax_code');
}
}
return FALSE;
}
public function save(&$tax_code_data, $tax_rate_data, $tax_code = -1)
{
if(!$this->exists($tax_code))
{
if($this->db->insert('tax_codes', $tax_code_data))
{
$this->save_tax_rates($tax_rate_data, $tax_code);
return TRUE;
}
return FALSE;
}
$this->db->where('tax_code', $tax_code);
if ($this->db->update('tax_codes', $tax_code_data))
{
$this->save_tax_rates($tax_rate_data, $tax_code);
return TRUE;
}
else
{
return FALSE;
}
}
public function save_tax_rates(&$tax_rate_data, $tax_code)
{
if(!$this->tax_rate_exists($tax_code, $tax_rate_data['rate_tax_category_id']))
{
if($this->db->insert('tax_code_rates', $tax_rate_data))
{
return TRUE;
}
return FALSE;
}
$this->db->where('rate_tax_code', $tax_code);
$this->db->where('rate_tax_category_id', $tax_rate_data['rate_tax_category_id']);
return $this->db->update('tax_code_rates', $tax_rate_data);
}
public function save_tax_rate_exceptions(&$tax_rate_data, $tax_code)
{
$success = TRUE;
$this->db->trans_start();
$this->delete_tax_rate_exceptions($tax_code);
if ($tax_rate_data != NULL)
{
foreach ($tax_rate_data as $row)
{
$row['rate_tax_code'] = $tax_code;
$success &= $this->db->insert('tax_code_rates', $row);
}
}
$this->db->trans_complete();
$success &= $this->db->trans_status();
return $success;
}
public function delete($tax_code)
{
return $this->db->delete('tax_codes', array('tax_code' => $tax_code));
}
public function delete_list($tax_codes)
{
$this->db->where_in('tax_code', $tax_codes);
return $this->db->delete('tax_codes');
}
public function delete_tax_rate_exceptions($tax_code)
{
$this->db->where('rate_tax_code', $tax_code);
$this->db->where('rate_tax_category_id !=', 0);
return $this->db->delete('tax_code_rates');
}
public function search($search, $rows = 0, $limit_from = 0, $sort = 'tax_code', $order = 'asc')
{
$this->db->from('tax_codes');
$this->db->join('tax_code_rates',
'tax_code = rate_tax_code and rate_tax_category_id = 0', 'LEFT');
if (!empty($search))
{
$this->db->like('tax_code', $search);
$this->db->or_like('tax_code_name', $search);
}
$this->db->order_by($sort, $order);
if($rows > 0)
{
$this->db->limit($rows, $limit_from);
}
return $this->db->get();
}
public function get_found_rows($search)
{
$this->db->from('tax_codes');
if (!empty($search))
{
$this->db->like('tax_code', $search);
$this->db->or_like('tax_code_name', $search);
}
return $this->db->get()->num_rows();
}
public function get_tax_code_type_name($tax_code_type) {
if ($tax_code_type == '0')
{
return $this->lang->line('taxes_sales_tax');
}
else
{
return $this->lang->line('taxes_vat_tax');
}
}
public function get_sales_tax_codes_search_suggestions($search, $limit = 25)
{
$suggestions = array();
$this->db->from('tax_codes');
if (!empty($search))
{
$this->db->like('tax_code', $search);
$this->db->or_like('tax_code_name', $search);
}
$this->db->order_by('tax_code_name', 'asc');
foreach($this->db->get()->result() as $row)
{
$suggestions[] = array('value' => $row->tax_code, 'label' => ($row->tax_code . ' ' . $row->tax_code_name));
}
if(count($suggestions > $limit))
{
$suggestions = array_slice($suggestions, 0,$limit);
}
return $suggestions;
}
public function get_tax_category_suggestions($search)
{
$suggestions = array();
$this->db->from('tax_categories');
$this->db->where('tax_category_id !=', 0);
if (!empty($search))
{
$this->db->like('tax_category', '%'.$search.'%');
}
$this->db->order_by('tax_category', 'asc');
foreach($this->db->get()->result() as $row)
{
$suggestions[] = array('value' => $row->tax_category_id, 'label' => $row->tax_category);
}
return $suggestions;
}
public function get_tax_category($tax_category_id)
{
$this->db->select('tax_category');
$this->db->from('tax_categories');
$this->db->where('tax_category_id', $tax_category_id);
return $this->db->get()->row()->tax_category;
}
public function get_all_tax_categories()
{
$this->db->from('tax_categories');
$this->db->order_by('tax_category_id');
return $this->db->get();
}
public function get_tax_category_id($tax_category)
{
$this->db->select('tax_category_id');
$this->db->from('tax_categories');
return $this->db->get()->row()->tax_category_id;
}
public function get_tax_code_rate_exceptions($tax_code)
{
$this->db->from('tax_code_rates');
$this->db->join('tax_categories', 'rate_tax_category_id = tax_category_id');
$this->db->where('rate_tax_code', $tax_code);
$this->db->where('rate_tax_category_id !=', 0);
$this->db->order_by('tax_category', 'asc');
return $this->db->get()->result_array();
}
}
?>