<?php

/* 
 * Author       : Kowsalya K
 * Purpose      : Model for Billing Mega Function
 * Created On   : 2016-08-05
 */

class Mega_function_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->mega_function = TBL_BILL_MEGA_FUNCTION;
        $this->mega_prasadam = TBL_BILL_MEGA_PRASADAM;
        $this->payment_type = TBL_PAY_TYPE;
        $this->payment = TBL_PAY;
        $this->god = TBL_GOD_NAME;
        $this->stars = TBL_STAR;
        $this->prasadam = TBL_PRASADAM;
        $this->refund = TBL_REFUND;
    }
    
    public function list_all() {
        $select = 'm.id, m.receipt_no, m.devotee_name1, m.tamil_name1, m.date, m.contact_no, s.name as service, s.prefix, m.amount as m_amount, m.status, m.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = m.service_id');
        #$this->db->where('m.status !=',2);
        $this->db->where('m.book_status',1);
        $this->db->order_by('m.receipt_no','desc');
        return $this->db->get($this->mega_function . ' as m')->result();
    }
    
    public function get( $tbl_name, $where = array(), $args = array() ) {
        if ( ! is_array($where)) $where = array();
        $where['status'] = 1;
        if($tbl_name == 'thiru_payment_types') :
            $where['menu_id'] = 0;
        endif;
        
        if( !empty( $args['order_by'] ) ) :
            $this->db->order_by( $args['order_by'] );
        endif;
        
        $q = $this->db->get_where( $tbl_name, $where );
        return $q->result_array();
            
    }
    
    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->mega_function,$data);
        return $this->db->insert_id();
    }
    
    public function insert_prasadam($data) {
        $this->db->insert($this->mega_prasadam,$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->mega_function,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam($data,$id) {
        $this->db->where('mega_id',$id);
        $this->db->update($this->mega_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam_by_id($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->mega_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function mega_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->mega_function)->num_rows();
    }
    
    public function get_by_id($id) {
        $select = 'f.*, CASE f.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, f.status AS status';
        $select .= ', CASE f.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str';
        $select .= ', s.name as service, g.name as god_name, star.name as star1_name, star1.name as star2_name, f.amount as f_amount, star.tamil_name as star_tamil_name1, star1.tamil_name as star_tamil_name2, s.amount as ser_amount, s.prefix';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = f.service_id');
        $this->db->join($this->god . ' as g','g.id = f.deity_id','left');
        $this->db->join($this->stars . ' as star','star.id = f.star1','left');
        $this->db->join($this->stars . ' as star1','star1.id = f.star2','left');
        $this->db->where('f.id',$id);
        return $this->db->get($this->mega_function . ' as f')->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 update_refund($data,$type_id,$id) {
        $this->db->where('menu_id',$type_id);
        $this->db->where('auto_id',$id);
        $this->db->update($this->refund,$data);
        return $this->db->affected_rows();
    }
    
    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 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 get_prasadam_by_id($id){
        $this->db->select('mp.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = mp.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = mp.deity_id','left');
        $this->db->where('mp.mega_id',$id);
        $this->db->where('mp.status !=',2);
        return  $this->db->get($this->mega_prasadam . ' as mp')->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 get_prasadam_json($id){
        $this->db->select('mp.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = mp.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = mp.deity_id','left');
        $this->db->where('mp.mega_id',$id);
        $this->db->where('mp.status !=',2);
        return  $this->db->get($this->mega_prasadam . ' as mp')->result_array();
    }
    
    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_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, god.name AS god_name, star1.name AS star_name_1, star2.name AS star_name_2, serv.prefix' );
        
        $this->db->join( $this->service . ' AS serv', 'serv.id = self.service_id', 'left' );
        $this->db->join( $this->god . ' AS god', 'god.id = self.deity_id', 'left' );
        $this->db->join( $this->stars . ' AS star1', 'star1.id = self.star1', 'left' );
        $this->db->join( $this->stars . ' AS star2', 'star2.id = self.star2', 'left' );
        $this->db->order_by('self.id','desc');
        $q = $this->db->get_where( $this->mega_function . ' AS self', $where );
        return $q->result_array();
    }
    
    public function ajax() {
        $post = $this->input->post();
       
        switch( $post['act'] ) :
            case 'booked' :
                
                /*
                 * Get all booked details
                 */
                $where['self.book_status'] = 2;
                $where['self.status'] = 1;
                
                if( !empty( $post['fromDate'] ) ) :
                    $e = explode("-", $post['fromDate']);
                    $where['DATE(self.date) >= '] = $e[2] . '-' . $e[1] . '-' . $e[0]; 
                endif;
                
                if( !empty( $post['toDate'] ) ) :
                    $e = explode("-", $post['toDate']);
                    $where['DATE(self.date) <= '] = $e[2] . '-' . $e[1] . '-' . $e[0]; 
                endif;
                
                if( !empty( $post['period'] ) ) :
                    $where['self.period'] = $post['period']; 
                endif;

                $results = $this->get_all( '', $where );
                
            break;    
            
        endswitch;
        
        return $results;
    }
    
    public function get_prasadam_price($id) {
        $this->db->where('id',$id);
        return $this->db->get($this->prasadam)->row();        
    }
    
    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();
    }
    
    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_deity_name($id) {
        $this->db->where('id',$id);
        return $this->db->get(TBL_GOD_NAME)->row();        
    }
}
?>

