<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class temple_institution_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->temple_institute = TBL_BILL_TEMPLE;
        $this->payment = TBL_PAY_TYPE;
        $this->service = TBL_SERVICE;
        $this->service_menu = TBL_SERVICE_MENU;
        $this->paid_type = TBL_PAY;
    }
    
    public function list_all() {
        $select = 'ti.*, ti.amount as ti_amount, s.name as service, s.prefix';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = ti.service_id');
        #$this->db->where('ti.status !=',2);
        $this->db->order_by('ti.receipt_no','desc');
        return $this->db->get($this->temple_institute . ' as ti')->result();
    }
    
    public function payment_mode_lists() {
        $this->db->where('status',1);
        $this->db->where('menu_id',0);
        return $this->db->get($this->payment)->result();
    }
    
    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 insert($data){
        $this->db->insert($this->temple_institute, $data);
        return $this->db->insert_id();
    }
    
    public function get_amount_by_service_id($id) {
        $this->db->where('id',$id);
        return $this->db->get($this->service)->row();
    }
    
    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->temple_institute);
    }
    
    public function update($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->temple_institute,$data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $select = 'ti.*, CASE ti.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, ti.amount as ti_amount, ti.status AS status';
        $select .= ', CASE ti.sex WHEN 1 THEN "Male" ELSE "Female" END as sex_str';
        $select .= ', CASE ti.term WHEN 1 THEN "Yearly" WHEN 2 THEN "Monthly" WHEN 3 THEN "Weekly" WHEN 4 THEN "Daily" ELSE "Evening" END as term_str';
        $select .= ', CASE ti.transport_facility WHEN 1 THEN "Yes" ELSE "No" END as transport';
        $select .= ', CASE ti.class_type WHEN 1 THEN "Individual in Home" WHEN 2 THEN "Individual in Temple" WHEN 3 THEN "Group in Home" WHEN 4 THEN "Group in Temple" ELSE "Evening" END as class_type_str';
        $select .= ', CASE ti.class WHEN 1 THEN "Morning" ELSE "Evening" END as class_str';
        $select .= ', s.name as service, s.amount as ser_amount, s.prefix';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = ti.service_id');
        $this->db->where('ti.id',$id);
        return $this->db->get($this->temple_institute . ' as ti')->row();
    }
    
    public function temple_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->temple_institute)->num_rows();
    }
    
    public function insert_payment($data){
        $this->db->insert($this->paid_type,$data);
        return $this->db->insert_id();
    }
    
    public function update_payment($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->paid_type,$data);
        return $this->db->affected_rows();
    }
    
    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->paid_type)->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 . ' 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->paid_type . ' as p')->result();
    }
    
    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->paid_type);
        return $this->db->affected_rows();
    }
    
    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->paid_type,$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->paid_type)->row();
    }
    
    public function delete_payment_by_paid_id($id) {
        $this->db->where_in('id',$id);
        $this->db->delete($this->paid_type);
        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->paid_type)->result();
    }
    
    public function get_ref_no($id) {
        $this->db->where('id',$id);
        return $this->db->get(TBL_STUDENT)->row()->ref_no;
    }
    
    function stu_update() {
        $post = $this->input->post();
        $date_now = date("Y-m-d H:i:s");
        $session_data = $this->session->userdata( TBL_PREFIX . 'user_logged_in' );
        parse_str($post['data'], $data);
        $db_id = $data['db_id'];
        
        //echo "<pre>Data: "; print_r( $data ); die;
       
        switch( $post['type'] ) : 
            case 'student' :
                
                $args = array(
                    'name'    => $data['name'],
                    'contact_no'    => $data['contact_no'],
                    'email'       => $data['email'],
                    'contact_person' => $data['contact_person'],
                    'sex' => $data['sex'],
                    'nric_no'      => $data['nric_no'],
                    'address'    => $data['address']
                );
                
                if( empty( $db_id ) ) :
                    // Insert
                    
                    /*
                    * Get last Ref. ID
                    */
                    $this->db->select('COUNT(id) AS last_count');
                    $q = $this->db->get_where( TBL_STUDENT, array() );
                    $ref_id = $q->row_array()['last_count'];
                    
                    $args['ref_no']   = fill_digits( ++$ref_id, 4 );
                    $args['created_on']   = $date_now;
                    $args['created_by']   = $session_data['user_id'];    
                    
                    $this->db->insert( TBL_STUDENT, $args );
                    $db_id = $this->db->insert_id();
                else :
                    // Update
                    $args['updated_on']   = $date_now;
                    $args['updated_by']   = $session_data['user_id'];    
                    
                    $this->db->update( TBL_STUDENT, $args, array('id' => $db_id) );
                    $db_id = $db_id;
                endif;
                return $db_id;
            break;
            
        endswitch;
        
    }
    
    function search_student() {
        $post = $this->input->post();
        $value = $post['value'];
        $where = array(
            's.status' => 1,
           // 'bdi.status' => 1
        );
        
        if( $post['include_contact'] == 'true' ) :
            $this->db->where('s.contact_no', $value);
            $this->db->or_where('s.ref_no', $value);
        else :
            // Search by only ref. no
            $where['s.ref_no'] = $value;
            
        endif;    
        
        $this->db->select('*, s.id AS id');
        $q = $this->db->get_where( TBL_STUDENT . ' AS s', $where );
        $results = $q->result_array();
        #echo "Query:"; error_log($this->db->last_query()); die;
        
        foreach( $results  as $k => $row ) :
            $return[$row['id']][] = $row;
        endforeach;
        
        return $return;
    }
}
?>