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


class student_model extends CI_Model {

    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
         public function promotion_id($val=0)
    {
       $this->db->select('id');
       $data=$this->db->from('promotion')->where('student_id',$val)->where('is_active',1)->get()->result_array()[0]['id'];
       return $data;
    }
    
     public function save_holiday($data)
	{
            $fields = array(

                            'branch_id' =>  $data['branch'],
                            'class_id' =>  $data['class'],
                            'section_id' =>  $data['section'],
                            'froom' =>  date('Y-m-d',strtotime($data['from'])),
                            'too' =>  date('Y-m-d',strtotime($data['to'])),
                            'h_id' =>  $data['h_id'],
                           
                );
              
             return $this->db->insert('holiday',$fields);
             
	}
	
	public function save_short_info($data,$url)
	{	
	     $branch = $data['branch'];
        $class = $data['class_admited'];
        $section = $data['section'];
        $session = "9";
        //'prev_class' => $data['class_id'],
        
            $fields_student = array(

                            'branch' =>  $branch,
                            'sid' => $data['sid'],
                            'grno' => $data['grno'],
                            'roll_no' => $data['roll_no'],
                            'student_name' => empty($data['name']) ? "" : $data['name'] ,
                            'student_cnic' => empty($data['student_cnic']) ? "" : $data['student_cnic'] ,
                            'class_admited' => $data['class_admited'],
                            'class_group' => $data['group_name'],
                            
                            'gender' => empty($data['gender']) ? "" : $data['gender'] ,
                            'student_contact' => empty($data['student_contact']) ? "" : $data['student_contact']
                           
                    
                );
              
             $this->db->insert('student',$fields_student);
             $student = $this->db->insert_id();
             $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
            
             $student_info = $this->db->select('id, student_name, sid, student_contact')->from('student')->where('id', $student )->get()->row();
           if($branch==1){
             $student_data = array(
               'student_id' => $student_info->id,
               'first_name' => $student_info->student_name ,
               'username' => $student_info->sid,
               'password' => md5('morning'),
               'ch_sh_pas'=>"morning",
               'teacher_id'=>0
             );  
           }else{
               $student_data = array(
               'student_id' => $student_info->id,
               'first_name' => $student_info->student_name ,
               'username' => $student_info->sid,
               'password' => md5('evening'),
               'ch_sh_pas'=>"evening",
               'teacher_id'=>0
             );
           }
             

            $this->db->insert('admin', $student_data);

             $promotion = array(
                 'student_id' => $student,
                 'class_id' => $class,
                 'section_id' => $section,
                 'session_id' => $session
             );

             $this->db->insert('promotion',$promotion);
             $prom_id = $this->db->insert_id();

            $fee_pack = $data['net_fee'];
            $admin_fee = $data['admin_fee'];
            $this->db->select("late_fine,due_date");
            $this->db->where("is_delete",'0');
            $this->db->where("id",$branch);
            $branch_data = $this->db->get("branch")->result_array()[0];
            
            $late_fine = $branch_data['late_fine'];

            $date = date('Y-m-d',strtotime($data['fftmo']));
            $exp_date =  date('Y-m-d',strtotime($data['exp_date']));

            $invoice = array(
                'branch_id' => $data['branch'],
                "student_id"=>$prom_id,
                "fee_pack"=>$fee_pack,
                "admin_fee"=>$admin_fee,
                "date"=>$date,
                "date_expire"=>$exp_date,
                "late_fine"=>$late_fine,
                "is_admitted"=>"1"
            );
            $this->db->insert("invoice",$invoice);
            $inv = $this->db->insert_id();
            if(@!empty($data['fee']))
            {
                foreach ($data['fee'] as $k => $d) {
                    $install = array(
                        'invoice' => $inv,
                        'amount' => $d,
                        'fee_id' => $k
                    );
                    $this->db->insert('fee_installment',$install);
                }	
            }
             return $inv;
	}
	public function save($data,$url)
	{

// echo "<pre>";print_r($data);die;
	$this->load->library('hajanasms');
        $branch = $data['branch'];
        $class = $data['class_admited'];
        $section = $data['section'];
        $session = $data['session'];
        $disc_value = $data['disc_value'];
        //'mark_identification' => empty($data['mark_of_identification']) ? "" : $data['mark_of_identification']  ,'dob_words' => empty($data['dob_words']) ? "" : $data['dob_words'] ,
          //'mother_tongue  ' => empty($data['mother_tongue']) ? "" : $data['mother_tongue'] ,
         // 'guardian_name' => empty($data['guardianname']) ? "" : $data['guardianname']  ,
          //'pob' => empty($data['pob']) ? "" : $data['pob'], 'surname'=>empty($data['surname']) ? "" : $data['surname']  ,
            $fields_student = array(

                            'branch' =>  $branch,
                            'sid' => $data['sid'],
                            'grno' => $data['grno'],
                            'roll_no' => $data['roll_no'],
                            'class_admited' => $data['class_admited'],
                            'class_group' => $data['group_name'],
                            'prev_class' => $data['class_id'],
                            'student_name' => empty($data['name']) ? "" : $data['name'] ,
                            'student_cnic' => empty($data['student_cnic']) ? "" : $data['student_cnic'] ,
                            'father_name'=>empty($data['fathername']) ? "" : $data['fathername'] ,
                             
                            'gender' => empty($data['gender']) ? "" : $data['gender'] ,
                            
                            'previous_school' => empty($data['preschool']) ? "" : $data['preschool']  ,
                            'relation_with_guardian' => empty($data['rwg']) ? "" : $data['rwg']  ,
                            'father_cnic' => empty($data['father_cnic']) ? "" : $data['father_cnic']  ,
                            'guardian_cnic' => empty($data['guardian_cnic']) ? "" : $data['guardian_cnic']  ,
                            
                            'religion' => empty($data['religion']) ? "" : $data['religion']  ,
                           
                            
                            'dob' => empty($data['dob']) ? "" : date('Y-m-d',strtotime($data['dob']))  ,
                            
                            'date_of_admission' => empty($data['doa']) ? "" : date('Y-m-d',strtotime($data['doa'] )) ,
                            'province' => "",
                            'taluka' => "" ,
                            'district' => "",
                            'city' => "" ,
                            'father_occupation' => empty($data['father_occ']) ? "" : $data['father_occ']  ,
                            'guardian_occupation' => empty($data['guardian_occ']) ? "" : $data['guardian_occ']  ,
                            'income_family' => empty($data['father_income']) ? "" : $data['father_income']  ,
                            'img' => $url  ,
                            'perment_address' => $data['address']  ,
                            'postal_address' => "" ,
                            'father_contact' => empty($data['father_contact']) ? "" : $data['father_contact']  ,
                            'student_contact' => empty($data['student_contact']) ? "" : $data['student_contact']  ,
                            'guardian_contact' => empty($data['guardian_contact']) ? "" : $data['guardian_contact']  ,
                            'disc_type' => empty($data['disc_type']) ? "0" : $data['disc_type']  ,
                            'disc_value' => $disc_value
                    
                );
              
             $this->db->insert('student',$fields_student);
             $student = $this->db->insert_id();
             $b_name = $this->db->select('name')->from('branch')->where('id',$branch)->get()->row()->name;
        
            
             $student_info = $this->db->select('id, student_name, sid, student_contact')->from('student')->where('id', $student )->get()->row();
           if($branch==1){
             $student_data = array(
               'student_id' => $student_info->id,
               'first_name' => $student_info->student_name ,
               'username' => $student_info->sid,
               'password' => md5('morning'),
               'ch_sh_pas'=>"morning",
               'teacher_id'=>0
             );  
           }else{
               $student_data = array(
               'student_id' => $student_info->id,
               'first_name' => $student_info->student_name ,
               'username' => $student_info->sid,
               'password' => md5('evening'),
               'ch_sh_pas'=>"evening",
               'teacher_id'=>0
             );
           }
             

            $this->db->insert('admin', $student_data);

          $parent_data= array(
               'student_id' => $student_info->id,
               'first_name' => $student_info->student_name ,
               'username' => $student_info->sid . '_parent',
               'password' => md5('softvilla'),
               //'contact_no' => $student_info->student_contact 
             );

             //$this->db->insert('admin', $parent_data); 

             $promotion = array(
                 'student_id' => $student,
                 'class_id' => $class,
                 'section_id' => $section,
                 'session_id' => $session
             );

             $this->db->insert('promotion',$promotion);
             $prom_id = $this->db->insert_id();

            // $this->db->select("tution_fee,admin_fee");
            // $this->db->where("is_delete",'0');
            // $this->db->where("class_id",$class);
            // $c_data = $this->db->get("class")->result_array()[0];
            // $fee_pack = $c_data['tution_fee'];
            // $admin_fee = $data['admin_fee'];

            $fee_pack = $data['net_fee'];
            $admin_fee = $data['admin_fee'];
            $this->db->select("late_fine,due_date");
            $this->db->where("is_delete",'0');
            $this->db->where("id",$branch);
            $branch_data = $this->db->get("branch")->result_array()[0];
            
            $late_fine = $branch_data['late_fine'];

            $date = date('Y-m-d',strtotime($data['fftmo']));
            $exp_date =  date('Y-m-d',strtotime($data['exp_date']));

            $invoice = array(
                'branch_id' => $data['branch'],
                "student_id"=>$prom_id,
                "fee_pack"=>$fee_pack,
                "admin_fee"=>$admin_fee,
                "date"=>$date,
                "date_expire"=>$exp_date,
                "late_fine"=>$late_fine,
                "is_admitted"=>"1"
            );
            $this->db->insert("invoice",$invoice);
            $inv = $this->db->insert_id();
            //echo "<pre>";print_r($data['fee']);exit;
            if(@!empty($data['fee']))
            {
                foreach ($data['fee'] as $k => $d) {
                    $install = array(
                        'invoice' => $inv,
                        'amount' => $d,
                        'fee_id' => $k
                    );
                    $this->db->insert('fee_installment',$install);
                }	
            }
            $msg="Dear Parents,\n$b_name welcomes your child $data[name] to our school.\nThank You!";
            $check = @$this->db->select('is_permit')->where('id',9)->get('tsms_permission')->row()->is_permit;
                if($check==1){
                      $this->hajanasms->sendOneNumber($data['father_contact'],$msg);
                }
             return $inv;
	}


public function studentchartl()       
    {
      $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
    
       $this->db->select("count(*) as total");
       $this->db->from("student");
       $this->db->where("status='25'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
       //var_dump($rslt);
       //die();
        return $rslt;
        }else
        {
       
        $this->db->select("count(*) as total");
       $this->db->from("student");
       $this->db->where("status='31'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    }

/*public function countstudent()       
    {
       $this->db->select("count(*) as totals");
       $this->db->from("student");
       //$this->db->where("status='25'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result_row();
        return $rslt;
    }*/

public function studentchartlc()       
    {
    
     $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
       $this->db->select("count(*) as lc");
       $this->db->from("student");
       $this->db->where("status='23'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
        else{
          $this->db->select("count(*) as lc");
       $this->db->from("student");
       $this->db->where("status='21'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    }
    
 public function studentchartex()       
    {
    
    $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
       $this->db->select("count(*) as ex");
       $this->db->from("student");
       $this->db->where("status='24'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }else
        {
         $this->db->select("count(*) as ex");
       $this->db->from("student");
       $this->db->where("status='35'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    }       
    
  public function studentchartdb()       
    {
     $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
       $this->db->select("count(*) as db");
       $this->db->from("student");
       $this->db->where("status='26'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
        else
        {
         $this->db->select("count(*) as db");
       $this->db->from("student");
       $this->db->where("status='32'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    }  
    
    public function studentchartsh()       
    {
      $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
       $this->db->select("count(*) as sh");
       $this->db->from("student");
       $this->db->where("status='27'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
        else
        {
         $this->db->select("count(*) as sh");
       $this->db->from("student");
       $this->db->where("status='33'");
       $this->db->where("is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    } 
    
      public function studentchartact()       
    {
      $branch=$this->user_model->getbranch();
    
     if($branch=='2')
     {
         $this->db->select("count(*) as act");
       $this->db->from("student");
        $this->db->join('promotion', 'promotion.student_id = student.id');
         $this->db->join('class', 'class.class_id = promotion.class_id');
          $this->db->join('section', 'section.section_id = promotion.section_id');
       $this->db->where("student.branch='2'");
     $this->db->where("student.status='0'");
       $this->db->where("student.is_delete='0'");
        $this->db->where("class.is_delete='0'");
         $this->db->where("section.is_delete='0'");
        $this->db->where("promotion.is_active='1'");
          $this->db->where("promotion.is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
        else
        {
         $this->db->select("count(*) as act");
       $this->db->from("student");
        $this->db->join('promotion', 'promotion.student_id = student.id');
         $this->db->join('class', 'class.class_id = promotion.class_id');
          $this->db->join('section', 'section.section_id = promotion.section_id');
       $this->db->where("student.branch='1'");
     $this->db->where("student.status='0'");
       $this->db->where("student.is_delete='0'");
        $this->db->where("class.is_delete='0'");
         $this->db->where("section.is_delete='0'");
        $this->db->where("promotion.is_active='1'");
          $this->db->where("promotion.is_delete='0'");
       $rslt=$this->db->get()->result();
        return $rslt;
        }
    } 
    
public function update($data,$url)
    {
//echo "<pre>";print_r($data);exit;
           $id=$data['id'];
           $class = $data['class_admited'];
           $section = $data['section'];
           $disc_value = empty($data['disc_value'])?0:$data['disc_value'];
           $pro=array( 'class_id'=>$class,
                        'section_id'=>$section,
                         'session_id'=>$data['session']

                     );//'mark_identification' => $data['mark_of_identification']  ,
                     //'dob_words' => $data['dob_words'] , //'mother_tongue  ' => $data['mother_tongue']  ,  'pob' => $data['pob']  ,
                     //'guardian_name' => $data['guardianname'], 'guardian_cnic' => $data['guardiancnic']  ,
                     //'guardian_contact' => empty($data['guardiancontact']) ? "" : $data['guardiancontact']  , 'guardian_occupation' => empty($data['guardianoccupation']) ? "" : $data['guardianoccupation']  ,
                      // 'relation_with_guardian' => $data['relationguardian']  ,  'surname'=>$data['surname']  , 
              $fields_student_update = array(

                            'branch' => $data['branch'],
                            'grno' => empty($data['gr']) ? "" : $data['gr'] ,
                            'roll_no' => empty($data['roll_no']) ? "" : $data['roll_no'] ,
                            'student_name' => empty($data['name']) ? "" : $data['name'] ,
                            'father_name'=>$data['fathername'] ,
                            'class_admited' => empty($data['class_admited']) ? "" : $data['class_admited'],
                            'class_group' => $data['group_name'],
                            'prev_class' => empty($data['class_id']) ? "" : $data['class_id'],
                           
                            'gender' => $data['gender'] ,
                            
                            'previous_school' => $data['preschool']  ,
                          
                            'father_cnic' => $data['fathercnic']  ,
                            
                            
                            'religion' => $data['religion']  ,
                           
                           
                            'dob' => empty($data['dob']) ? "" : date('Y-m-d',strtotime($data['dob'])) ,
                            
                            'date_of_admission' => empty($data['doa']) ? "" : date('Y-m-d',strtotime($data['doa']))  ,
                            'province' => "" ,
                            'taluka' => "" ,
                            'district' => "",
                            'father_occupation' => empty($data['fatheroccupation']) ? "" : $data['fatheroccupation']  ,
                           
                            'income_family' => empty($data['familyincome']) ? "" : $data['familyincome']  ,
                             'img' => $url  ,
                            'perment_address' => empty($data['permentaddess']) ? "" : $data['permentaddess']  ,
                            'postal_address' => "" ,
                            'father_contact' => empty($data['fathercontact']) ? "" : $data['fathercontact']  ,
                            'student_contact' => empty($data['student_contact']) ? "" : $data['student_contact']  ,
                            
                            'disc_type' => empty($data['disc_type']) ? "" : $data['disc_type']  ,
                            'disc_value' => empty($disc_value) ? "" : $disc_value,
                            'status' => "0"
                    
                );
              
            
            
            $this->db->where('id',$id);
            $this->db->update('student',$fields_student_update);
            $this->db->where('student_id',$id);
            $this->db->where('is_active',1);
            $this->db->update('promotion',$pro);
            return true;
                 
    }

    public function show()
    {
                                     $this->db->where('is_delete',0) ;
                       $teacher_view=$this->db->get('teacher')->result();
                       return $teacher_view;
    } 

    public function status_update($data)
    {
        
         
          
         $student=$data['student'];
         $status=$data['status'];
         $dol=$data['dol'];
         $rol=$data['rol'];
            $fields = array(
                            'dol' =>  $data['dol'],
                            'rol' =>  $data['rol'],
                            'status' =>  $data['status'],
                );
                
          foreach ($student as $key => $selected_student) {
          	$this->db->where('id',$selected_student);
                $this->db->update('student',$fields);
                
          }
           return true;
    }
    
    public function rollback($id){

                                $field = array(
                                        'status'=>'0'
                                    );
                    $this->db->where('id',$id);
             return $this->db->update('student',$field);
                
    }
    public function student_infor($id){

          return $this->db->query("SELECT *  FROM `student` inner join `promotion` on promotion.student_id=student.id inner join `class` on promotion.class_id=class.class_id inner join `section` on promotion.section_id=section.section_id  where student.id= $id ")->result_array();
    }
    public function getbranch()
    {
        $data=$this->db->where('is_delete',0)->get('branch');
        return $data;
    }
    public function all()
    {
        $this->db->select('student.id,student.student_name,student.roll_no,student.grno,class.class_name,section.section_name');    
        $this->db->from('student');
        $this->db->join('promotion', 'promotion.student_id=student.id');
        $this->db->join('class', 'promotion.class_id=class.class_id');
        $this->db->join('section', 'section.section_id=promotion.section_id')->where('student.status',0);
        $query = $this->db->get()->result_array();
        return $query;
    }
    public function studentatt_save($data)
    {
        $this->db->insert('studentatt',$data);
         return true;
    }
   public function studentatt_edit($value=0)
    {
            $this->db->select('s.student_name,studentatt.status_id,s.grno,s.roll_no,class.class_name,section.section_name,studentatt.date,studentatt.id,attendancestatus.status');    
            $this->db->from('studentatt');
            $this->db->join('student s', 'studentatt.student_id = s.id');
            $this->db->join('promotion', 'promotion.student_id=s.id');
            $this->db->join('class', 'promotion.class_id = class.class_id');
            $this->db->join('section', 'promotion.section_id = section.section_id');
            $this->db->join('attendancestatus', 'attendancestatus.id = studentatt.status_id');
            $this->db->where('is_deleted',0 )->where('studentatt.id',$value);
            $query = $this->db->get();
            return $query;
    }
    public function studentatt_del($value=0)
    {
        $data=array('is_deleted' => 1 );
        $this->db->where('id', $value);    
        $this->db->update('studentatt', $data);
        return true;
    }
    public function studentattupdate($data,$id)
    {
        $this->db->where('id',$id);
        $this->db->update('studentatt',$data);
        return true;
    }
     public function id($value=0)
    {
         $this->db->select('*,student.id as stid');    
        $this->db->from('student');
        $this->db->join('promotion', 'promotion.student_id=student.id');
        $this->db->join('class', 'promotion.class_id=class.class_id');
        $this->db->join('branch', 'branch.id=class.branch');
        $this->db->join('section', 'section.section_id=promotion.section_id')->where('student.id',$value)->where('promotion.is_active','1');
        $query = $this->db->get()->result_array()[0];
        return $query;
    }
    
    
     public function get_student_roll_no($class_admited=0,$section_id=0)
    {
         $this->db->select('student.roll_no');    
        $this->db->from('student');
        $this->db->join('promotion', 'promotion.student_id=student.id');
        $this->db->join('class', 'promotion.class_id=class.class_id');
        $this->db->join('branch', 'branch.id=class.branch');
        $this->db->join('section', 'section.section_id=promotion.section_id')->where('student.is_delete',0)->where('student.class_admited',$class_admited)->where('promotion.section_id',$section_id)->where('promotion.is_active','1');
        $this->db->order_by("student.roll_no", "desc");
        $query = $this->db->get()->row();
        return $query;
    }


}

