<?php

/* 
 * Author       : Kowsalya K
 * Purpose      : Model for Billing Gold/Silver Offering
 * Created On   : 2016-08-11
 */

class Gold_silver_offering_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->gold_silver = TBL_BILL_GOLD_SILVER_OFFERING;
        $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 = 'gs.id, gs.receipt_no, gs.name, gs.tamil_name, gs.contact_no, s.name as service, s.prefix, gs.amount as gs_amount, gs.status, gs.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = gs.service_id');
        #$this->db->where('gs.status !=',2);
        $this->db->order_by('gs.receipt_no','desc');
        return $this->db->get($this->gold_silver . ' as gs')->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->gold_silver);
    }
    
    public function insert($data) {
        $this->db->insert($this->gold_silver,$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->gold_silver,$data);
        return $this->db->affected_rows();
    }
    
    public function gold_silver_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->gold_silver)->num_rows();
    }
    
    public function get_by_id($id) {
        $select = 'gs.*, CASE gs.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, gs.status AS status';
        $select .= ', s.name as service, s.prefix, gs.amount as gs_amount, s.amount as ser_amount';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = gs.service_id');
        $this->db->where('gs.id',$id);
        return $this->db->get($this->gold_silver . ' as gs')->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.id, 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){
        $this->db->where('service_id',$type_id);
        $this->db->where('status !=',2);
        return $this->db->get($this->gold_silver)->result();
    }
    
    public function get_contact_by_service_type($type_id){
        $this->db->where('service_id',$type_id);
        $this->db->where('status !=',2);
        $this->db->group_by('contact_no');
        return $this->db->get($this->gold_silver)->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_GOLD_SILVER_OFFERING . ' 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();
    }
}
?>

