<?php

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

class Home extends CI_Controller {

    public function __construct() {
        parent::__construct();
        session_start();
        $this->load->model('site');
    }

    public function index() {
        $this->load->model('site');
        $this->load->view('header');
        $this->load->view('body');
        $this->load->view('footer');
    }

    public function login() {
        $surname = $this->input->post('surname');
        $regNo = $this->input->post('regNo');
        $check = $this->site->checkLogin($surname, $regNo);
        if ($check) {
            //retrieve all profile
            $profile = $this->site->profile($surname, $regNo);
            foreach ($profile as $row) {
                $_SESSION['nseregno'] = $row['nseregno'];
                $_SESSION['idd'] = $row['idd'];
            }
            redirect('home/home2', 'refresh');
        } else {
            $data['msg'] = 'Invalid Surname or Reg No';
            $this->load->view('header');
            $this->load->view('body', $data);
            $this->load->view('footer');
        }
    }

    public function home2() {
        $idd = $_SESSION['idd'];
        $nseregno = $_SESSION['nseregno'];
        $data['profile'] = $this->site->fullProfile($idd);
        $data['payitem'] = $this->site->loadPayItem();
        $data['financial'] = $this->site->financial($nseregno);
        $this->load->view('header2');
        $this->load->view('home', $data);
        $this->load->view('footer');
    }

    public function payDues() {
        $idd = $_SESSION['idd'];
        $nseregno = $_SESSION['nseregno'];
        $data['profile'] = $this->site->fullProfile($idd);
        $data['financial'] = $this->site->financial($nseregno);
        $this->load->view('header2');
        $this->load->view('paydues', $data);
        $this->load->view('footer');
    }

    public function payDues2() {
        $idd = $_SESSION['idd'];
        $nseregno = $_SESSION['nseregno'];
        $it = array_values($this->input->post('items'));
        $transNo = "3061" . $this->randomString(11);
        $date = date('Y-m-d');
        //deletes previous items in temptrans
        $this->site->deleteTempTrans($nseregno);
        if (empty($it)) {
            echo "An item must be selected";
            redirect('home/payDues', 'refresh');
            //exit();
        } else {
            foreach ($it as $item) {
                $itemName = $this->site->getItemName($item);
                $itemAmount = $this->site->itemAmount($nseregno, $itemName);
                $insert = $this->site->insertItem($nseregno, $item, $itemAmount, $itemName, $date);
            }
            if ($insert) {
                redirect('home/payduesQuick', 'refresh');
            }
        }

        /* $idd=$_SESSION['idd'];
          $nseregno=$_SESSION['nseregno'];
          $data['profile']=$this->site->fullProfile($idd);
          $data['financial']=$this->site->financial($nseregno);
          $this->load->view('header2');
          $this->load->view('paydues2',$data);
          $this->load->view('footer'); */
    }

    public function payduesQuick() {
        $idd = $_SESSION['idd'];
        $nseregno = $_SESSION['nseregno'];
        $data['profile'] = $this->site->fullProfile($idd);
        $data['financial'] = $this->site->getTempTrans($nseregno);
        $this->load->view('header2');
        $this->load->view('paydues2', $data);
        $this->load->view('footer');
    }

    public function randomString($randStringLength) {
        $timestring = microtime();
        $secondsSinceEpoch = (integer) substr($timestring, strrpos($timestring, " "), 100);
        $microseconds = (double) $timestring;
        $seed = mt_rand(0, 1000000000) + 10000000 * $microseconds + $secondsSinceEpoch;
        mt_srand($seed);
        $randstring = "";
        for ($i = 0; $i < $randStringLength; $i++) {
            $randstring .= mt_rand(0, 9);
        }
        return($randstring);
    }
    public function contact(){
        $this->load->view('header');
        $this->load->view('contact');
        $this->load->view('footer');
    }
    public function guide(){
        $this->load->view('header');
        $this->load->view('payguide');
        $this->load->view('footer');
    }
    public function payGuide(){
        $this->load->view('header2');
        $this->load->view('payguide2');
        $this->load->view('footer');
    }

