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

class StudentFee extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('text');
$this->load->helper(array('form','url'));
date_default_timezone_set('Asia/Kolkata');
$this->load->model('FeeModel');
$this->load->helper('date');
$this->load->database();    
$this->load->library('form_validation');
$this->load->helper(array('form', 'url'));
$this->load->library('session');
 $this->load->library('Pdf');
}

public function index(){
// $data['mdg']='';
// if(isset($_POST['login'])){ 
// $username=$this->input->post('username');
// $password=$this->input->post('password');
// if($username=='fee' && $password="mtmhss@95"){
// $this->session->set_userdata('fee',1);
// redirect(base_url().'KoylaNagarFee/dashboard');
// }else{
// $data['msg']="Error in username/password";
// }	
// }  
// $this->load->view('entab/feemanagement/login');
// $this->load->view('entab/feemanagement/dashboard');
// echo "Hello"; 

$this->load->view('erp/header');
// $this->load->view('erp/staff-nav');
$this->load->view('erp/new/sidebar');
$this->load->view('erp/fee-admin/all-invoices');
$this->load->view('erp/footer');


}

public function dashboard(){
if($this->session->userdata('fee_permission')==1){

}else{
redirect(base_url().'StudentFee/');    
}       

$this->load->view('erp/header');
// $this->load->view('erp/staff-nav');
$this->load->view('erp/new/sidebar');
$this->load->view('erp/fee-admin/dashboard');
$this->load->view('erp/fee-admin/all-student');
$this->load->view('erp/footer');   

// $this->load->view('entab/feemanagement/user/header');
// $this->load->view('entab/nav');
// $this->load->view('entab/feemanagement/user/dashboard');
// $this->load->view('entab/feemanagement/user/all-student');
// $this->load->view('entab/feemanagement/user/footer');    
}

public function fee_collection(){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }         
$this->load->view('entab/feemanagement/user/header');
$this->load->view('entab/nav');
$this->load->view('entab/feemanagement/user/fee-collection');
$this->load->view('entab/feemanagement/user/footer');       
}

public function fee_structure(){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
$this->load->view('erp/header');
// $this->load->view('erp/staff-nav');
$this->load->view('erp/new/sidebar');
$this->load->view('erp/fee-admin/fee-structure');
$this->load->view('erp/footer');    
}
public function get_student(){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
$sch_no = $_POST['sch_no'];
$mob_no = $_POST['mob_no'];
$table = $_POST['table'];
$student_data='';
if($mob_no !==''){
$field="contact_no";    
$student_data=$this->FeeModel->student_search($field,$mob_no);
$date = date('y-m-d');
$count =0;
echo "<div class='table-responsive'>
<table class='table table-bordered  table-stripped' id='table'>
<thead><tr><th>Sr. No.</th><th>Name</th><th>Sch. No.</th><th>Class</th><th>Section</th><th>Father Name</th><th>Contact No.</th><th>Student Profile</th><th>Edit Info.</th>
<th>Pay Fee</th>
</tr></thead><tbody>";

foreach ($student_data as $row) {	
$sid =$row['sch_no'];     
$count++;
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['student_name']."</td>";
echo "<td>".$row['sch_no']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['section']."</td>";
echo "<td>".$row['father_name']."</td>";
echo "<td>".$row['contact_no']."</td>";
echo "<td><a href='".base_url()."StudentFee/profile/".$row['id']."'><button class='btn btn-warning'>Profile</button></a></td>";

echo '<td>
<form style="text-align: right;" id="myForm" action="'.base_url().'Student/add_new_student/'.$row['id'].'" method="post">
<button class="btn btn-danger edit" name="edit" value="'.$row['id'].'" type="submit">Edit</button></form>
</td>';	



echo "<td><a class='' href='".base_url()."StudentFee/fee/".$row['id']."'><button class='btn btn-success'>Fee</button></a></td>";

echo "</tr>";
}
echo "</tbody></table></div>";



}else if($sch_no !==''){
$field="sch_no";    

$student_data=$this->FeeModel->student_search($field,$sch_no);

$date = date('y-m-d');

$count =0;

echo "<div class='table-responsive'><table id='table' class='table table-bordered  table-stripped'><thead>
<tr><th>Sr. No.</th><th>Name</th><th>Sch. No.</th><th>Class</th><th>Section</th><th>Father Name</th><th>Contact No.</th><th>Student Profile</th><th>Edit Info.</th><th>Pay Fee</th></tr></thead><tbody>";

foreach ($student_data as $row) {	
$sid =$row['sch_no'];     
$count++;
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['student_name']."</td>";
echo "<td>".$row['sch_no']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['section']."</td>";
echo "<td>".$row['father_name']."</td>";
echo "<td>".$row['contact_no']."</td>";
echo "<td><a href='".base_url()."StudentFee/profile/".$row['id']."'><button class='btn btn-warning'>Profile</button></a></td>";

echo '<td>
<form style="text-align: right;" id="myForm" action="'.base_url().'Student/add_new_student/'.$row['id'].'" method="post">
<button class="btn btn-danger edit" name="edit" value="'.$row['id'].'" type="submit">Edit</button></form>
</td>';	



echo "<td><a class='' href='".base_url()."StudentFee/fee/".$row['id']."'><button class='btn btn-success'>Fee</button></a></td>";

echo "</tr>";
}
echo "</tbody></table></div>";
}else{
echo "Enter Student Credentials";

}


}


