<?php

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

class attendance_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code
        $this->load->model('attendance_model');
        $this->load->model('staff_model');
        $this->load->model('accounting_model');
        $this->load->model('business_model');
        
        //echo $this->session->userdata('username'); exit();
//        
            if($this->session->userdata('username') !== 'attendance'){
                if($this->session->userdata('role') == '' ){
                redirect('logout');
            }
        }
    }

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

            $data['nav'] = 'report';
            $data['subnav'] = 'attendance';
            if(null!==$this->input->post('calendar_date', TRUE)){
                
                $date=$this->input->post('calendar_date', TRUE)."-01";
               
            } else {
                $date= date('Y-m-d');
            }

       // echo $date; exit();

            $businesstimings=$this->business_model->get_business_timing_margin();
            if(null!==$businesstimings and isset($businesstimings)){
                $businessopeningtime=$businesstimings->business_opening_time.':00';                
                $calTime=strtotime("+".($businesstimings->timein_margin + 1 )." minutes", strtotime($businessopeningtime));
                $businessopeningtime=date('h:i:s', $calTime);
            } else {
                $businessopeningtime='09:00:00';
            }
            
            //echo $businessopeningtime;exit();
            //get the posted values
            $data['date'] = $date;       
            $data['month'] = date("F", strtotime($date)); 
            $data['monthdigit'] = date("m", strtotime($date)); 
            $data['year'] = date("Y", strtotime($date)); 
            //$data['daysinmonth'] = cal_days_in_month(CAL_GREGORIAN, date("m", strtotime($date)), date("Y", strtotime($date)));
            //$data['daysinmonth'] = date("t");
            //$days = date("t");
            
            $month=date("m", strtotime($date));
            $year=date("Y", strtotime($date));
            $data['daysinmonth'] = $month  == 2 ? ($year % 4 ? 28 : ($year % 100 ? 29 : ($year % 400 ? 28 : 29))) : (($month - 1) % 7 % 2 ? 30 : 31);
            
            $data['business']=$this->business_model->getbusinessdetails();
            $staff_list=$this->attendance_model->get_active_staff();
            
            $data['staff'] = [];
            foreach($staff_list as $staff){
                $leaves = $this->attendance_model->get_staff_leaves($month, $year, $staff['id_staff']);
                $staff['leaves'] = $leaves;
                
                $data['staff'][] = $staff;
            }
