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

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

class dashboard
{

    //FUNCTION LISTS
    public function __construct($parent)
    {
        //GET PAGE MESSAGE
        $this->page_data = globalfunc::getMessage($parent, $parent->lang, 'dashboard');
        if (count($parent->params) == 0) {
            $this->dashboardfunc($parent);
        } else {
            $qry_params = explode("?", $parent->params[0]);
            $parameter_val = strtolower($qry_params[0]);

            switch ($parameter_val) {
                case "language":
                case "sales":
                case "learning":
                case "rewardpoints":
                case "redeemhistory":
                case "walletcredit":
                case "walletdebit":
                case "learnpath":
                case "managerdashboard":
                case "dashboardleaderboard":
                case "areamanagerdashboard":
                case "orderhistory":
                case "changempin":
                case "changeepin":
                case "newareamanagerdashboard":
                case "areamanagerdashboardapp":
                case "enquiry":
                case "dealerenquiry":
                case "dealerenquirysales":
                    $this->$parameter_val($parent);
                    break;

                default:

                    $this->dashboardfunc($parent);
                    break;
            }

        }

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

    /* /////////////////////////////// DASHBOARD //////////////////////////////////////////////// */
    private function language($parent)
    {

        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);

        //LOG
        $logstr = 'LANGUAGE';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $get_lang_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DASHBOARD //////////////////////////////////////////////// */
    private function dashboardfunc($parent)
    {

        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $year_data = escape_str($parent->params[0]);
        $last_data = escape_str($parent->params[1]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));
        //LEARNING
        $learning = projectfunc::userLearningLevel($parent, $parent->current_user_id);

        //if($parent->current_user_role>7 && $parent->current_user_role!=14 && $parent->current_user_role!=16)
        if (in_array($parent->current_user_role, $parent->dse_roles)) {
            //CREATE USER TABLES
            projectfunc::userTableCreate($parent, $parent->current_smpin);
        }
        //GET REWARD POINTS
        $query_rewardpointarr = $parent->selectQuery(TABLE_USER_EPOINTS, "*", "smpin='" . $parent->current_smpin . "' ", "1");
        $earning = ($query_rewardpointarr['nr'] > 0) ? (string) $query_rewardpointarr['result'][0]->points : '0';
        //TARGET
        $user_target = projectfunc::userSalesTarget($parent, $parent->current_user_id, '', $year_data, $month_data);
        //ACHIEVED
        $user_achieved = projectfunc::userSalesAchieved($parent, $parent->current_user_id, '', $year_data, $month_data);
        $sales = array('target' => $user_target, 'achieved' => $user_achieved, 'month' => date('M', strtotime('2018-' . $month_data . "-01")), 'year' => $year_data);

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);

        $query_getarr = $parent->selectQuery(DMS_EMPLOYEE_SALES, "*", "smpin='" . $parent->current_smpin . "' ORDER BY created_on DESC", "1");
        $sales_last_update = ($query_getarr['nr'] > 0) ? (string) $query_getarr['result'][0]->created_on : '0000-00-00 00:00:00';

        //REDEEM POINT
        $wallet_amount = '0';
        $unapproved_amount = '0';
        $is_redeem_point = (projectfunc::checkValidReedem($parent)) ? "1" : "0";
        if (in_array($parent->current_user_role, $parent->dse_roles)) {
            $wallet_amount = projectfunc::userWalletamount($parent, $parent->current_smpin);
            $unapproved_amount = projectfunc::userUnapprovedamount($parent, $parent->current_smpin);
        }

        //MIN REDEEM
        $data['setting'] = 'REDEEM';
        $data = globalfunc::getSiteSetting($parent, $data);

        $min_redeem_point = $data->REDEEM_MIN_VALUE;

        $data_arr = array('learning' => $learning, 'earning' => $earning, 'sales' => $sales, 'languages' => $get_lang_arr, 'sales_last_update' => $sales_last_update, 'is_redeem_point' => $is_redeem_point, 'wallet_amount' => $wallet_amount, 'min_redeem_point' => $min_redeem_point, 'unapproved_amount' => $unapproved_amount);

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

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

