<?php
class Donation_report_model extends CI_Model {
    
    function __construct() {
        parent::__construct();
        $this->services = TBL_SERVICE;
        $this->festival = TBL_BILL_FESTIVAL_FUNCTION;
        $this->donation = TBL_BILL_DONATION;
        $this->donation_pledge = TBL_BILL_DONATION_PLEDGE;
        $this->donation_pledge_trans = TBL_BILL_DONATION_TRANSACTION;
    }
    
    public function list_service_type(){
        $this->db->select('id,name as service_name');
        $this->db->where('menu_name',4);
        $this->db->where('status',1);
        return $this->db->get($this->services)->result();
    }
    
    public function get_data($start_date='',$end_date='',$type='',$mobile='',$pledge='',$pledge_type=''){
        $this->db->select('s.name as service_name, s.prefix, d.*, d.amount as total_amount',FALSE);
        $this->db->join($this->services . ' as s','s.id = d.service_id');
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('d.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('d.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('d.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('d.created_on <=',date('Y-m-d 23:59:59'));
        } 
        if(!empty($type)) {
            $this->db->where('d.service_id',$type);
        }
        if(!empty($mobile)) {
            $this->db->where('d.contact_no',$mobile);
        }
        if(!empty($pledge) && $pledge == 2) {
            $this->db->where('d.amount_type',3);
        }

        //$this->db->where('d.status',1);
        return $this->db->get($this->donation . ' as d')->result();
    }
    
    public function get_pledge_data($start_date='',$end_date='',$type='',$mobile='',$pledge='',$pledge_type='') {
        $this->db->select('s.name as service_name, s.prefix, dp.*, dp.pledge_amount as total_amount, dp.amount as paid_amount',FALSE);
        $this->db->join($this->services . ' as s','s.id = dp.service_id');
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('dp.created_on >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('dp.created_on <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('dp.created_on >=',date('Y-m-d 00:00:00'));
            $this->db->where('dp.created_on <=',date('Y-m-d 23:59:59'));
        }
        if(!empty($type)) {
            $this->db->where('dp.service_id',$type);
        }
        if(!empty($mobile)) {
            $this->db->where('dp.contact_no',$mobile);
        }
        if(!empty($pledge)) {
            $this->db->where('dp.amount_type',$pledge_type);
        } else {
            $this->db->where('dp.amount_type',1);
        }
        #$this->db->where('dp.status',1);
        $this->db->order_by('dp.id','desc');
        return $this->db->get($this->donation_pledge . ' as dp')->result();
    }
    
    public function get_donation_pledge_transaction($service_id='',$name='',$contact_no='',$amount_type='',$start_date='',$end_date='',$pledge_id='') {
        /* $this->db->join($this->donation_pledge . ' as dp','dp.service_id = dpt.service_id AND dp.name = dpt.name AND dp.contact_no = dpt.contact_no AND dp.pledge_status'); */
        if(!empty($start_date) && !empty($end_date)) {
            $this->db->where('dpt.pledge_date >=',date('Y-m-d 00:00:00',strtotime($start_date)));
            $this->db->where('dpt.pledge_date <=',date('Y-m-d 23:59:59',strtotime($end_date)));
        } else {
            $this->db->where('dpt.pledge_date >=',date('Y-m-d 00:00:00'));
            $this->db->where('dpt.pledge_date <=',date('Y-m-d 23:59:59'));
        }
        if(!empty($service_id)) {
            $this->db->where('dpt.service_id',$service_id);
        }
        if(!empty($name)) {
            $this->db->where('dpt.name',$name);
        }
        if(!empty($mobile)) {
            $this->db->where('dpt.contact_no',$contact_no);
        }
        if(!empty($amount_type)) {
            $this->db->where('dpt.amount_type',$amount_type);
        }
        if(!empty($pledge_id)) {
            $this->db->where('dpt.pledge_id',$pledge_id);
        }
        #$this->db->where('dpt.status',1);
        $this->db->order_by('dpt.id','desc');
        return $this->db->get($this->donation_pledge_trans . ' as dpt')->result();
    }
}
?>