<?php

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

class Undeposited_funds extends Admin_Controller {

    function __construct() {
        parent::__construct();
    }

    function searchundeposited_funds() {
        $this->session->set_userdata('top_menu', 'Reports');
        $this->session->set_userdata('sub_menu', 'undeposited_funds/searchundeposited_funds');
        $data['title'] = 'Search Expense';
		$data['payment_methods']=$this->payment_methods_model->select(); 
		 $data['cashbook']=$this->cashbook_accounts_model->select(); 
		 $this->form_validation->set_rules('cashbook_account12', 'Cashbook Account', 'required');
		 $this->form_validation->set_rules('payment_mode', 'Payment Mode', 'required');
		 
       if ($this->form_validation->run() == false) {
            $this->load->view('layout/header', $data);
            $this->load->view('admin/undeposited_funds/searchundeposited_funds', $data);
            $this->load->view('layout/footer', $data);
        }else{
			
        if ($this->input->server('REQUEST_METHOD') == "POST") {
            $search = $this->input->post('search');
            $payment_mode = $this->input->post('payment_mode');
            $cashbook_account12 = $this->input->post('cashbook_account12');
            if ($search == "search_filter") {
				
                $data['exp_title'] = 'Undeposited Funds History From ' . $this->input->post('date_from') . " To " . $this->input->post('date_to');
                $date_from = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_from')));
                $date_to = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_to')));
                $feeList = $this->studentfeemaster_model->getFeeBetweenDate($date_from, $date_to);

                $data['payment_mode'] = $payment_mode;
                $data['feeList'] = $feeList;
                $data['date_from2'] = $date_from;
                $data['date_to2'] = $date_to;
                $data['cashbook_account12'] = $cashbook_account12;
            }
            $this->load->view('layout/header', $data);
            $this->load->view('admin/undeposited_funds/searchundeposited_funds', $data);
            $this->load->view('layout/footer', $data);
        } else {
            $this->load->view('layout/header', $data);
            $this->load->view('admin/undeposited_funds/searchundeposited_funds', $data);
            $this->load->view('layout/footer', $data);
        }
	}
   }    
	function history() {
        $this->session->set_userdata('top_menu', 'Reports');
        $this->session->set_userdata('sub_menu', 'undeposited_funds/history');
        $data['title'] = 'Search History';
		$data['payment_methods']=$this->payment_methods_model->select(); 
		 $data['cashbook']=$this->cashbook_accounts_model->select(); 
        if ($this->input->server('REQUEST_METHOD') == "POST") {
            $search = $this->input->post('search');
            $payment_mode = $this->input->post('payment_mode');
            if ($search == "search_filter") {
				
                $data['exp_title'] = 'Undeposited Funds History From ' . $this->input->post('date_from') . " To " . $this->input->post('date_to');
                $date_from = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_from')));
                $date_to = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_to')));
                $feeList = $this->studentfeemaster_model->getUDFhistoryFeeBetweenDate($date_from, $date_to);

                $data['payment_mode'] = $payment_mode;
                $data['feeList'] = $feeList;
                $data['date_from2'] = $date_from;
                $data['date_to2'] = $date_to;
            }
            $this->load->view('layout/header', $data);
            $this->load->view('admin/undeposited_funds/history', $data);
            $this->load->view('layout/footer', $data);
        } else {
            $this->load->view('layout/header', $data);
            $this->load->view('admin/undeposited_funds/history', $data);
            $this->load->view('layout/footer', $data);
        }
    }
    function transfer_funds() {
		$this->form_validation->set_rules('transfer', 'Student Fee Master', 'required');
        if ($this->form_validation->run() == false) {
            $data = array(
                'transfer' => form_error('transfer'),
            );
            $array = array('status' => 'fail', 'error' => $data);
            echo json_encode($array);
        } else {

            				//CODE FOR GENERATING RECEIPT NUMBERS AND INSERTING THEM TO THE DATABASE
				$value2='';
				//Query to fetch last inserted invoice number
				$this->db->select('id');
				$this->db->from('transfered_funds');
				$query1 = $this->db->get();
				$result1 = $query1->num_rows();    // CODE FOR COUNTING NUMBER OF ROWS IN TOTAL
				
				if($result1 > 0) {					
						$value2 = $result1;
						//$value2 = substr($value2, 10, 13);//separating numeric part
						$value2 = $value2 + 1;//Incrementing numeric part
						$value2 = "UDF" . sprintf('%04s', $value2);//concatenating incremented value
						$value = $value2; 						
				} 
				else {
					$value2 = "UDF0001";
					$value = $value2;
									
				}
							
				$reference = $value;

					$credit = 0;
					$balance = 0;
					$udf_status = 1;
					$bank_rec = 0;
					$cashbook_data = array(
						'date_captured' => date('Y-m-d'),
						'date' => date('Y-m-d'),
						'cashbook' => $this->input->post('cashbook'),
						'description' => $this->input->post('description'),
						'account' => $this->input->post('account'),
						'module' => $this->input->post('module'),
						'receipt' => $reference,
						'debit' => $this->input->post('debit'),
						'credit' => $credit,
						'balance' => $balance,
						'bankrec' => $bank_rec,
						'date_from' => $this->input->post('date_from2'),
						'date_to' => $this->input->post('date_to2')
					);					

					$undeposited_funds_data = array( 
						'date' => date('Y-m-d'),
						'cashbook' => $this->input->post('cashbook'),
						'module' => $this->input->post('module'),
						'account' => $this->input->post('account'),
						'description' => $this->input->post('description'),			
						'reference' => $this->input->post('reference'),
						'deposits' => $this->input->post('debit'),
						'udf_status' => $udf_status,
						'date_from' => $this->input->post('date_from2'),
						'date_to' => $this->input->post('date_to2'),
						'payment_mode' => $this->input->post('payment_mode')
					);	
                /*					
					$data = array(
						'student_fees_master_id' => $this->input->post('student_fees_master_id'),
						'transfer' => $transfer
					);
                
					$this->cashbook_model->depositedata23($data);					
					*/			
					$transfer =1;

					
                        $student_fees_master_id = $this->input->post('student_fees_master_id');
                        $student_fees_master_id_array = isset($student_fees_master_id) ? $student_fees_master_id : array();

                        if (!empty($student_fees_master_id_array)) {
                            foreach ($student_fees_master_id_array as $key => $value) {

                                $insert_array = array(
                                    'student_fees_master_id' => $value,
                                    'transfer' => $transfer
                                );
                                $inserted_id = $this->cashbook_model->depositedata23($insert_array);

                                $preserve_record[] = $inserted_id;
                            }
                        }					
									
					$this->cashbook_model->TransferFundstoCB($cashbook_data);
					$this->cashbook_model->RecordUndeposited_funds($undeposited_funds_data);
					
					//SHOULD PUT A CODE TO UPDATE STUDENT FEES DEPOSITE SO THAT THEY DONT APPEAR TO THE FRONT END
					
					$array = array('status' => 'success', 'error' => '');
					echo json_encode($array);						
					
				}
        }
    
			
    function printundeposited_fundss() {


		        $setting_result = $this->setting_model->get();
                $data['settinglist'] = $setting_result;
				
                $data['title'] = 'Search Expense';

                $data['exp_title'] = 'undeposited_funds From ' . $this->input->post('date_from') . " To " . $this->input->post('date_to');
                $date_from = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_from')));
                $date_to = date('Y-m-d', $this->customlib->datetostrtotime($this->input->post('date_to')));
                $expenseList = $this->expense_model->search("", $date_from, $date_to);
                $incomeList = $this->income_model->search("", $date_from, $date_to);
                $feeList = $this->studentfeemaster_model->getFeeBetweenDate($date_from, $date_to);
                $data['expenseList'] = $expenseList;
                $data['incomeList'] = $incomeList;
                $data['feeList'] = $feeList;
                $data['date_from2'] = $date_from;
                $data['date_to2'] = $date_to;

            $this->load->view('print/printundeposited_fundss', $data);
    }		

}

?>