<?php
if (!defined('BASEPATH')) exit('No direct script access allowed');
class other_services_model extends CI_Model {
    public function __construct() {
        parent::__construct();
        $this->other_services = TBL_BILL_OTHERS;
        $this->payment = TBL_PAY_TYPE;
        $this->service = TBL_SERVICE;
        $this->service_menu = TBL_SERVICE_MENU;
        $this->stars = TBL_STAR;
        $this->paid_type = TBL_PAY;
    }
    
    public function list_all() {
        $select = 'os.id, os.receipt_no, os.devotee_name, os.contact_no, s.name as service, s.prefix, os.amount as os_amount, os.status, os.del_reason';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = os.service_id');
        #$this->db->where('os.status !=',2);
        $this->db->order_by('os.receipt_no','desc');
        return $this->db->get($this->other_services . ' as os')->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 star_lists() {
        $this->db->where('status',1);
        return $this->db->get($this->stars)->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->other_services, $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');
	    $this->db->order_by('id','desc');
        return $this->db->get($this->other_services);
    }
    
    public function update($data,$id){
        $this->db->where('id',$id);
        $this->db->update($this->other_services,$data);
        return $this->db->affected_rows();
    }
    
    public function get_by_id($id) {
        $select = 'os.*, CASE os.status WHEN 1 THEN "Active" ELSE "Inactive" END as status_str, os.status AS status';
        $select .= ', CASE os.period WHEN 1 THEN "Morning" ELSE "Evening" END as period_str, star.name as star, star.tamil_name as star_tamil_name, s.name as service, os.amount as os_amount, s.amount as ser_amount, s.prefix';
        $this->db->select($select,FALSE);
        $this->db->join($this->service . ' as s','s.id = os.service_id','left');
        $this->db->join($this->stars . ' as star','star.id = os.star_id','left');
        $this->db->where('os.id',$id);
        return $this->db->get($this->other_services . ' as os')->row();
    }
    
    public function other_service_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->other_services)->num_rows();
    }
    
    public function insert_payment($data){
        $this->db->insert($this->paid_type,$data);
        return $this->db->insert_id();
    }
    
    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 getPaidType($type,$id) {
        $this->db->select('pt.pay_amount, pay.name, pay.img_url', FALSE);
        $this->db->join($this->payment . ' as pay','pay.id = pt.pay_type_id');
        $this->db->where('pt.pay_ref_type',$type);
        $this->db->where('pt.pay_ref_id',$id);
        $this->db->where('pt.status',1);
        return $this->db->get($this->paid_type . ' as pt')->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->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_payment_date( $bot_id ) {
        $where['self.pay_ref_type'] = 14; // Billing Kattalai Archanai
        $where['self.pay_ref_id'] = $bot_id;
        $where['self.status'] = 1;        
        $q = $this->db->get_where( TBL_PAY . ' AS self', $where );
        return $q->result_array();
    }
}
?>