<?php
/****************************
 * File : user.php
 * Author : Cygnusa
 * @2018
 ****************************/

//SECURITY
if (!defined('APPLICATION_PATH')) {
    exit('No direct script access allowed');
}

//FUNCTION LISTS

class user
{
    private $user_fields;
    private $profile_path = "../../" . S3_PROFILE_IMAGE_URL;

    public function __construct($parent)
    {
        $this->user_fields = " `smpin`, `emp_code`,   `fname`, `lname`, `emailid`, `mobileno`, `role`,`dealer_code`, `address_1`, `address_2`, `address_3`,  `profile_photo`, `dob`,`doj`, `country`, `language`, `is_married`, `marriage_date`,   `last_password_change`, `is_first_login`, `is_profile_updated`, `mpin`,`epin` , `created_on`, `modified_on`";

        //GET PAGE MESSAGE
        $this->page_data = globalfunc::getMessage($parent, $parent->lang, 'user');
        if (count($parent->params) == 0) {
            $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
        } else {
            $qry_params = explode("?", $parent->params[0]);
            $parameter_val = strtolower($qry_params[0]);

            if (strtolower($parent->device_type) == 'app') {

                switch ($parameter_val) {
                    case "signup":
                    case "signup_otp":
                    case "otp_validation":
                    case "signin":
                    case "forgot_password":
                    case "change_password":
                    //case "check_token":
                    case "devicetoken":
                    case "reset_password":
                    case "profile":
                    case "profileimage":

                    case "resetuser":
                    case "resetfirstlogin":
                    case "getuserdetails":
                    case "userpasswordrest123":
                    case "mpin_otp":
                    case "epin_otp":
                    case 'acceptleadmodule':

                        $this->$parameter_val($parent);
                        break;

                    default:
                        $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
                        break;
                }
            } else if (strtolower($parent->device_type) == 'web') {
                switch ($parameter_val) {
                    case "signup":
                    case "signup_otp":
                    case "otp_validation":
                    case "signin":
                    case "forgot_password":
                    case "change_password":
                    //case "check_token":
                    case "devicetoken":
                    case "reset_password":
                    case "profile":
                    case "profileimage":

                    case "resetuser":
                    case "resetfirstlogin":
                    case "getuserdetails":
                    case "userpasswordrest123":
                    case "mpin_otp":
                    case "epin_otp":
                    case 'acceptleadmodule':

                        $this->$parameter_val($parent);
                        break;

                    default:
                        $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
                        break;
                }
            }

        }
        $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
    }

