<?php

/* 
 * Author       : Kowsalya K
 * Purpose      : Model for Billing Kumbabishegam
 * Created On   : 2016-08-09
 */

class Building_fund_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->building_fund = TBL_BILL_BUILDING_FUND;
        $this->payment_type = TBL_PAY_TYPE;
        $this->payment = TBL_PAY;
        $this->god = TBL_GOD_NAME;
        $this->stars = TBL_STAR;
        $this->prasadam = TBL_PRASADAM;
    }
    
    public function list_all() {
        $select = 'bf.id, bf.receipt_no, bf.name, bf.tamil_name, bf.contact_no, s.name as service, s.prefix, bf.amount as bf_amount, bf.status, bf.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = bf.service_id');
        #$this->db->where('bf.status !=',2);
        $this->db->order_by('bf.receipt_no','desc');
        return $this->db->get($this->building_fund . ' as bf')->result();
    }
    
    public function get( $tbl_name, $where = array() ) {
        if (!is_array($where)) {
            $where = array();
        }
        $where['status'] = 1;
        
        $q = $this->db->get_where( $tbl_name, $where );
        return $q->result_array();
            
    }
    
    public function get_amount_by_service_id($id) {
        $this->db->where('id',$id);
        return $this->db->get($this->service)->row();
    }
    
    public function get_service_type($type= ''){
        $this->db->where('s.menu_name',$type);
        $this->db->where('s.status',1);
        return $this->db->get($this->service . ' as s')->result();
    }
    
    public function payment_mode_lists() {
        $this->db->where('status',1);
        $this->db->where('menu_id',0);
        return $this->db->get($this->payment_type)->result();
    }
    
    public function service_type_exists($type) {
        $this->db->where('service_id',$type);
        //$this->db->where('status !=',2);
        $this->db->order_by('receipt_no','desc');
        return $this->db->get($this->building_fund);
    }
    
    public function insert($data) {
        $this->db->insert($this->building_fund,$data);
        return $this->db->insert_id();
    }
    
    public function insert_payment($data){
        $this->db->insert($this->payment,$data);
        return $this->db->insert_id();
    }
    
    public function update($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->building_fund,$data);
        return $this->db->affected_rows();
    }
    
    public function building_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->building_fund)->num_rows();
    }
    
    public function get_by_id($id) {
        $select = 'bf.*, CASE bf.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, bf.status AS status';
        $select .= ', s.name as service, s.prefix, bf.amount as bf_amount';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = bf.service_id');
        $this->db->where('bf.id',$id);
        return $this->db->get($this->building_fund . ' as bf')->row();
    }
    public function get_paid_type($menu,$type,$id) {
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('pay_type_id',$id);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function get_paid_type_by_id($menu,$type) {
        $this->db->select('p.pay_amount, pt.img_url, pt.name',FALSE);
        $this->db->join($this->payment_type . ' as pt','pt.id = p.pay_type_id');
        $this->db->where('p.pay_ref_type',$menu);
        $this->db->where('p.pay_ref_id',$type);
        $this->db->where('p.status',1);
        return $this->db->get($this->payment . ' as p')->result();
    }
    
    public function update_paid_type($data,$type_id,$id) {
        $this->db->where('pay_ref_type',$type_id);
        $this->db->where('pay_ref_id',$id);
        $this->db->update($this->payment,$data);
        return $this->db->affected_rows();
    }
    
    public function total_paid_amount($menu,$type) {
        $this->db->select('SUM(pay_amount) as paidAmount');
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->row();
    }
    
    public function delete_paid_type($type_id,$id) {
        $this->db->where('pay_ref_type',$type_id);
        $this->db->where('pay_ref_id',$id);
        $this->db->delete($this->payment);
        return $this->db->affected_rows();
    }
    
    public function get_data_by_service_type($type_id,$search_by){
        $this->db->where('service_id',$type_id);
        $this->db->where('status !=',2);
        if($search_by == 0) 
            $this->db->group_by('contact_no');
        
        return $this->db->get($this->building_fund)->result();
    }
    
    public function get_all( $id, $where = array()) {
        if( !empty( $id ) ) :
            $where['self.id'] = $id;
        endif;
        
        $where['self.status'] = 1;
        
        
        $this->db->select( 'self.*, self.status AS status, serv.name AS service_name, serv.prefix' );
        
        $this->db->join( TBL_SERVICE . ' AS serv', 'serv.id = self.service_id', 'left' );
        $q = $this->db->get_where( TBL_BILL_BUILDING_FUND . ' AS self', $where );
        return $q->result_array();
    }
    
    public function ajax() {
        $post = $this->input->post();
        
        /*
         * Get all booked details
         */
        $where['self.service_id'] = $post['service_type'];
        $where['self.status'] = 1;
        
        //if( !empty( $post['search_by'] ) ) :
            if($post['search_by'] == 1) {
                $where['self.receipt_no'] = $post['ref_no']; 
            } else if($post['search_by'] == 0) {
                $where['self.contact_no'] = $post['contact_no'];
            }
       //endif;

        $results = $this->get_all( '', $where);
        
        return $results;
    }
    
    public function delete_payment_by_paid_id($id) {
        $this->db->where_in('id',$id);
        $this->db->delete($this->payment);
        return $this->db->affected_rows();
    }
    
    public function get_paidID($menu,$type,$id) {
        $this->db->where('pay_ref_type',$menu);
        $this->db->where('pay_ref_id',$type);
        $this->db->where('pay_type_id',$id);
        $this->db->where('status',1);
        return $this->db->get($this->payment)->result();
    }
}
?>

