<?php

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

class Superuser_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('superuser_model');
        $this->load->model('accounting_model');
        $this->load->model('invoice_model');
        $this->load->model('voucher_model');
        $this->load->model('expense_model');
        $this->load->model('business_model');
        $this->load->model('visits_model');
        $this->load->model('sms_model');
        $this->load->model('tools_model');
        
        
        if($this->session->userdata('role')!=='Super User'){
            redirect('logout');
        }
    }

    public function supervise_invoices() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $date=$this->input->post('calendar_date', TRUE);

            if(!isset($date) || empty($date)){
                 $date = date('Y-m-d');
            }

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Invoices';

            //get the posted values
            $data['date'] = $date;        
            $data['business']=$this->business_model->getbusinessdetails();
            $data['invoices'] = $this->superuser_model->getsupervisordayinvoices($date);


            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_invoices');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function open_edit_invoice(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Invoices';

           //
            
            //get the posted values

            $data['business']=$this->business_model->getbusinessdetails();
            $data['invoice'] = $this->superuser_model->get_invoice_for_edit($this->input->post('id_invoice'));


            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_invoice_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_invoice(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
        
            $result = $this->superuser_model->update_super_invoice();

            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Invoice successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Invoice was not updated!');
            }

             redirect('super_invoices');
//            checkroles(0,0,1);
//
//            $data['nav'] = 'Super';
//            $data['subnav'] = 'Supervise Invoices';
//
//            //get the posted values
//
//            $data['business']=$this->business_model->getbusinessdetails();
//            $data['invoice'] = $this->superuser_model->get_invoice_for_edit($this->input->post('id_invoice'));
//
//
//            $this->load->view('includes/header', $data);
//            $this->load->view('superuser/supervise_invoice_edit');
//            $this->load->view('includes/footer');
        
        }
    }
 
    
    public function supervise_visits() {
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $date=$this->input->post('calendar_date', TRUE);

            if(!isset($date) || empty($date)){
                $date = date('Y-m-d');
            }

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Invoices';

            //get the posted values
            $data['date'] = $date;        
            $data['business']=$this->business_model->getbusinessdetails();
            $data['visits'] = $this->superuser_model->getsupervisordayvisits($date);


            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_visits');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function open_edit_visit(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Visits';

            //get the posted values

            $data['business']=$this->business_model->getbusinessdetails();
            $data['visit'] = $this->superuser_model->get_visit_for_edit($this->input->post('id_customer_visits'));
            $data['advances'] = $this->visits_model->getopenvisitadvancebyid($this->input->post('id_customer_visits'));

            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_visit_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_visit(){
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $result = $this->superuser_model->update_super_visit();

            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Visit successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Visit was not updated!');
            }

            checkroles(0,0,1);

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Visits';

            //get the posted values

            $data['business']=$this->business_model->getbusinessdetails();
            $data['visit'] = $this->superuser_model->get_visit_for_edit($this->input->post('id_customer_visits'));
             $data['advances'] = $this->visits_model->getopenvisitadvancebyid($this->input->post('id_customer_visits'));

            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_visit_edit');
            $this->load->view('includes/footer');
        }
        
    }
    
    function edit_advance(){
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            $result = $this->superuser_model->update_super_advance();

            if($result && $result>0){
                $this->session->set_flashdata('Success', 'Advance successfully updated!');
            } else {
                $this->session->set_flashdata('Error', 'Advance was not updated!');
            }

            checkroles(0,0,1);

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Visits';

            //get the posted values

            $data['business']=$this->business_model->getbusinessdetails();
            $data['visit'] = $this->superuser_model->get_visit_for_edit($this->input->post('id_customer_visits'));
             $data['advances'] = $this->visits_model->getopenvisitadvancebyid($this->input->post('id_customer_visits'));

            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_visit_edit');
            $this->load->view('includes/footer');
        }
    }
    
    function account_mapping(){
        $data['nav'] = 'Super';
        $data['subnav'] = 'Supervise Mapping';
            
        $data['events']=$this->accounting_model->get_all_events();
        $data['eventmapping']=[];
        foreach ($data['events'] as $event){ 
            array_push($data['eventmapping'], $this->accounting_model->get_event_mappings($event['id_account_events']));
        }
                  
        
        $this->load->view('includes/header', $data);
        $this->load->view('superuser/account_mapping');
        $this->load->view('includes/footer');
        
        
    }
    
    function add_event_mapping($eventid){
        $data['nav'] = 'Super';
        $data['subnav'] = 'Supervise Mapping';
            
        $data['event_id'] = $eventid;
        
        $data['accountevent'] = $this->accounting_model->get_account_event($eventid);
        
        $data['eventdetails'] = $this->accounting_model->get_event_mappings($eventid);
        $data['account_heads']= $this->accounting_model->get_account_heads();
        
        $this->load->view('includes/header', $data);
        $this->load->view('superuser/add_event_mapping');
        $this->load->view('includes/footer');
        
        
    }
    
    function edit_event_mapping($eventid){
        $data['nav'] = 'Super';
        $data['subnav'] = 'Supervise Mapping';
            
        $data['eventdetails'] = $this->accounting_model->get_event_mappings($eventid);
        
        //$data['mainaccounts']= $this->accounting_model->get_account_main();
        //$data['mainaccounts']= $this->accounting_model->get_account_heads();
        $data['mainaccounts'] = $this->accounting_model->get_account_heads('all','all',$this->session->userdata('businessid'));
        
        $this->load->view('includes/header', $data);
        $this->load->view('superuser/edit_event_mapping');
        $this->load->view('includes/footer');
        
        
    }
    
    function update_event_mapping(){
          
        $a = $this->superuser_model->update_event_mapping();
        
        if($a>0){
            $msg="success";
        }
        $rd='editmapping/'.$this->input->post('idaccountevent');
        redirect($rd);
        
    }
    
    function insert_event_mapping(){
        
        
        $a = $this->superuser_model->insert_event_mapping();
        
        if($a>0){
            $msg="success";
        }
        $rd='super_account_mapping';
        redirect($rd);
    }
    
    
    function add_account_event(){
        
         $a = $this->superuser_model->insert_account_event();
        
        if($a>0){
            $msg="success";
        }
        $rd='super_account_mapping';
        redirect($rd);
    }
    
    function edit_account_event(){
        
         $a = $this->superuser_model->update_account_event();
        
        if($a>0){
            $msg="success";
        }
        $rd='super_account_mapping';
        redirect($rd);
    }
    
    function supervise_setting(){
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

//            $date=$this->input->post('calendar_date', TRUE);
//
//            if(!isset($date) || empty($date)){
//                 $date = date('Y-m-d');
//            }

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Setting';

            //get the posted values
            //$data['date'] = $date;        
            $data['business']=$this->business_model->getbusinessdetails();
            $data['businesses']=$this->business_model->get_every_businesses();
            $data["cred"]=$this->sms_model->get_sms_cred($this->session->userdata('businessid'));
            $data['tasks']=$this->sms_model->get_scheduled_tasks();
            //$data['invoices'] = $this->superuser_model->getsupervisordayinvoices($date);


            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_setting');
            $this->load->view('includes/footer');
        }
        
    }
    
    function update_smscred(){
        $id= $this->input->post('id_sms_cred');
        
        if(null==$id){
            $a = $this->sms_model->insert_smscred();           
            if($a>0){
                $this->session->set_flashdata('Success', 'Successfully Added SMS Credentials!');
            }else {$this->session->set_flashdata('Error', 'SMS Credentials could not be saved!');}
            $rd='supervise_setting';
            redirect($rd);
        }else {
            $a = $this->sms_model->update_smscred($id);
            if($a>0){
                $this->session->set_flashdata('Success', 'Successfully updated SMS Credentials!');
            }else {$this->session->set_flashdata('Error', 'SMS Credentials could not be saved!');}
            
            $rd='supervise_setting';
            redirect($rd);
        }
        
        
    }
    function updatetask(){
        
        if($this->input->post('operation')=='test'){
            $sql = $this->input->post('s_query');
            if (strtolower(substr($sql,0,6))==="select"){
                $result = $this->tools_model->run_query($sql);

                //print_r($result); exit();
                $data['fields'] = $result['fields'];
                $data['resultset'] = $result['resultset'];

                $this->load->view('includes/header', $data);
                $this->load->view('superuser/task_results');
                $this->load->view('includes/footer');

            }else{
                return "invalid sql statement";
            }
        } else {
            
            $this->sms_model->update_scheduled_task();
            $rd='supervise_setting';
            redirect($rd);
        }
    }
    
    
    public function download_customers(){
        
        $businessid=$this->input->post('business_id');
        $businessname=$this->input->post('business_name');
                
        $filename='contacts_'.$businessname.date('D-M-Y').".csv";
        
        $list = $this->superuser_model->download_customers($businessid);
        
        $data="";
        
        foreach ($list as $line) {
          $data .= $line['customer_cell'].",".$line['customer_name'].",".$line['customer_email']."\n";
        }
        
        header('Content-Type: application/csv');
        header('Content-Disposition: attachment; filename="'.$filename.'"');
        echo $data; exit();
        
    }
    
    public function reopencashregister(){
        $cashregisterid=$this->input->post('id_cash_register');
        
        if($cashregisterid>0){
        
            $a = $this->superuser_model->reopencashregister($cashregisterid);  

                if($a>0){
                    echo '<h1>Cash Register is now Open!</h1>';
                    echo '<h4>Open Scheduler again to fix the Cash counts and close the Till again!</h4>';

                }else {
                    echo '<h1>Cash Register REOPEN FAILED!</h1>';
                    echo '<h4>Open Scheduler again to see the probelm!</h4>';   
                }
        } else {
                echo '<h1>Cash Register NOT SELECTED</h1>';
                echo '<h4>Open Scheduler again to see the probelm!</h4>';   
            }
        
    }
    
    public function supervise_business_setup(){
        
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $date=$this->input->post('calendar_date', TRUE);

            if(!isset($date) || empty($date)){
                 $date = date('Y-m-d');
            }

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Setup';

            //get the posted values
            $data['date'] = $date;        
            $data['business']=$this->business_model->getbusinessdetails();

            $this->load->view('includes/header', $data);
            $this->load->view('superuser/supervise_business_setup');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_business_details() {
        
        $this->superuser_model->update_business_details();
        
        $this->session->set_flashdata('Success', 'Business Details Updated!');
        redirect('supervise_business_setup');
        
    }
 
    public function supervise_hrm_setup(){
    
        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {
            checkroles(0,0,1);

            $date=$this->input->post('calendar_date', TRUE);

            if(!isset($date) || empty($date)){
                 $date = date('Y-m-d');
            }

            $data['nav'] = 'Super';
            $data['subnav'] = 'Supervise Setup';

            //get the posted values
            $data['date'] = $date;        
            $data['business']=$this->business_model->getbusinessdetails();
            
            $this->db->select('*');
            $deductionpolicy = $this->db->get('deduction_policy');
            $data['deduction_policies']=$deductionpolicy->result_array();
            
            $this->db->select('*');
            $this->db->where('business_id', $this->session->userdata('businessid'));
            $allowances =$this->db->get('allowances');
            
            $data['allowances']= $allowances->result_array();

            $this->load->view('includes/header', $data);
            $this->load->view('superuser/hrm_settings');
            $this->load->view('includes/footer');
        }
        
    }
    
    public function update_deduction_policy() {
       
        
        $this->db->set('deduction_policy', $this->input->post('deduction_policy'));
        $this->db->set('total_absents_allowed', $this->input->post('total_absents_allowed'));
        $this->db->set('total_late_allowed', $this->input->post('total_late_allowed'));
        $this->db->set('deduction_days_absent', $this->input->post('deduction_days_absent'));
        $this->db->set('deduction_days_late', $this->input->post('deduction_days_late'));
        $this->db->where('id_deduction_policy', $this->input->post('id_deduction_policy'));
        $this->db->update('deduction_policy');
        
        if($this->db->affected_rows()>0)
            $this->session->set_flashdata('Success', 'Successfully Update Policy!');
        else {
            $this->session->set_flashdata('Error', 'Update did not work!');
        }
        
       redirect('superuser_controller/supervise_hrm_setup');
        
    }
    
    public function add_deduction_policy(){
        
        $data = array(
            'total_absents_allowed' => 0,
            'total_late_allowed' => 0,
            'deduction_days_absent' => 0,
            'deduction_days_late' => 0
        );
        
        $this->db->insert('deduction_policy', $data);
        
        $this->session->set_flashdata('Success', 'Successfully Added Policy!');
        
        redirect('superuser_controller/supervise_hrm_setup');
        
    }
    
    
    public function update_allowance() {
       
        
        $this->db->set('allowance_name', $this->input->post('allowance_name'));
        $this->db->set('entity_name', $this->input->post('entity_name'));
        $this->db->where('id_allowances', $this->input->post('id_allowances'));
        $this->db->update('allowances');
        
        if($this->db->affected_rows()>0)
            $this->session->set_flashdata('Success', 'Successfully Update Policy!');
        else {
            $this->session->set_flashdata('Error', 'Update did not work!');
        }
        
       redirect('superuser_controller/supervise_hrm_setup');
        
    }
    
    public function add_allowance(){
        
        $data = array(
            'allowance_name' => 'not added',
            'entity_name' => 'not selected',
            'business_id' => $this->session->userdata('businessid'),
            'created_by' => $this->session->userdata('username'),
            'created_at' => date('Y-m-d H:i:s')
        );
        
        $this->db->insert('allowances', $data);
        
        $this->session->set_flashdata('Success', 'Successfully Added Policy!');
        
        redirect('superuser_controller/supervise_hrm_setup');
        
    }
    
}