<?php

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

class Programs_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        
        $this->load->model('business_model');
        $this->load->model('programs_model');
        $this->load->model('customer_model');
        $this->load->model('staff_model');
        $this->load->model('tools_model');
        $this->load->model('accounting_model');
        $this->load->model('service_model');
        
        if ($this->session->userdata('role') === 'Reception' || $this->session->userdata('role') === 'Program Admin' || $this->session->userdata('role') === 'Program Reception' || $this->session->userdata('role') === 'Admin' || $this->session->userdata('role') === 'Super User' || $this->session->userdata('role') === 'Accountant'|| $this->session->userdata('role') === 'Staff Schedule' || $this->session->userdata('role') === 'Trainer') {


        } else {
            redirect('logout');
        }
    }

    
    public function programs($program_type){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
        
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['program_type']= $this->programs_model->get_program_type($program_type);
           $data['programs']= $this->programs_model->get_programs_by_type($program_type);

           $this->load->view('includes/header', $data);
           $this->load->view('programs/programs');
           $this->load->view('includes/footer');
        }
        
    }
    
    public function programadd($program_type){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program_type']= $this->programs_model->get_program_type($program_type);


            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_add');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function insert_program(){
        
        $result = $this->programs_model->insert_program();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Program Added!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programs/'.$this->input->post('program_type_id'));
        
    }
    
    
    public function programedit($programid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program']= $this->programs_model->get_program_by_id($programid);
            $data['program_types']= $this->programs_model->get_program_types(); 

            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_program(){
        
        $result = $this->programs_model->update_program();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Program Updated!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programedit/'.$this->input->post('id_programs'));
        
    }

    public function programsession($programid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['programsessions']= $this->programs_model->get_program_sessions($programid);


            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_sessions');
            $this->load->view('includes/footer');
        }
        
    }
    
     public function programsessionadd($programid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program']= $this->programs_model->get_program_by_id($programid);

            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_session_add');
            $this->load->view('includes/footer');
        }
    }
    
    public function insert_program_session(){
        
        $result = $this->programs_model->insert_program_session();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Program Session Added!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsession/'.$this->input->post('program_id'));
        
    }
    
    public function programsessionedit($programsessionid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program']= $this->programs_model->get_program_session_id($programsessionid);

            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_session_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_program_session(){
        
        $result = $this->programs_model->update_program_session();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Program Updated!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsessionedit/'.$this->input->post('id_program_sessions'));
        
    }
    
    public function get_active_programs() {

        
        if(null == $this->input->post('programtypeid')){
            $programtypeid=0;
        } else {
            $programtypeid=$this->input->post('programtypeid');
        }
        
        $data = $this->programs_model->get_active_programs($programtypeid);
        echo(json_encode($data));
    }
    
    public function get_active_sessions() {
       
        $data = $this->programs_model->get_active_sessions($this->input->post('programid'));
        echo(json_encode($data));
        
    }
    
    public function print_enrollment($enrollment_id){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $data['nav'] = 'Programs';
            $data['subnav'] = 'ProgramMembers';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program_enrollment'] = $this->programs_model->get_enrollment_payments($enrollment_id);
            
           
           if($data['program_enrollment'][0]['id_program_types']==3){
               $data['program_schedule']= $this->programs_model->get_program_session_services($data['program_enrollment'][0]['id_program_sessions']);
               
            }else{
                $data['program_schedule']= $this->programs_model->get_program_session_classes($data['program_enrollment'][0]['id_program_sessions']);
            }
            
            $this->load->view('includes/header', $data);
            $this->load->view('programs/print_enrollment_view');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function print_invoice($enrollment_id){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $data['nav'] = 'Programs';
            $data['subnav'] = 'ProgramMembers';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program_enrollment'] = $this->programs_model->get_enrollment_payments($enrollment_id);
            $data['program_schedule']= $this->programs_model->get_program_session_classes($data['program_enrollment'][0]['id_program_sessions']);

            $this->load->view('includes/header', $data);
            $this->load->view('programs/print_invoice_view');
            $this->load->view('includes/footer');
        }
        
    }
    
    
    public function print_due_invoice($enrollment_id){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $data['nav'] = 'Programs';
            $data['subnav'] = 'ProgramMembers';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['program_enrollment'] = $this->programs_model->get_enrollment_payments($enrollment_id);
            $data['program_schedule']= $this->programs_model->get_program_session_classes($data['program_enrollment'][0]['id_program_sessions']);

            $this->load->view('includes/header', $data);
            $this->load->view('programs/print_due_invoice');
            $this->load->view('includes/footer');
        }
    }
    
    public function add_payment(){
       
        $payment=$this->input->post('txtTotalPayment');
        $enrollment_id=$this->input->post('txtenrollmentid');
        
        //echo $enrollment_id.' '.$payment; exit();
        $payment_id = $this->programs_model->addpayment();
        
        
        if($payment_id >=1){
            $this->session->set_flashdata('Success', 'Payment Added!');
        } else {
            $this->session->set_flashdata('Error', 'Could Not Insert New Payment! Check your input please.');
        }
        
        redirect('print_program_invoice/'.$enrollment_id);
        
    }
   
    public function add_invoice_payment(){
        
        $payment=$this->input->post('payingnow');
        $enrollment_id=$this->input->post('enrollment_id');
        
        //echo $enrollment_id.' '.$payment; exit();
        $payment_id = $this->programs_model->add_invoice_payment();
        
        
        if($payment_id >=1){
            $this->session->set_flashdata('Success', 'Payment Added!');
        } else {
            $this->session->set_flashdata('Error', 'Could Not Insert New Payment! Check your input please.');
        }
        
        redirect('print_program_invoice/'.$enrollment_id);
    }
    
    
    public function program_members($program_type=null){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            if($program_type==null){$this->input->post('programtypes');}

            $data['nav'] = 'Programs';
            $data['subnav'] = 'ProgramMembers';

            $data['business'] = $this->business_model->getbusinessdetails();

            if(null !== $this->input->post('programtypes')){$program_type=$this->input->post('programtypes');}
            if(null !== $this->input->post('activeprograms')){$data['selected_program']=$this->input->post('activeprograms');}
            if(null !==$this->input->post('activeprogramsessions')){$data['selected_session']=$this->input->post('activeprogramsessions');}

            $data['program_types'] = $this->programs_model->get_program_types();
            $data['program_type']=$program_type;
            $data['program_members'] = $this->programs_model->get_program_members($program_type);

            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_members_view');
            $this->load->view('includes/footer');       
        }
        
    }
    
    public function program_invoices($id_program_enrollment){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            
            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';
            
            $data['invoices']=$this->programs_model->get_enrollment_payments($id_program_enrollment);
            $data['id_program_enrollment']=$id_program_enrollment;
            
            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_invoices');
            $this->load->view('includes/footer');
        }
       
    }
    
    public function program_invoice_add($id_program_enrollment){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            
            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';
            
            $data['business'] = $this->business_model->getbusinessdetails();
            $data['details'] = $this->programs_model->get_enrollment_details($id_program_enrollment);
            $data['invoices']=$this->programs_model->get_enrollment_payments($id_program_enrollment);
           
            
            $data['months'] = $this->tools_model->get_months();
            
            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_invoice_add');
            $this->load->view('includes/footer');
        }
       
    }
    
    public function program_invoice_cancel(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            
            $invoice_id = $this->input->post('invoiceid');
            $enrollment_id = $this->input->post('enrollmentid');
            
            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';
            
            $this->programs_model->cancel_invoice($invoice_id);
            
            redirect('program_invoices/'.$enrollment_id);
        }
       
    }
    
    public function programsessionclasses($programsessionid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
            //checkroles(0, 0, 1)

            $data['nav'] = 'Programs';
            $data['subnav'] = 'TrainingPrograms';

            $data['business'] = $this->business_model->getbusinessdetails();
            $data['programsession']= $this->programs_model->get_program_session_id($programsessionid);
            
            if(sizeof($data['programsession'])>0){
                if($data['programsession'][0]['program_type_id']==3){
                    $data['programsessionclasses']= $this->programs_model->get_program_session_services($programsessionid);
                } else {
                    $data['programsessionclasses']= $this->programs_model->get_program_session_classes($programsessionid);
                }
            }else{
                $data['programsessionclasses']= $this->programs_model->get_program_session_services($programsessionid);
            }


            $this->load->view('includes/header', $data);
            $this->load->view('programs/program_session_classes');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function programsessionclassadd($programsessionid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['instructors'] = $this->staff_model->get_active_staff();
           $data['programsession']= $this->programs_model->get_program_session_id($programsessionid);
           $data['program_name']= $this->input->post('program_name');
           
           $this->load->view('includes/header', $data);
           $this->load->view('programs/program_session_class_add');
           $this->load->view('includes/footer');
        }
        
        
    }
    
    public function insert_program_session_class(){
        
        $result = $this->programs_model->insert_program_session_class();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Class Added!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsessionclasses/'.$this->input->post('program_session_id'));
        
    }
    
    
     public function programsessionserviceadd($programsessionid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['services'] = $this->programs_model->get_services();
           $data['programsession']= $this->programs_model->get_program_session_id($programsessionid);
           $data['program_name']= $this->input->post('program_name');
           
           $this->load->view('includes/header', $data);
           $this->load->view('programs/program_session_service_add');
           $this->load->view('includes/footer');
        }
        
        
    }
    
    public function insert_program_session_service(){
        
        
        $result = $this->programs_model->insert_program_session_service();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Service Added!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsessionclasses/'.$this->input->post('program_session_id'));
        
    }
    
    ///edit classes
    public function programsessionclassedit($classid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['instructors'] = $this->staff_model->get_active_staff();
           $data['programsessionclass']= $this->programs_model->get_program_session_class($classid);
           $data['programname']= $this->input->post('program_name');
           
           $this->load->view('includes/header', $data);
           $this->load->view('programs/program_session_class_edit');
           $this->load->view('includes/footer');
        }
        
        
    }
    
    public function update_program_session_class(){
       
        $result = $this->programs_model->update_program_session_class();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Class Updated!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsessionclasses/'.$this->input->post('program_session_id'));
        
    }
    
    
     public function programsessionserviceedit($serviceid){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['services'] = $this->programs_model->get_services();
           $data['programsessionservice']= $this->programs_model->get_program_session_service($serviceid);
           $data['programname']= $this->input->post('program_name');
           
           $this->load->view('includes/header', $data);
           $this->load->view('programs/program_session_service_edit');
           $this->load->view('includes/footer');
        }
        
        
    }
    
    public function update_program_session_service(){
        
        
        $result = $this->programs_model->update_program_session_service();
            
        if($result>0){
            $this->session->set_flashdata('Success', 'Service Updated!');
        } else {
            $this->session->set_flashdata('Error', 'Something went wrong!');
        }
        
       redirect('programsessionclasses/'.$this->input->post('program_session_id'));
        
    }
    
    public function program_service_provisioning($enrollment_id){
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            //storemanager,hr,reception...user serial
           //checkroles(0, 0, 1)

           $data['nav'] = 'Programs';
           $data['subnav'] = 'TrainingPrograms';

           $data['business'] = $this->business_model->getbusinessdetails();
           $data['staff'] = $this->programs_model->staff_list();
           $data['enrollmentservices']= $this->programs_model->get_enrollment_services($enrollment_id);
           $serviceids='';
          
          
           $data['programsessionpayments']= $this->programs_model->get_enrollment_payments($enrollment_id);
           $serviceid='';
           
           foreach($data['enrollmentservices'] as $service){
               $serviceid.=$service['id_business_services'].',';               
           }
           
           $serviceid = substr($serviceid, 0, strlen($serviceid)-1); 
           $data['enrollment_id'] = $enrollment_id;
           
           $data['availed']= $this->programs_model->get_availed_services($data['enrollmentservices'][0]['id_customers'], $serviceid, date('Y'));
           $data['services_booked']= $this->programs_model->services_booked($data['enrollmentservices'][0]['customer_id'], $serviceid);
            
           
           
           $data['programname']= $this->input->post('program_name');
           $data['months'] = $this->tools_model->get_months();
           
           
           $this->load->view('includes/header', $data);
           $this->load->view('programs/program_service_provisioning');
           $this->load->view('includes/footer');
        }
        
    }
    
    public function create_visits(){
        
               
        $visitid = $this->programs_model->create_visit();
        
        if($visitid>0){
            redirect('scheduler');
        }else{
            redirect('program_service_provisioning/'.$this->input->post('enrollment_id'));
        }
        
        
    }
}
