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

class Fees_management extends Admin_Core_Controller {

    public function __construct()
    {
        parent::__construct();
        $this->load->model('setting_model', 'setting');
        $this->load->model('user_model', 'user');
    }

    // Lead Start

    public function fees_instalments($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['student'] = $this->user->get_active_student_by_atp_id_and_student_id($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/fees_instalments');
        $this->load->view('admin/include/footer');
    }

    function generate_table()
    {
        $due_ammount = $this->input->post('due_ammount');
        $no_instalment = $this->input->post('no_instalment');
        $no_days = $this->input->post('no_days');
        $start_date = $this->input->post('start_date');

        $installment = $due_ammount / $no_instalment;
        $table = '';
        for ($i = 0; $i < $no_instalment; ++$i) 
        {
            $repeat = strtotime("+".$no_days." days",strtotime($start_date));
            $start_date = date('Y-m-d',$repeat);
    

            $table .= '<tr>
                    <td><input type="date" class="form-control" name="instalment_date[]" readonly value="'.$start_date.'"></td>
                    <td><input class="form-control" name="instalment_amount[]" readonly value="'.round($installment).'"></td>
                    <td><span class="badge badge-warning">Pending</span></td>
                </tr>';
        }

        echo json_encode($table);
    }

    function get_instalment_table_by_user_id($course_id, $student_id)
    {
        $this->db->where('instalment_student_id', $student_id);
        $this->db->where('instalment_course_id', $course_id);
        $this->db->where('is_down_payment', 0);
        $all_data = $this->db->get('student_payment_instalment')->result();
        $table = '';
        foreach ($all_data as $key => $data)
        {
            if($data->instalment_status == 0)
            {
                $status = '<span class="badge badge-warning">Pending</span>';
            }  
            else
            {
                $status = '<span class="badge badge-success">Complete</span>';
            }         

            $table .= '<tr>
                    <td><input type="date" class="form-control" name="instalment_date[]" readonly value="'.$data->instalment_date.'"></td>
                    <td><input class="form-control" name="instalment_amount[]" readonly value="'.$data->instalment_amount.'"></td>
                    <td>'.$status.'</td>
                </tr>';
        }

        echo json_encode($table);
    }

    function add_new_instalment()
    {
        if ($this->user->add_new_instalment()) 
        {
            $this->session->set_userdata('success', "Instalment Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Instalment Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function fees_receipts()
    {
        $atp_id =  user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_students'] = $this->user->get_all_admintted_student($atp_id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/fees_receipts');
        $this->load->view('admin/include/footer');
    }

    public function recive_fee($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['student'] = $this->user->get_active_student_by_atp_id_and_student_id($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/recive_fee');
        $this->load->view('admin/include/footer');
    }

    public function update_instalment_amount()
    {
        if ($this->user->update_instalment_amount()) 
        {
            $this->session->set_userdata('success', "Instalment Amount Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Instalment Amount Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function print_receipt($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->user->get_student_all_course_by_student_id($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/print_receipt');
        $this->load->view('admin/include/footer');
    }

    public function fees_adjustment($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['student'] = $this->user->get_active_student_by_atp_id_and_student_id($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/fees_adjustment');
        $this->load->view('admin/include/footer');
    }

    function add_new_adjustment()
    {
        if ($this->user->add_new_adjustment()) 
        {
            $this->session->set_userdata('success', "Instalment Successfully Added");
            redirect($this->agent->referrer());
        } else {
            $this->session->set_userdata('error', " Instalment Added Failed");
            redirect($this->agent->referrer());
        }
    }

    public function refund_fees($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['student'] = $this->user->get_active_student_by_atp_id_and_student_id($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/refund_fees');
        $this->load->view('admin/include/footer');
    }

    function update_refund()
    {
        if ($this->user->update_refund()) 
        {
            $this->session->set_userdata('success', "Refund Successfully Send");
            redirect(base_url().'admin/fees_management/fees_receipts');
        } else {
            $this->session->set_userdata('error', " Refund Sending Failed");
            redirect($this->agent->referrer());
        }
    }

    public function print_refund_receipt($id)
    {
        $atp_id = user_id();
        $data['app_setting'] = $this->setting_model->get_all_app_setting();
        $data['all_course'] = $this->user->get_student_all_course_by_student_id_for_refund($atp_id, $id);
        $this->load->view('admin/include/header', $data);
        $this->load->view('admin/fees_management/print_refund_receipt');
        $this->load->view('admin/include/footer');
    }


}