public function get_student_by_class(){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
$student_class = $_POST['student_class'];
$table = $_POST['table'];
$student_data='';
$field="class";    
$student_data=$this->FeeModel->student_search($field,$student_class);
$date = date('y-m-d');
$count =0;
echo "<div class='table-responsive'><table id='table' class='table table-bordered  table-stripped'>";
echo "<thead><th>Sr. No.</th><th>Name</th><th>Sch. No.</th><th>Class</th><th>Section</th><th>Father Name</th><th>Contact No</th><th>Student Profile</th><th>Edit Info.</th><th>Pay Fee</th></thead><tbody>";

foreach ($student_data as $row) {	
$sid =$row['sch_no'];     
$count++;
echo "<tr>";
echo "<td>".$row['id']."</td>";
echo "<td>".$row['student_name']."</td>";
echo "<td>".$row['sch_no']."</td>";
echo "<td>".$row['class']."</td>";
echo "<td>".$row['section']."</td>";
echo "<td>".$row['father_name']."</td>";
echo "<td>".$row['contact_no']."</td>";
echo '<td><a class="profile-button btn btn-warning" href="'.base_url().'StudentFee/profile/'.$row['id'].'">Profile</a></td>';
echo '<td><a class="fee-button btn btn-danger" href="'.base_url().'StudentFee/fee/'.$row["id"].'">Fee</a></td>';
echo '<td><form style="text-align: right;" id="myForm" action="'.base_url().'Student/add_new_student/'.$row['id'].'" method="post">
<button class="btn btn-success edit" name="edit" value="'.$row['id'].'" type="submit">Edit </button></form></td>';	

echo "</tr>";
}
echo "</tbody></table></div>";
   
    
}
public function fee($id){
// $this->load->view('erp/feemanagement/user/header');
// $this->load->view('entab/nav');
// $this->load->view('entab/feemanagement/user/fee',$id);
// $this->load->view('entab/feemanagement/user/footer');     


$this->load->view('erp/header');
// $this->load->view('erp/staff-nav');
$this->load->view('erp/new/sidebar');
// $this->load->view('erp/welcome/teaching-staff-welcome');
$this->load->view('erp/fee-admin/fee',$id);
$this->load->view('erp/footer');   

}
public function add_fee(){
if($this->session->userdata('fee_permission')!=1){
redirect(base_url().'welcome');
}     
$month = date('m');    
$day = date('d'); 
$year = date('y'); 
$sid = $_POST['sid'];
$schno = $_POST['schno'];
$total = $_POST['total'];
$fee_type = $_POST['fee_type'];
$x = $_POST['x'];
$y1 = $_POST['y1'];
$y2 = $_POST['y2'];
$table = $_POST['table'];
$total_months_no = $_POST['total_months_no'];
$field ="id";
$fee_date =$_POST['fee_date'];

$fee_note =$_POST['fee_note'];
$fee_mode =$_POST['fee_mode'];
$transaction_info =$_POST['transaction_info'];
$bill_book_no =$_POST['bill_book_no'];
$student_data =$this->FeeModel->student_search($field,$sid);

if($student_data){
foreach($student_data as $s_data){
    $student_class=$s_data['class'];
}    
}

$discount_amount = $_POST['discount_amount'];
$est_amount  =$_POST['est_amount'];
$status_1 = $_POST['status_1'];
$status_2 = $_POST['status_2'];
$status_3 = $_POST['status_3'];
$status_4 = $_POST['status_4'];
$status_5 = $_POST['status_5'];
$status_6 = $_POST['status_6'];
$status_7 = $_POST['status_7'];
$status_8 = $_POST['status_8'];
$status_9 = $_POST['status_9'];
$status_10 = $_POST['status_10'];
$status_11 = $_POST['status_11'];
$status_12 = $_POST['status_12'];
$cat= date('y-m-d');    
$cat_time= date('y-m-d H:i:s');
$data_array=array("sid"=>$sid,"sch_no"=>$schno,"total_amount"=>$total,"fee_type"=>$fee_type,"months"=>$x,"y1"=>$y1,"y2"=>$y2,"cat"=>$cat,"cat_time"=>$cat_time,"no_of_months"=>$total_months_no,"discount"=>$discount_amount,"estimate_amount"=>$est_amount,"january"=>$status_1,"february"=>$status_2,"march"=>$status_3,"april"=>$status_4,"may"=>$status_5,"june"=>$status_6,"july"=>$status_7,"august"=>$status_8,"september"=>$status_9,"october"=>$status_10,"november"=>$status_11,"december"=>$status_12,"month"=>$month,"day"=>$day,"year"=>$year,"student_class"=>$student_class,"fee_date"=>$fee_date,"txn_no"=>$transaction_info,"payment_mode"=>$fee_mode,"fee_note"=>$fee_note,"bill_book_no"=>$bill_book_no);


$check_fee_entry=$this->FeeModel->check_fee_entry($sid,$schno,$status_1,$status_2,$status_3,$status_4,$status_5,$status_6,$status_7,$status_8,$status_9,$status_10,$status_11,$status_12,$total);
if($check_fee_entry>0){
echo "Already Submitted";    
}
else{
$add_enquiry = $this->FeeModel->add_fee($data_array,$table);
}
if($add_enquiry){
echo "Your Fee Submitted Succesfully";    
}

}


