<?php

defined('BASEPATH') OR exit('No direct script access allowed');

require_once APPPATH . '/libraries/Custom_Model.php';
class user_management_model extends Custom_Model {

		public function __construct() 
		{
        	parent::__construct();
        }


        public function getContributionData($userid)
        {
          	
        	//$contributions = $this->user_management_model->selectCustomRecords(array('id', 'user_id', 'cont_type', 'cont_towards', 'amount', 'payment_mode', 'payment_ref','gen_dev_fund_remarks', 'mat_delivery_point', 'mat_delivery_date','DATE_FORMAT(timestamp,"%d-%m-%Y %h:%i %p") as contribute_date'),array('user_id' => $userid),CSR_CONTRIBUTIONS);

            $this->db->SELECT('CSR_CONTRIBUTIONS.id, CSR_CONTRIBUTIONS.user_id, CSR_CONTRIBUTIONS.cont_type, CSR_CONTRIBUTIONS.cont_towards, CSR_CONTRIBUTIONS.amount, CSR_CONTRIBUTIONS.amount, CSR_CONTRIBUTIONS.payment_mode,CSR_CONTRIBUTIONS.payment_ref,CSR_CONTRIBUTIONS.gen_dev_fund_remarks,CSR_CONTRIBUTIONS.mat_delivery_point,CSR_CONTRIBUTIONS.mat_delivery_date,DATE_FORMAT(convert_tz(CSR_CONTRIBUTIONS.timestamp,"+00:00","+05:30"),"%d-%m-%Y %h:%i %p") as contribute_date,CSR_TXN_DETAILS.merchant_txn_id,CSR_TXN_DETAILS.txnstatus,concat(DATE_FORMAT(convert_tz(CSR_CONTRIBUTIONS.timestamp,"+00:00","+05:30"),"%Y%m%d"),CSR_TXN_DETAILS.merchant_txn_id) as ref_id,CSR_TXN_DETAILS.trackid,CSR_CONTRIBUTIONS.payment_type,CSR_CONTRIBUTIONS.payment_date,CSR_CONTRIBUTIONS.bank_branch,CSR_CONTRIBUTIONS.mode_of_deposit,CSR_CONTRIBUTIONS.cheque_drop_address');
                    $this->db->FROM(CSR_CONTRIBUTIONS.' as CSR_CONTRIBUTIONS');
                    $this->db->JOIN(CSR_TXN_DETAILS.' as CSR_TXN_DETAILS','CSR_CONTRIBUTIONS.id = CSR_TXN_DETAILS.con_id','LEFT');
                    $this->db->WHERE('CSR_CONTRIBUTIONS.user_id',$userid);
                    $this->db->ORDER_BY('CSR_CONTRIBUTIONS.id','DESC');
                   $contributions= $this->db->GET()->result_array();
                    //print_r($contributions);die();

        	if(count($contributions))
        	{
        		foreach ($contributions as $contribution_key => $contribution) 
        		{
	        			
	        		$this->db->SELECT('CSR_CONTRIBUTION_DETAILS.id, CSR_CONTRIBUTION_DETAILS.cont_id, CSR_CONTRIBUTION_DETAILS.req_id, CSR_CONTRIBUTION_DETAILS.qty, CSR_CONTRIBUTION_DETAILS.amount, CSR_CONTRIBUTION_DETAILS.school_id, CSR_CONTRIBUTION_DETAILS.dist_id,STUDENTS_SCHOOL_CHILD_COUNT.district_name,STUDENTS_SCHOOL_CHILD_COUNT.school_name,CSR_MATERIAL_MASTER.material_name');
	          		$this->db->FROM(CSR_CONTRIBUTION_DETAILS.' as CSR_CONTRIBUTION_DETAILS','CSR_USER.id = CSR_CONTRIBUTIONS.CSR_USER.id');
	          		$this->db->JOIN(STUDENTS_SCHOOL_CHILD_COUNT.' as STUDENTS_SCHOOL_CHILD_COUNT','STUDENTS_SCHOOL_CHILD_COUNT.school_id = CSR_CONTRIBUTION_DETAILS.school_id');
                    $this->db->JOIN(CSR_SCHOOL_REQUIREMENTS.' as CSR_SCHOOL_REQUIREMENTS','CSR_CONTRIBUTION_DETAILS.req_id = CSR_SCHOOL_REQUIREMENTS.id');
                    $this->db->JOIN(CSR_MATERIAL_MASTER.' as CSR_MATERIAL_MASTER','CSR_SCHOOL_REQUIREMENTS.mat_id = CSR_MATERIAL_MASTER.id');
	          		$this->db->WHERE('CSR_CONTRIBUTION_DETAILS.cont_id',$contribution['id']);
	          		$child_data = $this->db->GET()->result_array();
                    //print_r($this->db->last_query());die();
	          		$contributions[ $contribution_key ]['child_data'] = $child_data;

          		}
        	}

        	return $contributions;
          	
        }