    public function quickteller() {
        $this->load->library('soap_client_view');
        $email = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "email");
        $phone = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "mobilephone");
        $lastname = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "lastname");
        $middlename = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "middlename");
        $firstname = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "firstname");

        if (empty($middlename)) {
            $middlename = 'NA';
        }
        if (empty($firstname)) {
            $firstname = 'NA';
        }
        $regNo = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "nseregno");
        if (empty($email)) {
            $myemail = 'info@nse.org.ng';
        } else {
            $myemail = $email;
        }

        $transID = '3061' . $this->randomString(11);
        $param['applicant_id'] = $_SESSION['nseregno'];
        $param['email'] = $myemail;
        $param['trans_no'] = $transID;
        $param['trans_name'] = "Payment of Dues";
        $param['trans_amount'] = $this->input->post('amount');
        $amount = $this->input->post('amount');


        $transCharge = 300.00;

        $portalFee = 200.00;
        $bankShare = 100.00;
        $schoolShare = $amount - $portalFee - $bankShare;
        $totalAmount = $portalFee + $bankShare + $schoolShare;
        //$names=explode(" ",$this->input->post('name'));
        $surname = $this->site->getAny("nsemem", $_SESSION['idd'], "idd", "lastname");
        $email = $myemail;
        $phone = $phone;
        $applicantID = $_SESSION['idd'];

        $soapParam = array(
            'portalFee' => $portalFee,
            'bankShare' => $bankShare,
            'schoolShare' => $schoolShare,
            'transCharge' => ($portalFee + $bankShare),
            'surname' => $surname,
            'firstname' => $firstname,
            'othernames' => $middlename,
            'email' => $email,
            'phone' => $phone,
            'totalAmount' => $totalAmount,
            'applicantID' => $applicantID,
            'transactionID' => $transID,
            'transYear' => date('Y'),
            'itemCode' => 'NSE01',
            'description' => 'Payment of Dues',
            'installmentID' => 2
        );

        $response = $this->soap_client_view->logTransaction($soapParam);
        // print_r($response);
        if (!isset($response)) {
            throw new Exception("Web Service Error");
        }
        if ($response['responseCode'] == "00") {
            $date = date('Y-m-d');
            $insert = $this->site->insertTrans($_SESSION['nseregno'], $transID, $date, $schoolShare, 'NOT PAID', '2014', 'Paychoice');

            if ($insert) {
                //select from temptrans table
                $tranx = $this->site->getTempTrans2($_SESSION['nseregno'], $date);
                foreach ($tranx as $t) {
                    //updates individual item   
                    $name = $t['name'];
                    $itemAmount = $this->site->amountItem($name, $_SESSION['nseregno']);
                    $updateTrans = $this->site->updateTrans($name, $_SESSION['nseregno'], $transID, $itemAmount);
                }
                //delete from temptrans
                $this->site->deleteTempTrans($_SESSION['nseregno']);
                redirect('home/payslip/' . $transID, 'refresh');
            }
        }
    }

    public function ups() {
        $date = date('Y-m-d');
        $transID = "1403" . $this->randomString(11);
        $schoolShare = $this->input->post('amount');
        $insert = $this->site->insertTrans($_SESSION['nseregno'], $transID, $date, $schoolShare, 'NOT PAID', '2014', 'UPS');

        if ($insert) {
            //select from temptrans table
            $tranx = $this->site->getTempTrans2($_SESSION['nseregno'], $date);
            foreach ($tranx as $t) {
                //updates individual item   
                $name = $t['name'];
                $itemAmount = $this->site->amountItem($name, $_SESSION['nseregno']);
                $updateTrans = $this->site->updateTrans($name, $_SESSION['nseregno'], $transID, $itemAmount);
            }
            $this->site->deleteTempTrans($_SESSION['nseregno']);
            redirect('home/ups2/'.$transID,'refresh');
        }
        
    }
    public function ups2($transID){
        $idd = $_SESSION['idd'];
        $data['profile'] = $this->site->fullProfile($idd);
        $data['transID'] = $transID;
        $data['details']=$this->site->retrieveTransDetails($transID);
        $this->load->view('header2');
        $this->load->view('ups', $data);
        $this->load->view('footer');
    }

    public function payslip($transID) {
        $idd = $_SESSION['idd'];
        $nseregno = $_SESSION['nseregno'];
        $data['profile'] = $this->site->fullProfile($idd);

        $data['transID'] = $transID;

        $data['details'] = $this->site->retrieveTransDetails($transID);
        $this->load->view('header2');
        $this->load->view('payslip', $data);
        $this->load->view('footer');
    }

    public function logout() {
        session_destroy();
        echo "You are now been logged out, redirecting to login";
        redirect('home/index', 'refresh');
    }

    public function page($id) {
        $data['news'] = $this->site->getNews();
        $data['content'] = $this->site->getContent($id);
        $data['membership'] = $this->site->getContent(44);
        $data['events'] = $this->site->getEvents();
        $this->load->view('header');
        $this->load->view('body2', $data);
        $this->load->view('footer');
    }

    public function finStand() {
        $this->load->model('site');
        $this->load->library('pagination');
        $config['base_url'] = site_url('home/finStand');
        $config['total_rows'] = $this->site->countMembers();
        $config['per_page'] = 30;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['sno'] = $page;
        $this->pagination->initialize($config);
        $data['pages'] = $this->pagination->create_links();
        $data['stand'] = $this->site->standing($config['per_page'], $page);
        //$data['stand'] = $this->site->Standing();
        $this->load->view('header');
        $this->load->view('stand', $data);
        //$this->load->view('footer');
    }

    public function news($id) {
        $data['news'] = $this->site->getNews();
        $data['content'] = $this->site->newsDetails($id);
        $data['membership'] = $this->site->getContent(44);
        $data['events'] = $this->site->getEvents();
        $this->load->view('header');
        $this->load->view('news', $data);
        $this->load->view('footer');
    }

    public function searchFin($surname) {
        $data['stand'] = $this->site->searchFin($surname);
        $this->load->view('searchFin', $data);
    }

    public function searchFin2($regNo) {
        $data['stand'] = $this->site->searchFin2($regNo);
        $this->load->view('searchFin', $data);
    }

    public function events($id) {
        $data['news'] = $this->site->getNews();
        $data['content'] = $this->site->eventDetails($id);
        $data['membership'] = $this->site->getContent(44);
        $data['events'] = $this->site->getEvents();
        $this->load->view('header');
        $this->load->view('events', $data);
        $this->load->view('footer');
    }

    public function members() {
        $this->load->library('pagination');
        $config['base_url'] = site_url('welcome/members');
        $config['total_rows'] = $this->site->countMembers();
        $config['per_page'] = 30;
        $config["uri_segment"] = 3;

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['sno'] = $page;
        $this->pagination->initialize($config);
        $data['pages'] = $this->pagination->create_links();
        $data['news'] = $this->site->getNews();
        $data['membership'] = $this->site->getContent(44);
        $data['members'] = $this->site->members($config['per_page'], $page);
        $data['events'] = $this->site->getEvents();
        $this->load->view('header');
        $this->load->view('members', $data);
        $this->load->view('footer');
    }

    
}

/* End of file home.php */
/* Location: ./application/controllers/home.php */