    /* /////////////////////////////// SALES //////////////////////////////////////////////// */
    private function sales($parent)
    {
        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));
        $data_arr = array();

        //GET MODEL
        $model_cond = ($parent->current_user_role > 7 && $parent->current_user_role < 14) ? " AND user_type=0 " : " AND user_type=1 ";

        //GET SEGMENT
        //$segment= projectfunc::userSegment($parent,$parent->current_smpin);
        $segment = $parent->current_user_data->segment;

        $salemodel_arr = array();
        $query_salesmodelarr = $parent->selectQuery(DMS_EMPLOYEE_SALES, " model ", "smpin='" . $parent->current_smpin . "' AND sale_date>='" . date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01")) . "' AND sale_date<='" . date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01")) . "' GROUP BY model ", "");
        if ($query_salesmodelarr['nr']) {
            foreach ($query_salesmodelarr['result'] as $res) {
                $salemodel_arr[] = $res->model;
            }
            $salemodel_quotes = array_map("quotes_array", $salemodel_arr);
            $model_cond .= " AND ( (segment='" . $segment . "' OR segment='0' AND model_is_target=1)OR (model_name IN (" . @implode(',', $salemodel_quotes) . ")) )";
        } else {
            $model_cond .= " AND (segment='" . $segment . "' OR segment='0' )";
        }

        $query_modelarr = $parent->selectQuery(TABLE_MODEL, " model_name,model_image ", " user_type=0  " . $model_cond . " ORDER BY model_id", "");
        if ($query_modelarr['nr']) {

            foreach ($query_modelarr['result'] as $res) {
                $res->model_image_url = ($res->model_image != '') ? SITE_URL . "hypermupload/model/" . $res->model_image : NOIMAGE_URL;
                $user_target = projectfunc::userSalesTarget($parent, $parent->current_user_id, $res->model_name, $year_data, $month_data);

//                if($user_target>0)
                //                {
                $res->target = ($user_target) ? $user_target : 0;
                $res->achieved = projectfunc::userSalesAchieved($parent, $parent->current_user_id, $res->model_name, $year_data, $month_data);
                $res->achieved = ($res->achieved) ? $res->achieved : 0;
                $data_arr[] = $res;
//                }
            }
        }

        //LOG
        $logstr = 'SALES LIST';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// LEARNING //////////////////////////////////////////////// */
    /*private function learning($parent)
    {
    //dashboard/2018/2
    globalfunc::checkMethodType($parent,array('GET'));
    globalfunc::checkParamCount($parent,2);
    globalfunc::checkAuthrozation($parent,array('type'=>'user'));

    $year_data=escape_str($parent->params[0]);
    $last_data = escape_str($parent->params[1]);
    $last_data_arr= explode('?',$last_data);
    $month_data=trim($last_data_arr[0]);

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

    /* /////////////////////////////// REWARDPOINTS //////////////////////////////////////////////// */
    private function rewardpoints($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //PAGINATION SETTINGS
        $limit_cond = '';
        $data_arr = array();
        $limit_str = globalfunc::setPageLimit($parent, $parent->params[0]);
        $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';
        $data = @explode('?', $parent->params[1]);

        $query_rewardpointarr = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "*", "smpin='" . $parent->current_smpin . "' AND transaction_type='C' ORDER BY created_on DESC " . $limit_cond, "");
        if ($query_rewardpointarr['nr']) {
//            foreach($query_rewardpointarr['result'] as $res)
            //            {
            //                $data_arr[]=$res;
            //            }
            $data_arr = $query_rewardpointarr['result'];
        }

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

        //GET COUNT
        $get_rewardpoint_count = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "COUNT(hid)as e_count", " smpin='" . $parent->current_smpin . "' AND transaction_type='C' ", "1");
        $total = ($get_rewardpoint_count['nr'] > 0) ? $get_rewardpoint_count['result'][0]->e_count : '0';

        //TOTAL REWARD POINTS
        $total_rewardpoint_arr = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "SUM(points) as total_point", " smpin='" . $parent->current_smpin . "' AND transaction_type='C' ORDER BY created_on DESC ", "");
        $total_point = ($total_rewardpoint_arr['nr'] > 0 && $total_rewardpoint_arr['result'][0]->total_point) ? $total_rewardpoint_arr['result'][0]->total_point : '0';

        //TOTAL POINTS
        $total_currentpoint_arr = $parent->selectQuery(TABLE_USER_EPOINTS, "points", " smpin='" . $parent->current_smpin . "'", "1");
        $current_point = ($total_currentpoint_arr['nr'] > 0) ? $total_currentpoint_arr['result'][0]->points : '0';

        //REDEEM POINT
        $is_redeem_point = (projectfunc::checkValidReedem($parent)) ? "1" : "0";

        $last_page = ($total) ? ceil($total / $parent->current_limit) - 1 : 0;

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

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'total' => $total, 'total_point' => $total_point, 'current_point' => $current_point, 'is_redeem_point' => $is_redeem_point, 'min_redeem_point' => $min_redeem_point, 'last_page' => $last_page, 'current_page' => $parent->current_page, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// REDEEMHISTORY //////////////////////////////////////////////// */
    private function redeemhistory($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //PAGINATION SETTINGS
        $limit_cond = '';
        $data_arr = array();
        $limit_str = globalfunc::setPageLimit($parent, $parent->params[0]);
        $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';
        $data = @explode('?', $parent->params[1]);

        $query_rewardpointarr = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "*", "smpin='" . $parent->current_smpin . "' AND transaction_type='D' ORDER BY created_on DESC " . $limit_cond, "");
        if ($query_rewardpointarr['nr']) {
            foreach ($query_rewardpointarr['result'] as $res) {
                $data_arr[] = $res;
            }
        }

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

        //GET COUNT
        $get_rewardpoint_count = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "COUNT(hid)as e_count", " smpin='" . $parent->current_smpin . "' AND transaction_type='D' ", "1");
        $total = ($get_rewardpoint_count['nr']) ? $get_rewardpoint_count['result'][0]->e_count : '0';

        //TOTAL REWARD POINTS
        $total_rewardpoint_arr = $parent->selectQuery(TABLE_USER_EPOINTS_HISTORY, "SUM(points) as total_point", " smpin='" . $parent->current_smpin . "' AND transaction_type='D' ORDER BY created_on DESC ", "");
        $total_point = ($total_rewardpoint_arr['nr'] > 0 && $total_rewardpoint_arr['result'][0]->total_point) ? $total_rewardpoint_arr['result'][0]->total_point : '0';

        //TOTAL POINTS
        $total_currentpoint_arr = $parent->selectQuery(TABLE_USER_EPOINTS, "points", " smpin='" . $parent->current_smpin . "'", "1");
        $current_point = ($total_currentpoint_arr['nr'] > 0) ? $total_currentpoint_arr['result'][0]->points : '0';

        //REDEEM POINT
        $is_redeem_point = (projectfunc::checkValidReedem($parent)) ? "1" : "0";

        $last_page = ($total) ? ceil($total / $parent->current_limit) - 1 : 0;

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

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'total' => $total, 'total_point' => $total_point, 'current_point' => $current_point, 'is_redeem_point' => $is_redeem_point, 'min_redeem_point' => $min_redeem_point, 'last_page' => $last_page, 'current_page' => $parent->current_page, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// LEARNPATH //////////////////////////////////////////////// */
    private function learnpath($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //GET LEVEL BASED ASSESSMENTS
        $data_arr = array();
//        $assessment_str = "SELECT a.assessmentid,a.lvl_id,a.assessmentname,au.highscore,au.highscorearid,au.lvl_status,l.lvl_name FROM  ".TABLE_ASSESSMENT." as a JOIN ".TABLE_ASSESSMENT_USER." as au on a.assessmentid=au.assessmentid LEFT JOIN ".TABLE_TRAINING_LEVEL." l ON l.lvl_id=a.lvl_id  WHERE a.status='1' AND a.atype='1' AND au.userid='".$parent->current_user_id."' AND au.status='1' AND highscorearid!='0' AND a.publisheddate<='".NOW."' ORDER BY a.lvl_id ASC,au.created_on ASC ";
        //        $assessment_arr=$parent->getQueryValue($assessment_str);
        //        if(count($assessment_arr)>0)
        //        {
        //            foreach($assessment_arr as $res)
        //            {
        //                //GET REWARD
        //                $get_exam_time=$parent->selectQuery(TABLE_ASSESSMENTRESULT,"*"," arid='".$res->highscorearid."' AND userid='".$parent->current_user_id."'" ,"1");
        //                $res->examtime=($get_exam_time['nr']>0)?$get_exam_time['result'][0]->enddatetime:'';
        //                $data_arr[]=$res;
        //            }
        //        }
        $learning = (object) projectfunc::userLearningLevel($parent, $parent->current_user_id);

        //BASIC
        if ($learning->basic == 3) {
            $get_date = $parent->selectQuery(TABLE_USER_LEARNING, "created_on", " lvl_id='1' AND smpin='" . $parent->current_user_data->smpin . "'", "1");
            $examtime = ($get_date['nr'] > 0) ? $get_date['result'][0]->created_on : '';
            $res = (object) array('assessmentid' => 0, 'lvl_id' => 1, 'assessmentname' => "Base", 'highscore' => '100', 'highscorearid' => 0, 'lvl_status' => 3, 'lvl_name' => "Base", 'examtime' => $examtime);
            $data_arr[] = $res;
        }
        //BRONZE
        if ($learning->bronze == 3) {
            $get_date = $parent->selectQuery(TABLE_USER_LEARNING, "created_on", " lvl_id='2' AND smpin='" . $parent->current_user_data->smpin . "'", "1");
            $examtime = ($get_date['nr'] > 0) ? $get_date['result'][0]->created_on : '';
            $res = (object) array('assessmentid' => 0, 'lvl_id' => 2, 'assessmentname' => "Bronze", 'highscore' => '100', 'highscorearid' => 0, 'lvl_status' => 3, 'lvl_name' => "Bronze", 'examtime' => $examtime);
            $data_arr[] = $res;
        }
        //SILVER
        if ($learning->silver > 0) {
            $get_date = $parent->selectQuery(TABLE_USER_LEARNING, "created_on", " lvl_id IN (3,4) OR AND smpin='" . $parent->current_user_data->smpin . "'", "1");
            $examtime = ($get_date['nr'] > 0) ? $get_date['result'][0]->created_on : '';
            switch ($learning->silver) {
                case '3':$res = (object) array('assessmentid' => 0, 'lvl_id' => 3, 'assessmentname' => "Silver", 'highscore' => '100', 'highscorearid' => 0, 'lvl_status' => 3, 'lvl_name' => "Silver", 'examtime' => $examtime);
                    $data_arr[] = $res;
                    break;
                case '7':
                case '4':
                case '2':
                    $res = (object) array('assessmentid' => 0, 'lvl_id' => 3, 'assessmentname' => "Silver", 'highscore' => '50', 'highscorearid' => 0, 'lvl_status' => $learning->silver, 'lvl_name' => "Silver", 'examtime' => $examtime);
                    $data_arr[] = $res;
                    break;
            }
        }
        //GOLD
        if ($learning->gold > 0) {
            $get_date = $parent->selectQuery(TABLE_USER_LEARNING, "created_on", " lvl_id IN (5,6) AND smpin='" . $parent->current_user_data->smpin . "'", "1");
            $examtime = ($get_date['nr'] > 0) ? $get_date['result'][0]->created_on : '';
            switch ($learning->gold) {
                case '3':$res = (object) array('assessmentid' => 0, 'lvl_id' => 4, 'assessmentname' => "Gold", 'highscore' => '100', 'highscorearid' => 0, 'lvl_status' => 3, 'lvl_name' => "Gold", 'examtime' => $examtime);
                    $data_arr[] = $res;
                    break;
                case '7':
                case '4':
                case '2':
                    $res = (object) array('assessmentid' => 0, 'lvl_id' => 4, 'assessmentname' => "Gold", 'highscore' => '50', 'highscorearid' => 0, 'lvl_status' => $learning->silver, 'lvl_name' => "Gold", 'examtime' => $examtime);
                    $data_arr[] = $res;
                    break;
            }
        }

        //LOG
        $logstr = 'LEARNPATH';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// MANAGER DASHBOARD //////////////////////////////////////////////// */
    private function managerdashboard($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array(3, 4, 5, 6, 7, 14));

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));

        //LEARNING
        $learning_arr = projectfunc::managerLearningDashboard($parent, $parent->current_user_role, $parent->current_user_id); //NOT BASED ON YEAR OR MONTH
        $assessment_arr = projectfunc::managerAssessmentDashboard($parent, $parent->current_user_role, $parent->current_user_id); //NOT BASED ON YEAR OR MONTH
        $sales_arr = projectfunc::managersalesDashboard($parent, $parent->current_user_role, $year_data, $month_data, $parent->current_user_id); // BASED ON YEAR OR MONTH

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);
        $data_arr = array('sales' => $sales_arr, 'learning' => $learning_arr, 'assessment' => $assessment_arr, 'languages' => $get_lang_arr);

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

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

    /* /////////////////////////////// AREA MANAGER DASHBOARD //////////////////////////////////////////////// */
    private function areamanagerdashboard($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array(3, 4, 5, 6, 7, 14));

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));

        //FIELDS VALIDATION
        $REQUEST_KEYS = array('area' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $area_manager = escape_str($parent->_request["area"]);

        $get_role = (in_array($parent->current_user_role, array(7, 14))) ? $parent->current_user_role : "7";
        //LEARNING
        $learning_arr = projectfunc::newmanagerLearningDashboard($parent, $get_role, $area_manager); //NOT BASED ON YEAR OR MONTH
        $assessment_arr = projectfunc::newmanagerAssessmentDashboard($parent, $get_role, $area_manager); //NOT BASED ON YEAR OR MONTH
        $sales_arr = projectfunc::newmanagersalesDashboard($parent, $get_role, $year_data, $month_data, $area_manager); // BASED ON YEAR OR MONTH

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);
        $data_arr = array('sales' => $sales_arr, 'learning' => $learning_arr, 'assessment' => $assessment_arr, 'languages' => $get_lang_arr);

        //LOG
        $logstr = 'AREA MANAGER DASHBOARD';
        globalfunc::insertUserLog($parent, $logstr);

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

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

        $start_date = date('Y-m-d', strtotime(NOW));
        $end_date = date('Y-m-t', strtotime(NOW));

        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $dealer_code_cond = '';
        if ($parent->current_user_role < 7) {
            //FIELDS VALIDATION
            $REQUEST_KEYS = array('area' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $area_manager = escape_str($parent->_request["area"]);
        } else if ($parent->current_user_role == '7') {
            $area_manager = $userdata->emailid;

        } else if ($parent->current_user_role == '14') {
            //FIELDS VALIDATION
            $REQUEST_KEYS = array('area' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $area_manager = escape_str($parent->_request["area"]);

            $dealer_code = $userdata->current_user_data->dealer_code;
            $dealer_code_cond = " AND dealer_code='" . $dealer_code . "'";
        } else {
            $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
        }

        $user_cond = "";
        $role = $parent->current_user_role;

        $get_dealer_str = "SELECT dealer_code,city,zone FROM " . DMS_DEALER . "  WHERE am_email='" . $area_manager . "' " . $dealer_code_cond . " GROUP BY dealer_code ";
        $dealer_arr = $parent->getQueryValue($get_dealer_str);
        if (count($dealer_arr) > 0) {
            $zone_arr = array();
            $city_arr = array();
            foreach ($dealer_arr as $res_dealer) {
                if (!in_array($res_dealer->zone, $zone_arr)) {
                    $zone_arr[] = $res_dealer->zone;
                }

                if (!in_array($res_dealer->city, $city_arr)) {
                    $city_arr[] = $res_dealer->city;
                }

                $dealer_cond_arr[] = " filter_dealer LIKE '%" . $res_dealer->dealer_code . "%'";
            }
            $user_cond .= " AND ( filter_dealer='' OR " . @implode(" OR ", $dealer_cond_arr) . " ) ";

            if (count($zone_arr) > 0) {
                foreach ($zone_arr as $zone) {
                    $zone_cond_arr[] = " filter_zone LIKE '%" . $zone . "%'";
                }
                $user_cond .= " AND (filter_zone='' OR " . @implode(" OR ", $zone_cond_arr) . " ) ";
            } else {
                $user_cond .= " AND (filter_zone='') ";
            }

            if (count($city_arr) > 0) {
                foreach ($city_arr as $city) {
                    $city_cond_arr[] = " filter_region LIKE '%" . $city . "%'";
                }
                $user_cond .= " AND (filter_region='' OR " . @implode(" OR ", $city_cond_arr) . " ) ";
            } else {
                $user_cond .= " AND (filter_region='') ";
            }

        } else {
            $user_cond .= " AND (filter_dealer='') ";
        }

        $user_table_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $area_manager . "' " . $dealer_code_cond . ")";
        $user_cond .= " AND a.user_type=0";

        $assessment_str = "SELECT a.* FROM `" . TABLE_ASSESSMENT . "` as a  WHERE a.atype='0' AND a.assettypeid='1' " . $lcond . " AND a.publisheddate<='" . NOW . "' " . $user_cond;
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessment_data_arr = array();
        if (count($assessment_arr) > 0) {
            foreach ($assessment_arr as $res_assess) {
                $assessmentid_arr[] = $res_assess->assessmentid;
            }

            //GET ALL 3 USER RANK
            $parent->executeQuery("SET @rownum := 0");
//AND u.status=1
            $get_user_rank_str = "SELECT * FROM ( SELECT @rownum := @rownum + 1 AS rank,COALESCE(user_score,'0')as user_score,CASE WHEN user_score IS NULL THEN 0 ELSE (user_score/" . count($assessment_arr) . ") END as percentage_score,userid,total_time,last_modified FROM (SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified  FROM " . TABLE_USER . " as u JOIN (SELECT * FROM  " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") GROUP BY userid )as ar ON u.userid=ar.userid  WHERE u.role>='3' AND  u.is_deleted=0  " . $user_table_cond . "  GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC )as a )as b LIMIT 3 ";

            $q1 = $get_user_rank_str;
            $get_user_rank_arr = $parent->getQueryValue($get_user_rank_str);
            if ($get_user_rank_arr) {
                foreach ($get_user_rank_arr as $res_user) {
                    $user_data_arr = new stdClass();
                    $get_time_str = "SELECT * FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND userid='" . $res_user->userid . "'";
                    $get_time_arr = $parent->getQueryValue($get_time_str);

                    $user_data_arr->user = projectfunc::getuserdata($parent, $res_user->userid);
                    $user_data_arr->rank = (count($get_time_arr) > 0) ? $res_user->rank : '-';
                    $user_data_arr->tot_user_score = (count($get_time_arr) > 0) ? $res_user->user_score : '-';
                    $user_data_arr->percentage_score = (count($get_time_arr) > 0) ? round($res_user->percentage_score) : '-';
                    $user_data_arr->percentage = $user_data_arr->percentage_score;
                    $user_data_arr->total_time = $res_user->total_time;
                    $user_data_arr->last_time = (count($get_time_arr) > 0) ? $res_user->last_modified : '-';
                    $assessment_data_arr[] = $user_data_arr;
                }
            }

        }

        $sales_data_arr = array();
        $parent->executeQuery("SET @rownum := 0");
        $get_user_rank_str = "SELECT * FROM (SELECT @rownum := @rownum + 1 AS rank, salecount,targetcount,smpin,userid,last_sale,first_sale,sale_per FROM (SELECT c.*, CASE WHEN targetcount=0 THEN 0 ELSE (salecount/targetcount)*100 END as 'sale_per' FROM ( SELECT a.userid,a.smpin,a.salecount,b.targetcount,a.last_sale,a.first_sale FROM (SELECT COALESCE(COUNT(s.id),'0') as scount, COALESCE(SUM(s.sales_count),'0') as salecount, u.smpin,u.userid,MAX(s.sale_date) last_sale,MIN(s.sale_date) first_sale FROM " . TABLE_USER . " as u  LEFT JOIN (SELECT id,smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $start_date . "' AND sale_date<='" . $end_date . "') as s ON s.smpin=u.smpin WHERE u.is_deleted=0  AND u.status=1 " . $user_table_cond . " GROUP BY u.smpin ) as a JOIN  (SELECT  COALESCE(SUM(t.target),'0') as targetcount,u.smpin,u.userid FROM " . TABLE_USER . " as u LEFT JOIN (SELECT id,smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $start_date . "' AND sale_month<='" . $end_date . "')as t ON t.smpin=u.smpin WHERE u.is_deleted=0  AND u.status=1 " . $user_table_cond . "  GROUP BY u.smpin ) as b ON a.userid=b.userid )c ORDER BY sale_per DESC,last_sale ASC,first_sale ASC,userid ASC)as a )as d LIMIT 3";
        $q2 = $get_user_rank_str;
        $get_user_rank_arr = $parent->getQueryValue($get_user_rank_str);
        if (count($get_user_rank_arr) > 0) {
            foreach ($get_user_rank_arr as $key => $value) {
                $get_user_rank_arr[$key]->user = projectfunc::getuserdata($parent, $value->userid);
                $get_user_rank_arr[$key]->percentage = round($value->sale_per);
            }
            $sales_data_arr = $get_user_rank_arr;
        }

        $data_arr = array('sales' => $sales_data_arr, 'assessment' => $assessment_data_arr);
        //LOG
        $logstr = 'MANAGER DASHBOARD LEADERBOARD';
        globalfunc::insertUserLog($parent, $logstr);

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

    /* /////////////////////////////// WALLETCREDIT //////////////////////////////////////////////// */
    private function walletcredit($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //PAGINATION SETTINGS
        $limit_cond = '';
        $data_arr = array();
        $limit_str = globalfunc::setPageLimit($parent, $parent->params[0]);
        $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';
        $data = @explode('?', $parent->params[1]);

        $query_walletarr = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "*", "smpin='" . $parent->current_smpin . "' AND transaction_type='C' ORDER BY created_on DESC " . $limit_cond, "");
        if ($query_walletarr['nr']) {
//            foreach($query_walletarr['result'] as $res)
            //            {
            //                $data_arr[]=$res;
            //            }
            $data_arr = $query_walletarr['result'];
        }

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

        //GET COUNT
        $get_wallet_count = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "COUNT(hid)as e_count", " smpin='" . $parent->current_smpin . "' AND transaction_type='C' ", "1");
        $total = ($get_wallet_count['nr'] > 0) ? $get_wallet_count['result'][0]->e_count : '0';

        //TOTAL WALLET POINTS
        $total_wallet_arr = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "SUM(amount) as total_amount", " smpin='" . $parent->current_smpin . "' AND transaction_type='C' ORDER BY created_on DESC ", "");
        $total_amount = ($total_wallet_arr['nr'] > 0 && $total_wallet_arr['result'][0]->total_amount) ? $total_wallet_arr['result'][0]->total_amount : '0';

        //TOTAL WALLET
        $currentwallet_arr = $parent->selectQuery(TABLE_USER_WALLET, "amount", " smpin='" . $parent->current_smpin . "'", "1");
        $current_amount = ($currentwallet_arr['nr'] > 0) ? $currentwallet_arr['result'][0]->amount : '0';

        $last_page = ($total) ? ceil($total / $parent->current_limit) - 1 : 0;
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'total' => $total, 'total_amount' => $total_amount, 'current_amount' => $current_amount, 'last_page' => $last_page, 'current_page' => $parent->current_page, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// WALLETDEBIT //////////////////////////////////////////////// */
    private function walletdebit($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //PAGINATION SETTINGS
        $limit_cond = '';
        $data_arr = array();
        $limit_str = globalfunc::setPageLimit($parent, $parent->params[0]);
        $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';
        $data = @explode('?', $parent->params[1]);

        $query_walletarr = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "*", "smpin='" . $parent->current_smpin . "' AND transaction_type='D' ORDER BY created_on DESC " . $limit_cond, "");
        if ($query_walletarr['nr']) {
            /*foreach($query_walletarr['result'] as $res)
            {
            $data_arr[]=$res;
            }*/
            $data_arr = $query_walletarr['result'];
        }

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

        //GET COUNT
        $get_wallet_count = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "COUNT(hid)as e_count", " smpin='" . $parent->current_smpin . "' AND transaction_type='D' ", "1");
        $total = ($get_wallet_count['nr']) ? $get_wallet_count['result'][0]->e_count : '0';

        //TOTAL WALLET POINTS
        $total_wallet_arr = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "SUM(amount) as total_amount", " smpin='" . $parent->current_smpin . "' AND transaction_type='D' ORDER BY created_on DESC ", "");
        $total_amount = ($total_wallet_arr['nr'] > 0 && $total_wallet_arr['result'][0]->total_amount) ? $total_wallet_arr['result'][0]->total_amount : '0';

        //TOTAL WALLET
        $total_wallet_arr = $parent->selectQuery(TABLE_USER_WALLET, "amount", " smpin='" . $parent->current_smpin . "'", "1");
        $current_amount = ($total_wallet_arr['nr'] > 0) ? $total_wallet_arr['result'][0]->amount : '0';

        //REDEEM WALLET
        $is_redeem_point = (projectfunc::checkValidReedem($parent)) ? "1" : "0";

        $last_page = ($total) ? ceil($total / $parent->current_limit) - 1 : 0;
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'total' => $total, 'total_amount' => $total_amount, 'current_amount' => $current_amount, 'last_page' => $last_page, 'current_page' => $parent->current_page, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// ORDERHISTORY //////////////////////////////////////////////// */
    private function orderhistory($parent)
    {

        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //PAGINATION SETTINGS
        $limit_cond = '';
        $data_arr = array();
        $limit_str = globalfunc::setPageLimit($parent, $parent->params[0]);
        $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';
        $data = @explode('?', $parent->params[1]);

        $query_arr = $parent->selectQuery(TABLE_ORDER, "*", "	smpin='" . $parent->current_smpin . "'  ORDER BY order_id DESC " . $limit_cond, "");
        if ($query_arr['nr']) {
            foreach ($query_arr['result'] as $key => $res) {
                if ($res->is_success == '1') {
                    $query_arr['result'][$key]->status = '1';

                } else if ($res->is_success == '0' && $res->is_refund == '1') {
                    $query_arr['result'][$key]->status = '0';

                } else if ($res->is_success == '0' && $res->is_refund == '0') {
                    $query_arr['result'][$key]->status = '2';
                }

                if ($res->card_data) {
                    $query_arr['result'][$key]->card_data = json_decode(stripslashesFull($res->card_data));
                    if (!isset($query_arr['result'][$key]->card_data->cardnumber)) {
                        $query_arr['result'][$key]->card_data->cardnumber = $query_arr['result'][$key]->card_data->cardNumber;
                    }
                    if (!isset($query_arr['result'][$key]->card_data->card_price)) {
                        $query_arr['result'][$key]->card_data->card_price = $query_arr['result'][$key]->card_data->amount;
                    }
                    if (!isset($query_arr['result'][$key]->card_data->expiry_date)) {
                        $query_arr['result'][$key]->card_data->expiry_date = date('Y-m-d', strtotime($query_arr['result'][$key]->card_data->validity));
                    }
                    if (!isset($query_arr['result'][$key]->card_data->activation_code)) {
                        $query_arr['result'][$key]->card_data->activation_code = $query_arr['result'][$key]->card_data->activationCode;
                    }
                    if (!isset($query_arr['result'][$key]->card_data->name)) {
                        $query_arr['result'][$key]->card_data->name = $query_arr['result'][$key]->card_data->productName;
                    }
                    if (!isset($query_arr['result'][$key]->card_data->card_pin)) {
                        $query_arr['result'][$key]->card_data->card_pin = $query_arr['result'][$key]->card_data->cardPin;
                    }
                    if (!isset($query_arr['result'][$key]->card_data->pin_or_url)) {
                        $query_arr['result'][$key]->card_data->pin_or_url = $query_arr['result'][$key]->card_data->activationUrl;
                    }
                    if (isset($query_arr['result'][$key]->card_data->cardNumber)) {
                        unset($query_arr['result'][$key]->card_data->cardNumber);
                    }
                } else {
                    $query_arr['result'][$key]->card_data = new stdClass();
                }
                if (!$res->images != '') {
                    $query_arr['result'][$key]->images = NOIMAGE_URL;
                }

            }
            $data_arr = $query_arr['result'];
        }

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

        //GET COUNT
        $get_order_count = $parent->selectQuery(TABLE_ORDER, "COUNT(id)as ord_count", " smpin='" . $parent->current_smpin . "' AND  is_success='1'  ", "1");
        $total = ($get_order_count['nr']) ? $get_order_count['result'][0]->ord_count : '0';

        //TOTAL WALLET POINTS
        $total_wallet_arr = $parent->selectQuery(TABLE_USER_WALLET_HISTORY, "SUM(amount) as total_amount", " smpin='" . $parent->current_smpin . "' AND transaction_type='D' ORDER BY created_on DESC ", "");
        $total_amount = ($total_wallet_arr['nr'] > 0 && $total_wallet_arr['result'][0]->total_amount) ? $total_wallet_arr['result'][0]->total_amount : '0';

        //TOTAL WALLET
        $total_wallet_arr = $parent->selectQuery(TABLE_USER_WALLET, "amount", " smpin='" . $parent->current_smpin . "'", "1");
        $current_amount = ($total_wallet_arr['nr'] > 0) ? $total_wallet_arr['result'][0]->amount : '0';

        $unapproved_amount = projectfunc::userUnapprovedamount($parent, $parent->current_smpin);

        //REDEEM POINT
        $is_redeem_point = (projectfunc::checkValidReedem($parent)) ? "1" : "0";

        $last_page = ($total) ? ceil($total / $parent->current_limit) - 1 : 0;
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'total' => $total, 'total_amount' => $total_amount, 'current_amount' => $current_amount, 'unapproved_amount' => $unapproved_amount, 'last_page' => $last_page, 'current_page' => $parent->current_page, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// CHANGE MPIN //////////////////////////////////////////////// */
    private function changempin($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

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

        $parent->executeQuery("UPDATE  " . TABLE_USER . " SET mpin='" . $mpin . "' WHERE userid='" . $parent->current_user_id . "' ");

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

    /* /////////////////////////////// CHANGE EPIN //////////////////////////////////////////////// */
    private function changeepin($parent)
    {

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

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

        $parent->executeQuery("UPDATE  " . TABLE_USER . " SET epin='" . $epin . "' WHERE userid='" . $parent->current_user_id . "' ");
        $parent->response(array('response' => 'success', 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// AREA MANAGER DASHBOARD //////////////////////////////////////////////// */
    private function newareamanagerdashboard($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array(3, 4, 5, 6, 7, 14));

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));

        $qry_params = explode("?", $parent->params[3]);
        $type = strtolower($qry_params[0]);

        $user_cond = "";
        switch ($type) {
            case "zm":
                if ($parent->current_user_role == '3') {
                    $REQUEST_KEYS = array('zmemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = $parent->_request["zmemail"];
                    if ($emailid != 'all') {
                        //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE zm_email='". $emailid."')";
                        $get_role = '4';
                        $manageremail = $emailid;
                    } else {
                        //$user_cond.="";
                        $get_role = '3';
                        $manageremail = '';
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "rm":
                if ($parent->current_user_role < '7') {
                    if ($parent->current_user_role == '5') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                        $emailid = $parent->_request["rmemail"];
                        if ($emailid == 'all') {

                            // $zone_cond.=(count($trainer_arr)>0)?" zone='".$trainer_arr[0]->zone."'":" zone='0'";
                            //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE ".$zone_cond.")";
                            $get_role = '5';
                            $manageremail = $trainer_arr[0]->emailid;
                        } else {

                            //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE rm_email='".$emailid."')";
                            $get_role = '6';
                            $manageremail = $emailid;
                        }
                    } else {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                        $emailid = $parent->_request["rmemail"];
                        if ($emailid == 'all') {
                            $REQUEST_KEYS = array('zmemail' => 'string');
                            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

                            $emailid = $parent->_request["zmemail"];
                            // $user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE zm_email='". $emailid."')";
                            $get_role = '4';
                            $manageremail = $emailid;
                        } else {
                            //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE rm_email='".$emailid."')";
                            $get_role = '6';
                            $manageremail = $emailid;
                        }
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "am":
                if ($parent->current_user_role == '14') {
                    //$dealer_code_cond=" dealer_code='".$parent->current_user_data->dealer_code."'";
                    $get_role = '14';
                    $manageremail = '';
                } else {
                    $REQUEST_KEYS = array('amemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = $parent->_request["amemail"];
                    if ($emailid == 'all') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $emailid = $parent->_request["rmemail"];
                        //$user_cond.=" rm_email='".$emailid."'";
                        $get_role = '6';
                        $manageremail = $emailid;
                    } else {
                        //$dealer_code_cond="  am_email='".$emailid."'";
                        $get_role = '7';
                        $manageremail = $emailid;
                    }
                }
                //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE  ".$dealer_code_cond.")";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        //FIELDS VALIDATION
        //$REQUEST_KEYS=array('area'=>'string');
        //globalfunc::mandatoryKey($parent,$REQUEST_KEYS);
        //$area_manager = escape_str($parent->_request["area"]);

        //$get_role=(in_array($parent->current_user_role,array(7,14)))?$parent->current_user_role:"7";

        //LEARNING
        $learning_arr = projectfunc::newmanagerLearningDashboard($parent, $get_role, $manageremail); //NOT BASED ON YEAR OR MONTH
        $assessment_arr = projectfunc::newmanagerAssessmentDashboard($parent, $get_role, $manageremail); //NOT BASED ON YEAR OR MONTH
        $sales_arr = projectfunc::newmanagersalesDashboard($parent, $get_role, $year_data, $month_data, $manageremail); // BASED ON YEAR OR MONTH

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);
        $data_arr = array('sales' => $sales_arr, 'learning' => $learning_arr, 'assessment' => $assessment_arr, 'languages' => $get_lang_arr);

        //LOG
        $logstr = 'AREA MANAGER DASHBOARD';
        globalfunc::insertUserLog($parent, $logstr);

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

    /* /////////////////////////////// AREA MANAGER DASHBOARD //////////////////////////////////////////////// */
    private function areamanagerdashboardapp($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array(3, 4, 5, 6, 7, 14));

        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));

        $zmemailid = (isset($parent->_request["zmemail"])) ? escape_str($parent->_request["zmemail"]) : 'all';
        $rmemailid = (isset($parent->_request["rmemail"])) ? escape_str($parent->_request["rmemail"]) : 'all';
        $amemailid = (isset($parent->_request["amemail"])) ? escape_str($parent->_request["amemail"]) : 'all';

        if ($parent->current_user_role == '3') //HO
        {
            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else if ($rmemailid != 'all') {
                $get_role = '6';
                $manageremail = $rmemailid;

            } else if ($zmemailid != 'all') {
                $get_role = '4';
                $manageremail = $zmemailid;
            } else {
                $get_role = '3';
                $manageremail = '';
            }

        } else if ($parent->current_user_role == '4') //ZM
        {

            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else if ($rmemailid != 'all') {
                $get_role = '6';
                $manageremail = $rmemailid;

            } else if ($zmemailid != 'all') {
                $get_role = '4';
                $manageremail = $zmemailid;
            } else {
                $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
            }

        } else if ($parent->current_user_role == '5') //TRAINER
        {
            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else if ($rmemailid != 'all') {
                $get_role = '6';
                $manageremail = $rmemailid;

            } else {
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);

                $get_role = '5';
                $manageremail = $trainer_arr[0]->emailid;
            }
        } else if ($parent->current_user_role == '6') //RM
        {
            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else if ($rmemailid != 'all') {
                $get_role = '6';
                $manageremail = $rmemailid;

            } else {
                $get_role = '6';
                $manageremail = $userdata->emailid;

            }
        } else if ($parent->current_user_role == '7') //AM
        {
            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else {
                $get_role = '7';
                $manageremail = $userdata->emailid;
            }
        } else if ($parent->current_user_role == '14') //DEALER
        {
            $get_role = '14';
            $manageremail = '';
        }

        //LEARNING
        $learning_arr = projectfunc::newmanagerLearningDashboard($parent, $get_role, $manageremail); //NOT BASED ON YEAR OR MONTH
        $assessment_arr = projectfunc::newmanagerAssessmentDashboard($parent, $get_role, $manageremail); //NOT BASED ON YEAR OR MONTH
        $sales_arr = projectfunc::newmanagersalesDashboard($parent, $get_role, $year_data, $month_data, $manageremail); // BASED ON YEAR OR MONTH

        //LANGUAGE
        $get_lang_str = "SELECT `language_id`,`language_code`, `language_title` FROM " . TABLE_MASTER_LANGUAGE . " WHERE is_active='1' AND is_deleted='0' ORDER BY is_default DESC,language_title ASC";
        $get_lang_arr = $parent->getQueryValue($get_lang_str);
        $data_arr = array('sales' => $sales_arr, 'learning' => $learning_arr, 'assessment' => $assessment_arr, 'languages' => $get_lang_arr);

        //LOG
        $logstr = 'AREA MANAGER DASHBOARD';
        globalfunc::insertUserLog($parent, $logstr);

        $parent->response(array('response' => 'success', 'data' => $data_arr, 'test' => $get_role, 'mail' => $manageremail, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// ENQUIRY //////////////////////////////////////////////// */
    private function enquiry($parent)
    {
        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $year_data = escape_str($parent->params[1]);
        $last_data = escape_str($parent->params[2]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));
        $data_arr = array();

        $datecond = " AND created_on>='" . date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01")) . " 00:00:00' AND created_on<='" . date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01")) . " 23:59:59'";

        //GIXXER 250
        //$query_salesmodelarr=$parent->selectQuery(DMS_ENQUIRY_MASTER,"COUNT(id) as achieved ","smpin='".$parent->current_smpin."' ".$datecond." AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM ".DMS_ENQUIRY_MODEL." WHERE title_code IN ('GSX','GSXSF') AND variant LIKE '%250%')  GROUP BY model1 ","");
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $parent->current_smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant LIKE '%250%') ", "1");

        $target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'GIXXER 250', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER 250";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'GSX';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER 250";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        //GIXXER 150
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $parent->current_smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant NOT LIKE '%250%') ", "1");
        $target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'GIXXER', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';
            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
            $mobj->target = $target;
            $mobj->modelname = 'GIXXER';
            //$mobj->modelname=($model=='GSX')? "GIXXER":"GIXXER SF 150";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'GSX';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER 250";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        //ACCESS
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $parent->current_smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 IN ('AC','ACZ')", "1");

        $target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'ACCESS 125', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';
            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
            $mobj->target = $target;
            $mobj->modelname = 'ACCESS 125';
            //$mobj->modelname=($model=='GSX')? "GIXXER":"GIXXER SF 150";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'AC';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "ACCESS 125";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        $is_intruder = 0;
        $is_burgman = 0;
        //$is_access=0;

        //OTHER LISTED MODEL THAN GIXXER
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $parent->current_smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 IN ('GL','UB125' )  GROUP BY model1 ", "");
        if ($query_salesmodelarr['nr'] > 0) {

            foreach ($query_salesmodelarr['result'] as $res) {
                $mobj = new stdClass();
                $mobj->achieved = $res->achieved;
                $obj = new stdClass();
                $obj->model = $res->model1;
                $obj->variant = $res->variant1;
                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
                switch ($obj->model) {
                    case 'GL': //INTRUDER
                        $mobj->modelname = 'INTRUDER';
                        $mobj->target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'INTRUDER', $year_data, $month_data) * 3;
                        $is_intruder = 1;
                        break;
                    case 'UB125': //BURGMAN
                        $mobj->modelname = 'BURGMAN';
                        $mobj->target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'BURGMAN', $year_data, $month_data) * 3;
                        $is_burgman = 1;
                        break;
                }

                //$mobj->target=0;
                $data_arr[] = $mobj;
            }
        }

        if ($is_intruder == 0) //INTRUDER
        {
            $target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'INTRUDER', $year_data, $month_data) * 3;
            if ($target > 0) {
                $mobj = new stdClass();
                $mobj->achieved = 0;
                $obj = new stdClass();
                $obj->model = 'GL';
                $obj->variant = '';
                $obj->variantname = '';

                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

                $mobj->target = $target;
                $mobj->modelname = 'INTRUDER';
                //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
                $data_arr[] = $mobj;
            }
        }

        if ($is_burgman == 0) //BURGMAN
        {
            $target = projectfunc::userSalesTarget($parent, $parent->current_user_id, 'BURGMAN', $year_data, $month_data) * 3;

            if ($target > 0) {
                $mobj = new stdClass();
                $mobj->achieved = 0;
                $obj = new stdClass();
                $obj->model = 'UB125';
                $obj->variant = '';
                $obj->variantname = '';

                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

                $mobj->target = $target;
                $mobj->modelname = 'BURGMAN';

                $data_arr[] = $mobj;
            }
        }

        //OTHER MODEL
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved ", "smpin='" . $parent->current_smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 NOT IN ('GSX','GSXSF','GL','UB125','AC','ACZ') ", "");
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $mobj->model_image_url = NOIMAGE_URL;
            $mobj->modelname = 'OTHERS';
            //$mobj->target=projectfunc::userSalesTarget($parent,$parent->current_user_id,'OTHERS',$year_data,$month_data)*3;
            $mobj->target = 0;
            $data_arr[] = $mobj;

        }
        //LOG

        $logstr = 'ENQUIRY MODEL WISE LIST';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DEALER ENQUIRY //////////////////////////////////////////////// */
    private function dealerenquiry($parent)
    {
        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $smpin = escape_str($parent->params[1]);
        $dse_details = $this->getdsedetails($parent, $smpin);

        $year_data = escape_str($parent->params[2]);
        $last_data = escape_str($parent->params[3]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));
        $data_arr = array();

        $datecond = " AND created_on>='" . date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01")) . " 00:00:00' AND created_on<='" . date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01")) . " 23:59:59'";

        //GIXXER 250
        //$query_salesmodelarr=$parent->selectQuery(DMS_ENQUIRY_MASTER,"COUNT(id) as achieved ","smpin='".$parent->current_smpin."' ".$datecond." AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM ".DMS_ENQUIRY_MODEL." WHERE title_code IN ('GSX','GSXSF') AND variant LIKE '%250%')  GROUP BY model1 ","");
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant LIKE '%250%') ", "1");

        $target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'GIXXER 250', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER 250";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'GSX';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER 250";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        //GIXXER 150
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0   AND model1 IN ('GSX','GSXSF') AND variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant NOT LIKE '%250%') ", "1");
        $target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'GIXXER', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';
            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
            $mobj->target = $target;
            $mobj->modelname = 'GIXXER';
            //$mobj->modelname=($model=='GSX')? "GIXXER":"GIXXER SF 150";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'GSX';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "GIXXER";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        //ACCESS
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 IN ('AC','ACZ')", "1");
        $target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'ACCESS 125', $year_data, $month_data) * 3;
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $obj = new stdClass();
            $obj->model = $res->model1;
            $obj->variant = $res->variant1;
            $obj->variantname = '';
            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
            $mobj->target = $target;
            $mobj->modelname = 'ACCESS 125';
            //$mobj->modelname=($model=='GSX')? "GIXXER":"GIXXER SF 150";
            $data_arr[] = $mobj;

        } else if ($target > 0) {
            $mobj = new stdClass();
            $mobj->achieved = 0;
            $obj = new stdClass();
            $obj->model = 'AC';
            $obj->variant = '';
            $obj->variantname = '';

            $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

            $mobj->target = $target;
            $mobj->modelname = "ACCESS 125";
            //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
            $data_arr[] = $mobj;
        }

        $is_intruder = 0;
        $is_burgman = 0;
        //$is_access=0;

        //OTHER LISTED MODEL THAN GIXXER
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved,model1,variant1 ", "smpin='" . $smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 IN ('GL','UB125')  GROUP BY model1 ", "");
        if ($query_salesmodelarr['nr'] > 0) {
            foreach ($query_salesmodelarr['result'] as $res) {
                $mobj = new stdClass();
                $mobj->achieved = $res->achieved;
                $obj = new stdClass();
                $obj->model = $res->model1;
                $obj->variant = $res->variant1;
                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);
                switch ($obj->model) {
                    case 'GL': //INTRUDER
                        $mobj->modelname = 'INTRUDER';
                        $mobj->target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'INTRUDER', $year_data, $month_data) * 3;
                        $is_intruder = 1;
                        break;
                    case 'UB125': //BURGMAN
                        $mobj->modelname = 'BURGMAN';
                        $mobj->target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'BURGMAN', $year_data, $month_data) * 3;
                        $is_burgman = 1;
                        break;