        function getProfileData($userid)
        {
            $this->db->SELECT('CSR_USER.id, CSR_USER.name, CSR_USER.email, CSR_USER.mobile, CSR_USER.user_type, CSR_USER.isactive, CSR_USER.profile_share, CSR_USER.notify_pref,CSR_USER_ORG.orgname,CSR_USER_ORG.org_address,CSR_USER_ORG.org_pan,CSR_USER_ORG.id as org_id');
            $this->db->FROM(CSR_USER.' as CSR_USER');
            $this->db->JOIN(CSR_USER_ORG.' as CSR_USER_ORG','CSR_USER.id = CSR_USER_ORG.user_id','LEFT');
            $this->db->WHERE('CSR_USER.id',$userid);
            return $this->db->GET()->result_array();
        }

        function getMailContentForOnlineTransaction($merchanttxnid)
        {
            $this->db->SELECT('CSR_TXN_DETAILS.merchant_txn_id,CSR_TXN_DETAILS.amount,CSR_TXN_DETAILS.trackid,CSR_TXN_DETAILS.con_id,CSR_TXN_DETAILS.user_id,concat(DATE_FORMAT(convert_tz(CSR_CONTRIBUTIONS.timestamp,"+00:00","+05:30"),"%Y%m%d"),CSR_TXN_DETAILS.merchant_txn_id) as ref_id,CSR_USER.id, CSR_USER.name,CSR_USER.email,CSR_TXN_DETAILS.txnstatus');
            $this->db->FROM(CSR_TXN_DETAILS.' as CSR_TXN_DETAILS');
            $this->db->JOIN(CSR_CONTRIBUTIONS.' as CSR_CONTRIBUTIONS','CSR_TXN_DETAILS.con_id = CSR_CONTRIBUTIONS.id');
            $this->db->JOIN(CSR_USER.' as CSR_USER','CSR_TXN_DETAILS.user_id = CSR_USER.id');
            $this->db->WHERE('CSR_TXN_DETAILS.merchant_txn_id',$merchanttxnid);
            return $this->db->GET()->result_array();
        }