    /* /////////////////////////////// USER SIGNUP //////////////////////////////////////////////// */
    private function signup($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        $REQUEST_KEYS = array('username' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $username = escape_str($parent->_request['username']);

        //CHECK USER ALREADY REGISTER
        $query_getuserarr = $parent->selectQuery(TABLE_USER, "userid", " (emp_code='" . $username . "' OR smpin='" . $username . "' ) AND is_deleted=0 AND role>2", "1");
        if ($query_getuserarr['nr'] > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error8']), 406);
        }

        //CHECK IN EMPLOYEE TABLE
        $query_emparr = $parent->selectQuery(DMS_EMPLOYEE, "*", "smpin='" . $username . "' AND  status='1' ", "1");

        if (!$query_emparr['nr'] > 0) {
            //CHECK IN HOUSER
            $query_houserarr = $parent->selectQuery(DMS_HOUSER, "*", "emp_code='" . $username . "' AND status='1'", "1");
            if (!$query_houserarr['nr'] > 0) {
                //NOT FOUND IN EMPLOYEE AND HO USER
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
            } else {
                //HO USER FOUND
                $parent->executeQuery("DELETE FROM " . TABLE_TEMP_REGISTRATION . " WHERE emp_code='" . $username . "'");
                $insert_temp_str = "INSERT INTO " . TABLE_TEMP_REGISTRATION . "(`smpin`, `emp_code`, `mobile`,  `created_on`) VALUES ('','" . $username . "','" . $query_houserarr['result'][0]->mobile . "', '" . NOW . "')";
                $parent->executeQuery($insert_temp_str);
                $query_houserarr['result'][0]->username = strtolower($username);

                $parent->response(array('response' => 'success', 'data' => $query_houserarr['result'][0], 'message' => $this->page_data['success1']), 200);
            }
        } else {
            //EMPLOYEE FOUND

            //DELETE OLD TEMP REGISTER
            $parent->executeQuery("DELETE FROM " . TABLE_TEMP_REGISTRATION . " WHERE smpin='" . $username . "'");
            //INSERT IN TEMP REGISTRATION
            $insert_temp_str = "INSERT INTO " . TABLE_TEMP_REGISTRATION . "(`smpin`, `emp_code`, `mobile`,`created_on`) VALUES ('" . $username . "','','" . $query_emparr['result'][0]->mobile . "','" . NOW . "')";
            $parent->executeQuery($insert_temp_str);

            $query_emparr['result'][0]->username = strtolower($username);
            $parent->response(array('response' => 'success', 'data' => $query_emparr['result'][0], 'message' => $this->page_data['success1']), 200);
        }

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// SEND OTP  //////////////////////////////////////////////// */
    private function signup_otp($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);

        $REQUEST_KEYS = array('username' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $username = escape_str($parent->_request['username']);
        $device_type = escape_str($parent->_request['device_type']);
        $hashkey = escape_str($parent->_request['hashkey']);

        $query_temp_user = $parent->selectQuery(TABLE_TEMP_REGISTRATION, "*", "smpin='" . $username . "' OR emp_code='" . $username . "'", "1");
        if ($query_temp_user['nr'] > 0) {
            $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
            $otp = globalfunc::generateOTP();

            $update_temp_str = "UPDATE " . TABLE_TEMP_REGISTRATION . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "'  WHERE tid='" . $query_temp_user['result'][0]->tid . "'";
            $parent->executeQuery($update_temp_str);

            //SEND SMS
            $parent->current_smpin = $query_temp_user['result'][0]->smpin;
            $parent->current_emp_code = $query_temp_user['result'][0]->emp_code;

//            if($device_type=='android')
            //            {
            //                $sms_message='<#>HYPER-M One Time Password (OTP) to register is '.$otp.'. Valid for 1 minute '.$hashkey;
            //            }else
            //            {
            //                $sms_message='Dear User, Your One Time Password (OTP) to register with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
            //            }
            //$sms_message='Dear User, Your One Time Password (OTP) to register with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
            $sms_message = "Your one time password is " . $otp;
            // globalfunc::sendSMS($parent, $query_temp_user['result'][0]->mobile, $sms_message);

            $parent->response(array('response' => 'success', 'data' => array('otp_expiry' => OTP_EXPIRY_MIN), 'message' => $this->page_data['success1']), 200);
        } else {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
        }

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// SEND OTP  //////////////////////////////////////////////// */
    private function mpin_otp($parent)
    {
        globalfunc::checkMethodType($parent, array('GET', 'POST'));

        switch ($parent->get_request_method()) {
            case "GET":
                globalfunc::checkParamCount($parent, 1);
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
                $otp = globalfunc::generateOTP();

                $update_user_str = "UPDATE " . TABLE_USER . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "' WHERE userid='" . $parent->current_user_id . "'";
                $parent->executeQuery($update_user_str);

                //$sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                $sms_message = "Your one time password is " . $otp;
                // globalfunc::sendSMS($parent, $parent->current_user_data->mobileno, $sms_message);

                $parent->response(array('response' => 'success', 'data' => array('otp_expiry' => OTP_EXPIRY_MIN), 'message' => $this->page_data['success1']), 200);
                break;
            case "POST":
                globalfunc::checkParamCount($parent, 1);
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $device_type = escape_str($parent->_request['device_type']);
                $hashkey = escape_str($parent->_request['hashkey']);

                $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
                $otp = globalfunc::generateOTP();

                $update_user_str = "UPDATE " . TABLE_USER . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "' WHERE userid='" . $parent->current_user_id . "'";
                $parent->executeQuery($update_user_str);

//                if($device_type=='android')
                //                {
                //                    $sms_message='<#>HYPER-M One Time Password (OTP) to change mpin is '.$otp.'. Valid for 1 minute '.$hashkey;
                //                }else
                //                {
                //                    $sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                //                }
                //$sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                $sms_message = "Your one time password is " . $otp;
                // globalfunc::sendSMS($parent, $parent->current_user_data->mobileno, $sms_message);

                $parent->response(array('response' => 'success', 'data' => array('otp_expiry' => OTP_EXPIRY_MIN), 'message' => $this->page_data['success1']), 200);
                break;
        }

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// SEND OTP  //////////////////////////////////////////////// */
    private function epin_otp($parent)
    {
        globalfunc::checkMethodType($parent, array('GET', 'POST'));

        switch ($parent->get_request_method()) {
            case "GET":
                globalfunc::checkParamCount($parent, 1);
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
                $otp = globalfunc::generateOTP();

                $update_user_str = "UPDATE " . TABLE_USER . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "' WHERE userid='" . $parent->current_user_id . "'";
                $parent->executeQuery($update_user_str);

                //$sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                $sms_message = "Your one time password is " . $otp;
                // globalfunc::sendSMS($parent, $parent->current_user_data->mobileno, $sms_message);

                $parent->response(array('response' => 'success', 'data' => array('otp_expiry' => OTP_EXPIRY_MIN), 'message' => $this->page_data['success1']), 200);
                break;
            case "POST":
                globalfunc::checkParamCount($parent, 1);
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $device_type = escape_str($parent->_request['device_type']);
                $hashkey = escape_str($parent->_request['hashkey']);

                $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
                $otp = globalfunc::generateOTP();

                $update_user_str = "UPDATE " . TABLE_USER . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "' WHERE userid='" . $parent->current_user_id . "'";
                $parent->executeQuery($update_user_str);

//                if($device_type=='android')
                //                {
                //                    $sms_message='<#>HYPER-M One Time Password (OTP) to change mpin is '.$otp.'. Valid for 1 minute '.$hashkey;
                //                }else
                //                {
                //                    $sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                //                }
                //$sms_message='Dear User, Your One Time Password (OTP) to change mpin with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
                $sms_message = "Your one time password is " . $otp;
                // globalfunc::sendSMS($parent, $parent->current_user_data->mobileno, $sms_message);

                $parent->response(array('response' => 'success', 'data' => array('otp_expiry' => OTP_EXPIRY_MIN), 'message' => $this->page_data['success1']), 200);
                break;
        }

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER OTP VALIDATION //////////////////////////////////////////////// */
    private function otp_validation($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        $REQUEST_KEYS = array('otp' => 'string', 'type' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        $otp = escape_str($parent->_request['otp']);
        $type = escape_str($parent->_request['type']);

        switch ($type) {
            case "register":
                $REQUEST_KEYS = array('username' => 'string');
                globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                $username = escape_str($parent->_request['username']);

                //CHECK IN TEMP REGISTER
                $query_check_temparr = $parent->selectQuery(TABLE_TEMP_REGISTRATION, "*", " (smpin='" . $username . "' OR emp_code='" . $username . "')", "1");
                if (!$query_check_temparr['nr'] > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
                }

                //CHECK OTP
                if ($query_check_temparr['result'][0]->otp != $otp) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error11']), 406);
                }

                if (!(strtotime($query_check_temparr['result'][0]->otp_expiry) > strtotime(NOW))) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error12']), 406);
                }

                //CHECK USER ALREADY REGISTER
                $query_checkuserarr = $parent->selectQuery(TABLE_USER, "userid", " (emp_code='" . $username . "' OR smpin='" . $username . "' ) AND is_deleted=0 AND role>2", "1");
                if ($query_checkuserarr['nr'] > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error8']), 406);
                }

                //GENERATE TOKEN
                $access_token = globalfunc::generateToken($parent, 'access', 128);
                $update_temp_str = "UPDATE " . TABLE_TEMP_REGISTRATION . " SET `auth_token`='" . $access_token . "'  WHERE tid='" . $query_check_temparr['result'][0]->tid . "'";
                $parent->executeQuery($update_temp_str);

                $parent->response(array('response' => 'success', 'access_token' => $access_token, 'message' => $this->page_data['success1']), 200);
                break;

            case "forgot_password":
                $REQUEST_KEYS = array('username' => 'string', 'otp' => 'string');
                globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                $username = escape_str($parent->_request['username']);
                $otp = escape_str($parent->_request['otp']);

                //CHECK USER EXIST
                $query_checkuserarr = $parent->selectQuery(TABLE_USER, "*", " (emp_code='" . $username . "' OR smpin='" . $username . "' ) AND is_deleted=0 AND role>2", "1");
                if (!$query_checkuserarr['nr'] > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
                }

                //CHECK OTP AND EXIPRY
                if ($query_checkuserarr['result'][0]->otp != $otp) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error11']), 406);
                }

                if (!(strtotime($query_checkuserarr['result'][0]->otp_expiry) > strtotime(NOW))) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error12']), 406);
                }

                //GET USER DATA
                $current_user_data = $this->getuserdata($parent, $query_checkuserarr['result'][0]->userid);

                //GENERATE TOKEN
                $access_token = globalfunc::generateToken($parent, 'access', 128);
                $parent->executeQuery(" UPDATE " . TABLE_USER . " SET auth_token='" . $access_token . "',otp='',otp_expiry='0000-00-00 00:00:00'  WHERE userid='" . $query_checkuserarr['result'][0]->userid . "'");

                //LOG
                $logstr = 'FORGOT PASSWORD UPDATE';
                $parent->current_user_id = $userid;
                globalfunc::insertUserLog($parent, $logstr);

                $parent->response(array('response' => 'success', 'access_token' => $access_token, 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);
                break;
            case "mpin":
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $REQUEST_KEYS = array('otp' => 'string');
                globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                //CHECK USER EXIST
                $query_checkuserarr = $parent->selectQuery(TABLE_USER, "*", " userid='" . $parent->current_user_id . "' AND is_deleted=0 AND role>2", "1");
                if (!$query_checkuserarr['nr'] > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
                }

                //CHECK OTP AND EXIPRY
                if ($query_checkuserarr['result'][0]->otp != $otp) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error11']), 406);
                }

                if (!(strtotime($query_checkuserarr['result'][0]->otp_expiry) > strtotime(NOW))) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error12']), 406);
                }

                $parent->executeQuery(" UPDATE " . TABLE_USER . " SET otp='',otp_expiry='0000-00-00 00:00:00'  WHERE userid='" . $query_checkuserarr['result'][0]->userid . "'");

                //LOG
                $logstr = 'MPIN OTP';
                globalfunc::insertUserLog($parent, $logstr);

                //GET USER DATA
                $current_user_data = $this->getuserdata($parent, $parent->current_user_id);

                $parent->response(array('response' => 'success', 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);

                break;
            case "epin":
                globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                $REQUEST_KEYS = array('otp' => 'string');
                globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                //CHECK USER EXIST
                $query_checkuserarr = $parent->selectQuery(TABLE_USER, "*", " userid='" . $parent->current_user_id . "' AND is_deleted=0 AND role>2", "1");
                if (!$query_checkuserarr['nr'] > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error9']), 406);
                }

                //CHECK OTP AND EXIPRY
                if ($query_checkuserarr['result'][0]->otp != $otp) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error11']), 406);
                }

                if (!(strtotime($query_checkuserarr['result'][0]->otp_expiry) > strtotime(NOW))) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error12']), 406);
                }

                $parent->executeQuery(" UPDATE " . TABLE_USER . " SET otp='',otp_expiry='0000-00-00 00:00:00'  WHERE userid='" . $query_checkuserarr['result'][0]->userid . "'");

                //LOG
                $logstr = 'EPIN OTP';
                globalfunc::insertUserLog($parent, $logstr);

                //GET USER DATA
                $current_user_data = $this->getuserdata($parent, $parent->current_user_id);

                $parent->response(array('response' => 'success', 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);

                break;
            default:
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error10']), 406);
                break;
        }

        $parent->response(array('response' => 'success', 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER SIGN IN //////////////////////////////////////////////// */
    private function signin($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        $REQUEST_KEYS = array('username' => 'string', 'password' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $username = escape_str($parent->_request['username']);
        $password = escape_str($parent->_request['password']);

        //CHECK USER EXIST
        $query_getuserarr = $parent->selectQuery(TABLE_USER, "userid,role,status", " ((smpin='" . $username . "' AND role in (8,9,10,11,12,13,15)) OR (emp_code='" . $username . "' AND ( role<8 OR role='14' OR role='16' OR role='17'))) AND is_deleted=0 AND role>2", "1");
        if (!$query_getuserarr['nr'] > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error3']), 406);
        }

        if ($query_getuserarr['nr'] > 0 && $query_getuserarr['result'][0]->status != '1') {

            $msg = ($query_getuserarr['result'][0]->role > 7 && $query_getuserarr['result'][0]->role < 14) ? 'error14' : 'error16';
            $parent->response(array('response' => 'failed', 'message' => $this->page_data[$msg]), 406);
        }

        $userid = $query_getuserarr['result'][0]->userid;
        if ($parent->device_type != 'app' && in_array($parent->current_user_role, array(16, 17))) {
            $parent->response(array('response' => 'failed', 'message' => "Sorry Invalid Device Access"), 406);
        }

        //INVALID PASSWORD
        $query_password_validation = $parent->selectQuery(TABLE_USER, 'userid,' . $this->user_fields, "userid='" . $userid . "' AND ( password = '" . md5($password) . "') AND status=1 AND is_deleted=0 AND role>2", "1");
        if (!$query_password_validation['nr'] > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

        //GENERATE TOKEN
        $access_token = globalfunc::generateToken($parent, 'access', 128);
        $parent->executeQuery(" UPDATE " . TABLE_USER . " SET auth_token='" . $access_token . "'  WHERE userid='" . $userid . "'");

        //USER LOG    UPDATE
        $parent->current_user_id = $query_password_validation['result'][0]->userid;
        $parent->current_user_data = $query_password_validation['result'][0];
        $parent->current_user_data->access_token = $access_token;

        //LOG
        $logstr = 'SIGNIN';
        globalfunc::insertUserLog($parent, $logstr);

        $current_user_data = $this->getuserdata($parent, $parent->current_user_id);

        //MIN REDEEM
        $data['setting'] = 'REDEEM';
        $data = globalfunc::getSiteSetting($parent, $data);
        $min_redeem_point = $data->REDEEM_MIN_VALUE;
        $current_user_data->min_redeem_point = $min_redeem_point;

        $parent->response(array('response' => 'success', 'access_token' => $access_token, 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER CHANGE PASSWORD//////////////////////////////////////// */
    private function change_password($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));

        globalfunc::checkParamCount($parent, 1);
        $REQUEST_KEYS = array('new_password' => 'string', 'type' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $new_password = $parent->_request['new_password'];
        $type = escape_str($parent->_request['type']);
        if ($type == 'reset') {
            $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'set_password'));
        } else if ($type == 'old') {
            $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));

            $REQUEST_KEYS = array('current_password' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

            $current_password = $parent->_request['current_password'];
            if ($new_password == $current_password) {
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error15']), 401);
            }

            $query_getuserarr = $parent->selectQuery(TABLE_USER, "userid", " userid='" . $parent->current_user_id . "' AND password='" . md5($current_password) . "' AND status=1 AND is_deleted=0 AND role>2", "1");

            if (!$query_getuserarr['nr'] > 0) {
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error7']), 406);
            }

        } else if ($type == 'register') {
            $access_token = globalfunc::getAuthrozation($parent);

            $query_check_temparr = $parent->selectQuery(TABLE_TEMP_REGISTRATION, "*", " auth_token='" . $access_token . "'", "1");
            if (!$query_check_temparr['nr'] > 0) {
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error13']), 401);
            }

            $REQUEST_KEYS = array('username' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $username = escape_str($parent->_request['username']);

            //VALIDATE PASSWORD
            globalfunc::passwordPolicy($parent, array('password' => $new_password));

            $doj = '0000-00-00';

            //EMPLOYEE
            $query_emparr = $parent->selectQuery(DMS_EMPLOYEE, "*", "smpin='" . $username . "' AND status='1'  ", "1");
            if ($query_emparr['nr'] > 0) {
                $smpin = $query_emparr['result'][0]->smpin;
                $emp_code = $query_emparr['result'][0]->emp_code;
                $fname = $query_emparr['result'][0]->fname;
                $lname = $query_emparr['result'][0]->lname;
                $emailid = $query_emparr['result'][0]->emailid;
                $mobileno = $query_emparr['result'][0]->mobile;
                //GET USER ROLE ID
                $role = projectfunc::getUserRoleId($parent, $query_emparr['result'][0]->role);
                $dealer_code = $query_emparr['result'][0]->dealer_code;
                $address_1 = $query_emparr['result'][0]->address_1;
                $address_2 = $query_emparr['result'][0]->address_2;
                $address_3 = $query_emparr['result'][0]->address_3;
                $doj = $query_emparr['result'][0]->date_of_join;
                $segment = $query_emparr['result'][0]->segment;
                //DELETE TEMP REGISTER
                $parent->executeQuery("DELETE FROM " . TABLE_TEMP_REGISTRATION . " WHERE smpin='" . $username . "'");

            } else {

                $query_houserarr = $parent->selectQuery(DMS_HOUSER, "*", "emp_code='" . $username . "' AND status='1'", "1");
                if ($query_houserarr['nr'] > 0) {
                    $smpin = '';
                    $emp_code = $query_houserarr['result'][0]->emp_code;
                    $fname = $query_houserarr['result'][0]->fname;
                    $lname = $query_houserarr['result'][0]->lname;
                    $emailid = $query_houserarr['result'][0]->emailid;
                    $mobileno = $query_houserarr['result'][0]->mobile;
                    //GET USER ROLE ID
                    $role = projectfunc::getUserRoleId($parent, $query_houserarr['result'][0]->role);
                    $dealer_code = $query_houserarr['result'][0]->dealer_code;
                    $address_1 = $query_houserarr['result'][0]->address_1;
                    $address_2 = $query_houserarr['result'][0]->address_2;
                    $address_3 = $query_houserarr['result'][0]->address_3;
                    $segment = 0;
                    //DELETE TEMP REGISTER
                    $parent->executeQuery("DELETE FROM " . TABLE_TEMP_REGISTRATION . " WHERE emp_code='" . $username . "'");

                } else {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data['error8']), 406);
                }
            }
            $password = md5(globalfunc::generateToken($parent, '', 10));
            $country = '105';
            $language = 'en_us';

            //GENERATE TOKEN
            $access_token = globalfunc::generateToken($parent, 'access', 128);

            //INSERT IN USER
            $insert_user_str = "INSERT INTO " . TABLE_USER . "(`smpin`, `emp_code`, `password`, `fname`, `lname`, `emailid`, `mobileno`, `role`, `doj`, `segment`, `dealer_code`, `address_1`, `address_2`, `address_3`, `auth_token`,`country`,   `language`, `status`,`is_first_login`, `is_deleted`, `created_on`, `modified_on`) VALUES ('" . $smpin . "','" . $emp_code . "','" . md5($new_password) . "','" . $fname . "','" . $lname . "','" . $emailid . "','" . $mobileno . "','" . $role . "','" . $doj . "','" . $segment . "','" . $dealer_code . "','" . $address_1 . "','" . $address_2 . "','" . $address_3 . "','" . $access_token . "','" . $country . "', 'en_us','1','0','0','" . NOW . "','" . NOW . "')";
            $parent->executeQuery($insert_user_str);
            $userid = $parent->lastInsertId();

            $query_userarr = $parent->selectQuery(TABLE_USER, "*", "userid='" . $userid . "'", "1");

            //INSERT LEARNING FOR DSE
            if ($query_emparr['nr'] > 0) {
                //CHECK LEARNING EXIST
                $query_learning = $parent->selectQuery(TABLE_USER_LEARNING, "*", "smpin='" . $smpin . "'", "1");
                if (!($query_learning['nr'] > 0)) {
                    $insert_learn_str = "INSERT INTO " . TABLE_USER_LEARNING . "( `smpin`, `lvl_id`, `status`, `current_status`, `created_on`, `modified_on`) VALUES ('" . $smpin . "','1','0','1','" . NOW . "','" . NOW . "')";
                    $parent->executeQuery($insert_learn_str);
                }

                //CREATE USER TABLES
                projectfunc::userTableCreate($parent, $smpin);

                //NEW CONDITION POINTS FOR FIRST 500 USER ONLY
                $query_countuserarr = $parent->selectQuery(TABLE_USER, "COUNT(userid) as ucount", "role in (8,9,10,11,12,13) ", "1");
                if ($query_countuserarr['nr'] && $role != 15) //NO POINTS FOR INTERNATIONAL
                {
                    if ($query_countuserarr['result'][0]->ucount < REGISTER_REWARD_LIMIT) {
                        //ADD REWARD POINTS
                        $obj = new stdClass();
                        $obj->type = "registration";
                        $obj->type_id = "0";
                        $obj->description = "Points for Login Registration";
                        $obj->points = REWARD_POINT_ONE;
                        projectfunc::userAddPoints($parent, $obj, $userid);
                    } else {
                        //ADD REWARD POINTS
                        $obj = new stdClass();
                        $obj->type = "registration";
                        $obj->type_id = "0";
                        $obj->description = "Points for Login Registration";
                        $obj->points = REWARD_POINT_TWO;
                        projectfunc::userAddPoints($parent, $obj, $userid);
                    }
                }
            }

            //LOG
            $logstr = 'REGISTER';
            $parent->current_user_id = $userid;
            globalfunc::insertUserLog($parent, $logstr);
            $current_user_data = $this->getuserdata($parent, $parent->current_user_id);

            $parent->response(array('response' => 'success', 'access_token' => $access_token, 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);

        } else {

            $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        }

        //VALIDATE PASSWORD
        globalfunc::passwordPolicy($parent, array('password' => $new_password));
        //UPDATE PASSWORD
        globalfunc::updatePassword($parent, array('password' => $new_password, 'current_password' => $current_password, 'userid' => $parent->current_user_id, 'type' => 'change'));

        if ($type == 'reset') {
            //LOG
            $logstr = 'RESET CHANGE PASSWORD';
            globalfunc::insertUserLog($parent, $logstr);

            //REST USER TOKEN
            $parent->executeQuery(" UPDATE " . TABLE_USER . " SET valid_token='', validate_time='0000-00-00 00:00:00' WHERE userid='" . $parent->current_user_id . "'");
        } else {
            //LOG
            $logstr = 'CHANGE PASSWORD';
            globalfunc::insertUserLog($parent, $logstr);
        }

        if ($parent->current_user_data->is_first_login == '1') {
            //FIRST TIME CHANGE PASSWORD
            $parent->executeQuery("UPDATE " . TABLE_USER . " SET  is_first_login='0' WHERE userid='" . $parent->current_user_id . "'");
        }

        $parent->response(array('response' => 'success', 'message' => $this->page_data['success2']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DEVICETOKEN ////////////////////////////////////////// */
    private function devicetoken($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $REQUEST_KEYS = array('token' => 'string', 'devicetype' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        $token = escape_str($parent->_request['token']);
        $deviceid = escape_str($parent->_request['deviceid']);
        $devicetype = strtolower(escape_str($parent->_request['devicetype']));
        $model = escape_str($parent->_request['model']);
        $manufacture = escape_str($parent->_request['manufacture']);
        $version = escape_str($parent->_request['version']);
        $client_ip = globalfunc::getIp($parent, $data = array());

        $user_device = $parent->selectQuery(TABLE_USER_DEVICES, "*", "userid='" . $parent->current_user_id . "' AND is_deleted='0'", "1");
        if ($user_device['nr']) {
            //REMOVE DEVICE OF PREV USER
            $update_prev_str = "UPDATE  " . TABLE_USER_DEVICES . " SET is_deleted='1' WHERE token='" . $token . "' AND id!='" . $user_device['result'][0]->id . "'";
            $parent->executeQuery($update_prev_str);

            $update_token_str = "UPDATE " . TABLE_USER_DEVICES . " SET  `devicetype`='" . $devicetype . "',`token`='" . $token . "',`android_id`='" . $deviceid . "',`model`='" . $model . "',`manufacture`='" . $manufacture . "',`version`='" . $version . "',`client_ip`='" . $client_ip . "',`modified_on`='" . NOW . "',is_deleted='0' WHERE id='" . $user_device['result'][0]->id . "'";
            $user_device_res = $parent->executeQuery($update_token_str);
        } else {
            $insert_token_str = "INSERT INTO " . TABLE_USER_DEVICES . "(`userid`, `devicetype`, `token`, `android_id`, `model`, `manufacture`, `version`, `client_ip`, `created_on`, `modified_on`) VALUES ('" . $parent->current_user_id . "','" . $devicetype . "','" . $token . "','" . $deviceid . "','" . $model . "','" . $manufacture . "','" . $version . "','" . $client_ip . "','" . NOW . "','" . NOW . "')";
            $user_device_res = $parent->executeQuery($insert_token_str);

        }
        //LOG
        $logstr = 'DEVICE TOKEN';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'message' => $this->page_data['success1']), 200);
    }
    /* ////////////////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER FORGOT PASSWORD////////////////////////////////////////////// */
    private function forgot_password($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);

        $REQUEST_KEYS = array('username' => 'string');
        globalfunc::checkAnyKey($parent, $REQUEST_KEYS);

        $username = escape_str($parent->_request['username']);
        $device_type = escape_str($parent->_request['device_type']);
        $hashkey = escape_str($parent->_request['hashkey']);

        $query_getuserarr = $parent->selectQuery(TABLE_USER, "*", " (smpin='" . $username . "' OR emp_code='" . $username . "') AND is_deleted=0  ", "1");
        if ($query_getuserarr['nr'] <= 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error3']), 406);
        }

        if ($query_getuserarr['result'][0]->status != 1) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error14']), 406);
        }

        $userid = $query_getuserarr['result'][0]->userid;

        //SEND SMS
        $parent->current_user_id = $query_getuserarr['result'][0]->userid;
        $parent->current_smpin = $query_getuserarr['result'][0]->smpin;
        $parent->current_emp_code = $query_getuserarr['result'][0]->emp_code;

        //OTP UPDATE
        $otp_expiry = globalfunc::generateExpiryTime($parent, OTP_EXPIRY_MIN);
        $otp = globalfunc::generateOTP();

        $update_update_str = "UPDATE  " . TABLE_USER . " SET `otp`='" . $otp . "', `otp_expiry`='" . $otp_expiry . "' WHERE userid='" . $userid . "'";
        $parent->executeQuery($update_update_str);

//        if($device_type=='android')
        //        {
        //            $sms_message='<#>HYPER-M One Time Password (OTP) to reset password is '.$otp.'. Valid for 1 minute '.$hashkey;
        //        }else{
        //            $sms_message='Dear User, Your One Time Password (OTP) to reset password with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
        //        }
        //$sms_message='Dear User, Your One Time Password (OTP) to reset password with HYPER-M is '.$otp.' and is valid for 1 minute. Thank you, Team HYPER-M';
        $sms_message = "Your one time password is " . $otp;
        // globalfunc::sendSMS($parent, $query_getuserarr['result'][0]->mobileno, $sms_message);

        //LOG
        $logstr = 'FORGOT PASSWORD OTP';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $query_getuserarr['result'][0]->mobileno, 'otp_expiry' => OTP_EXPIRY_MIN, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER PROFILE //////////////////////////////////////////////// */
    private function profile($parent)
    {

        globalfunc::checkMethodType($parent, array('GET', 'POST'));
        switch ($parent->get_request_method()) {
            case "GET":
                globalfunc::checkParamCount($parent, 1);
                $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));

                //LOG
                $logstr = 'PROFILE';
                globalfunc::insertUserLog($parent, $logstr);

                $current_user_data = $this->getuserdata($parent, $parent->current_user_id);
                //MIN REDEEM
                $data['setting'] = 'REDEEM';
                $data = globalfunc::getSiteSetting($parent, $data);

                $min_redeem_point = $data->REDEEM_MIN_VALUE;
                $current_user_data->min_redeem_point = $min_redeem_point;

                $parent->response(array('response' => 'success', 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);
                break;
            case "POST":
                globalfunc::checkParamCount($parent, 1);
                $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));
                $REQUEST_KEYS = array('dob' => 'date|Y-m-d', 'email' => 'string');
                globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                //$fname=escape_str($parent->_request['fname']);
                //$lname=escape_str($parent->_request['lname']);
                //$mobileno=escape_str($parent->_request['mobileno']);
                $dob = escape_str($parent->_request['dob']);
//                $doj=escape_str($parent->_request['doj']);
                //                if(isset($parent->_request['doj']) && $parent->_request['doj']!='' && $parent->_request['doj']!='0000-00-00' )
                //                {
                //                    $REQUEST_KEYS=array('doj'=>'date|Y-m-d');
                //                    globalfunc::mandatoryKey($parent,$REQUEST_KEYS);
                //                }

                $email = escape_str($parent->_request['email']);

                $is_married = (escape_str($parent->_request['is_married']) == '1') ? '1' : '0';
                $marriage_date = ($is_married == '1' && escape_str($parent->_request['marriage_date'])) ? escape_str($parent->_request['marriage_date']) : '0000-00-00';

                //CHECK FILE
                if (isset($_FILES['file']) && $_FILES['file']['name']) {
                    //CONNECT S3
                    $s3 = awsfileserver::connectS3();
                    //IMAGE UPLOAD
                    $obj = new stdClass();
                    $obj->filekey = 'file';
                    $obj->size = '10'; //IN MB
                    $obj->type = array('jpg', 'jpeg', 'png');
                    globalfunc::validateFile($parent, $obj);
                    $obj->path = $this->profile_path;
                    $obj->filename = 'profile_' . $parent->current_user_id . '_' . date('Ymd') . uniqid();
                    if (isset($parent->_request['width'])) {
                        $obj->width = $parent->_request['width'];
                    }

                    if (isset($parent->_request['height'])) {
                        $obj->height = $parent->_request['height'];
                    }

                    $obj->cord = (isset($parent->_request['cord'])) ? $parent->_request['cord'] : null;
                    $fileName = globalfunc::uploadFile($parent, $obj);
                    awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $fileName, dirname(__FILE__) . "/../../../" . S3_PROFILE_IMAGE_URL . $fileName);

                    //UNLINK
                    @unlink(dirname(__FILE__) . '/../' . $this->profile_path . $fileName);
                    $econd = " ,profile_photo='" . $fileName . "'";
                }
                //$parent->executeQuery(" UPDATE ".TABLE_USER." SET fname='".$fname."',lname='".$lname."',mobileno='".$mobileno."',modified_on='".NOW."' ".$econd." WHERE userid='".$parent->current_user_id."'");
                $emailcond = '';
                if ($parent->current_user_role > 7) {
                    $emailcond = " ,emailid='" . $email . "'";
                }
                $parent->executeQuery(" UPDATE " . TABLE_USER . " SET  dob='" . $dob . "',is_married='" . $is_married . "', marriage_date='" . $marriage_date . "',is_profile_updated='1' " . $econd . $emailcond . " WHERE userid='" . $parent->current_user_id . "'");

                //LOG
                $logstr = 'PROFILE UPDATE';
                globalfunc::insertUserLog($parent, $logstr);
                $current_user_data = $this->getuserdata($parent, $parent->current_user_id);
                $parent->response(array('response' => 'success', 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);
                break;
        }

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER PROFILE IMAGE //////////////////////////////////////////////// */
    private function profileimage($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //CHECK FILE
        if (!isset($_FILES['file'])) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error5']), 406);
        }

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        //IMAGE UPLOAD
        $obj = new stdClass();
        $obj->filekey = 'file';
        $obj->size = '10'; //IN MB
        $obj->type = array('jpg', 'jpeg', 'png');
        globalfunc::validateFile($parent, $obj);
        $obj->path = $this->profile_path;
        $obj->filename = 'profile_' . $parent->current_user_id . '_' . date('Ymd') . "_" . uniqid();
        if (isset($parent->_request['width'])) {
            $obj->width = $parent->_request['width'];
        }

        if (isset($parent->_request['height'])) {
            $obj->height = $parent->_request['height'];
        }

        $obj->cord = (isset($parent->_request['cord'])) ? $parent->_request['cord'] : null;
        $fileName = globalfunc::uploadFile($parent, $obj);
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $fileName, dirname(__FILE__) . "/../../../" . S3_PROFILE_IMAGE_URL . $fileName);

        //UNLINK
        @unlink(dirname(__FILE__) . '/../' . $this->profile_path . $fileName);
        $parent->executeQuery(" UPDATE " . TABLE_USER . " SET profile_photo='" . $fileName . "',modified_on='" . NOW . "' WHERE userid='" . $parent->current_user_id . "'");

        $current_user_data = $this->getuserdata($parent, $parent->current_user_id);

        //LOG
        $logstr = 'PROFILE IMAGE UPDATE';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $current_user_data, 'message' => $this->page_data['success1']), 200);

    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// USER DATA //////////////////////////////////////////////// */
    private function getuserdata($parent, $userid)
    {
        if (!isset($userid)) {
            globalfunc::callErrorMsg($parent, array('message' => 'user(getuserdata):REQUIRED userid'));
        }

        $query_getuserarr = $parent->selectQuery(TABLE_USER, "  `smpin`, `emp_code`,  `fname`, `lname`, `emailid`, `mobileno`, `role`,   `dealer_code`, `address_1`, `address_2`, `address_3`,  `profile_photo`, `dob`,`doj`, `country`,  `language`, `is_married`, `marriage_date`, `mpin`,epin,  `last_password_change`, `is_first_login`,`is_profile_updated` , `created_on`, `modified_on`", "userid='" . $userid . "' AND is_deleted='0' ", "1");
        if ($query_getuserarr['nr'] <= 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error6']), 406);
        }

        $query_getuserarr['result'][0]->username = ($query_getuserarr['result'][0]->role < 8 || in_array($query_getuserarr['result'][0]->role, array("14", "16", "17"))) ? $query_getuserarr['result'][0]->emp_code : $query_getuserarr['result'][0]->smpin;
        $query_getuserarr['result'][0]->dob_time = ($query_getuserarr['result'][0]->dob == '0000-00-00') ? "0000-00-00 00:00:00" : $query_getuserarr['result'][0]->dob . " 00:00:00";
        $query_getuserarr['result'][0]->doj_time = ($query_getuserarr['result'][0]->doj == '0000-00-00') ? "0000-00-00 00:00:00" : $query_getuserarr['result'][0]->doj . " 00:00:00";
        $query_getuserarr['result'][0]->marriage_date_time = ($query_getuserarr['result'][0]->marriage_date == '0000-00-00') ? "0000-00-00 00:00:00" : $query_getuserarr['result'][0]->marriage_date . " 00:00:00";

        $query_getuserarr['result'][0]->doj = ($query_getuserarr['result'][0]->doj == '0000-00-00') ? '0000-00-00' : $query_getuserarr['result'][0]->doj;

        if ($query_getuserarr['result'][0]->profile_photo != '') {
            //CONNECT S3
            $s3 = awsfileserver::connectS3();
            $query_getuserarr['result'][0]->profile_photo = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_PROFILE_IMAGE_URL . $query_getuserarr['result'][0]->profile_photo);
        } else {
            $query_getuserarr['result'][0]->profile_photo = USER_NOIMAGE_URL;
        }
        //GET ROLE
        $query_rolearr = $parent->selectQuery(TABLE_ROLE, "*", "id='" . $query_getuserarr['result'][0]->role . "' ", "1");
        $query_getuserarr['result'][0]->rolename = ($query_rolearr['nr'] > 0) ? $query_rolearr['result'][0]->rolename : '';

        //GET DELAER
        $query_getuserarr['result'][0]->dealer = array();
        $query_getdealerarr = $parent->selectQuery(DMS_DEALER, "`dealer_code`, `dealer_name`,address,email, `city`, `state`, `zone`, `country`, `phone`, `zm_name`, `rm_name`, `am_name`, `zm_email`, `rm_email`, `am_email`, `zm_mobile`, `rm_mobile`, `am_mobile`,is_enquirymodule", "dealer_code='" . $query_getuserarr['result'][0]->dealer_code . "' ", "1");
        //ENQUIRY
        $query_getuserarr['result'][0]->is_enquirymodule = ($query_getdealerarr['nr'] > 0) ? $query_getdealerarr['result'][0]->is_enquirymodule : 0;
        $query_getuserarr['result'][0]->country = 'India';

        if ($query_getuserarr['result'][0]->role == '5') {
            $query_dataarr = $parent->selectQuery(DMS_HOUSER, "*", "emp_code='" . $query_getuserarr['result'][0]->emp_code . "' ", "1");
            $query_getuserarr['result'][0]->zone = ($query_dataarr['nr'] > 0) ? $query_dataarr['result'][0]->zone : '';
        } else {
            $query_getuserarr['result'][0]->zone = ($query_getdealerarr['nr'] > 0) ? $query_getdealerarr['result'][0]->zone : '';
        }
        $query_getuserarr['result'][0]->state = ($query_getdealerarr['nr'] > 0) ? $query_getdealerarr['result'][0]->state : '';
        $query_getuserarr['result'][0]->city = ($query_getdealerarr['nr'] > 0) ? $query_getdealerarr['result'][0]->city : '';

        $query_getuserarr['result'][0]->dealer = ($query_getdealerarr['nr'] > 0) ? $query_getdealerarr['result'][0] : new stdClass();

        $query_getuserarr['result'][0]->is_password_expired = projectfunc::checkUserPasswordExpiry($parent, $userid);
        if ($query_getuserarr['result'][0]->role > 7 && $query_getuserarr['result'][0]->role < 14) {
            $query_getuserarr['result'][0]->is_redeem_point = (projectfunc::checkValidReedem($parent, $userid)) ? "1" : "0";
            $query_getuserarr['result'][0]->wallet_amount = projectfunc::userWalletamount($parent, $query_getuserarr['result'][0]->smpin);
            $query_getuserarr['result'][0]->unapproved_amount = projectfunc::userUnapprovedamount($parent, $query_getuserarr['result'][0]->smpin);
        } else {
            $query_getuserarr['result'][0]->is_redeem_point = '0';
            $query_getuserarr['result'][0]->wallet_amount = '0';
            $query_getuserarr['result'][0]->unapproved_amount = '0';
        }

        return $query_getuserarr['result'][0];
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// ACCEPT LEAD LOGIN //////////////////////////////////////////////// */
    private function acceptleadmodule($parent)
    {
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkMethodType($parent, array('GET'));
        $user_details = globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('14'));
        $dealer_code = $parent->current_user_data->dealer_code;

        $parent->executeQuery("UPDATE " . DMS_DEALER . " SET  is_enquirymodule='1' WHERE dealer_code='" . $dealer_code . "'");
        $parent->executeQuery("UPDATE " . TABLE_USER . " SET  epin='' WHERE role=14 AND dealer_code='" . $dealer_code . "'");

        //LOG
        $logstr = 'ACCEPT LEAD MODULE';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'message' => "SUCCESS"), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/
    /*                                                                                                                    */
    /*                                                                                                                    */
    /*                                                                                                                    */
    /*                                            FOR DEVELOPMENT TESTING ONLY                                            */
    /*                                                                                                                    */
    /*                                                                                                                    */
    /*                                                                                                                    */
    /* /////////////////////////////////////////////////////////////////////////////////////////////////////////////////*/