//            echo '<pre>';
//            print_r($data['staff']);exit();
            
            $data['attendance']=$this->attendance_model->get_staff_attendance($date,$businessopeningtime);
            
            $data['holidays'] = $this->attendance_model->get_holidays($month, $year);
            
           // print_r($data['holidays']);exit();
            
            $this->load->view('includes/header', $data);
            $this->load->view('attendance_sheet');
            $this->load->view('includes/footer');
            //$data = $this->appointment_model->get_appointments();
            //echo(json_encode($data));
        }
    }
    
    public function attendance_qr(){
       
        if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
            
            $data['nav'] = 'report';
            $data['subnav'] = 'attendance';
            
            $data['business']=$this->business_model->getbusinessdetails();
            
            $qr= $this->getRandomString(8);
            
            $this->attendance_model->update_qr($qr);
            
            $data['key_code'] = $this->attendance_model->get_qr();
            
            $this->load->view('includes/header', $data);
            $this->load->view('key_code_generator');
            $this->load->view('includes/footer');
            
             
        }
        
    }

    public function update_qr(){
        $qr= $this->getRandomString(8);
            
        $this->attendance_model->update_qr($qr);
        
        $key_code = $this->attendance_model->get_qr();

        echo(json_encode($key_code));

    }
    
    function getRandomString($length = 8) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $string = '';

        for ($i = 0; $i < $length; $i++) {
            $string .= $characters[mt_rand(0, strlen($characters) - 1)];
        }

        return $string;
    }
  
    function mark_holidays(){
        if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
           $data['calendar']='calendar';
            $data['nodatatable']='nodatatable'; 
            
            $data['nav'] = 'hrm';
            $data['subnav'] = 'holidays';
            
            if(!empty($_REQUEST)){
                
                $exists = $this->attendance_model->check_holiday($this->input->post('txtDate'));
               
                if(null == $exists){
                    if(null!==$this->input->post('txtHolidyName')){
                        $pDate = date_create($this->input->post('txtDate'));
                        $day = $pDate->format('D');

                        $d = array(
                            'holiday_date' => $this->input->post('txtDate'),
                            'holiday_reason' => $this->input->post('txtHolidyName'),
                            'holiday_day' => $day,
                            'business_id' => $this->session->userdata('businessid'),
                            'created_by' =>  $this->session->userdata('username')
                        );

                        $this->db->insert('holidays', $d);
                    }
                }
            }
            
            
            $data['business']=$this->business_model->getbusinessdetails();
            $data['holidays']= json_encode($this->attendance_model->get_calendar_holidays(null, null, null));
            //print_r($data['holidays']); exit();
            
            $this->load->view('includes/header', $data);
            $this->load->view('staff/mark_holidays');
            $this->load->view('includes/footer');
            
             
        }
    }
    
    function delete_holiday(){
        
        $event_id = $this->input->post('event_id');
        
        $this->db->where('id_holidays', $event_id);
        $this->db->delete('holidays');
        
        echo 'success';
    }
    
    function getholiday_events(){
    
        echo json_encode($this->attendance_model->get_calendar_holidays(null, null, null));
        
    }

    
    function mark_leaves(){
        if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
           $data['calendar']='calendar';
            $data['nodatatable']='nodatatable'; 
            
            $data['nav'] = 'hrm';
            $data['subnav'] = 'leaves';
            
            if(!empty($_REQUEST)){
                if(null!==$this->input->post('txtStaffid')){
                    $rangArray = [];

                    $startDate = strtotime($this->input->post('txtFromDate'));
                    $endDate = strtotime($this->input->post('txtToDate'));

                    for ($currentDate = $startDate; $currentDate <= $endDate; $currentDate += (86400)) {
                        $date = date('Y-m-d', $currentDate);
                        $rangArray[] = $date;
                    }

                    foreach($rangArray as $mDate){                        
                        $exists = $this->attendance_model->check_leaves($this->input->post('txtStaffid'), $mDate);
                        if(null == $exists){
                            
                            $d = array(
                                'staff_id' => $this->input->post('txtStaffid'),
                                'leave_type' => $this->input->post('txtLeaveTypeId'),
                                'staff_leave_date' => $mDate,
                                'leave_reason' => $this->input->post('txtLeaveReason'),                            
                                'created_by' =>  $this->session->userdata('username')
                            );

                            $this->db->insert('staff_leaves', $d);
                        }
                    }
                }                
            }
            
            $data['staff'] = $this->staff_model->get_active_staff();
            $data['leave_types'] = $this->attendance_model->get_leave_types();
            $data['business'] = $this->business_model->getbusinessdetails();
            $holidays = $this->attendance_model->get_calendar_holidays(null, null, null);
            $staff_leaves = $this->attendance_model->get_calendar_leaves(null, null, null);
            //print_r($data['staff_leaves']); exit();
            
            $data['events'] = json_encode(array_merge($holidays, $staff_leaves));
            
            $data['staffid'] = $this->input->post('txtStaffid');
            $data['leavetype'] = $this->input->post('txtLeaveTypeId');
            $data['leavereason'] = $this->input->post('txtLeaveReason');
            
            
            $this->load->view('includes/header', $data);
            $this->load->view('staff/mark_leaves');
            $this->load->view('includes/footer');
            
             
        }
    }
    
    function remove_leave(){
        
        $event_id = $this->input->post('event_id');
        
        $this->db->where('id_staff_leaves', $event_id);
        $this->db->set('staff_leave_status', 'InActive');
        $this->db->update('staff_leaves');
        
        echo 'success';
    }
    
    
    public function leave_applications($application_id=null){
        
         if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
        
            $status = $this->input->post('statusfilter');
            if($status == '' || null == $status ){
                $status = 'Pending';
            }
            $staffid= $this->input->post('stafffilter');;
            if($staffid == '' || null == $staffid){
                $staffid = 0;
            }
            
            $data['status']=$status;
            $data['staffid']=$staffid;
            
            $data['business'] = $this->business_model->getbusinessdetails();
            $data['applications'] = $this->attendance_model->getleaveapplications($status, $staffid);
            
            $data['leave_types'] = $this->attendance_model->get_leave_types();
            $data['staff_list']=$this->attendance_model->get_active_staff();
            
            $this->load->view('includes/header', $data);
            $this->load->view('staff/leave_applications');
            $this->load->view('includes/footer');
        
        }
        
    }
    
    
    public function update_leave_app(){
        $staffid = $this->input->post('txtstaffid');
        $appid = $this->input->post('txtleaveid');        
        $status = $this->input->post('txt_leave_status'); 
        $leave_type = $this->input->post('txt_leave_type'); 
      
        $remaining_leaves = $this->attendance_model->get_remaining_leaves($staffid);
        $rl = $remaining_leaves->remaining_leaves;
        
        ///mark leaves
        $rangArray = [];
        $startDate = strtotime($this->input->post('txtreqfrom'));
        $endDate = strtotime($this->input->post('txtreqto'));
        
        if($status == 'Approved'){
            $create_leave=true;
            if($leave_type == 'Annual Leave' && $rl <= 0){
                $create_leave=false;
            }
            if($create_leave==true){
                for ($currentDate = $startDate; $currentDate <= $endDate; $currentDate += (86400)) {
                    $date = date('Y-m-d', $currentDate);
                    $rangArray[] = $date;
                }
                foreach($rangArray as $mDate){                        
                    $exists = $this->attendance_model->check_leaves($this->input->post('txtStaffid'), $mDate);
                    if(null == $exists){                    
                        $d = array(
                            'staff_id' => $staffid,
                            'leave_type' => $leave_type,
                            'staff_leave_date' => $mDate,
                            'leave_reason' =>  $this->input->post('txt_comment'),                            
                            'created_by' =>  $this->session->userdata('username')                            
                        );

                        $this->db->insert('staff_leaves', $d);
                    }
                }
            } else {
                $this->session->set_flashdata('Warning', 'Application Status not changed');
                exit();
            }
        } 
        //update the application
        $this->db->where('id_leave_applications', $appid);
        $this->db->set('application_status', $status);
        $this->db->set('approved_by', $this->session->userdata('username') );
        $this->db->set('approved_on', date('Y-m-d H:i:s'));
        $this->db->set('leave_type', $leave_type);
        $this->db->set('comments', $this->input->post('txt_comment'));
        $this->db->update('leave_applications');
        
        $this->session->set_flashdata('Success', 'Leaves Added to Scheduler for '.$this->input->post('txtstaffname'));
        redirect(base_url('leave_applications'));
    }
    
    public function open_loan_applications(){
         if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
        
            $status = $this->input->post('statusfilter');
            if($status == '' || null == $status ){
                $status = 'Pending';
            }
            $staffid= $this->input->post('stafffilter');;
            if($staffid == '' || null == $staffid){
                $staffid = 0;
            }
            
            $data['status']=$status;
            $data['staffid']=$staffid;
            
            $data['business'] = $this->business_model->getbusinessdetails();
           // $data['applications'] = $this->attendance_model->getadvsalapplications($status, $staffid);
           $data['staff_list']=$this->staff_model->get_active_staff();
            
            
            $this->load->model('accounting_model');
            
            $data['bank_accounts'] = $this->accounting_model->get_account_heads(3,'all',$this->session->userdata('businessid'));
            $data['cash_accounts'] = $this->accounting_model->get_account_heads(2,'all',$this->session->userdata('businessid'));
            $data['card_accounts'] = $this->accounting_model->get_account_heads(8,'all',$this->session->userdata('businessid'));
            
            $this->load->view('includes/header', $data);
            $this->load->view('staff/advsal_applications');
            $this->load->view('includes/footer');
        
        }
        
    }

    public function getloanapplications(){

         //** get all table definitions **//
         $data['draw']= $this->input->post('draw');
         $data['$start'] = $this->input->post('start');
         $data['$length'] = $this->input->post('length');
         
         $data['id_loan_application']= $this->input->post('id_loan_application');
         $data['staff_id']= $this->input->post('staff_id');
         $data['created_on']= $this->input->post('created_on');
         $data['requested_amount']= $this->input->post('requested_amount');
         $data['approved_amount']= $this->input->post('approved_amount');
         $data['approved_by']= $this->input->post('approved_by');
         $data['approved_on']= $this->input->post('approved_on');
         $data['installment']= $this->input->post('installment');        
         $data['num_of_months']= $this->input->post('num_of_months');
         $data['application_status']= $this->input->post('application_status');       
         
         $total= $this->attendance_model->get_countofapplications();
         $data['data'] = $this->attendance_model->getloanapplications();
         
         $filtered=  sizeof($data['data']);
         
         $data['recordsTotal'] = $total;
         $data['recordsFiltered'] = $total;

         echo(json_encode($data));
    }


    public function get_loan_application(){

            $sql = "select * from loan_applications where id_loan_applications = ".$this->input->post('id_loan_application');
            $query = $this->db->query($sql);
            $app = $query   ->row();

            echo(json_encode($app));

    }
    
    public function insert_loan_app(){

        $num_of_months = $this->input->post('num_of_months');
        $required_amount = $this->input->post('required_loan_amount');
        $installment =$this->input->post('required_loan_amount')/$this->input->post('num_of_months');

        $data = array(
            'staff_id' => $this->input->post('staff_id'),
            'staff_name' => $this->input->post('staff_name'),
            'required_loan_amount' => $this->input->post('required_loan_amount'),
            'application_status' => $this->input->post('application_status'),
            'created_by' => $this->session->userdata('username'),
            'comments'=> $this->input->post('comments'),
            'num_of_months' => $this->input->post('num_of_months'),
            'installment' => $installment
        );

        $this->db->insert('loan_applications', $data);

        $this->session->set_flashdata('Success', 'Application '.$this->db->insert_id().' Added!');

       

        redirect('loan_applications');

    }

    public function update_loan_application(){        
        
        $id_loan_applications = $this->input->post('id_loan_applications');
        $staff_id = $this->input->post('staff_id');
        $staff_name = $this->input->post('staff_name');
        $status = $this->input->post('application_status');
        $amount = $this->input->post('approved_loan_amount');
        $months = $this->input->post('num_of_months');
        $comment = $this->input->post('comment');
        //$voucher_payment_mode = $this->input->post('voucher_payment_mode');
        //$bank_accounts = $this->input->post('bank_accounts');
        //$cash_accounts = $this->input->post('cash_accounts');
        //$instrument_number = $this->input->post('instrument_number');
        //$account_voucher_date = $this->input->post('account_voucher_date');

        $installment = $amount/$months;       
        
        if($status == 'Pending'){



            redirect(base_url().'loan_applications');
            exit();
        
        } else if($status == 'Approved'){            
            //create payable loan voucher
            
            $data=array(
                'staff_id' => $staff_id,
                'staff_name' => $staff_name,
                'payment_date' => date('Y-m-d'), 
                'payment_mode' => 'Cash',
                'payment_type' => 'Loan',
                'payment_amount' => $amount,
                'payment_remarks' => 'Payable Loan',                 
                'created_by' => $this->session->userdata('userid'),
                'payment_month' => date('F'),
                'payment_year' => date('Y'),
                'bank_account_id' => 1,
                'deduction_amount' => 0,
                'month_number' => date('m')                
            );
            $this->db->insert('staff_payments', $data);
            $staff_payment_id = $this->db->insert_id();

        } else if($status == 'Rejected'){
            //update the application only
            return false;
            
        } else if($status == 'Disbursed'){
            //create  loan expense voucher

            return false;
        }
       // exit();
        $debit_accounts=[];
        $credit_accounts=[];
        
        $account_heads = $this->accounting_model->get_mapped_accounts(26);
        //var_dump ($account_heads); exit();
        foreach($account_heads as $ah){
            if($ah['transaction_type']=='debit'){
                array_push($debit_accounts,$ah['account_head_id']."|".$ah['transaction_type']."|".$ah['entity_name']);
            } else {
                array_push($credit_accounts, $ah['account_head_id']."|".$ah['transaction_type']."|".$ah['entity_name']);
            }
        }
        
        $bank_name='';
        //if(null!==$this->input->post('bank_accounts')){$bank_name=$this->input->post('bank_accounts');}
        $data = array( 
            'business_id' => $this->session->userdata('businessid'),
            'description' => 'Loan Payable booked on ' .  date('d-m-Y').', for '. $staff_name .' staff ID : '. $staff_id. ' on approval of application id '. $id_loan_applications .' by '. $this->session->userdata('username'),
            'voucher_date' => date('Y-m-d H:i:s'),
            'voucher_amount' => $amount,
            'voucher_status' => 'Active',
            'created_by' => $this->session->userdata('username'),
            'cost_center'=> '1',
            'cost_center_name'=> 'Bakc Office',
            'business_partner'=> 3,
            'business_partner_id'=>$staff_id,
            'business_partner_name'=>$staff_name,
            'voucher_type'=> 3,
            'payment_mode'=> 'Cash',            
            'staffpayment_id'=> $staff_payment_id,
            'auto_voucher'=>'Yes',
            'loan_application_id' => $id_loan_applications
        );

        $this->db->insert('account_vouchers', $data);
        $account_voucherid =  $this->db->insert_id();
        
        //enter debit entries
        foreach($debit_accounts as $debit_account){
            $debitamount=0;
            $debit = explode("|", $debit_account);
            //check entity
            if($debit[2]=="loan_payable" && $amount > 0){$debitamount= $amount;}            
            
            $data_detail=array(
                'account_voucher_id' => $account_voucherid,
                'account_head_id' => $debit[0],                
                'debit' => round($debitamount,2),
                'credit' => 0
            );
            //insert debit detail
            if($debitamount>0){
                $this->db->insert('account_voucher_detail', $data_detail);
            }
        }
        //enter credit entries
        foreach($credit_accounts as $credit_account){
            $creditamount=0;
            $credit = explode("|", $credit_account);
            //check entity
            if($credit[2]=="salary_payable" && $amount > 0){$creditamount= $amount;}            
            
            $data_detail=array(
                'account_voucher_id' => $account_voucherid,
                'account_head_id' => $credit[0],                
                'credit' => round($creditamount,2),
                'debit' => 0
            );
            //insert debit detail
            if($creditamount>0){
                $this->db->insert('account_voucher_detail', $data_detail);
            }
        }
        
        
        //update the voucher id in payment notes 
        $this->db->where('idstaff_payments', $staff_payment_id);
        $this->db->set('account_voucher_id', $account_voucherid);
        $this->db->update('staff_payments');
        
        //update the status of the application
        $this->db->where('id_loan_applications', $id_loan_applications);
        $this->db->set('application_status', 'Approved');
        $this->db->set('approved_on', date('Y-m-d H:i:s'));
        $this->db->set('approved_by', $this->session->userdata('username'));
        $this->db->set('voucher_id', $account_voucherid);
        $this->db->set('approved_amount', $amount);
        $this->db->set('num_of_months', $months);
        $this->db->set('installment', $installment);
        $this->db->update('loan_applications');
        

        redirect(base_url().'loan_applications');
    }
    
    function attendance_requests(){

        if($this->session->userdata('businessid')==''){
            $this->load->view('login_view');
        } else {
       
            
            $data['business'] = $this->business_model->getbusinessdetails();                               
            
            $this->load->view('includes/header', $data);
            $this->load->view('staff/attendance_requests');
            $this->load->view('includes/footer');
        
        }

    }

    public function getattendancerequests(){

        //** get all table definitions **//
        $data['draw']= $this->input->post('draw');
        $data['$start'] = $this->input->post('start');
        $data['$length'] = $this->input->post('length');
        
        $data['id_attendance_request']= $this->input->post('id_attendance_request');
        $data['staff_id']= $this->input->post('staff_id');
        $data['staff_name']= $this->input->post('staff_name');
        $data['request_time_in']= $this->input->post('request_time_in');
        $data['request_time_out']= $this->input->post('request_time_out');
        $data['address']= $this->input->post('address');
        $data['approved_by']= $this->input->post('approved_by');

        if(null == $this->input->post('request_status') || $this->input->post('request_status') == ''){
            $data['request_status']="pending";
        }else{
            $data['request_status']= $this->input->post('request_status');        
        }
                
        
        $total= $this->attendance_model->get_countofattendancereq();
        $data['data'] = $this->attendance_model->getattendancerequests();
        
        $filtered=  sizeof($data['data']);
        
        $data['recordsTotal'] = $total;
        $data['recordsFiltered'] = $total;

        echo(json_encode($data));
    }


    function get_attendance_request(){
        $attendance_request_id = $this->input->post('id_attendance_request');

        $sql = "Select attendance_request.*, staff.staff_fullname staff_name from attendance_request join staff on staff.id_staff = staff_id  where id_attendance_request = ".$attendance_request_id;
        $query = $this->db->query($sql);

        echo(json_encode($query->row()));

    }

    function update_attendance(){

        $attendance_request_id = $this->input->post('id_attendance_request');
        $request_status = $this->input->post('request_status');

        $sql = "Select attendance_request.*, staff.staff_fullname staff_name from attendance_request join staff on staff.id_staff = staff_id  where id_attendance_request = ".$attendance_request_id;
        $query = $this->db->query($sql);
        $request = $query->row();

        $sql = "select * from staff_attendance where date(time_in) = '".$request->request_date."' and staff_id = ".$request->staff_id;

        $query = $this->db->query($sql);

        if($request_status == 'approved'){
            if($query->num_rows() > 0){
                //update timeout only                
                $attendance = $query->row();
                if($attendance->time_out == '' || $attendance->time_out == null){
                    if($request->request_time_out != null && $request->request_time_out != ''){
                        $this->db->set('time_out', $request->request_time_out);
                    }
                }

                $this->db->where('id_attendance_request', $attendance_request_id);
                $this->db->set('request_status', 'approved');
                $this->db->set('approved_by', $this->session->userdata('username'));
                $this->db->update('attendance_request');

            } else {
                $data = array(
                    'staff_id'=>$request->staff_id,
                    'time_in'=>$request->request_time_in,
                    'time_out'=>$request->request_time_out,
                    'address'=>$request->address,
                    'business_id'=>$request->business_id
                );
                $this->db->insert('staff_attendance', $data);

                $this->db->where('id_attendance_request', $attendance_request_id);
                $this->db->set('request_status', 'approved');
                $this->db->set('approved_by', $this->session->userdata('username'));
                $this->db->update('attendance_request');
            }
        } else {
            $this->db->where('id_attendance_request', $attendance_request_id);
            $this->db->set('request_status', 'rejected');
            $this->db->set('approved_by', $this->session->userdata('username'));
            $this->db->update('attendance_request');
        }
        echo json_encode('success');
    }

    function yearly_staff_attendance($year=null, $month=null){
        
        if($year==null){
            if($this->input->post('selectyear')!=  null){
                $year = $this->input->post('selectyear');
            }else{
                $year = date('Y');
            }
        }
        
        if($month==null){
            $month = date('m');
        }
        //echo $year; exit();
        $data['attendance'] = $this->attendance_model->staff_year_attendance($year);
        $data['monthName'] =$month;
        $data['year'] =$year;
        
        
        $this->load->view('includes/header', $data);
        $this->load->view('staff/yearlyattendance');
        $this->load->view('includes/footer');
        
        
    }
    
    // FingerPrint
    public function get_staff_fingerprint(){
        $data = $this->attendance_model->get_staff_fingerprint();
        echo (json_encode($data));
        die;
    }
    public function update_staff_fingerprint() {
        $result = $this->attendance_model->update_staff_fingerprint();
        echo "success|" . $result;
    }    
    public function get_all_staff_fingerprints(){
        $data = $this->attendance_model->get_all_staff_fingerprints();
        echo (json_encode($data));
        die;
    }
    public function mark_staff_attendance_fingerprint(){
        $staffId = urlencode($this->input->post('id_staff', TRUE));
        $staffFCM = urlencode($this->input->post('staff_fcm', TRUE));
        $staffCell = urlencode($this->input->post('staff_cell', TRUE));
        $attendanceDate = urlencode($this->input->post('attendance_date', TRUE));
        $attendanceTime = urlencode($this->input->post('attendance_time', TRUE));
        $businessId = urlencode($this->input->post('businessId', TRUE));
        $latitude = urlencode($this->input->post('latitude', TRUE));
        $longitude = urlencode($this->input->post('longitude', TRUE));
        $address = urlencode($this->input->post('address', TRUE));
        $sql = "select current_key, serverurl from key_code where business_id = '".$businessId."';";
        $query = $this->db->query($sql);
        $row = $query->row();
        $key_code = $row->current_key;
        $curl = curl_init();
        curl_setopt_array($curl, array(
          CURLOPT_URL => base_url().'staffapp/attendance?cellNumber='.$staffCell.'&fcm_id='.$staffFCM.'&key='.$key_code.'&date='.$attendanceDate.'&time='.$attendanceTime.'&businessId='.$businessId.'&latitude='.$latitude.'&longitude='.$longitude.'&address='.$address,
          CURLOPT_RETURNTRANSFER => true,
          CURLOPT_ENCODING => '',
          CURLOPT_MAXREDIRS => 10,
          CURLOPT_TIMEOUT => 0,
          CURLOPT_FOLLOWLOCATION => true,
          CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
          CURLOPT_CUSTOMREQUEST => 'GET',
          CURLOPT_HTTPHEADER => array(
            'SKED-API-KEY: $kedw!$e-8547-65265-7444-12',
            'Cookie: ci_session=eio1idloumjfltn46nluvhbg1jepkdvp'
          ),
        ));
        $response = curl_exec($curl);
        curl_close($curl);
        echo $response;

    }
    // FingerPrint End
}