        function getKTContributionData($userid)//STUDENTS_SCHOOL_CHILD_COUNT.district_id,STUDENTS_SCHOOL_CHILD_COUNT.district_name
        {
            $this->db->SELECT('CSR_KT_CONTRIBUTIONS.kt_id,CSR_KT_CONTRIBUTIONS.fk_district_id,STUDENTS_SCHOOL_CHILD_COUNT.district_name,CSR_KT_CONTRIBUTIONS.con_type,CSR_KT_CONTRIBUTIONS.description,CSR_KT_CONTRIBUTIONS.con_sub_type,CSR_KT_CONTRIBUTIONS.electric_other,concat(DATE_FORMAT(convert_tz(CSR_KT_CONTRIBUTIONS.createdon,"+00:00","+05:30"),"%Y-%m-%d %h:%i:%s %p")) as createdon');
            $this->db->FROM(CSR_KT_CONTRIBUTIONS.' as CSR_KT_CONTRIBUTIONS');
            $this->db->JOIN(SCHOOLNEW_DISTRICT.' as STUDENTS_SCHOOL_CHILD_COUNT','CSR_KT_CONTRIBUTIONS.fk_district_id = STUDENTS_SCHOOL_CHILD_COUNT.id');
            //$this->db->JOIN(CSR_VOLUNTEER_INFO.' as CSR_VOLUNTEER_INFO','CSR_KT_CONTRIBUTIONS.kt_id = CSR_VOLUNTEER_INFO.fk_kt_id','LEFT');
           // $this->db->JOIN(CSR_USER.' as CSR_USER','CSR_KT_CONTRIBUTIONS.user_id = CSR_USER.id');
            $this->db->WHERE('CSR_KT_CONTRIBUTIONS.fk_user_id',$userid);
            //return
            $kt_con =  $this->db->GET()->result_array();
            if($kt_con)
            {
                  foreach ($kt_con as $key => $value) 
                  {
                        if($value['con_type'] == 3)//volunteer
                        {
                             $this->db->SELECT('CSR_VOLUNTEER_INFO.volunteer_info_id,CSR_VOLUNTEER_INFO.kt_type_desc,CSR_VOLUNTEER_INFO.date_from,CSR_VOLUNTEER_INFO.date_to,CSR_VOLUNTEER_INFO.time_from,CSR_VOLUNTEER_INFO.subject,CSR_VOLUNTEER_INFO.subject_others,CSR_VOLUNTEER_INFO.time_to,concat(DATE_FORMAT(convert_tz(CSR_VOLUNTEER_INFO.createdon,"+00:00","+05:30"),"%Y-%m-%d %h:%i:%s %p")) as createdon');
                              $this->db->FROM(CSR_VOLUNTEER_INFO.' as CSR_VOLUNTEER_INFO');
                              $this->db->WHERE('CSR_VOLUNTEER_INFO.fk_kt_id',$value['kt_id']);
                               $volunteer_info =  $this->db->GET()->result_array();
                               $kt_con[$key]['volunteer_info'] = $volunteer_info;
                        }
                        if($value['con_type'] == 2)//devices
                        {
                             $this->db->SELECT('CSR_KT_DEVICES.id, CSR_KT_DEVICES.kt_con_id, CSR_KT_DEVICES.model, CSR_KT_DEVICES.serial_no, CSR_KT_DEVICES.accessoris,concat(DATE_FORMAT(convert_tz(CSR_KT_DEVICES.createdon,"+00:00","+05:30"),"%Y-%m-%d %h:%i:%s %p")) as createdon');
                              $this->db->FROM(CSR_KT_DEVICES.' as CSR_KT_DEVICES');
                              $this->db->WHERE('CSR_KT_DEVICES.kt_con_id',$value['kt_id']);
                               $devices_info =  $this->db->GET()->result_array();
                               $kt_con[$key]['devices_info'] = $devices_info;
                        }
                        if($value['con_type'] == 1)//books
                        {
                             $this->db->SELECT('CSR_KT_BOOKS.id,CSR_KT_BOOKS.kt_con_id,CSR_KT_BOOKS.book,CSR_KT_BOOKS.author, CSR_KT_BOOKS.qty,concat(DATE_FORMAT(convert_tz(CSR_KT_BOOKS.createdon,"+00:00","+05:30"),"%Y-%m-%d %h:%i:%s %p")) as createdon');
                              $this->db->FROM(CSR_KT_BOOKS.' as CSR_KT_BOOKS');
                              $this->db->WHERE('CSR_KT_BOOKS.kt_con_id',$value['kt_id']);
                               $books_info =  $this->db->GET()->result_array();
                               $kt_con[$key]['books_info'] = $books_info;
                        }
                  }  
            }
             return $kt_con;
        }


}