    /* /////////////////////////////// RESET FIRST LOGIN //////////////////////////////////////////////// */
    private function resetuser($parent)
    {
        globalfunc::checkParamCount($parent, 2);
        if (IS_LIVE_SERVER) {
            $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
        }
        exit;
        $smpin = escape_str($parent->params[1]);

        if ($smpin) {
            $parent->executeQuery(" DELETE FROM  " . TABLE_USER . " WHERE smpin='" . $smpin . "' OR emp_code='" . $smpin . "'");
        }

        $parent->response(array('response' => 'success', 'message' => "SUCCESS"), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// RESET FIRST LOGIN //////////////////////////////////////////////// */
    private function resetfirstlogin($parent)
    {
        globalfunc::checkParamCount($parent, 2);
        if (IS_LIVE_SERVER) {
            $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
        }
        exit;
        $userid = escape_str($parent->params[1]);
        $parent->executeQuery(" UPDATE " . TABLE_USER . " SET  is_first_login='1' WHERE userid='" . $userid . "'");

        $parent->response(array('response' => 'success', 'message' => "SUCCESS"), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// RESET FIRST LOGIN //////////////////////////////////////////////// */
    private function getuserdetails($parent)
    {
        globalfunc::checkParamCount($parent, 2);
        if (IS_LIVE_SERVER) {
            $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
        }
        exit;
        $userid = escape_str($parent->params[1]);
        $query_getuserarr = $parent->selectQuery(TABLE_USER, "*", "smpin='" . $userid . "' OR emp_code='" . $userid . "'", "1");

        $data = ($query_getuserarr['nr'] > 0) ? $query_getuserarr['result'][0] : null;
        $parent->response(array('response' => 'success', 'data' => $data, 'message' => "SUCCESS"), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// RESET FIRST LOGIN //////////////////////////////////////////////// */
    private function userpasswordrest123($parent)
    {
        globalfunc::checkParamCount($parent, 2);
        if (IS_LIVE_SERVER) {
            $parent->response(array('response' => 'failed', 'message' => 'PAGE NOT FOUND'), 404);
        }
        exit;
        $userid = escape_str($parent->params[1]);
        $parent->executeQuery(" UPDATE " . TABLE_USER . " SET  password='" . md5('123') . "' WHERE userid='" . $userid . "'");

        $parent->response(array('response' => 'success', 'message' => "SUCCESS"), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

}