public function getallfeeinvoices(){
if($this->session->userdata('fee_permission')!=1){
redirect(base_url().'welcome/');
}     
$sid=$_POST['sid_url'];   
$data=$this->FeeModel->get_fee_invoices($sid);
foreach ($data as $row){	
echo "<tr>";
echo "<td>".$row['sid']."</td>";
echo "<td>".$row['sch_no']."</td>";
echo "<td>".$row['total_amount']."</td>";
echo "<td>".$row['estimate_amount']."</td>";
echo "<td>".$row['discount']."</td>";
echo "<td style='text-transform:uppercase;'>".$row['months']."</td>";
echo "<td>".$row['no_of_months']."</td>";
echo "<td>".$row['fee_type']."</td>";
echo "<td>".date("d-m-Y", strtotime($row['fee_date']))."</td>";
echo "<td><a class='btn btn-info' href='".base_url()."StudentFee/feeInvoice/".$row['fee_no']."'>Print</a></td>";
echo "</tr>";
}
}

public function getmonths(){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
$sid=$_POST['sid_url'];   
$data=$this->FeeModel->get_fee_invoices($sid);
$field='id';
$student_class=$this->FeeModel->student_search($field,$sid);
foreach($student_class as $scla){
$class_no =$scla['class']; 
}
$april =0;$may =0;$june=0;$july=0;$august=0;$september=0;$october=0;$november=0;$december=0;$january=0;$february=0;$march=0;

foreach ($data as $row) {
if($row['april'] ==1){
$april =1;    
}
if($row['may'] ==1){
$may =1;
}
if($row['june'] ==1){
$june =1;
}
if($row['july'] ==1){
$july =1;
}
if($row['august'] ==1){
$august =1;
}
if($row['september'] ==1){
$september =1;
}
if($row['october'] ==1){
$october =1;
}
if($row['november'] ==1){
$november =1;
}
if($row['december'] ==1){
$december =1;
}
if($row['january'] ==1){
$january =1;
}
if($row['february'] ==1){
$february =1;
}
if($row['march'] ==1){
$march =1;
}

}

$table11="school_fee_structure";
$fetch_fee_structure = $this->FeeModel->fetch_class_fee($table11,$class_no);
// if($fetch_fee_structure){

foreach ($fetch_fee_structure as $row1) {	

if($april ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Apr
<input type='checkbox' name='4'  value=".$row1['april']." class='fee' id='4' month='april'>
<span class='checkmark'></span>
</label>
<span><input type='number' value=".$row1['april']." class='4' id='4' name='april' disabled ></span>
</div> 
 ";    
}
if($may ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>May
<input type='checkbox' name='5'  value=".$row1['may']." class='fee' id='5' month='may'>
<span class='checkmark'></span>
<span value=".$row1['may']." class='5' id='may'></span>
</label>
</div> 
 ";     
}
if($june ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Jun
<input type='checkbox' name='6'  value=".$row1['june']." class='fee' id='6' month='june'>
<span class='checkmark'></span>
<span value=".$row1['june']." class='6' id='june'></span>
</label>
</div> 
 ";     
}
if($july ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Jul
<input type='checkbox' name='7'  value=".$row1['july']." class='fee' id='7' month='july'>
<span class='checkmark'></span>
<span value=".$row1['july']." class='7' id='july'></span>
</label>
</div> 
 ";     
}
if($august ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Aug
<input type='checkbox' name='8'  value=".$row1['august']." class='fee' id='8' month='august'>
<span class='checkmark'></span>
<span value=".$row1['august']." class='8' id='august'></span>
</label>
</div> 
 ";     
}
if($september ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Sep
<input type='checkbox' name='9'  value=".$row1['september']." class='fee' id='9' month='september'>
<span class='checkmark'></span>
<span value=".$row1['september']." class='9' id='september'></span>
</label>
</div> 
 ";     
}
if($october ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Oct
<input type='checkbox' name='10'  value=".$row1['october']." class='fee' id='10' month='october'>
<span class='checkmark'></span>
<span value=".$row1['october']." class='10' id='october'></span>
</label>
</div> 
 ";     
}
if($november ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Nov
<input type='checkbox' name='11'  value=".$row1['november']." class='fee' id='11' month='november'>
<span class='checkmark'></span>
<span value=".$row1['november']." class='11' id='november'></span>
</label>
</div> 
 ";     
}
if($december ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Dec
<input type='checkbox' name='12'  value=".$row1['december']." class='fee' id='12' month='december'>
<span class='checkmark'></span>
<span value=".$row1['december']." class='12' id='december'></span>
</label>
</div> 
 ";     
}
if($january ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Jan
<input type='checkbox' name='1'  value=".$row1['january']." class='fee' id='1' month='january'>
<span class='checkmark'></span>
<span value=".$row1['january']." class='1' id='january'></span>
</label>
</div> 
 ";     
}
if($february ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Feb
<input type='checkbox' name='2'  value=".$row1['february']." class='fee' id='2' month='february'>
<span class='checkmark'></span>
<span value=".$row1['february']." class='2' id='february'></span>
</label>
</div> 
 ";     
}
if($march ==0){
 echo "
 <div class='month-item'>
<label class='form-group-1'>Mar
<input type='checkbox' name='3'  value=".$row1['march']." class='fee' id='3' month='march'>
<span class='checkmark'></span>
<span value=".$row1['march']." class='3' id='march'></span>
</label>
</div> 
 ";     
}
    
 
}
}


