<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class Payment_type_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->pay_type = TBL_PAY_TYPE;
    }
    
    public function list_all() {
        $this->db->select('p.*, CASE p.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->where('p.status !=', 2);
        $this->db->where('p.status !=', 3); // Disabled
        $this->db->order_by('p.id', 'desc');
        return $this->db->get($this->pay_type . ' as p')->result();
    }
    
    public  function insert($data){  
        $this->db->insert($this->pay_type, $data);  
        return $this->db->insert_id();
    }
    
    public function payment_url_key_exists($url_key, $id) {
        if (!empty($id)) {
            $this->db->where('id !=', $id);
        }
        $this->db->where('url_key', $url_key);
        return $this->db->get($this->pay_type)->num_rows();
    }
   
    public function get_by_id($id) {
        $this->db->select('p.*, CASE p.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str',FALSE);
        $this->db->where('p.id',$id);
        return $this->db->get($this->pay_type . ' as p')->row();
    }
    
    public function update($data, $id){
        $this->db->where('id', $id);
        $this->db->update($this->pay_type, $data);
        return $this->db->affected_rows();
    }
    public function payment_name_exists($name,$id='') {
        $this->db->where('name', $name);
        if($id != '')
            $this->db->where('id !=', $id);
       
        $this->db->where('status !=', 2);
        $this->db->where('status !=', 3); // Disabled
        if($this->db->get($this->pay_type)->num_rows())
            return 1;
        else 
            return 0;
   }
}
?>