<?php

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

class Staffapp_controller extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        
        $this->load->library('xmlrpc');
        $this->load->library('xmlrpcs');
        $this->load->model('Staffapp_model');
        $this->load->model('Staff_model');
       // $this->load->library('session');
        
        $config['functions']['updateFCMToken'] = array('function' => 'Staffapp_controller.updateFCMToken');
        $config['functions']['login'] = array('function' => 'Staffapp_controller.login');
        $config['functions']['checkMobileNumber'] = array('function' => 'Staffapp_controller.checkMobileNumber');
        $config['functions']['getVisits'] = array('function' => 'Staffapp_controller.getVisits');
        $config['functions']['staffservicereport'] = array('function' => 'Staffapp_controller.staffservicereport');
        $config['functions']['staffretailreport'] = array('function' => 'Staffapp_controller.staffretailreport');
        $config['functions']['servicetypes'] = array('function' => 'Staffapp_controller.servicetypes');
        $config['functions']['servicecategories'] = array('function' => 'Staffapp_controller.servicecategories');
        $config['functions']['attendance'] = array('function' => 'Staffapp_controller.attendance');
        
        $config['functions']['getleavetypes'] = array('function' => 'Staffapp_controller.getleavetypes');
        $config['functions']['get_remaining_leaves'] = array('function' => 'Staffapp_controller.get_remaining_leaves');
        
        $config['functions']['leave_application'] = array('function' => 'Staffapp_controller.leave_application');
        $config['functions']['loan_application'] = array('function' => 'Staffapp_controller.loan_application');        
        $config['functions']['adv_salary_application'] = array('function' => 'Staffapp_controller.adv_salary_application');
        
        
        $config['functions']['leave_application_report'] = array('function' => 'Staffapp_controller.leave_application_report');
        $config['functions']['adv_salary_application_report'] = array('function' => 'Staffapp_controller.adv_salary_application_report');
        $config['functions']['loan_application_report'] = array('function' => 'Staffapp_controller.loan_application_report');
        
        $config['functions']['staff_attendance_report'] = array('function' => 'Staffapp_controller.staff_attendance_report');
        
        $config['debug'] = 'True';
        $config['object'] = $this;
        
        $this->xmlrpcs->initialize($config);
        $this->xmlrpcs->serve();

    }
    
    
    public function updateFCMToken($req){
        $params = $req->output_parameters();
        if(isset($params)){
            if(isset($params[0]['idStaff'])){
                $r = $this->Staffapp_model->updateFCMToken($params[0]);
            }else{
                $json = json_decode($params[0],true);
                $r = $this->Staffapp_model->updateFCMToken($json);
            }
            
            if($r){
                return $this->xmlrpc->send_response($r);
            }else{
                return $this->xmlrpc->send_error("101","Invalid Access");
            }
        }
    }
    
    public function login($req){
        $params = $req->output_parameters();
        if(isset($params[0]['cellNumber'])){
            $resp = null;
            $resp = $this->Staffapp_model->login($params[0]);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->login($row);
        }
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        }
    }
    
    public function checkMobileNumber($req){
        $params = $req->output_parameters();
         
        if(isset($params[0]['cellNumber'])){
            $resp = null;
            if(isset($params[0]['cellNumber'])){
                $resp = $this->Staffapp_model->checkMobileNumber($params[0]["cellNumber"]);
            }
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->checkMobileNumber($row["cellNumber"]);
        }
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        }
    }
    
    public function getVisits($req){
        $params = $req->output_parameters();
        $resp = null;
        if(isset($params[0]['staffId']) && isset($params[0]['startDate']) && isset($params[0]['endDate']) && isset($params[0]['businessId'])){
            $resp = $this->Staffapp_model->getVisits($params[0]['staffId'], $params[0]['startDate'], $params[0]['endDate'], $params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->getVisits($row['staffId'], $row['startDate'], $row['endDate'], $row['businessId']);
        }
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        }
    }
    

    public function staffservicereport($req){
        $params = $req->output_parameters();
        $resp = null;
        
        if(isset($params[0]['staffId']) && isset($params[0]['startDate']) && isset($params[0]['endDate']) && isset($params[0]['businessId']) && isset($params[0]['typeid']) && isset($params[0]['categoryid'])){
            
            $resp = $this->Staffapp_model->commission_details($params[0]['staffId'], $params[0]['startDate'], $params[0]['endDate'], $params[0]['businessId'], $params[0]['typeid'] , $params[0]['categoryid']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->commission_details($row['staffId'], $row['startDate'], $row['endDate'], $row['businessId'], $row['typeid'], $row['categoryid']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
         
    }
    
    public function staffretailreport($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staffId']) && isset($params[0]['startDate']) && isset($params[0]['endDate']) && isset($params[0]['businessId'])){
            $resp = $this->Staffapp_model->retail_commission_details($params[0]['staffId'], $params[0]['startDate'], $params[0]['endDate'], $params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->retail_commission_details($row['staffId'], $row['startDate'], $row['endDate'], $row['businessId']);
        }
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function servicetypes($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['businessId'])){
            $resp = $this->Staffapp_model->get_services_types($params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->get_services_types($row['businessId']);
        }
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    public function servicecategories($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['servicetypeid']) && isset($params[0]['service_type_flag']) && isset($params[0]['businessId'])){
            $resp = $this->Staffapp_model->get_services_categories($params[0]['servicetypeid'], $params[0]['service_type_flag'], $params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->get_services_categories($row['servicetypeid'], $row['service_type_flag'],  $row['businessId']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function attendance($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['cellNumber']) && isset($params[0]['fcm_id']) && isset($params[0]['key']) && isset($params[0]['date']) && isset($params[0]['time']) && isset($params[0]['businessId']) && isset($params[0]['address']) && isset($params[0]['longitude']) && isset($params[0]['latitude'])){
            $resp = $this->Staffapp_model->mark_staff_attendance($params[0]['cellNumber'], $params[0]['fcm_id'], $params[0]['key'], $params[0]['date'], $params[0]['time'], $params[0]['businessId'], $params[0]['address'] , $params[0]['longitude'] , $params[0]['latitude']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->mark_staff_attendance($row['cellNumber'], $row['fcm_id'],  $row['key'],  $row['date'],  $row['time'],  $row['businessId'],  $row['longitude'],  $row['latitude']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    /***********HRM Work******************/ 
    public function getleavetypes($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['businessId'])){
            $resp = $this->Staffapp_model->get_leave_types($params[0]['businessId']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->get_leave_types($row['businessId']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    public function get_remaining_leaves($req) {
        
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_model->get_remaining_leaves($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->get_remaining_leaves($row['staff_id']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
        
    }
    
    public function leave_application($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['required_from']) && isset($params[0]['required_to']) && isset($params[0]['business_id']) && isset($params[0]['leave_type']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_model->leave_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['required_from'], $params[0]['required_to'], $params[0]['business_id'], $params[0]['leave_type'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->leave_application($row['staff_id'], $row['fcm_id'],  $row['required_from'],  $row['required_to'],  $row['business_id'], $row['leave_type'], $row['comment']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function adv_salary_application($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['amount']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_model->adv_salary_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['amount'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->adv_salary_application($row['staff_id'], $row['fcm_id'],  $row['amount'], $row['comment']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function loan_application($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['fcm_id']) && isset($params[0]['amount']) && isset($params[0]['num_of_months']) && isset($params[0]['comment'])){
            $resp = $this->Staffapp_model->loan_application($params[0]['staff_id'], $params[0]['fcm_id'], $params[0]['amount'], $params[0]['num_of_months'], $params[0]['comment']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->loan_application($row['staff_id'], $row['fcm_id'], $row['amount'], $row['num_of_months'], $row['comment']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function staff_attendance_report($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id']) && isset($params[0]['month']) && isset($params[0]['year'])){
            $resp = $this->Staffapp_model->staff_month_attendance_detail($params[0]['staff_id'], $params[0]['year'], $params[0]['month']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->staff_month_attendance_detail($row['staff_id'], $row['year'], $row['month']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
        
        
    }
    
    
    public function leave_application_report($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_model->leave_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->leave_application_report($row['staff_id']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
        
        
    }
    
    public function adv_salary_application_report($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_model->adv_salary_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->adv_salary_application_report($row['staff_id']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
    
    
    public function loan_application_report($req){
        $params = $req->output_parameters();
        
        $resp = null;
        if(isset($params[0]['staff_id'])){
            $resp = $this->Staffapp_model->loan_application_report($params[0]['staff_id']);
        }else{
            $row = json_decode($params[0],true);
            $resp = $this->Staffapp_model->loan_application_report($row['staff_id']);
        }
        
        if($resp){
            return $this->xmlrpc->send_response($resp);
        }else{
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        } 
    }
}
        