public function feeInvoice($fee_no){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
if($this->uri->segment(3))
{
$fee_no = $this->uri->segment(3);}
  
$old_limit = ini_set("memory_limit", "16M");
$dompdf = new Pdf();
        
$dompdf->set_paper('letter', 'portrait'); //landscape , portrait
$html_content = '';        
$html_content .= $this->FeeModel->fetch_single_fee_details($fee_no);
$this->pdf->loadHtml($html_content);
$this->pdf->render();
$this->pdf->stream("".$fee_no.".pdf", array("Attachment"=>0));
}
public function logout()
{
$this->session->sess_destroy();
redirect(base_url());
}


public function profile($sid){
// $this->load->view('entab/feemanagement/user/header');
// $this->load->view('entab/nav');
// // $this->load->view('entab/feemanagement/user/dashboard');
// $this->load->view('entab/feemanagement/user/single-student');
// $this->load->view('entab/feemanagement/user/footer');   



$this->load->view('erp/header');
// $this->load->view('erp/staff-nav');    
$this->load->view('erp/new/sidebar');
$this->load->view('erp/fee-admin/single-student');
$this->load->view('erp/footer');
}


public function update_profile($sid){
$this->load->view('erp/header');
$this->load->view('erp/new/sidebar');    
// $this->load->view('entab/feemanagement/user/header');
// $this->load->view('entab/nav');
// $this->load->view('entab/feemanagement/user/dashboard');
// $this->load->view('entab/feemanagement/user/edit-profile');
$this->load->view('entab/feemanagement/add-student',$data);
// $this->load->view('entab/feemanagement/user/footer');   
$this->load->view('erp/footer');
}

