<?php defined('BASEPATH')or exit("NO direct Acccess is Allowed");

class AccountModel extends CI_Model{

  public function __construct(){
    parent::__construct();
  }
  public function teacehr_current_pay_mode($data){
    $this->db->insert('teacehr_current_pay_mode',$data);
    return $this->db->insert_id();
  }
  public function teacehr_current_pay_mode_num_row_id($teacher_id){
    $query =$this->db->select('id')->from('teacehr_current_pay_mode')->where('teacher_id',$teacher_id)->get();
    return $query->num_rows();
  }
  public function teacehr_current_pay_mode_num_row_multi($teacher_id,$paymode,$slary_unit_price){
      $query =$this->db->select('id')->from('teacehr_current_pay_mode')->where('teacher_id',$teacher_id)
    ->where('pay_mode',$paymode)
    ->where('salary_unit_price',$slary_unit_price)
    ->get();
    return $query->num_rows();
  }
  public function teacehr_current_pay_mode_update($data){
    $this->db->update('teacehr_current_pay_mode',$data);
    return true;
  }
  public function teacehr_current_pay_mode_history($data){
    $this->db->insert('teacehr_current_pay_mode_history',$data);
    return $this->db->insert_id();
  }
  public function latest_payments_student_dashboard_list($limit, $offset){
    	$offset = ($offset-1)*$limit;
    $query = $this->db->select(['student_current_wallet.*',
    '(select wallet_add_date_time from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as wallet_add_date_time',
    '(select wallet_amount from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as w_amount',
    '(select sum(wallet_amount) from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id) as total_amount',

    '(select mode_of_pay from mode_of_payemnt where mode_of_payemnt.id=(select mode_of_payment from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1)) as mode_of_payment',
    '(select remarks from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as remarks',
    '(select concat_ws(" ",master_user.userid,master_user.first_name,master_user.last_name) from master_user where
    master_user.id=student_current_wallet.student_id)as student_name'])->from('student_current_wallet')

    ->limit($limit, $offset)->get();
    //echo $this->db->last_query();die;
    return $query->result();
  }
  public function latest_payments_student_dashboard_numrows(){
    $query = $this->db->select(['student_current_wallet.*',
    '(select wallet_add_date_time from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as wallet_add_date_time',
    '(select wallet_amount from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as w_amount',
    '(select sum(wallet_amount) from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id) as total_amount',
    '(select mode_of_pay from mode_of_payemnt where mode_of_payemnt.id=(select mode_of_payment from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1)) as mode_of_payment',
    '(select remarks from student_wallet_amount_history where
    student_wallet_amount_history.current_wallet_id=student_current_wallet.id order by id desc limit 1) as remarks',
    '(select concat_ws(" ",master_user.userid,master_user.first_name,master_user.last_name) from master_user where
    master_user.id=student_current_wallet.student_id)as student_name'])->from('student_current_wallet')
      ->get();
    return $query->num_rows();
  }
  public function student_current_wallet_list($limit, $offset){
    	$offset = ($offset-1)*$limit;
    $query = $this->db->select(['student_current_wallet.*',
    '(select concat_ws(" ",master_user.first_name,master_user.last_name) from master_user
     where master_user.id=student_current_wallet.student_id)as student_name',
     '(select master_user.userid from master_user
      where master_user.id=student_current_wallet.student_id)as userid'])->from('student_current_wallet')
    ->limit($limit, $offset)->get();
    return $query->result();
  }

  public function student_current_wallet_list_num_rows(){
    $query = $this->db->select(['student_current_wallet.id'])->from('student_current_wallet')
    ->get();
    return $query->num_rows();
  }

  public function current_amount_student_fetch($id){
    $query = $this->db->select(['student_current_wallet.*','student_wallet_amount_history.id as historyid',
    'student_wallet_amount_history.current_wallet_id','student_wallet_amount_history.wallet_add_date_time',
    'student_wallet_amount_history.wallet_amount as w_amount','student_wallet_amount_history.mode_of_payment',
    'student_wallet_amount_history.remarks','(select concat_ws(" ",master_user.userid,master_user.first_name,master_user.last_name) from master_user where master_user.id=student_current_wallet.student_id)as student_name'])->from('student_current_wallet')
    ->join('student_wallet_amount_history','student_wallet_amount_history.current_wallet_id=student_current_wallet.id','inner')
    ->where('student_current_wallet.id',$id)->get();
    return $query->result();
  }
  public function insert_current_ammount_student($data_array){
    $this->db->insert('student_current_wallet',$data_array);
    return $this->db->insert_id();
  }
  public function update_current_ammount_student($stid,$amount){
    $this->db->set('wallet_amount',$amount);
    $this->db->where('student_id',$stid);
    $this->db->update('student_current_wallet');
    return true;
  }
  public function update_historyammount_student($id,$array){
    $this->db->where('id',$stid);
    $this->db->update('student_wallet_amount_history',$array);
    return true;
  }
  public function find_duplicate_current_amount_student($stid){
    $query = $this->db->select('*')->from('student_current_wallet')->where('student_id',$stid)->get();
    return $query->num_rows();
  }
  public function find_row_current_amount_student($stid){
    $query = $this->db->select('*')->from('student_current_wallet')->where('student_id',$stid)->get();
    return $query->row();
  }
  public function amount_history_sum($stid){
    $query = $this->db->select('sum(wallet_amount) as totalsum')->from('student_current_wallet')->where('student_id',$stid)->get();
    return $query->row();
  }
  public function insert_history_ammount_student($data_array){
    $this->db->insert('student_wallet_amount_history',$data_array);
    return $this->db->insert_id();
  }
  public function course_list($limit, $offset){
		$offset = ($offset-1)*$limit;
		$query  = $this->db->select(['*','(select CONCAT(first_name, " ", last_name) from v2_faculty  join master_user on master_user.id=v2_faculty.teacher_id where course.faculty=v2_faculty.id) as faculty',
    '(select session_tag_id from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as session_name',
    '(select total_attendee from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as total_attendee',
    '(select attendee_limit from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as attendee_limit',
    // '(select count_month from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as parallel_session_tag',
    '(select category from category where category.id=course.category)as category_id',
    '(select subject from subject where subject.id=course.subject)as subject_id',
    '(select class from class where class.id=course.class)as class_id','(select count(id) from course_vise_student where course_id=course.id and status=1)as course_student',
    '(select count(id) from course_vise_student where course_id=course.id and status=0)as inactive_student'])->from('course')
    	->limit($limit, $offset)
		  ->get();
		//echo $this->db->last_query();die;
		return $query->result();
	}
  public function course_list_num_rows(){
    $query  = $this->db->select(['*','(select CONCAT(first_name, " ", last_name) from v2_faculty  join master_user on master_user.id=v2_faculty.teacher_id where course.faculty=v2_faculty.id) as faculty',
    '(select session_tag_id from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as session_name',
    '(select total_attendee from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as total_attendee',
    '(select attendee_limit from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as attendee_limit',
    // '(select count_month from parallel_session_purchased where parallel_session_purchased.id=course.parallel_session_purchased)as parallel_session_tag',
    '(select category from category where category.id=course.category)as category_id',
    '(select subject from subject where subject.id=course.subject)as subject_id',
    '(select class from class where class.id=course.class)as class_id','(select count(id) from course_vise_student where course_id=course.id and status=1)as course_student',
    '(select count(id) from course_vise_student where course_id=course.id and status=0)as inactive_student'])->from('course')
      ->get();
		//echo $this->db->last_query();die;
		return $query->num_rows();
	}
  public function student_lists(){
      $query		=	  $this->db->select(['id','first_name','last_name','gender','email','userid'])->from('master_user')->where('category',2)->order_by("id", "desc")->get();
      $return 	= 	array();
      if($query->num_rows() > 0) {
          $return[''] = ('Select Student');
          foreach($query->result_array() as $row) {
              $return[$row['id']] = strtoupper($row['userid'])." | ".strtoupper($row['first_name'])." ".strtoupper($row['last_name']);
          }
      }
      return $return;
  }
  public function teacher_lists(){
      $query		=	  $this->db->select(['master_user.id','master_user.first_name',
      'master_user.last_name','master_user.gender','master_user.email','master_user.userid',])->from('v2_faculty')
      ->join('master_user','master_user.id=v2_faculty.teacher_id')
      ->where('master_user.category',1)->order_by("id", "desc")->get();
      $return 	= 	array();
      if($query->num_rows() > 0) {
          $return[''] = ('Select Teacher');
          foreach($query->result_array() as $row) {
              $return[$row['id']] = strtoupper($row['userid'])." | ".strtoupper($row['first_name'])." ".strtoupper($row['last_name']);
          }
      }
      return $return;
  }
  public function payment_mode_teacher(){
      $query		=	  $this->db->select(['teacher_payment_type.id','teacher_payment_type.payment_mode'])->from('teacher_payment_type')
      ->order_by("id", "desc")->get();
      $return 	= 	array();
      if($query->num_rows() > 0) {
          $return[''] = ('Select Pay Mode');
          foreach($query->result_array() as $row) {
              $return[$row['id']] = strtoupper($row['payment_mode']);
          }
      }
      return $return;
  }
  public function mode_of_payemnt_lists(){
      $query		=	  $this->db->select(['*'])->from('mode_of_payemnt')->order_by("id", "desc")->get();
      $return 	= 	array();
      if($query->num_rows() > 0) {
          $return[''] = ('Select Payment Mode');
          foreach($query->result_array() as $row) {
              $return[$row['id']] = strtoupper($row['mode_of_pay'])." | ".strtoupper($row['type']);
          }
      }
      return $return;
  }

}
