<?php

/* 
 * Author       : Kowsalya K
 * Purpose      : Model for Billing Festival and Function
 * Created On   : 2016-08-04
 */

class Festival_function_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->festival = TBL_BILL_FESTIVAL_FUNCTION;
        $this->festival_prasadam = TBL_BILL_FESTIVAL_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;
        $this->bill_devotee = TBL_BD;
    }
    
    public function list_all() {
        $select = 'f.id, f.receipt_no, f.devotee_name1, f.tamil_name1, f.date, f.contact_no, s.name as service, s.prefix, f.amount as f_amount, f.status, f.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = f.service_id');
        #$this->db->where('f.status !=',2);
        $this->db->where('f.book_status',1);
        $this->db->order_by('f.receipt_no','desc');
        return $this->db->get($this->festival . ' as f')->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 service_type_exists($type, $bk_status = '') {
        $this->db->where('service_id',$type);
        $this->db->where('status !=',2);
        if( $bk_status != '' ) :
            $this->db->where('book_status', $bk_status);
        endif;
        
        $this->db->order_by('receipt_no','desc');
        return $this->db->get($this->festival);
    }
    
    public function insert($data) {
        $this->db->insert($this->festival,$data);
        return $this->db->insert_id();
    }
    
    public function insert_prasadam($data) {
        $this->db->insert($this->festival_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->festival,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam($data,$id) {
        $this->db->where('festival_id',$id);
        $this->db->update($this->festival_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam_by_id($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->festival_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function festival_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->festival)->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, s.prefix, g.name as god_name, star.name as star1_name, star1.name as star2_name, star.tamil_name as star_tamil_name1, star1.tamil_name as star_tamil_name2, f.amount as f_amount, s.amount as ser_amount';
        $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->festival . ' 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('fp.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = fp.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = fp.deity_id','left');
        $this->db->where('fp.festival_id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->festival_prasadam . ' as fp')->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('fp.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = fp.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = fp.deity_id','left');
        $this->db->where('fp.festival_id',$id);
        $this->db->where('fp.status !=',2);
        return  $this->db->get($this->festival_prasadam . ' as fp')->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( TBL_SERVICE . ' AS serv', 'serv.id = self.service_id', 'left' );
        $this->db->join( TBL_GOD_NAME . ' AS god', 'god.id = self.deity_id', 'left' );
        $this->db->join( TBL_STAR . ' AS star1', 'star1.id = self.star1', 'left' );
        $this->db->join( TBL_STAR . ' AS star2', 'star2.id = self.star2', 'left' );
        $this->db->order_by('self.id','desc');
        $q = $this->db->get_where( TBL_BILL_FESTIVAL_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('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();        
    }
}
?>

