<?php
/****************************
 * File : managerenquiry.php
 * Author : Cygnusa
 * @2020
 ****************************/

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

//FUNCTION LISTS
function comparator($object1, $object2)
{
    return $object1->order > $object2->order;
}
class managerenquiry
{
    private $smpin = '';

    public function __construct($parent)
    {

        //GET PAGE MESSAGE
        $this->page_data = globalfunc::getMessage($parent, $parent->lang, 'enquiry'); //ERROR MSG FROM ENQUIRY
        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]);

            switch ($parameter_val) {
                case 'getzmlist':
                case 'getrmlist':
                case 'getamlist':
                case 'getdealerlist':
                case 'getdselist':
                case 'dashboard':
                case 'enquirysales':
                case 'listdata':
                case 'plannerlist':
                    $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);
    }

    /* /////////////////////////////// ZONE MANAGER LIST //////////////////////////////////////////////// */
    private function getzmlist($parent)
    {

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

        projectfunc::authorizedRole($parent, array('3'));

        $query_str = "SELECT zm_name as name,zm_email as value FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0'  GROUP BY zm_email ORDER BY zm_name ASC";
        $data_arr = $parent->getQueryValue($query_str);

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

    /* /////////////////////////////// REGIONAL MANAGER LIST //////////////////////////////////////////////// */
    private function getrmlist($parent)
    {

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

        projectfunc::authorizedRole($parent, array('3', '4', '5'));

        //USER WISE CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            $REQUEST_KEYS = array('zmemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

            $zmemail = escape_str($parent->_request["zmemail"]);
            $dcond = " AND zm_email='" . $zmemail . "'";
        } else if ($parent->current_user_role == '4') //ZM
        {
            $dcond = " and zm_email='" . $parent->current_user_data->emailid . "'";
        } else if ($parent->current_user_role == '5') //TRAINER
        {
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';
        }

        $query_str = "SELECT rm_name as name,rm_email as value FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $dcond . " GROUP BY rm_email  ORDER BY rm_name ASC ";
        $data_arr = $parent->getQueryValue($query_str);

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

    /* /////////////////////////////// AREA MANAGER LIST //////////////////////////////////////////////// */
    private function getamlist($parent)
    {

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

        projectfunc::authorizedRole($parent, array('3', '4', '5', '6'));

        //USER WISE CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            $REQUEST_KEYS = array('zmemail' => 'string', 'rmemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        } else if ($parent->current_user_role == '4') //ZM
        {
            $dcond = " and zm_email='" . $parent->current_user_data->emailid . "'";

            $REQUEST_KEYS = array('rmemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        } else if ($parent->current_user_role == '5') //TRAINER
        {
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';

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

        } else if ($parent->current_user_role == '6') //RM
        {
            $dcond = " and rm_email='" . $parent->current_user_data->emailid . "'";
        }

        $zmemail = escape_str($parent->_request["zmemail"]);
        $dcond .= ($zmemail != 'all' && $zmemail != '') ? " AND zm_email='" . $zmemail . "'" : '';

        $rmemail = escape_str($parent->_request["rmemail"]);
        $dcond .= ($rmemail != 'all' && $rmemail != '') ? " AND rm_email='" . $rmemail . "'" : '';

        $query_str = "SELECT am_name as name,am_email as value FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $dcond . " GROUP BY am_email  ORDER BY am_name ASC";
        $data_arr = $parent->getQueryValue($query_str);

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

    /* /////////////////////////////// DEALER MANAGER LIST //////////////////////////////////////////////// */
    private function getdealerlist($parent)
    {

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

        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

        //USER WISE CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            $REQUEST_KEYS = array('zmemail' => 'string', 'rmemail' => 'string', 'amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        } else if ($parent->current_user_role == '4') //ZM
        {
            $dcond = " and zm_email='" . $parent->current_user_data->emailid . "'";

            $REQUEST_KEYS = array('rmemail' => 'string', 'amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        } else if ($parent->current_user_role == '5') //TRAINER
        {
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';

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

        } else if ($parent->current_user_role == '6') //RM
        {
            $dcond = " and rm_email='" . $parent->current_user_data->emailid . "'";
            $REQUEST_KEYS = array('amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        } else if ($parent->current_user_role == '7') //AM
        {
            $dcond = " and am_email='" . $parent->current_user_data->emailid . "'";
        }

        $zmemail = escape_str($parent->_request["zmemail"]);
        $dcond .= ($zmemail != 'all' && $zmemail != '') ? " AND zm_email='" . $zmemail . "'" : '';

        $rmemail = escape_str($parent->_request["rmemail"]);
        $dcond .= ($rmemail != 'all' && $rmemail != '') ? " AND rm_email='" . $rmemail . "'" : '';

        $amemail = escape_str($parent->_request["amemail"]);
        $dcond .= ($amemail != 'all' && $amemail != '') ? " AND am_email='" . $amemail . "'" : '';

        $query_str = "SELECT dealer_name as name,dealer_code as value FROM " . DMS_DEALER . " WHERE status='1' AND is_deleted='0' " . $dcond . "  ORDER BY dealer_name ASC ";
        $data_arr = $parent->getQueryValue($query_str);

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

    /* /////////////////////////////// DSE LIST //////////////////////////////////////////////// */
    private function getdselist($parent)
    {

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

        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

        //USER WISE CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            $REQUEST_KEYS = array('zmemail' => 'string', 'rmemail' => 'string', 'amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        } else if ($parent->current_user_role == '4') //ZM
        {
            $dcond = " and d.zm_email='" . $parent->current_user_data->emailid . "'";

            $REQUEST_KEYS = array('rmemail' => 'string', 'amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        } else if ($parent->current_user_role == '5') //TRAINER
        {
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND d.zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';

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

        } else if ($parent->current_user_role == '6') //RM
        {
            $dcond = " and d.rm_email='" . $parent->current_user_data->emailid . "'";
            $REQUEST_KEYS = array('amemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        } else if ($parent->current_user_role == '7') //AM
        {
            $dcond = " and d.am_email='" . $parent->current_user_data->emailid . "'";
        }

        $zmemail = escape_str($parent->_request["zmemail"]);
        $dcond .= ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';

        $rmemail = escape_str($parent->_request["rmemail"]);
        $dcond .= ($rmemail != 'all' && $rmemail != '') ? " AND d.rm_email='" . $rmemail . "'" : '';

        $amemail = escape_str($parent->_request["amemail"]);
        $dcond .= ($amemail != 'all' && $amemail != '') ? " AND d.am_email='" . $amemail . "'" : '';

        $dealer_code = escape_str($parent->_request["dealer_code"]);
        $dcond .= ($dealer_code != 'all' && $dealer_code != '') ? " AND d.dealer_code='" . $dealer_code . "'" : '';

        $segment = escape_str($parent->_request["segment"]);
        $history = escape_str($parent->_request["history"]);
        $segment_cond = $user_cond = "";

        switch ($segment) {
            case '1':$user_cond .= " AND e.segment='1'";
                break;
            case '2':$user_cond .= " AND e.segment='2'";
                break;
        }

        $user_cond .= ($history == '1') ? " AND u.status='1'  AND u.is_deleted=0" : " AND e.is_current='1'";

        $query_str = "SELECT e.smpin,e.fname,e.lname,e.mobile,CASE WHEN u.userid is NULL THEN '0' WHEN u.userid is NOT NULL AND u.status=1 THEN '1' ELSE '2' END as userstatus ,CASE WHEN u.userid is NULL THEN '3' WHEN u.userid is NOT NULL AND u.status=1 THEN '1' ELSE '2' END as userorder,COALESCE(u.profile_photo,'') as profile_photo FROM " . DMS_EMPLOYEE . " e JOIN " . DMS_DEALER . " d ON d.dealer_code=e.dealer_code LEFT JOIN " . TABLE_USER . " u ON u.smpin=e.smpin  WHERE e.status=1 " . $dcond . $segment_cond . $user_cond . " ORDER BY userorder ASC,e.fname ASC";
        $data_arr = $parent->getQueryValue($query_str);

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

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

        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

        //USER CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            //ALL INDIA

        } else if ($parent->current_user_role == '4') //ZM
        {
            $dcond = " and d.zm_email='" . $parent->current_user_data->emailid . "'";

        } else if ($parent->current_user_role == '5') //TRAINER
        {
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND d.zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';

        } else if ($parent->current_user_role == '6') //RM
        {
            $dcond = " and d.rm_email='" . $parent->current_user_data->emailid . "'";

        } else if ($parent->current_user_role == '7') //AM
        {
            $dcond = " and d.am_email='" . $parent->current_user_data->emailid . "'";
        }

        $zmemail = escape_str($parent->_request["zmemail"]);
        $dcond .= ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';

        $rmemail = escape_str($parent->_request["rmemail"]);
        $dcond .= ($rmemail != 'all' && $rmemail != '') ? " AND d.rm_email='" . $rmemail . "'" : '';

        $amemail = escape_str($parent->_request["amemail"]);
        $dcond .= ($amemail != 'all' && $amemail != '') ? " AND d.am_email='" . $amemail . "'" : '';

        $dealer_code = escape_str($parent->_request["dealer_code"]);
        $dcond .= ($dealer_code != 'all' && $dealer_code != '') ? " AND d.dealer_code='" . $dealer_code . "'" : '';

        $smpin = escape_str($parent->_request["smpin"]);
        if ($smpin != '') //FOR DSE
        {
            $today_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . CUR_DATE . "' AND modeofcontact IN ('S','V') AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            //TESTRIDE
            $today_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . CUR_DATE . "' AND enquiry_stage='T'  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            //CALL
            $today_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . CUR_DATE . "'  AND modeofcontact IN ('E','F','O','T')  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");

            //pending task last 10 days only
            $last_ten_date = date('Y-m-d', strtotime('-10 day', strtotime(NOW)));
            //PENDING ENQUIRY MASTER
            $pending_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND modeofcontact IN ('S','V')   AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            $pending_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND enquiry_stage='T' AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            $pending_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND modeofcontact IN ('E','F','O','T')  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            //TOMORROW ENQUIRY MASTER
            $tomorrow_date = date('Y-m-d', strtotime('+1 day', strtotime(NOW)));
            $tomorrow_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . $tomorrow_date . "' AND modeofcontact IN ('S','V') AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            //TESTRIDE
            $tomorrow_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . $tomorrow_date . "' AND enquiry_stage='T'  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            //CALL
            $tomorrow_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND  DATE(newvechicledatatime)= '" . $tomorrow_date . "'  AND modeofcontact IN ('E','F','O','T')   AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");

        } else {

            $join_table = " JOIN " . TABLE_USER . " u ON e.smpin=u.smpin ";
            $dcond .= "  AND u.status='1' AND u.is_deleted = 0";

            //VISIT
            $today_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND e.modeofcontact IN ('S','V') AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            //TESTRIDE
            $today_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND enquiry_stage='T'  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            //CALL
            $today_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND modeofcontact IN ('E','F','O','T')  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");

            //pending task last 10 days only
            $last_ten_date = date('Y-m-d', strtotime('-10 day', strtotime(NOW)));
            //PENDING ENQUIRY MASTER
            $pending_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", "   e.newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND e.newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND e.modeofcontact IN ('S','V') AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $pending_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", "   newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND enquiry_stage='T' AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $pending_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", "   newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00' AND modeofcontact IN ('E','F','O','T')  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");

            //TOMORROW ENQUIRY MASTER
            //VISIT
            $tomorrow_visit_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND e.modeofcontact IN ('S','V') AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            //TESTRIDE
            $tomorrow_testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND   enquiry_stage='T'  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            //CALL
            $tomorrow_call_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table, "COUNT(e.id) data_count", " DATE(e.newvechicledatatime)= '" . CUR_DATE . "' AND modeofcontact IN ('E','F','O','T')  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V') AND p.status=1 AND p.is_current=1 " . $dcond, "1");
        }

        $today_visit = $today_visit_arr['result'][0]->data_count;
        $today_testride = $today_testride_arr['result'][0]->data_count;
        $today_call = $today_call_arr['result'][0]->data_count;

        $tomorrow_visit = $tomorrow_visit_arr['result'][0]->data_count;
        $tomorrow_testride = $tomorrow_testride_arr['result'][0]->data_count;
        $tomorrow_call = $tomorrow_call_arr['result'][0]->data_count;

        $pending_visit = $pending_visit_arr['result'][0]->data_count;
        $pending_testride = $pending_testride_arr['result'][0]->data_count;
        $pending_call = $pending_call_arr['result'][0]->data_count;

        $today = array('visit' => $today_visit, 'testride' => $today_testride, 'call' => $today_call);
        $pending = array('visit' => $pending_visit, 'testride' => $pending_testride, 'call' => $pending_call);
        $tomorrow = array('visit' => $tomorrow_visit, 'testride' => $tomorrow_testride, 'call' => $tomorrow_call);

        $data = array('today' => $today, 'pending' => $pending, 'tomorrow' => $tomorrow);

        //LOG
        $logstr = 'MANAGER ENQUIRY DASHBOARD';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data, 'message' => $this->page_data['success1']), 200);

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

    /* /////////////////////////////// DASHBOARD SALES //////////////////////////////////////////////// */
    private function enquirysales($parent)
    {

        //sales/2018/2
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

        $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"]) && $parent->_request["zmemail"] != '') ? escape_str($parent->_request["zmemail"]) : 'all';
        $rmemailid = (isset($parent->_request["rmemail"]) && $parent->_request["rmemail"] != '') ? escape_str($parent->_request["rmemail"]) : 'all';
        $amemailid = (isset($parent->_request["amemail"]) && $parent->_request["amemail"] != '') ? escape_str($parent->_request["amemail"]) : 'all';

        //USER CONDITION
        if ($parent->current_user_role == '3') //HO
        {
            //ALL INDIA
            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 {
                $get_role = '4';
                $manageremail = $parent->current_user_data->emailid;
            }
            $dcond = " and d.zm_email='" . $parent->current_user_data->emailid . "'";

        } 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;
            }
            $get_trainer_str = "SELECT h.zone 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);
            $dcond = (count($trainer_arr) > 0) ? " AND d.zone='" . $trainer_arr[0]->zone . "'" : ' AND id=0';

        } else if ($parent->current_user_role == '6') //RM
        {
            //$vkvtest = escape_str($parent->_request["vkvtest"]);

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

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

            }
            $dcond = " and d.rm_email='" . $parent->current_user_data->emailid . "'";

        } else if ($parent->current_user_role == '7') //AM
        {
            if ($amemailid != 'all') {
                $get_role = '7';
                $manageremail = $amemailid;
            } else {
                $get_role = '7';
                $manageremail = $parent->current_user_data->emailid;
            }
            $dcond = " and d.am_email='" . $parent->current_user_data->emailid . "'";
        }

        $zmemail = escape_str($parent->_request["zmemail"]);
        $dcond .= ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';

        $rmemail = escape_str($parent->_request["rmemail"]);
        $dcond .= ($rmemail != 'all' && $rmemail != '') ? " AND d.rm_email='" . $rmemail . "'" : '';

        $amemail = escape_str($parent->_request["amemail"]);
        $dcond .= ($amemail != 'all' && $amemail != '') ? " AND d.am_email='" . $amemail . "'" : '';

        $dealer_code = escape_str($parent->_request["dealer_code"]);
        $dcond .= ($dealer_code != 'all' && $dealer_code != '') ? " AND d.dealer_code='" . $dealer_code . "'" : '';

        $smpin = escape_str($parent->_request["smpin"]);
        if ($smpin != '') {
            //*****************SALES****************

            //TARGET
            $user_target = projectfunc::userSmpinSalesTarget($parent, $smpin, '', $year_data, $month_data);
            //ACHIEVED
            $user_achieved = projectfunc::userSmpinSalesAchieved($parent, $smpin, '', $year_data, $month_data);
            $sales = array('target' => $user_target, 'achieved' => $user_achieved, 'month' => date('M', strtotime('2018-' . $month_data . "-01")), 'year' => $year_data);

            //****************ENQUIRY****************
            $achieved_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%'  AND is_dms=1 AND is_deleted=0", "1");
            //TARGET
            //$enquiry_target = (string) ($user_target * 3); //SALES TARGET multiple by 3

            $emp_arr = $parent->selectQuery(DMS_EMPLOYEE, "segment", "smpin='" . $smpin . "'", "1");
            $segment = ($emp_arr['nr'] > 0) ? $emp_arr['result'][0]->segment : 0;
            switch ($segment) {
                case '1': //TARGET
                    $enquiry_target = (string) ($user_target * 5); //SALES TARGET multiple by 5
                    break;
                case '2': //TARGET
                    $enquiry_target = (string) ($user_target * 3); //SALES TARGET multiple by 3
                    break;
                default:
                    $enquiry_target = (string) ($user_target * 3); //SALES TARGET multiple by 3
                    break;
            }

            //ACHIEVED
            $enquiry_achieved = $achieved_arr['result'][0]->data_count;
            $enquiry = array('target' => $enquiry_target, 'achieved' => $enquiry_achieved, 'month' => date('M', strtotime('2018-' . $month_data . "-01")), 'year' => $year_data);

            //*****************STATUS
            $hot_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND status='H'  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            $warm_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND status='W'  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");
            $cold_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND status='P'  AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')", "1");

            $hot = $hot_arr['result'][0]->data_count;
            $warm = $warm_arr['result'][0]->data_count;
            $cold = $cold_arr['result'][0]->data_count;
            $status_arr = array("hot" => $hot, "warm" => $warm, "cold" => $cold);

            //*****************STAGE
            $testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND enquiry_stage='T' AND is_dms=1 AND is_deleted=0 ", "1");
            $booked_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND enquiry_stage='O' AND is_dms=1 AND is_deleted=0 ", "1");
            $invoiced_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND enquiry_stage IN('Z','P') AND is_dms=1 AND is_deleted=0 ", "1");
            $lost_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND status='L' AND is_dms=1 AND is_deleted=0 ", "1");
            $postponed_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND status='E' AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V') ", "1");
            $delivered_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER, "COUNT(id) data_count", " smpin='" . $smpin . "' AND created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND enquiry_stage IN ('O','V') AND is_dms=1 AND is_deleted=0 ", "1");
        } else {
            //*****************SALES****************

            //TARGET
            //$user_target= projectfunc::userSmpinSalesTarget($parent,$smpin,'',$year_data,$month_data);
            $sales_arr = projectfunc::managersalesEnquiryDashboard($parent, $get_role, $year_data, $month_data, $manageremail); // BASED ON YEAR OR MONTH
            $user_target = $sales_arr['target'];

            $join_table = " JOIN " . TABLE_USER . " u ON e.smpin=u.smpin ";
            $dcond .= "  AND u.status='1' AND u.is_deleted = 0";
            //ACHIEVED
            //$user_achieved= projectfunc::userSmpinSalesAchieved($parent,$smpin,'',$year_data,$month_data);
            $user_achieved = $sales_arr['achieved'];
            $sales = array('target' => $user_target, 'achieved' => $user_achieved, 'month' => date('M', strtotime('2018-' . $month_data . "-01")), 'year' => $year_data);

            //****************ENQUIRY****************
            $achieved_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", "  e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%'  AND e.is_dms=1 AND e.is_deleted=0 AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            //TARGET
            $enqtarget_arr = projectfunc::managerEnquiryTarget($parent, $get_role, $year_data, $month_data, $manageremail); // BASED ON YEAR OR MONTH
            $enquiry_target = $enqtarget_arr['target'];
            //$enquiry_target = (string) ($user_target * 3); //SALES TARGET multiple by 3

            //ACHIEVED
            $enquiry_achieved = $achieved_arr['result'][0]->data_count;
            $enquiry = array('target' => $enquiry_target, 'achieved' => $enquiry_achieved, 'month' => date('M', strtotime('2018-' . $month_data . "-01")), 'year' => $year_data);

            //*****************STATUS
            $hot_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", "  e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.status='H'  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V')  AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $warm_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.status='W'  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V')  AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $cold_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.status='P'  AND e.is_dms=1 AND e.is_deleted=0 AND e.enquiry_stage NOT IN ('Z','O','P','V')  AND p.status=1 AND p.is_current=1 " . $dcond, "1");

            $hot = $hot_arr['result'][0]->data_count;
            $warm = $warm_arr['result'][0]->data_count;
            $cold = $cold_arr['result'][0]->data_count;
            $status_arr = array("hot" => $hot, "warm" => $warm, "cold" => $cold);

            //*****************STAGE
            $testride_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.enquiry_stage='T' AND e.is_dms=1 AND e.is_deleted=0 AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $booked_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.enquiry_stage='O' AND e.is_dms=1 AND e.is_deleted=0 AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $invoiced_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.enquiry_stage IN('Z','P') AND e.is_dms=1 AND e.is_deleted=0 ", "1");
            $lost_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.status='L' AND e.is_dms=1 AND e.is_deleted=0 AND p.status=1 AND p.is_current=1 " . $dcond, "1");
            $postponed_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.status='E' AND e.is_dms=1 AND e.is_deleted=0 AND p.status=1 AND p.is_current=1 AND e.enquiry_stage NOT IN ('Z','O','P','V') " . $dcond, "1");
            $delivered_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e JOIN " . DMS_EMPLOYEE . " p  ON e.smpin=p.smpin JOIN " . DMS_DEALER . " d ON d.dealer_code=p.dealer_code " . $join_table . " ", "COUNT(e.id) data_count", " e.created_on LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%' AND e.enquiry_stage IN ('O','V') AND e.is_dms=1 AND e.is_deleted=0 " . $dcond, "1");
        }

        $testride = $testride_arr['result'][0]->data_count;
        $booked = $booked_arr['result'][0]->data_count;
        $invoiced = $invoiced_arr['result'][0]->data_count;
        $lost = $lost_arr['result'][0]->data_count;
        $postponed = $postponed_arr['result'][0]->data_count;
        $delivered = $delivered_arr['result'][0]->data_count;

        $stage_arr = array("testride" => $testride, 'booked' => $booked, 'invoiced' => $invoiced, 'lost' => $lost, 'postponed' => $postponed, 'delivered' => $delivered);

        $data = array('enquiry' => $enquiry, 'sales' => $sales, 'status' => $status_arr, 'stage' => $stage_arr);
        //LOG
        $logstr = 'MANAGER SALES DASHBOARD';
        globalfunc::insertUserLog($parent, $logstr);
        $parent->response(array('response' => 'success', 'data' => $data, 'message' => $this->page_data['success1']), 200);

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

    /* /////////////////////////////// GET DSE DATA //////////////////////////////////////////////// */
    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['error1']), 406);
        }
        return $data['result'][0];
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// LIST DATA //////////////////////////////////////////////// */
    private function listdata($parent)
    {

        //sales/2018/2
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

        $smpin_str = @explode('?', escape_str($parent->params[1]));
        $smpin = $smpin_str[0];
        $dse_details = $this->getdsedetails($parent, $smpin);

        //$smpin = $parent->current_user_data->smpin;
        try {
            //PAGINATION SETTINGS
            $limit_cond = '';
            $data_arr = array();
            $limit_str = globalfunc::setPageLimit($parent, $parent->params[1]);
            $limit_cond = ($limit_str) ? " LIMIT " . $limit_str : '';

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

            $cond = '';
            switch ($listtype) {
                case 'today':
                    $todaytype = escape_str($parent->_request['todaytype']);

                    if (strtolower($todaytype) == 'visit') {
                        $cond = " AND e.modeofcontact IN ('S','V')";
                    } else if (strtolower($todaytype) == 'test drive') {
                        $cond = " AND e.enquiry_stage='T'";
                    } else if (strtolower($todaytype) == 'call') {
                        $cond = " AND e.modeofcontact IN ('E','F','O','T')";
                    }
                    $cond .= " AND enquiry_stage NOT IN ('Z','O','P','V') AND DATE(e.newvechicledatatime)= '" . CUR_DATE . "'";
                    break;
                case 'tomorrow':
                    $todaytype = escape_str($parent->_request['todaytype']);

                    if (strtolower($todaytype) == 'visit') {
                        $cond = " AND e.modeofcontact IN ('S','V')";
                    } else if (strtolower($todaytype) == 'test drive') {
                        $cond = " AND e.enquiry_stage='T'";
                    } else if (strtolower($todaytype) == 'call') {
                        $cond = " AND e.modeofcontact IN ('E','F','O','T')";
                    }

                    $tomorrow_date = date('Y-m-d', strtotime('+1 day', strtotime(NOW)));
                    $cond .= " AND enquiry_stage NOT IN ('Z','O','P','V') AND DATE(e.newvechicledatatime)= '" . $tomorrow_date . "'";
                    break;
                case 'date':
                    $REQUEST_KEYS = array('date' => 'date|Y-m-d');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $date = escape_str($parent->_request['date']);

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

                    if (strtolower($todaytype) == 'visit') {
                        $cond = " AND e.modeofcontact IN ('S','V')";
                    } else if (strtolower($todaytype) == 'test drive') {
                        $cond = " AND e.enquiry_stage='T'";
                    } else if (strtolower($todaytype) == 'call') {
                        $cond = " AND e.modeofcontact IN ('E','F','O','T')";
                    }
                    $cond .= " AND enquiry_stage NOT IN ('Z','O','P','V') AND DATE(newvechicledatatime)= '" . $date . "'";
                    break;
                case 'pending':
                    $REQUEST_KEYS = array('pendingtype' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $pendingtype = escape_str($parent->_request['pendingtype']);

                    if (strtolower($pendingtype) == 'visit') {
                        $cond = " AND e.modeofcontact IN ('S','V')";
                    } else if (strtolower($pendingtype) == 'test drive') {
                        $cond = "AND e.enquiry_stage='T'";
                    } else if (strtolower($pendingtype) == 'call') {
                        $cond = " AND e.modeofcontact IN ('E','F','O','T')";
                    }
                    $last_ten_date = date('Y-m-d', strtotime('-10 day', strtotime(NOW)));
                    $cond .= " AND enquiry_stage NOT IN ('Z','O','P','V') AND e.newvechicledatatime<'" . CUR_DATE . " 00:00:00' AND newvechicledatatime>'" . $last_ten_date . " 00:00:00'";
                    //$cond.=(in_array(strtolower($pendingtype),array("visit","test drive","call")))?" AND enquiry_stage='".$pendingtype."'":$parent->response(array('response'=>'failed', 'message' => $this->page_data['error5']), 406);
                    //$cond.= " AND enquiry_stage='".$pendingtype."'";
                    //$cond.= " AND enquiry_stage='".$pendingtype."'";
                    break;
                case 'hot':
                case 'warm':
                case 'cold':

                case 'testride':
                case 'booked':
                case 'invoiced':
                case 'lost':
                case 'postponed':

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

                    if (in_array($listtype, array("hot", "warm", "cold", 'lost', 'postponed'))) {
                        switch ($listtype) {
                            case 'hot':$listtype_str = 'H';
                                break;
                            case 'warm':$listtype_str = 'W';
                                break;
                            case 'cold':$listtype_str = 'P';
                                break;
                            case 'lost':$listtype_str = 'L';
                                break;
                            case 'postponed':$listtype_str = 'E';
                                break;
                        }
                        if ($listtype != 'lost') {
                            $cond .= " AND enquiry_stage NOT IN ('Z','O','P','V')";
                        }
                        $cond .= " AND e.status='" . $listtype_str . "'";
                    } else {
                        // $str=($listtype=='testride')?"T":$listtype;
                        switch ($listtype) {
                            case 'testride':$str = 'T';
                                break;
                            case 'booked':$str = 'O';
                                break;
                            case 'invoiced':$str = 'Z';
                                break;

                        }
                        $cond = " AND e.enquiry_stage='" . $str . "'";
                    }

                    $cond .= "AND e.created_on  LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%'";
                    break;
                case 'model':
                    $REQUEST_KEYS = array('month' => 'string', 'year' => 'number', 'model' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $month_data = escape_str($parent->_request['month']);
                    $year_data = escape_str($parent->_request['year']);
                    $model = escape_str($parent->_request['model']);

                    switch ($model) {
                        case 'INTRUDER':$cond .= " AND e.model1='GL'";
                            break;
                        case 'BURGMAN':$cond .= " AND e.model1='UB125'";
                            break;
                        case 'ACCESS 125':$cond .= " AND e.model1 IN ('AC','ACZ')";
                            break;
                        case 'GIXXER 250':$cond .= " AND e.model1 IN ('GSX','GSXSF') AND e.variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant LIKE '%250%')";
                            break;
                        case 'GIXXER':$cond .= " AND e.model1 IN ('GSX','GSXSF') AND e.variant1 IN (SELECT variant_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title_code IN ('GSX','GSXSF') AND variant NOT LIKE '%250%')";
                            break;
                        case 'OTHERS':$cond .= " AND e.model1 NOT IN ('GSX','GSXSF','GL','UB125','AC','ACZ')";
                            break;
                        default:$parent->response(array('response' => 'failed', 'message' => $this->page_data['error6']), 406);
                    }
                    $cond .= " AND e.created_on  LIKE '" . date('Y-m', strtotime($year_data . '-' . $month_data . "-01")) . "-%'";
                    break;
                default:$parent->response(array('response' => 'failed', 'message' => $this->page_data['error6']), 406);
                    break;
            }
            //SEARCH
            $model_cond = '';
            $search = escape_str($parent->_request['search']);
            if ($search != '') {
                $model = $parent->getQueryValue("SELECT title_code  FROM " . DMS_ENQUIRY_MODEL . " WHERE title LIKE '%" . $search . "%'");
                if (count($model) > 0) {
                    $model_arr = array_column($model, 'title_code');
                    $model_quotes = array_map("quotes_array", $model_arr);

                    $model_cond = (count($model_quotes) > 0) ? " OR e.model1 IN (" . @implode(',', $model_quotes) . ")" : '';
                }

                $cond = " AND (e.enq_num LIKE '%" . $search . "%' OR e.first_name LIKE '%" . $search . "%'  OR e.mobile LIKE '%" . $search . "%' " . $model_cond . ") ";
            }
            //$pending_testride_arr=$parent->selectQuery(DMS_ENQUIRY_MASTER,"COUNT(id) data_count"," smpin='".$smpin."' AND newvechicledatatime<'".CUR_DATE." 00:00:00' AND enquiry_stage='T' AND is_dms=1 AND is_deleted=0 AND enquiry_stage NOT IN ('Z','O','P','V')","1");

            //echo  "smpin='".$smpin."' ".$cond."  ORDER BY newvechicledatatime ASC, id DESC ".$limit_cond; exit;
            $data_arr = $parent->selectQuery(DMS_ENQUIRY_MASTER . " e ", "SQL_CALC_FOUND_ROWS e.*", "e.smpin='" . $smpin . "' AND e.is_dms=1 AND e.is_deleted=0 " . $cond . " ORDER BY e.newvechicledatatime ASC, id DESC " . $limit_cond, "");

            $total_arr = $parent->getQueryValue("SELECT FOUND_ROWS() as total_record");
            $total = (count($total_arr) > 0) ? $total_arr[0]->total_record : 0;

            //DATE TO STRING
            if ($data_arr['nr'] > 0) {
                foreach ($data_arr['result'] as $key => $res) {
                    //MASK
                    $data_arr['result'][$key]->mobile = maskenquiry($res->mobile);

                    //LEFT JOIN ".DMS_ENQUIRY_MODEL." m ON  m.title_code=e.model1
                    //,m.title as model_name_str
                    $model_arr = $parent->selectQuery(DMS_ENQUIRY_MODEL, "title as model_name,variant as variant_name,color as color_name", "title_code='" . $res->model1 . "' AND variant_code='" . $res->variant1 . "' AND color_code='" . $res->color1 . "'", "");

                    $data_arr['result'][$key]->model_name = ($model_arr['nr'] > 0) ? $model_arr['result'][0]->model_name : $res->model1;
                    $data_arr['result'][$key]->variant_name = ($model_arr['nr'] > 0) ? $model_arr['result'][0]->variant_name : $res->variant1;
                    $data_arr['result'][$key]->color_name = ($model_arr['nr'] > 0) ? $model_arr['result'][0]->color_name : $res->color1;
                    $data_arr['result'][$key]->enq_through = ($res->is_starapp == 1) ? "HYPER-M App" : "DMS";
                    $data_arr['result'][$key]->newvechicledatatime_str = projectfunc::enqdatediff($parent, NOW, $res->newvechicledatatime);

                    $checkdate = date('Y-m-d', strtotime($res->newvechicledatatime));
                    //0-overdue,1-current day,2-future
                    switch (true) {
                        case (strtotime($checkdate) < strtotime(CUR_DATE)): $data_arr['result'][$key]->enq_date_type = 0;
                            break;
                        case (strtotime($checkdate) == strtotime(CUR_DATE)): $data_arr['result'][$key]->enq_date_type = 1;
                            break;
                        case (strtotime($checkdate) > strtotime(CUR_DATE)): $data_arr['result'][$key]->enq_date_type = 2;
                            break;
                    }
                    ///PENDING ENQUIRY MASTER
                    if (strtoupper($res->enquiry_stage) == 'T') {
                        $data_arr['result'][$key]->icon_type = 1;
                    } else if (strtoupper($res->modeofcontact) == 'S' || strtoupper($res->modeofcontact) == 'V') {
                        $data_arr['result'][$key]->icon_type = 2;
                    } else if (in_array(strtoupper($res->modeofcontact), array('E', 'F', 'O', 'T'))) {
                        $data_arr['result'][$key]->icon_type = 3;
                    } else {
                        $data_arr['result'][$key]->icon_type = 0;
                    }
                    //visit,call,testdrive
                }
            }

            //LOG
            $logstr = 'MANAGER DSE ENQUIRY LIST';
            globalfunc::insertUserLog($parent, $logstr);

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

        } catch (PDOException $e) {
            $parent->response(array('response' => 'failed', 'message' => $e->getMessage()), 500);

        } catch (Exception $e) {
            $parent->response(array('response' => 'failed', 'message' => $e->getMessage()), 500);
        }
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// PLANNER LIST //////////////////////////////////////////////// */
    private function plannerlist($parent)
    {
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7'));

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

        $data = $parent->selectQuery(TABLE_USER, "userid", "smpin='" . $smpin . "' AND is_deleted='0' AND status='1' AND role>7 AND role<14", "1");
        $userid = ($data['nr'] > 0) ? $data['result'][0]->userid : '';

        //$userid = $parent->current_user_id;
        $type = escape_str($parent->_request['type']);

        $monthdata = array();
        $todaydata = array();
        $date_arr = array();

        $REQUEST_KEYS = array('type' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        if ($type == 'month') {
            $REQUEST_KEYS = array('month' => 'number', 'year' => 'number');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $month = escape_str($parent->_request['month']);
            $year = escape_str($parent->_request['year']);
            if (!globalfunc::isValidDateTimeString($year . '-' . $month . '-01', 'Y-m-d', 'Asia/Kolkata')) {
                $parent->response(array('response' => 'failed', 'message' => $this->page_data['error21']), 406);
            }

            $start_date = date('Y-m-d', strtotime($year . '-' . $month . '-01'));
            $end_date = date('Y-m-t', strtotime($year . '-' . $month . '-01'));

            $temp = $start_date;
            while (strtotime($temp) <= strtotime($end_date)) {
                $date_str = @explode('-', $temp);
                $value = $date_str[2];
                $monthdata[$value] = array('date' => $temp, 'data' => array());
                $temp = date('Y-m-d', strtotime('+ 1 day', strtotime($temp)));
            }

            $get_list_arr = $parent->selectQuery(TABLE_ENQUIRYPLANNER, "*,DATE_FORMAT(plan_date, '%d-%m-%Y')plandate,DATE_FORMAT(plan_fromtime, '%H:%i') fromtime,DATE_FORMAT(plan_totime, '%H:%i') totime", " is_deleted='0' AND userid='" . $userid . "' AND plan_date>='" . $start_date . "' AND plan_date<='" . $end_date . "' ORDER BY plan_fromtime ASC", "");

            if ($get_list_arr['nr'] > 0) {
                foreach ($get_list_arr['result'] as $res) {
                    $value = date('d', strtotime($res->plan_date));
                    $monthdata[$value]['data'][] = $res;
                }
            }
        } else if ($type == 'today') {
            $start_date = CUR_DATE;
            $end_date = CUR_DATE;

            $get_list_arr = $parent->selectQuery(TABLE_ENQUIRYPLANNER, "*,DATE_FORMAT(plan_date, '%d-%m-%Y')plandate,DATE_FORMAT(plan_fromtime, '%H:%i') fromtime,DATE_FORMAT(plan_totime, '%H:%i') totime", " is_deleted='0' AND userid='" . $userid . "'  AND plan_date>='" . $start_date . "' AND plan_date<='" . $end_date . "' ORDER BY plan_fromtime ASC", "");
            $todaydata = ($get_list_arr['nr'] > 0) ? $get_list_arr['result'] : array();
            $monthdata = new stdClass();
        } else if ($type == 'date') {
            $REQUEST_KEYS = array('date' => 'date|Y-m-d');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

            $date = escape_str($parent->_request['date']);
            $start_date = $date;
            $end_date = $date;

            $get_list_arr = $parent->selectQuery(TABLE_ENQUIRYPLANNER, "*,DATE_FORMAT(plan_date, '%d-%m-%Y')plandate,DATE_FORMAT(plan_fromtime, '%H:%i') fromtime,DATE_FORMAT(plan_totime, '%H:%i') totime", " is_deleted='0' AND userid='" . $userid . "'  AND plan_date>='" . $start_date . "' AND plan_date<='" . $end_date . "' ORDER BY plan_fromtime ASC", "");
            $date_arr = ($get_list_arr['nr'] > 0) ? $get_list_arr['result'] : array();

        } else {
            $parent->response(array('response' => 'failed', 'message' => "Invalid type format"), 406);
        }

        $data = array('month' => $monthdata, 'today' => $todaydata, 'date' => $date_arr);

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

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