public function get_date_fee_collection(){
$fee_date = $_POST['fee_date'];
$count=0;
$data=$this->FeeModel->get_date_fee_invoices($fee_date);
echo "<div class='table-responsive'><table class='table table-bordered table-striped'>
<thead><th>Count</th><th>Sch. No.</th><th>Total Amount</th><th>Estiamte Amount</th><th>Discount</th><th>Months</th><th>No Of Months</th><th>Fee Type</th><th>Date</th><th>Invoices</th></thead><tbody>";
foreach ($data as $row) {
$count++;
echo "<tr>";
echo "<td>".$count."</td>";
echo "<td>".$row['sch_no']."</td>";
echo "<td>".$row['total_amount']."</td>";
echo "<td>".$row['estimate_amount']."</td>";
echo "<td>".$row['discount']."</td>";
echo "<td>".$row['months']."</td>";
echo "<td>".$row['no_of_months']."</td>";
echo "<td>".$row['fee_type']."</td>";
echo "<td>".$row['fee_date']."</td>";
echo "<td><a class='btn btn-info' href='".base_url()."StudentFee/feeInvoice/".$row['fee_no']."'>Invoice</a></td>";
echo "</tr>";
}

echo "</tbody></table></div>";
}


public function get_date_fee_collection_pdf($date){
// if($this->session->userdata('fee')!=1){
// redirect(base_url().'KoylaNagarFee/');
// }     
$datee= date('Y-m-d');
if($this->uri->segment(3))
{
$fee_no = $this->uri->segment(3);}
  
$old_limit = ini_set("memory_limit", "64M");
$dompdf = new Pdf();
        
$dompdf->set_paper('letter', 'portrait'); //landscape , portrait
// <th>Student Name</th>

$html_content=$this->FeeModel->pdf_header();
$html_content .= '<div class="table-responsive ">';
$html_content .="<table class='table-striped' width='100%'>
<tr width='100%'><td><img src='assets/images/mtmlogo.png' width='100px' height='70px' /></td><td><h4>Mother Teresa Mission Higher Secondary School Koyla Nagar Kanpur.</h4></td></tr>
<tr><td><span>Date : </span></td><td><span>".$datee."</span></td></tr>

</table>";

$html_content.='<table class="table table-bordered table-striped"><thead><tr><th>Count</th><th>Sch No</th><th>Name</th><th>Class</th><th>Fee Months</th><th>Discount</th><th>Fee</th></tr></thead><tbody>"';        


$count=0;
$data=$this->FeeModel->fetch_date_fee_details($date);
$total=0;
if($data){
    
foreach($data as $row){
$count ++;    
$total=$total + $row['total_amount'];
$sid=$row['sid'];
$table='student';
$data_student=$this->FeeModel->fetch_single_student($table,$sid);
foreach($data_student as $student){$student_name=$student['student_name'];}

$html_content .= "<tr>";
$html_content .= "<td>".$count."</td>";
$html_content .= "<td>".$row['sch_no']."</td>";
$html_content .= "<td>".$student_name."</td>";
$html_content .= "<td>".$row['student_class']."</td>";
$html_content .= "<td>".$row['months']."</td>";
$html_content .= "<td>".$row['discount']."</td>";
$html_content .= "<td>".$row['total_amount']."</td>";
$html_content .= "</tr>";
}    
}



$html_content .="</tbody></table></div>";
$html_content .="<p style='text-align:right;box-shadow:0 0 5px silver;height:35px;background:red;color:#fff;padding:5px;font-size:16px;'>Total:".$total."</p>";

// $html_content .= $this->load->view('entab/feemanagement/user/footer');
$html_content .="</body></html>";
$this->pdf->loadHtml($html_content);
$this->pdf->render();
$this->pdf->stream("".$date.".pdf", array("Attachment"=>0));    
}

}