<?php

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

class Ubayam_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->service = TBL_SERVICE;
        $this->ubayam = TBL_BILL_UBAYAM;
        $this->ubayam_prasadam = TBL_BILL_UBAYAM_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 = 'u.id, u.receipt_no, u.devotee_name1, u.tamil_name1, u.date, u.contact_no, s.name as service, u.amount as u_amount, s.prefix, u.status, u.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = u.service_id');
        #$this->db->where('u.status !=',2);
        $this->db->where('u.book_status',1);
        $this->db->order_by('u.receipt_no','desc');
        return $this->db->get($this->ubayam . ' as u')->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->ubayam);
    }
    
    public function insert($data) {
        $this->db->insert($this->ubayam,$data);
        return $this->db->insert_id();
    }
    
    public function insert_prasadam($data) {
        $this->db->insert($this->ubayam_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->ubayam,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam($data,$id) {
        $this->db->where('ubayam_id',$id);
        $this->db->update($this->ubayam_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function update_prasadam_by_id($data,$id) {
        $this->db->where('id',$id);
        $this->db->update($this->ubayam_prasadam,$data);
        return $this->db->affected_rows();
    }
    
    public function ubayam_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->ubayam)->num_rows();
    }
    
    public function get_by_id($id) {
        $select = 'u.*, CASE u.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, u.status AS status';
        $select .= ', CASE u.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, u.amount as u_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 = u.service_id');
        $this->db->join($this->god . ' as g','g.id = u.deity_id','left');
        $this->db->join($this->stars . ' as star','star.id = u.star1','left');
        $this->db->join($this->stars . ' as star1','star1.id = u.star2','left');
        $this->db->where('u.id',$id);
        return $this->db->get($this->ubayam . ' as u')->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 get_prasadam_by_id($id){
        $this->db->select('up.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = up.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = up.deity_id','left');
        $this->db->where('up.ubayam_id',$id);
        $this->db->where('up.status !=',2);
        return  $this->db->get($this->ubayam_prasadam . ' as up')->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 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 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_json($id){
        $this->db->select('up.*, g.name as god_name, p.name as prasadam_name',FALSE);
        $this->db->join($this->prasadam . ' p','p.id = up.prasadam','left');
        $this->db->join($this->god . ' as g','g.id = up.deity_id','left');
        $this->db->where('up.ubayam_id',$id);
        $this->db->where('up.status !=',2);
        return  $this->db->get($this->ubayam_prasadam . ' as up')->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_UBAYAM . ' 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();        
    }

    public function get_payment_date( $bu_id ) {
        $where['self.pay_ref_type'] = 17; // Billing Kattalai Archanai
        $where['self.pay_ref_id'] = $bu_id;
        $where['self.status'] = 1;        
        $q = $this->db->get_where( TBL_PAY . ' AS self', $where );
        return $q->result_array();
    }
}
?>