//                    case 'AC':
                        //                    case 'ACZ':
                        //                        $mobj->modelname='ACCESS';
                        //                        $mobj->target=projectfunc::userSmpinSalesTarget($parent,$smpin,'ACCESS 125',$year_data,$month_data)*3;
                        //                        $is_access=1;
                        //                        break;
                }
                //$mobj->target = 0;
                $data_arr[] = $mobj;
            }
        }

        if ($is_intruder == 0) //INTRUDER
        {
            $target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'INTRUDER', $year_data, $month_data) * 3;
            if ($target > 0) {
                $mobj = new stdClass();
                $mobj->achieved = 0;
                $obj = new stdClass();
                $obj->model = 'GL';
                $obj->variant = '';
                $obj->variantname = '';

                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

                $mobj->target = $target;
                $mobj->modelname = 'INTRUDER';
                //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
                $data_arr[] = $mobj;
            }
        }

        if ($is_burgman == 0) //BURGMAN
        {
            $target = projectfunc::userSmpinSalesTarget($parent, $smpin, 'BURGMAN', $year_data, $month_data) * 3;
            if ($target > 0) {
                $mobj = new stdClass();
                $mobj->achieved = 0;
                $obj = new stdClass();
                $obj->model = 'UB125';
                $obj->variant = '';
                $obj->variantname = '';

                $mobj->model_image_url = projectfunc::modelimage($parent, $obj);

                $mobj->target = $target;
                $mobj->modelname = 'BURGMAN';
                //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
                $data_arr[] = $mobj;
            }
        }

//        if($is_access==0) //ACCESS
        //        {
        //            $target=projectfunc::userSmpinSalesTarget($parent,$smpin,'ACCESS 125',$year_data,$month_data)*3;
        //            if($target>0)
        //            {
        //                $mobj=new stdClass();
        //                $mobj->achieved=0;
        //                $obj=new stdClass();
        //                $obj->model='AC';
        //                $obj->variant='';
        //                $obj->variantname='';
        //
        //                $mobj->model_image_url=projectfunc::modelimage($parent, $obj);
        //
        //                $mobj->target=$target;
        //                $mobj->modelname='ACCESS 125';
        //                //$obj->modelname=($model=='GSX')? "GIXXER 250":"GIXXER SF 250";
        //                $data_arr[]=$mobj;
        //            }
        //        }

        //OTHER MODEL
        $query_salesmodelarr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) as achieved ", "smpin='" . $smpin . "' " . $datecond . " AND is_dms=1 AND is_deleted=0 AND model1 NOT IN ('GSX','GSXSF','GL','UB125','AC','ACZ') ", "");
        if ($query_salesmodelarr['nr'] > 0 && $query_salesmodelarr['result'][0]->achieved > 0) {
            $res = $query_salesmodelarr['result'][0];

            $mobj = new stdClass();
            $mobj->achieved = $res->achieved;
            $mobj->model_image_url = NOIMAGE_URL;
            $mobj->modelname = 'OTHERS';
            //$mobj->target=projectfunc::userSmpinSalesTarget($parent,$smpin,'OTHERS',$year_data,$month_data)*3;
            $mobj->target = 0;
            $data_arr[] = $mobj;

        }
        //LOG

        $logstr = 'DEALER ENQUIRY MODEL WISE LIST';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// DSEDETAILS //////////////////////////////////////////////// */
    private function getdsedetails($parent, $smpin)
    {
        $data = $parent->selectQuery(DMS_EMPLOYEE, "*", "smpin='" . $smpin . "' AND status=1 AND is_current=1", "1");
        if (!$data['nr'] > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data['error2']), 406);
        }

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

    /* /////////////////////////////// SALES //////////////////////////////////////////////// */
    private function dealerenquirysales($parent)
    {
        //dashboard/2018/2
        globalfunc::checkMethodType($parent, array('GET'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        $smpin = escape_str($parent->params[1]);
        $dse_details = $this->getdsedetails($parent, $smpin);

        $year_data = escape_str($parent->params[2]);
        $last_data = escape_str($parent->params[3]);
        $last_data_arr = explode('?', $last_data);
        $month_data = escape_str(trim($last_data_arr[0]));
        $data_arr = array();

        //GET MODEL
        $model_cond = " AND user_type=0 ";

        //GET SEGMENT
        //$segment= projectfunc::userSegment($parent,$parent->current_smpin);
        $segment = $dse_details->segment;

        $salemodel_arr = array();
        $query_salesmodelarr = $parent->selectQuery(DMS_EMPLOYEE_SALES, " model ", "smpin='" . $smpin . "' AND sale_date>='" . date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01")) . "' AND sale_date<='" . date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01")) . "' GROUP BY model ", "");
        if ($query_salesmodelarr['nr']) {
            foreach ($query_salesmodelarr['result'] as $res) {
                $salemodel_arr[] = $res->model;
            }
            $salemodel_quotes = array_map("quotes_array", $salemodel_arr);
            $model_cond .= " AND ( (segment='" . $segment . "' OR segment='0' AND model_is_target=1)OR (model_name IN (" . @implode(',', $salemodel_quotes) . ")) )";
        } else {
            $model_cond .= " AND (segment='" . $segment . "' OR segment='0' )";
        }

        $query_modelarr = $parent->selectQuery(TABLE_MODEL, " model_name,model_image ", " user_type=0  " . $model_cond . " ORDER BY model_id", "");
        if ($query_modelarr['nr']) {

            foreach ($query_modelarr['result'] as $res) {
                $res->model_image_url = ($res->model_image != '') ? SITE_URL . "hypermupload/model/" . $res->model_image : NOIMAGE_URL;
                $user_target = projectfunc::userSmpinSalesTarget($parent, $smpin, $res->model_name, $year_data, $month_data);

//                if($user_target>0)
                //                {
                $res->target = ($user_target) ? $user_target : 0;
                $res->achieved = projectfunc::userSmpinSalesAchieved($parent, $smpin, $res->model_name, $year_data, $month_data);
                $res->achieved = ($res->achieved) ? $res->achieved : 0;
                $data_arr[] = $res;
//                }
            }
        }

        //LOG
        $logstr = 'DEALER DSE SALES LIST';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data_arr, 'message' => $this->page_data['success1']), 200);
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

}
