<?php

/* * **************************
 * File : report.php
 * Author : Cygnusa
 * @2019
 * ************************** */

include "Connection.php";
$conn = new Connection();

if (isset($_REQUEST["funName"])) {
    $db = $conn->getConnection();
    $fun = $_REQUEST["funName"];

    switch ($fun) {
        case "user_registration":
        case "inactive_user_registration":
        case "user_registration_list": //list
        case "inactive_user_registration_list": //list
        case "page_access": //list
        case "content_report":
        case "content_report_list": //list
        case "accumulation_report":
        case "accumulation_report_list": //list
        case "redemption_report":
        case "redemption_report_list": //list
        case "redemptionapproved_report":
        case "redemptionapproved_report_list": //list
        case "assessment_report":
        case "assessment_report_list": //list
        case "sales_report":
        case "sales_report_list": //list
        case "certification_report":
        case "certification_report_list": //list
        case "setsalestarget_report":
        case "setsalestarget_report_list": //list
        case "attrition_report":
        case "activeinactive_report":
        case "user_registration_percent":
        case "inactive_user_registration_percent":
        case "customerexperiencelogin":
        case "enquiry":
        case "customerexperiencefeedback":
        case "customerexperiencefeedbackdetails":

        case 'user_registration_percent_v2':
        case 'activeinactive_report_v2':
        case 'page_access_v2':
        case 'dse_remark':

        case 'points_report_v2':
        case 'sales_report_v2':
        case 'certification_report_v2':
        case 'enquiry_report_v2':
        case 'customerexperience_report_v2':
        case "assessment_report_v2":
        case "setsalestarget_report_v2":
        case 'enquiry_report_v3': //ENQIURY REPORT SINGLE 19 FEB
        //case 'cefeedbacklist': //FEEDBACK LIST
        case "setsalestarget_report_v3":

        case "reward_point_dse":
        case "rewardpoint_data":
        case "useraccess_data":

        case 'bulletin_report':
        case 'bulletin_report_list':

            $fun();
            break;
        case 'content_report_v2':
        case 'content_report_list_v2':
        case 'content_report_zm':
        case 'content_report_trainer':
        case 'content_report_rm':
        case 'content_report_am':
        case 'content_report_list_zm':
        case 'content_report_list_trainer':
        case 'content_report_list_rm':
        case 'content_report_list_am':
            require_once "report_content.php";
            $fun();
            break;
        case 'enquiry_report':
        case 'enquiry_report_terms_condition':
        case 'temp_suspension':
            require_once "report_enquiry.php";
            $fun();
            break;

        default:
            $conn->message(false, "PAGE NOT FOUND");exit;
            break;
    }
}

//isValidDateTimeString
function isValidDateTimeString($str_dt, $str_dateformat, $str_timezone)
{
    $date = DateTime::createFromFormat($str_dateformat, $str_dt, new DateTimeZone($str_timezone));
    return $date && $date->format($str_dateformat) == $str_dt;
}

//getReportFormat
function getReportFormat($from_date, $to_date)
{
    $data_arr = array();
    //check same month

    if (date('Y-m', strtotime($from_date)) == date('Y-m', strtotime($to_date))) {

        $data_arr = array('type' => 'date');
        $data_arr['date'][] = $from_date;
        $temp_date = $from_date;

        while (strtotime($temp_date) < strtotime($to_date)) {
            $temp_date = date('Y-m-d', strtotime('+1 day', strtotime($temp_date)));
            $data_arr['date'][] = $temp_date;
        }
        $data_arr['from_date'] = $from_date;
        $data_arr['to_date'] = $to_date;

    } else {
        $data_arr = array('type' => 'month');
        $data_arr['date'][] = $from_date;
        $temp_date = date('Y-m', strtotime($from_date)) . "-01";
        $end_month = date('Y-m', strtotime($to_date)) . "-01";
        while (strtotime($temp_date) < strtotime($end_month)) {
            $temp_date = date('Y-m', strtotime('+1 month', strtotime($temp_date))) . "-01";
            $data_arr['date'][] = $temp_date;
        }
        $data_arr['from_date'] = $from_date;
        $data_arr['to_date'] = $to_date;
    }
    return $data_arr;
}
//getmonthReportFormat
function getmonthReportFormat($from_date, $to_date)
{
    $data_arr = array();

    $data_arr = array('type' => 'month');
    $data_arr['date'][] = $from_date;
    $temp_date = date('Y-m', strtotime($from_date)) . "-01";
    $end_month = date('Y-m', strtotime($to_date)) . "-01";
    while (strtotime($temp_date) < strtotime($end_month)) {
        $temp_date = date('Y-m', strtotime('+1 month', strtotime($temp_date))) . "-01";
        $data_arr['date'][] = $temp_date;
    }
    $data_arr['from_date'] = $from_date;
    $data_arr['to_date'] = $to_date;

    return $data_arr;
}

//converttype
function converttype()
{
    $_REQUEST['datetype'] = (isset($_REQUEST["datetype"])) ? escape_str($_REQUEST["datetype"]) : '0';
    switch ($_REQUEST['datetype']) {
        case "1":
            $_REQUEST["from_date"] = date('Y-m', strtotime('-1 month', strtotime(NOW))) . "-01";
            $_REQUEST["to_date"] = date('Y-m-t', strtotime('-1 month', strtotime(NOW)));
            break;
        case "2":
            $_REQUEST["from_date"] = date('Y-m-d', strtotime('-4 month', strtotime(NOW)));
            $_REQUEST["to_date"] = date('Y-m-t', strtotime('-1 month', strtotime(NOW)));
            break;
        case "3":
            $cur_month = date('n', strtotime(NOW));

            if ($cur_month <= 3) {
                $_REQUEST["from_date"] = (date('Y', strtotime(NOW)) - 1) . "-10-01";
                $_REQUEST["to_date"] = (date('Y', strtotime(NOW)) - 1) . "-12-31";

            } else if ($cur_month >= 4 && $cur_month <= 6) {
                $_REQUEST["from_date"] = date('Y', strtotime(NOW)) . "-01-01";
                $_REQUEST["to_date"] = date('Y', strtotime(NOW)) . "-03-31";

            } else if ($cur_month >= 7 && $cur_month <= 9) {
                $_REQUEST["from_date"] = date('Y', strtotime(NOW)) . "-04-01";
                $_REQUEST["to_date"] = date('Y', strtotime(NOW)) . "-06-30";

            } else if ($cur_month >= 10 && $cur_month <= 12) {
                $_REQUEST["from_date"] = date('Y', strtotime(NOW)) . "-07-01";
                $_REQUEST["to_date"] = date('Y', strtotime(NOW)) . "-09-30";
            }

            break;
        case "4":
            break;
        default:
            $_REQUEST['datetype'] = 0;
            //$_REQUEST["from_date"]=date('Y',strtotime(NOW))."-07-01";
            //$_REQUEST["to_date"]=(date('Y',strtotime(NOW))-1)."-09-30";
            break;

    }
}

// user_registration
function user_registration()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= " AND e.is_current=1 ";
            }

            $label_arr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT COUNT(u.userid) as ucount FROM " . TABLE_USER . " u  " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(u.created_on,'%Y-%m-%d')='" . $res . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $count);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT COUNT(u.userid) as ucount FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(u.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $count);
                }

            }

            $query_str = "SELECT COUNT(u.userid) as totcount FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.created_on>='" . $from_date . " 00:00:00' AND u.created_on<='" . $to_date . " 23:59:59' " . $user_cond . "  LIMIT 1 ";
            $count_arr = $conn->getQueryValue($query_str);
            $totcount = (count($count_arr) > 0) ? $count_arr[0]->totcount : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $totcount);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration
function inactive_user_registration()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted=0  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= " AND e.is_current=1 ";
            }

            $label_arr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT COUNT(u.userid) as ucount FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=2 AND DATE_FORMAT(u.created_on,'%Y-%m-%d')='" . $res . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $count);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT COUNT(u.userid) as ucount FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=2 AND DATE_FORMAT(u.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $count);
                }

            }

            $query_str = "SELECT COUNT(u.userid) as totcount FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=2 AND u.created_on>='" . $from_date . " 00:00:00' AND u.created_on<='" . $to_date . " 23:59:59' " . $user_cond . "  LIMIT 1 ";
            $count_arr = $conn->getQueryValue($query_str);
            $totcount = (count($count_arr) > 0) ? $count_arr[0]->totcount : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $totcount);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration_list
function user_registration_list()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            //$data_arr=getReportFormat($from_date,$to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $user_cond .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.status,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " u LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.created_on>='" . $from_date . " 00:00:00' AND u.created_on<='" . $to_date . " 23:59:59' " . $user_cond;
            $query = $conn->query($query_str);

            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration_list
function inactive_user_registration_list()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        /*if(isset($_SESSION["userid"]))
        { */
        //CONVERT converttype()
        converttype();
        //$from_date = escape_str($_REQUEST["from_date"]);
        //$to_date = escape_str($_REQUEST["to_date"]);

        //if(!($from_date!='' && $to_date!=''))  $conn->message(false,$conn->getMessage($lang,'report','error6'));
        //if(!(isValidDateTimeString($from_date,'Y-m-d','Asia/Kolkata') && isValidDateTimeString($to_date,'Y-m-d','Asia/Kolkata')))   $conn->message(false,$conn->getMessage($lang,'report','error7'));

        //$data_arr=getReportFormat($from_date,$to_date);

        $zm_email = escape_str($_REQUEST["zm_email"]);
        $rm_email = escape_str($_REQUEST["rm_email"]);
        $am_email = escape_str($_REQUEST["am_email"]);

        $user_cond = '';

        if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
            $mcond = '';
            $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
            $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
            $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
            //AM
            $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
        } else {
            //PAN
            $user_cond = '';
        }

        $pn = escape_str($_REQUEST["pn"]);
        $nr = escape_str($_REQUEST["nr"]);
        $key = escape_str($_REQUEST["key"]);

        // Search
        if (!empty($key)) {
            $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
        }

        $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

        if ($regtype == '1') {

            $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
            $user_cond .= " AND e.is_current=1 ";
        }

        // Pagination
        if (!empty($pn) and !empty($nr)) {
            // Navigation
            $user_cond .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
        }

        //$query_str="SELECT SQL_CALC_FOUND_ROWS u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.status,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM ".TABLE_USER." u LEFT JOIN ".DMS_DEALER." d ON u.dealer_code=d.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=2 AND u.created_on>='".$from_date." 00:00:00' AND u.created_on<='".$to_date." 23:59:59' ".$user_cond;
        $query_str = "SELECT SQL_CALC_FOUND_ROWS u.userid,u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.status,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " u " . $tablejoin . " LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=2 " . $user_cond;
        $query = $conn->query($query_str);

        $data_arr = array();
        if ($query->rowCount() > 0) {
            $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
        }
        $arr['report'] = $data_arr;

        // Total records
        $rows_query = $conn->query("select FOUND_ROWS() as total_records");
        $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
        $total_records = $rows_result[0]->total_records;
        $arr['total_records'] = $total_records;

        $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        /*}else{
    $conn->message(false,$conn->getMessage($lang,'report','error1'));
    }*/
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// page_access
function page_access()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            $page = escape_str($_REQUEST["page"]);

            if (!($page != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }
            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,MAX(ac.created_on) as last_access,d.dealer_name FROM " . TABLE_USER_ACCESSCOUNT . " as ac JOIN " . TABLE_USER . " as u ON ac.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  AND ac.page='" . $page . "' " . $user_cond . " GROUP BY ac.smpin ORDER BY last_access DESC " . $limitcond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// content_report
function content_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);
            $cateid = escape_str($_REQUEST["cateid"]);
            $type = escape_str($_REQUEST["type"]);

            if (!($from_date != '' && $to_date != '' && $cateid != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            if ($type != '') {
                $excond = " AND f.filetypeid='" . $type . "'";
            }

            //GET ALL CONTENTS OF CATEGORY
            $query_str = "SELECT c.assettopicid,ca.cateid,c.title,t.topicname,c.contentid,c.title FROM `" . TABLE_ASSET_CONTENT . "` as c JOIN " . TABLE_ASSET_TOPIC . " as t ON c.assettopicid=t.assettopicid JOIN " . TABLE_CATEGORY . " as ca ON ca.cateid=t.cateid JOIN " . TABLE_ASSET_FILE . " as f ON f.contentid=c.contentid  WHERE ca.cateid ='" . $cateid . "' AND  t.status='1' AND t.is_deleted='0' AND  c.status='1' AND c.is_deleted='0' " . $excond . " GROUP BY c.contentid ORDER BY t.position ASC,t.assettopicid ASC,c.contentid ASC ";
            $result_arr = $conn->getQueryValue($query_str);

            $new_arr = array();

            $label_arr = array();
            $label_valarr = array();
            if ($data_arr['type'] == 'date') {
                //DATE LOOP
                foreach ($data_arr['date'] as $res) {
                    $content_arr = array();
                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    if (count($result_arr) > 0) {
                        //CONTENT LOOOP
                        foreach ($result_arr as $catkey => $rescontent) {
                            $query_str = "SELECT COUNT(ac.id) as ucount FROM " . TABLE_USER_ACCESSCOUNT . " ac JOIN " . TABLE_USER . " as u ON ac.smpin=u.smpin WHERE  ac.page='learning' AND ac.contentid='" . $rescontent->contentid . "' AND DATE_FORMAT(ac.created_on,'%Y-%m-%d')='" . $res . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $user_cond . "  LIMIT 1 ";
                            $countresult_arr = $conn->getQueryValue($query_str);
                            $count = (count($countresult_arr) > 0) ? $countresult_arr[0]->ucount : '0';

                            $new_arr[$catkey]['title'] = $rescontent->title;
                            $new_arr[$catkey]['value'][] = $count;

                            $content_arr[] = array("title" => $rescontent->title, 'value' => $count);
                        }
                    }
                    $label_valarr[] = array('title' => $la_str, 'content' => $content_arr);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $content_arr = array();
                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    if (count($result_arr) > 0) {
                        //CONTENT LOOOP

                        foreach ($result_arr as $catkey => $rescontent) {
                            $query_str = "SELECT COUNT(ac.id) as ucount FROM " . TABLE_USER_ACCESSCOUNT . " ac JOIN " . TABLE_USER . " as u ON ac.smpin=u.smpin WHERE ac.page='learning' AND ac.contentid='" . $rescontent->contentid . "' AND DATE_FORMAT(ac.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $user_cond . "   LIMIT 1 ";
                            $countresult_arr = $conn->getQueryValue($query_str);
                            $count = (count($countresult_arr) > 0) ? $countresult_arr[0]->ucount : '0';

                            $new_arr[$catkey]['title'] = $rescontent->title;
                            $new_arr[$catkey]['value'][] = $count;

                            $content_arr[] = array("title" => $rescontent->title, 'value' => $count);
                        }
                    }
                    $label_valarr[] = array('title' => $la_str, 'content' => $content_arr);
                }

            }

//            $query_str="SELECT COUNT(userid) as totcount FROM ".TABLE_USER." WHERE role>7 and role!=14 AND created_on>='".$from_date." 00:00:00' AND created_on<='".$to_date." 23:59:59' ".$user_cond."  LIMIT 1 ";
            //            $count_arr=$conn->getQueryValue($query_str);
            //            $totcount=(count($count_arr)>0)?$count_arr[0]->totcount:'0';
            $tot_arr = array();
            if (count($result_arr) > 0) {
                foreach ($result_arr as $rescontent) {
                    $query_str = "SELECT COUNT(ac.id) as ucount FROM " . TABLE_USER_ACCESSCOUNT . " ac JOIN " . TABLE_USER . " as u ON ac.smpin=u.smpin WHERE ac.page='learning' AND  ac.contentid='" . $rescontent->contentid . "' AND  ac.created_on>='" . $from_date . " 00:00:00' AND ac.created_on<='" . $to_date . " 23:59:59' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $user_cond . "  LIMIT 1 ";
                    $countresult_arr = $conn->getQueryValue($query_str);
                    $count = (count($countresult_arr) > 0) ? $countresult_arr[0]->ucount : '0';

                    $tot_arr[] = array("title" => $rescontent->title, 'value' => $count);
                }
            }

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total_value' => $tot_arr, 'content' => $new_arr);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// content_report_list
function content_report_list()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);
            $cateid = escape_str($_REQUEST["cateid"]);
            $type = escape_str($_REQUEST["type"]);

            if (!($from_date != '' && $to_date != '' && $cateid != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            //CATEGORY
            $query_str = "SELECT `cateid`, `catename` FROM  " . TABLE_CATEGORY . " WHERE cateid='" . $cateid . "' LIMIT 1";
            $cat_arr = $conn->getQueryValue($query_str);
            if (!count($cat_arr) > 0) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }

            if ($type != '') {
                $excond = " AND f.filetypeid='" . $type . "'";
            }

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }

            //GET ALL CONTENTS OF CATEGORY
            $query_str = "SELECT c.assettopicid,ca.cateid,c.title,t.topicname,c.contentid,c.title FROM `" . TABLE_ASSET_CONTENT . "` as c LEFT JOIN " . TABLE_ASSET_TOPIC . " as t ON c.assettopicid=t.assettopicid LEFT JOIN " . TABLE_CATEGORY . " as ca ON ca.cateid=t.cateid LEFT JOIN " . TABLE_ASSET_FILE . " as f ON f.contentid=c.contentid  WHERE ca.cateid ='" . $cateid . "' AND  t.status='1' AND t.is_deleted='0' AND  c.status='1' AND c.is_deleted='0' " . $excond . " GROUP BY c.contentid ORDER BY t.position ASC,t.assettopicid ASC,c.contentid ASC ";
            $contentresult_arr = $conn->getQueryValue($query_str);

            if (count($contentresult_arr)) {

                $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`, d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER_ACCESSCOUNT . " as ac JOIN " . TABLE_USER . " as u ON ac.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND ac.page='learning' AND ac.created_on>='" . $from_date . " 00:00:00' AND  ac.created_on<='" . $to_date . " 23:59:59'  AND ac.cateid ='" . $cateid . "' " . $user_cond . " GROUP BY ac.smpin ORDER BY COUNT(ac.id) DESC ";
                // Pagination
                if (!empty($pn) and !empty($nr)) {
                    // Navigation
                    $query_str .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
                }
                $query = $conn->query($query_str);
                $data_arr = array();
                if ($query->rowCount() > 0) {
                    $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
                }
                // Total records
                $rows_query = $conn->query("select FOUND_ROWS() as total_records");
                $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
                $total_records = $rows_result[0]->total_records;
                $arr['total_records'] = $total_records;

                if (count($data_arr) > 0) {
                    foreach ($data_arr as $key => $res) {
                        $data_arr[$key]->content = array();
                        //USER CONTENT COUNT
                        foreach ($contentresult_arr as $res_content) {
                            $query_str = "SELECT COUNT(id) as ucount FROM " . TABLE_USER_ACCESSCOUNT . " WHERE smpin='" . $res_user->smpin . "'  AND contentid='" . $res_content->contentid . "' AND page='learning' AND created_on>='" . $from_date . " 00:00:00' AND  created_on<='" . $to_date . " 23:59:59' LIMIT 1 ";
                            $countresult_arr = $conn->getQueryValue($query_str);
                            $count = (count($countresult_arr) > 0) ? $countresult_arr[0]->ucount : '0';

                            $data_arr[$key]->content[] = array("title" => $res_content->title, 'count' => $count);

                        }
                    }
                }
                $arr['report'] = $data_arr;
            }
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// accumulation_report
function accumulation_report()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE DATE_FORMAT(h.created_on,'%Y-%m-%d')='" . $res . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  DATE_FORMAT(h.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }
            }

            $query_str = "SELECT  SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE h.created_on>='" . $from_date . " 00:00:00' AND h.created_on<='" . $to_date . " 23:59:59' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " LIMIT 1 ";
            $point_arr = $conn->getQueryValue($query_str);
            $totcount = (count($point_arr) > 0 && $point_arr[0]->sumpoints) ? $point_arr[0]->sumpoints : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $totcount);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// accumulation_report_list
function accumulation_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,SUM(h.points) as sum_points,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER_EPOINTS_HISTORY . " as h JOIN " . TABLE_USER . " as u ON h.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' AND h.created_on>='" . $from_date . " 00:00:00' AND  h.created_on<='" . $to_date . " 23:59:59' " . $user_cond . " GROUP BY h.smpin ORDER BY sum_points DESC,u.userid ASC " . $limit_cond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// redemption_report
function redemption_report()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE DATE_FORMAT(h.created_on,'%Y-%m-%d')='" . $res . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  DATE_FORMAT(h.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }
            }

            $query_str = "SELECT  SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE h.created_on>='" . $from_date . " 00:00:00' AND h.created_on<='" . $to_date . " 23:59:59' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " LIMIT 1 ";
            $point_arr = $conn->getQueryValue($query_str);
            $totcount = (count($point_arr) > 0 && $point_arr[0]->sumpoints) ? $point_arr[0]->sumpoints : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $totcount);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// redemption_report_list
function redemption_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,SUM(h.points) as sum_points,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER_EPOINTS_HISTORY . " as h JOIN " . TABLE_USER . " as u ON h.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' AND h.created_on>='" . $from_date . " 00:00:00' AND  h.created_on<='" . $to_date . " 23:59:59' " . $user_cond . " GROUP BY h.smpin ORDER BY sum_points DESC,u.userid ASC " . $limit_cond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);

        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// redemptionapproved_report
function redemptionapproved_report()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE DATE_FORMAT(h.modified_on,'%Y-%m-%d')='" . $res . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE  DATE_FORMAT(h.modified_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;

                    $sumpoints = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $sumpoints);
                }
            }

            $query_str = "SELECT  SUM(h.points) as sumpoints FROM " . TABLE_USER . " u JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE h.modified_on>='" . $from_date . " 00:00:00' AND h.modified_on<='" . $to_date . " 23:59:59' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " LIMIT 1 ";
            $point_arr = $conn->getQueryValue($query_str);
            $totcount = (count($point_arr) > 0 && $point_arr[0]->sumpoints) ? $point_arr[0]->sumpoints : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $totcount);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// redemptionapproved_report_list
function redemptionapproved_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,SUM(h.points) as sum_points,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER_WALLET_APPROVE . " as h JOIN " . TABLE_USER . " as u ON h.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND  u.is_deleted=0 h.is_approved='1' AND  h.modified_on>='" . $from_date . " 00:00:00' AND  h.modified_on<='" . $to_date . " 23:59:59' " . $user_cond . " GROUP BY h.smpin ORDER BY sum_points DESC,u.userid ASC  " . $limit_cond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// assessment_report
function assessment_report()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $assessmentid = escape_str($_REQUEST["assessmentid"]);

            $query_str = "SELECT `assessmentid`,`atype`, `lvl_id`, `assessmentname`,user_type  FROM " . TABLE_ASSESSMENT . " WHERE status=1 AND assettypeid=1 AND  assessmentid='" . $assessmentid . "'";
            $assessment_arr = $conn->getQueryValue($query_str);

            if (!($assessmentid != '' && count($assessment_arr) > 0)) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            $score_arr = array();
            $score_arr[] = array("from" => 0, 'to' => 10);
            $score_arr[] = array("from" => 11, 'to' => 20);
            $score_arr[] = array("from" => 21, 'to' => 30);
            $score_arr[] = array("from" => 31, 'to' => 40);
            $score_arr[] = array("from" => 41, 'to' => 50);
            $score_arr[] = array("from" => 51, 'to' => 60);
            $score_arr[] = array("from" => 61, 'to' => 70);
            $score_arr[] = array("from" => 71, 'to' => 80);
            $score_arr[] = array("from" => 81, 'to' => 90);
            $score_arr[] = array("from" => 91, 'to' => 100);

            if (count($score_arr) > 0) {
                foreach ($score_arr as $res_score) {
                    $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $user_cond . " GROUP BY au.assessmentid LIMIT 1";
                    $result_arr = $conn->getQueryValue($query_str);
                    $la_str = $res_score['from'] . " to " . $res_score['to'];
                    $label_arr[] = $la_str;
                    $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                    $label_valarr[] = array('title' => $la_str, 'value' => $usercount);
                }
            }
            $returnarr = array('label' => $label_arr, 'value' => $label_valarr);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// assessment_report_list
function assessment_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $assessmentid = escape_str($_REQUEST["assessmentid"]);

            $query_str = "SELECT `assessmentid`,`atype`, `lvl_id`, `assessmentname`,user_type  FROM " . TABLE_ASSESSMENT . " WHERE status=1 AND assettypeid=1 AND  assessmentid='" . $assessmentid . "'";
            $assessment_arr = $conn->getQueryValue($query_str);
            if (!($assessmentid != '' && count($assessment_arr) > 0)) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,highscore as score,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_ASSESSMENT_USER . " as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND u.is_deleted=0 AND   au.assessmentid='" . $assessmentid . "' AND au.highscorearid!=0 " . $user_cond . " GROUP BY au.userid ORDER BY au.highscore DESC,au.modified_on ASC " . $limit_cond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// sales_report
function sales_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            $valarr = array();
            if ($data_arr['type'] == 'date') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(s.sales_count) as tot_sales FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE_SALES . " s ON u.smpin=s.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(s.sale_date,'%Y-%m-%d')='" . $res . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('d M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0 && $result_arr[0]->tot_sales) ? $result_arr[0]->tot_sales : '0';

                    $query_str = "SELECT SUM(t.target) as tot_target FROM " . TABLE_USER . " u JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(t.sale_month,'%Y-%m-%d')='" . $res . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);
                    $target = (count($result_arr) > 0 && $result_arr[0]->tot_target) ? $result_arr[0]->tot_target : '0';

                    $label_valarr[] = array('title' => $la_str, 'sales' => $count, "target" => $target);

                    $valarr[] = array('title' => $la_str, 'values' => array($count, $target));
                }

            } else if ($data_arr['type'] == 'month') {

                foreach ($data_arr['date'] as $res) {
                    $query_str = "SELECT SUM(s.sales_count) as tot_sales FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE_SALES . " s ON u.smpin=s.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(s.sale_date,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);

                    $la_str = strtoupper(date('M Y', strtotime($res)));
                    $label_arr[] = $la_str;
                    $count = (count($result_arr) > 0 && $result_arr[0]->tot_sales) ? $result_arr[0]->tot_sales : '0';

                    $query_str = "SELECT SUM(t.target) as tot_target FROM " . TABLE_USER . " u JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND DATE_FORMAT(t.sale_month,'%Y-%m')='" . date('Y-m', strtotime($res)) . "'  " . $user_cond . " LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);
                    $target = (count($result_arr) > 0 && $result_arr[0]->tot_target) ? $result_arr[0]->tot_target : '0';

                    $label_valarr[] = array('title' => $la_str, 'sales' => $count, "target" => $target);
                    $valarr[] = array('title' => $la_str, 'values' => array($count, $target));
                }

            }
            //TOTAL SALES
            $query_str = "SELECT SUM(s.sales_count) as tot_sales FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE_SALES . " s ON u.smpin=s.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND s.sale_date>='" . $from_date . "' AND s.sale_date<='" . $to_date . "' " . $user_cond . " LIMIT 1 ";

            $count_arr = $conn->getQueryValue($query_str);
            $tot_sale = (count($count_arr) > 0 && $count_arr[0]->tot_sales) ? $count_arr[0]->tot_sales : '0';

            //TOTAL TARGET
            $query_str = "SELECT SUM(t.target) as tot_target FROM " . TABLE_USER . " u JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND t.sale_month>='" . $from_date . "' AND t.sale_month<='" . $to_date . "' " . $user_cond . " LIMIT 1 ";

            $count_arr = $conn->getQueryValue($query_str);
            $tot_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => array('sales' => $tot_sale, 'target' => $tot_target), 'graph' => $valarr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// sales_report_list
function sales_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            // $query_str="SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,SUM(h.points) as sum_points,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM ".TABLE_USER_WALLET_APPROVE." as h JOIN ".TABLE_USER." as u ON h.smpin=u.smpin JOIN ".DMS_DEALER." as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND h.is_approved='1' AND  h.modified_on>='".$from_date." 00:00:00' AND  h.modified_on<='".$to_date." 23:59:59' ".$user_cond." GROUP BY h.smpin ORDER BY sum_points DESC,u.userid ASC  ".$limit_cond;

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,COALESCE(SUM(s.sales_count),0) as tot_sales,COALESCE(SUM(t.target),0) as tot_target,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " as u  LEFT JOIN (SELECT * FROM  " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "' )as s ON s.smpin=u.smpin LEFT JOIN (SELECT * FROM  " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' )as t ON t.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND u.is_deleted=0 " . $user_cond . " GROUP BY u.smpin ORDER BY  tot_sales,tot_target DESC,u.userid ASC  " . $limit_cond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// certification_report
function certification_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= " AND e.is_current=1 ";
            }

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=1 AND l.lvl_id=1 AND l.status=3 " . $user_cond . " LIMIT 1 ";
            $base_arr = $conn->getQueryValue($query_str);
            $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  AND u.status=1 AND l.lvl_id=2 AND l.status=3 " . $user_cond . " LIMIT 1 ";
            $bronze_arr = $conn->getQueryValue($query_str);
            $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  AND u.status=1 AND l.lvl_id=4 AND l.status=3 " . $user_cond . " LIMIT 1 ";
            $silver_arr = $conn->getQueryValue($query_str);
            $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_LEARNING . " l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  AND u.status=1 AND l.lvl_id=6 AND l.status=3 " . $user_cond . " LIMIT 1 ";
            $gold_arr = $conn->getQueryValue($query_str);
            $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

            $returnarr = array('base' => $base_count, 'bronze' => $bronze_count, 'silver' => $silver_count, 'gold' => $gold_count);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// certification_report_list
function certification_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
//            //CONVERT converttype()
            //            converttype();
            //
            //            $from_date = escape_str($_REQUEST["from_date"]);
            //            $to_date = escape_str($_REQUEST["to_date"]);
            //
            //            if(!($from_date!='' && $to_date!=''))  $conn->message(false,$conn->getMessage($lang,'report','error6'));
            //            if(!(isValidDateTimeString($from_date,'Y-m-d','Asia/Kolkata') && isValidDateTimeString($to_date,'Y-m-d','Asia/Kolkata')))   $conn->message(false,$conn->getMessage($lang,'report','error7'));

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,CASE WHEN a.lvl_id iS NULL THEN 'Not completed' ELSE  'Completed' END as base_status,CASE WHEN b.lvl_id iS NULL THEN 'Not completed' ELSE  'Completed' END as bronze_status,CASE WHEN c.lvl_id iS NULL THEN 'Not completed' ELSE  'Completed' END as silver_status,CASE WHEN d.lvl_id iS NULL THEN 'Not completed' ELSE  'Completed' END as gold_status,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code  LEFT JOIN (SELECT * FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3) as a ON a.smpin=u.smpin LEFT JOIN (SELECT * FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3) as b ON b.smpin=u.smpin LEFT JOIN (SELECT * FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3) as c ON c.smpin=u.smpin LEFT JOIN (SELECT * FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3) as d ON d.smpin=u.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $user_cond . " GROUP BY u.smpin ORDER BY d.lvl_id,c.lvl_id,b.lvl_id,a.lvl_id DESC " . $limit_cond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// setsalestarget_report
function setsalestarget_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //$from_date = escape_str($_REQUEST["from_date"]);
            //$to_date = escape_str($_REQUEST["to_date"]);

            $month = escape_str($_REQUEST["month"]);
            $year = escape_str($_REQUEST["year"]);

            if (!($month != '' && $year != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $from_date = date('Y-m-d', strtotime($year . "-" . $month . "-01"));
            $to_date = date('Y-m-t', strtotime($year . "-" . $month . "-01"));

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            $label_arr = array();
            $valarr = array();
            //TOTAL TARGET
            $query_str = "SELECT SUM(t.target) as tot_target FROM " . TABLE_USER . " u JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin   WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND t.sale_month>='" . $from_date . "' AND t.sale_month<='" . $to_date . "' " . $user_cond . " LIMIT 1 ";

            $count_arr = $conn->getQueryValue($query_str);
            $tot_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

            $query_str = "SELECT SUM(t.target)as tot_target FROM " . TABLE_USER . " u JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin JOIN `tbl__user_saletarget_status` s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE s.status IN (2,3) AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND t.sale_month>='" . $from_date . "' AND t.sale_month<='" . $to_date . "' " . $user_cond . " LIMIT 1 ";
            $sales_arr = $conn->getQueryValue($query_str);
            $aptot_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

            $returnarr = array('total' => $tot_target, 'approved' => $aptot_target);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// sales_report_list
function setsalestarget_report_list()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $month = escape_str($_REQUEST["month"]);
            $year = escape_str($_REQUEST["year"]);

            if (!($month != '' && $year != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $from_date = date('Y-m-d', strtotime($year . "-" . $month . "-01"));
            $to_date = date('Y-m-t', strtotime($year . "-" . $month . "-01"));

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = ' AND d.is_deleted=0 AND d.status=1';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $user_cond .= " and (u.smpin like '%" . $key . "%' OR u.fname like '%" . $key . "%' OR u.lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond = " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            // $query_str="SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,SUM(h.points) as sum_points,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM ".TABLE_USER_WALLET_APPROVE." as h JOIN ".TABLE_USER." as u ON h.smpin=u.smpin JOIN ".DMS_DEALER." as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND h.is_approved='1' AND  h.modified_on>='".$from_date." 00:00:00' AND  h.modified_on<='".$to_date." 23:59:59' ".$user_cond." GROUP BY h.smpin ORDER BY sum_points DESC,u.userid ASC  ".$limit_cond;

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`, COALESCE(SUM(t.target),0) as tot_target,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " as u LEFT JOIN (SELECT * FROM  " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' )as t ON t.smpin=u.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14  and u.role!=16 AND u.is_deleted=0 " . $user_cond . " GROUP BY u.smpin ORDER BY  tot_target DESC,u.userid ASC  " . $limit_cond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);

                foreach ($data_arr as $ukey => $res) {
                    $statusquery_str = "SELECT * FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE  smpin='" . $res->smpin . "' AND  sale_month='" . $from_date . "' ORDER BY id DESC LIMIT 1";
                    $sales_arr = $conn->getQueryValue($statusquery_str);
                    if (count($sales_arr) > 0) {
                        switch ($sales_arr[0]->status) {
                            case '1':
                                $data_arr[$ukey]->sale_status = '1';
                                $data_arr[$ukey]->sale_status_str = 'Waiting';
                                break;
                            case '2':
                                $data_arr[$ukey]->sale_status = '2';
                                $data_arr[$ukey]->sale_status_str = 'Approval';
                                break;
                            case '3':
                                $data_arr[$ukey]->sale_status = '3';
                                $data_arr[$ukey]->sale_status_str = 'Modification';
                                break;
                        }

                    } else {
                        $data_arr[$ukey]->sale_status = '0';
                        $data_arr[$ukey]->sale_status_str = 'No Action';
                    }
                }

            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// activeinactive_report
function activeinactive_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            //converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);
            if ($to_date != '') {
                $to_date = date('Y-m-t', strtotime($to_date));
            }

            $type = escape_str($_REQUEST["type"]);

            if (!($from_date != '' && $to_date != '' && $type != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getmonthReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }
            //INCATIVE COND
            $inactivecond = ($type == 0) ? " AND module='AUTOMATIC INACTIVE'" : " AND module='MANUAL INACTIVE'";

            $label_arr = array();
            $valarr = array();

            foreach ($data_arr['date'] as $res) {
                $la_str = strtoupper(date('M Y', strtotime($res)));
                $label_arr[] = $la_str;

                //INITIATED USERS
                $iniquery_str = "SELECT COUNT(id) as ucount FROM " . DMS_EMPLOYEE . " WHERE DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "'   " . $user_cond . " LIMIT 1 ";
                $iniresult_arr = $conn->getQueryValue($iniquery_str);
                $initalcount = (count($iniresult_arr) > 0) ? $iniresult_arr[0]->ucount : '0';

                //ACTIVE USERS
                $activequery_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $user_cond . " LIMIT 1 ";
                $activeresult_arr = $conn->getQueryValue($activequery_str);
                $activecount = (count($activeresult_arr) > 0) ? $activeresult_arr[0]->ucount : '0';

                //INACTIVE USERS
                $inactivequery_str = "SELECT COUNT(u.userid) as ucount FROM (SELECT userid FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND  is_deleted=0 " . $user_cond . ") as u JOIN " . TABLE_USER_LOG . " as l ON u.userid=l.userid WHERE DATE_FORMAT(l.created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $inactivecond . "  LIMIT 1 ";
                $inactiveresult_arr = $conn->getQueryValue($inactivequery_str);
                $inactivecount = (count($inactiveresult_arr) > 0) ? $inactiveresult_arr[0]->ucount : '0';

                $label_valarr[] = array('title' => $la_str, 'inital' => $initalcount, 'active' => $activecount, 'inactive' => $inactivecount);
                $valarr[] = array('title' => $la_str, 'values' => array($initalcount, $activecount, $inactivecount));
            }

            //INITIATED
            $iniquery_str = "SELECT COUNT(id) as totcount FROM " . DMS_EMPLOYEE . " WHERE  created_on<='" . NOW . "' " . $user_cond . "  LIMIT 1 ";
            $count_arr = $conn->getQueryValue($iniquery_str);
            $totinitalcount = (count($count_arr) > 0) ? $count_arr[0]->totcount : '0';

            //ACTIVE USERS
            $query_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND created_on<'" . NOW . "' " . $user_cond . " LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $activecount = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';

            //INACTIVE USERS
            // $inactivequery_str="SELECT COUNT(userid) as ucount FROM ".TABLE_USER." WHERE userid IN (SELECT userid FROM ".TABLE_USER_LOG." WHERE created_on<'".NOW."' ".$inactivecond."  ) AND role>7 and role!=14 and role!=16 AND  is_deleted=0 AND status=0 ".$user_cond." LIMIT 1 ";
            $inactivequery_str = "SELECT COUNT(u.userid) as ucount FROM (SELECT userid FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND  is_deleted=0 " . $user_cond . ") as u JOIN " . TABLE_USER_LOG . " as l ON u.userid=l.userid WHERE created_on<'" . NOW . "' " . $inactivecond . "  LIMIT 1";

            $inactiveresult_arr = $conn->getQueryValue($inactivequery_str);
            $inactivecount = (count($inactiveresult_arr) > 0) ? $inactiveresult_arr[0]->ucount : '0';

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => array('inital' => $totinitalcount, 'active' => $activecount, 'inactive' => $inactivecount));

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// attrition_report
function attrition_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $data_arr = getmonthReportFormat($from_date, $to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0'  AND status='1' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }
            //INCATIVE COND
            $inactivecond = " AND (module='AUTOMATIC INACTIVE' OR  module='MANUAL INACTIVE')";

            $label_arr = array();
            $valarr = array();

            foreach ($data_arr['date'] as $res) {
                $la_str = strtoupper(date('M Y', strtotime($res)));
                $label_arr[] = $la_str;

                //STARTING USER COUNT
                $startquery_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND created_on<'" . $res . " 00:00:00' " . $user_cond . " LIMIT 1 ";
                $startresult_arr = $conn->getQueryValue($startquery_str);
                $startcount = (count($startresult_arr) > 0) ? $startresult_arr[0]->ucount : '0';

                //ACTIVE USERS
                $activequery_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $user_cond . " LIMIT 1 ";
                $activeresult_arr = $conn->getQueryValue($activequery_str);
                $activecount = (count($activeresult_arr) > 0) ? $activeresult_arr[0]->ucount : '0';

                //INACTIVE USERS
                $inactivequery_str = "SELECT COUNT(u.userid) as ucount FROM (SELECT userid FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND  is_deleted=0 " . $user_cond . ") as u JOIN " . TABLE_USER_LOG . " as l ON u.userid=l.userid WHERE DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime($res)) . "' " . $inactivecond . "  LIMIT 1 ";
                $inactiveresult_arr = $conn->getQueryValue($inactivequery_str);
                $inactivecount = (count($inactiveresult_arr) > 0) ? $inactiveresult_arr[0]->ucount : '0';

                $divideby = $startcount + $activecount;
                $rate = ($divideby > 0) ? (($inactivecount * 100) / ($divideby)) : 0;

                $label_valarr[] = array('title' => $la_str, 'rate' => $rate);
                $valarr[] = array('title' => $la_str, 'values' => array($initalcount, $activecount, $inactivecount));
            }

            //ACTIVE USERS
            $query_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND created_on<'" . date('Y-m-', strtotime(NOW)) . "-01' " . $user_cond . " LIMIT 1 ";

            $result_arr = $conn->getQueryValue($query_str);
            $startcount = (count($result_arr) > 0) ? $result_arr[0]->ucount : '0';

            //ACTIVE USERS
            $activequery_str = "SELECT COUNT(userid) as ucount FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND is_deleted=0 AND DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime(NOW)) . "' " . $user_cond . " LIMIT 1 ";
            $activeresult_arr = $conn->getQueryValue($activequery_str);
            $activecount = (count($activeresult_arr) > 0) ? $activeresult_arr[0]->ucount : '0';

            //INACTIVE USERS
            $inactivequery_str = "SELECT COUNT(u.userid) as ucount FROM (SELECT userid FROM " . TABLE_USER . " WHERE role>7 and role!=14 and role!=16 AND  is_deleted=0 " . $user_cond . ") as u JOIN " . TABLE_USER_LOG . " as l ON u.userid=l.userid WHERE DATE_FORMAT(created_on,'%Y-%m')='" . date('Y-m', strtotime(NOW)) . "' " . $inactivecond . "  LIMIT 1 ";
            $inactiveresult_arr = $conn->getQueryValue($inactivequery_str);
            $inactivecount = (count($inactiveresult_arr) > 0) ? $inactiveresult_arr[0]->ucount : '0';

            $divideby = $startcount + $activecount;
            $rate = ($divideby > 0) ? (($inactivecount * 100) / ($divideby)) : 0;

            $returnarr = array('label' => $label_arr, 'value' => $label_valarr, 'total' => $rate);

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration_percent
function user_registration_percent()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //            //CONVERT converttype()
            //            converttype();
            //            $from_date = escape_str($_REQUEST["from_date"]);
            //            $to_date = escape_str($_REQUEST["to_date"]);
            //
            //            if(!($from_date!='' && $to_date!=''))  $conn->message(false,$conn->getMessage($lang,'report','error6'));
            //            if(!(isValidDateTimeString($from_date,'Y-m-d','Asia/Kolkata') && isValidDateTimeString($to_date,'Y-m-d','Asia/Kolkata')))   $conn->message(false,$conn->getMessage($lang,'report','error7'));
            //
            //            $data_arr=getReportFormat($from_date,$to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                //$user_cond=" AND am_email='".$am_email."' ";
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                //$user_cond=" AND rm_email='".$rm_email."'";
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                //$user_cond=" AND zm_email='".$zm_email."' ";
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $returnarr = array();

            $tablejoin = '';
            $user_reg_cond = '';
            $emp_reg_cond = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_reg_cond = " AND e.is_current=1 ";
                $emp_reg_cond = " AND is_current=1 ";
            }

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' status=1 AND is_deleted='0') " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $user_cond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        // $emp_query_str="SELECT COUNT(id) as total_employee FROM ".DMS_EMPLOYEE." WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM ".DMS_DEALER." WHERE rm_email='".$val->rm_email."') ".$emp_reg_cond;
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $user_cond . " AND status=1 AND is_deleted='0') " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $user_cond . " AND status=1 AND is_deleted='0') AND u.role>7 AND u.role<16 AND u.role!='14'  " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $user_cond . " AND status=1 AND is_deleted='0' ) " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . "  u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $user_cond . " AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14'  " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $dealer_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code='" . $val->dealer_code . "' " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($dealer_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . "  WHERE u.is_deleted=0 AND u.dealer_code='" . $val->dealer_code . "' AND u.role>7 AND u.role<16 AND u.role!='14' " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            }

//            $emp_query_str="SELECT COUNT(id) as total_employee FROM ".DMS_EMPLOYEE." WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM ".DMS_DEALER." WHERE 1=1 ".$user_cond.") ".$emp_reg_cond;
            //            $emp_data=$conn->getQueryValue($emp_query_str);
            //            $employee_count=(count($emp_data)>0)?$emp_data[0]->total_employee:0;
            //
            //            //GET EMPLOYEE REGISTER
            //            $reg_query_str="SELECT COUNT(u.userid) as total_reg FROM ".TABLE_USER." u ".$tablejoin." WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM ".DMS_DEALER." WHERE 1=1 ".$user_cond."  ) AND u.role>7 AND u.role<16 AND u.role!='14' ".$user_reg_cond;
            //            $reg_data=$conn->getQueryValue($reg_query_str);
            //            $reg_count=(count($reg_data)>0)?$reg_data[0]->total_reg:0;
            //            $percent=($employee_count>0)?round(($reg_count/$employee_count)*100):0;
            //
            //            $total_arr=array('emp_count'=>$employee_count,'reg_count'=>$reg_count,"percent"=>$percent);
            //            $list_arr=$returnarr;
            //            $returnarr=array('total'=>$total_arr,'list'=>$list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration_percent
function inactive_user_registration_percent()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        if (isset($_SESSION["userid"])) {

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                // $user_cond=" AND am_email='".$am_email."' ";
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                // $user_cond=" AND rm_email='".$rm_email."'";
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                //$user_cond=" AND  zm_email='".$zm_email."' ";
                $type = "zm";
            } else {
                //PAN
                //$user_cond='';
                $type = "pan";
            }
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $returnarr = array();
            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0') ";
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(userid) as total_reg FROM " . TABLE_USER . " WHERE is_deleted=0 AND status=2 AND dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0' ) AND role>7 AND role<16 AND role!='14'";
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' AND status=1 AND is_deleted='0') ";
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(userid) as total_reg FROM " . TABLE_USER . " WHERE is_deleted=0 AND status=2 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' AND status=1 AND is_deleted='0' ) AND role>7 AND role<16 AND role!='14'";
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $user_cond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' AND status=1 AND is_deleted='0' ) ";
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(userid) as total_reg FROM " . TABLE_USER . " WHERE is_deleted=0 AND status=2 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "'  AND status=1 AND is_deleted='0'  ) AND role>7 AND role<16 AND role!='14'";
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0'  " . $user_cond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $dealer_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1  AND dealer_code='" . $val->dealer_code . "' ";
                        $emp_data = $conn->getQueryValue($dealer_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(userid) as total_reg FROM " . TABLE_USER . " WHERE is_deleted=0 AND status=2 AND dealer_code='" . $val->dealer_code . "' AND role>7 AND role<16 AND role!='14'";
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            }
            //print $reg_query_str;

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// customerexperiencelogin
function customerexperiencelogin()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0' AND status=1 " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,c.`salutation`,c. `firstname`, c.`lastname`, c.`email`,c. `mobile`,c. `age`,c.smpin,c.dsename,d.`dealer_code`, d.`dealer_name`,d. `city`, d.`state`, d.`zm_name`,  d.`rm_name`,  d.`am_name`  FROM " . TABLE__CUSTOMEREXP . " as c JOIN " . TABLE_USER . " as u ON c.userid=u.userid JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE  u.is_deleted=0 AND c.created_on>='" . $from_date . " 00:00:00' AND  c.created_on<='" . $to_date . " 23:59:59'  " . $user_cond . "  ORDER BY c.created_on DESC " . $limitcond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// enquiry
function enquiry()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0' AND status=1  " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.fname,u.lname, c.`model_id`, c.`variant_id`, c.`name`, c.`mobile`, c.`email`,d.`dealer_code`, d.`dealer_name`,d. `city`, d.`state`, d.`zone`, d.`zm_name`,  d.`rm_name`,d.`am_name`,m.model_name,v.title as variant_name  FROM " . TABLE_EXPLORE_ENQUIRY . " as c JOIN " . TABLE_USER . " as u ON c.user_id=u.userid LEFT JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code JOIN " . TABLE_COMPARE_MODEL . " as m ON  m.model_id=c.model_id JOIN " . TABLE_MODEL_VARIANT . " v ON c.variant_id=v.id  WHERE u.is_deleted=0 AND c.created_on>='" . $from_date . " 00:00:00' AND  c.created_on<='" . $to_date . " 23:59:59' " . $user_cond . "   ORDER BY c.created_on DESC " . $limitcond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// customerexperiencefeedback
function customerexperiencefeedback()
{
    global $conn;

    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0' AND status=1  " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,c.`salutation`,c. `firstname`, c.`lastname`, c.`email`,c. `mobile`,c. `age`,c.smpin,c.dsename,d.`dealer_code`, d.`dealer_name`,d. `city`, d.`state`, d.`zm_name`,  d.`rm_name`,  d.`am_name`,f.id  FROM " . TABLE__FEEDBACKUSER . " f JOIN " . TABLE__CUSTOMEREXP . " as c ON f.customerid=c.id JOIN " . TABLE_USER . " as u ON c.userid=u.userid JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE  u.is_deleted=0 AND c.created_on>='" . $from_date . " 00:00:00' AND  c.created_on<='" . $to_date . " 23:59:59'  " . $user_cond . "  ORDER BY f.created_on,c.created_on DESC " . $limitcond;
            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// customerexperiencefeedbackdetails
function customerexperiencefeedbackdetails()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $id = escape_str($_REQUEST["id"]);
            $query_str = "SELECT id  FROM " . TABLE__FEEDBACKUSER . " WHERE id='" . $id . "'";
            $result_arr = $conn->getQueryValue($query_str);
            if (!($id != '' && count($result_arr) > 0)) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }

            $query_str = "SELECT r.survey_rating,q.questiontext,q.questiontypeid FROM " . TABLE__FEEDBACKRESULT . " r JOIN " . TABLE__EXPQUESTION . " as q ON r.qid=q.id WHERE r.arid='" . $id . "' GROUP BY r.qid";
            $feedresult_arr = $conn->getQueryValue($query_str);
            $arr = array();
            if (count($feedresult_arr) > 0) {
                foreach ($feedresult_arr as $res) {
                    $arr[] = $res;
                }
            }

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// user_registration_percent_v2
function user_registration_percent_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            //CONVERT converttype()
            converttype();
            $datetype = escape_str($_REQUEST['datetype']);
            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if ($datetype != 4) //FOR ALL LIST
            {
                if (!($from_date != '' && $to_date != '')) {
                    $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
                }

                if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                    $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
                }

            }
            //$data_arr=getReportFormat($from_date,$to_date);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                //$user_cond=" AND am_email='".$am_email."' ";
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                //$user_cond=" AND rm_email='".$rm_email."'";
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                //$user_cond=" AND zm_email='".$zm_email."' ";
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $returnarr = array();

            $tablejoin = '';
            $user_reg_cond = '';
            $emp_reg_cond = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_reg_cond = " AND e.is_current=1 ";
                $emp_reg_cond = " AND is_current=1 ";
            }
            if ($datetype != '4') {
                $user_reg_cond .= " AND u.created_on>='" . $from_date . " 00:00:00' AND u.created_on<='" . $to_date . " 23:59:59' ";
            }
            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0') " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "'  AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        // $emp_query_str="SELECT COUNT(id) as total_employee FROM ".DMS_EMPLOYEE." WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM ".DMS_DEALER." WHERE rm_email='".$val->rm_email."') ".$emp_reg_cond;
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $user_cond . " AND status=1 AND is_deleted='0') " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $user_cond . " AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14'  " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $user_cond . " AND status=1 AND is_deleted='0'  ) " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($emp_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . "  u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $user_cond . " AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14'  " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $user_cond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $percent = 0;

                        //GET EMPLOYEE DATA
                        $dealer_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code='" . $val->dealer_code . "' " . $emp_reg_cond;
                        $emp_data = $conn->getQueryValue($dealer_query_str);
                        $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

                        //GET EMPLOYEE REGISTER
                        $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . "  WHERE u.is_deleted=0 AND u.dealer_code='" . $val->dealer_code . "' AND u.role>7 AND u.role<16 AND u.role!='14' " . $user_reg_cond;
                        $reg_data = $conn->getQueryValue($reg_query_str);
                        $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;

                        $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
                    }
                }
            }

            $emp_query_str = "SELECT COUNT(id) as total_employee FROM " . DMS_EMPLOYEE . " WHERE status=1 AND dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $user_cond . " " . $mcond . ") " . $emp_reg_cond;
            $emp_data = $conn->getQueryValue($emp_query_str);
            $employee_count = (count($emp_data) > 0) ? $emp_data[0]->total_employee : 0;

            //GET EMPLOYEE REGISTER
            $reg_query_str = "SELECT COUNT(u.userid) as total_reg FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . "   " . $mcond . " ) AND u.role>7 AND u.role<16 AND u.role!='14' " . $user_reg_cond;
            $reg_data = $conn->getQueryValue($reg_query_str);
            $reg_count = (count($reg_data) > 0) ? $reg_data[0]->total_reg : 0;
            $percent = ($employee_count > 0) ? round(($reg_count / $employee_count) * 100) : 0;

            $total_arr = array('emp_count' => $employee_count, 'reg_count' => $reg_count, "percent" => $percent);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// activeinactive_report
function activeinactive_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"] = 1;
        if (isset($_SESSION["userid"])) {

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);
            $dealer_code = escape_str($_REQUEST["dealer_code"]);
            $user_cond = '';
            if ($dealer_code != 'all' && $dealer_code != '') {
                $type = "dealer";
            } else if ($am_email != 'all' && $am_email != '') {
                //AM
                //$user_cond=" AND am_email='".$am_email."' ";
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                //$user_cond=" AND rm_email='".$rm_email."'";
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                //$user_cond=" AND zm_email='".$zm_email."' ";
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($dealer_code != 'all' && $dealer_code != '') ? " AND dealer_code='" . $dealer_code . "'" : '';
                $mcond .= ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $returnarr = array();

            $tablejoin = '';
            $user_reg_cond = '';

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_reg_cond = " AND e.is_current=1 ";

            }

            // $user_reg_cond.=" AND u.created_on>='".$from_date." 00:00:00' AND u.created_on<='".$to_date." 23:59:59' ";
            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {

                        //ACTIVE USER
                        $active_query_str = "SELECT COUNT(u.userid) as total_act FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status=1  " . $user_reg_cond;
                        $active_data = $conn->getQueryValue($active_query_str);
                        $active_count = (count($active_data) > 0) ? $active_data[0]->total_act : 0;

                        //INACTIVE USER
                        $inactive_query_str = "SELECT COUNT(u.userid) as total_inact FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND  status=1 AND is_deleted='0'  ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status!=1  " . $user_reg_cond;
                        $inactive_data = $conn->getQueryValue($inactive_query_str);
                        $inactive_count = (count($inactive_data) > 0) ? $inactive_data[0]->total_inact : 0;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'active_count' => $active_count, 'inactive_count' => $inactive_count);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $user_cond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        //ACTIVE USER
                        $active_query_str = "SELECT COUNT(u.userid) as total_act FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' AND status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status=1 " . $user_reg_cond;
                        $active_data = $conn->getQueryValue($active_query_str);
                        $active_count = (count($active_data) > 0) ? $active_data[0]->total_act : 0;

                        //INACTIVE USER
                        $inactive_query_str = "SELECT COUNT(u.userid) as total_inact FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' AND  status=1 AND is_deleted='0'  ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status!=1  " . $user_reg_cond;
                        $inactive_data = $conn->getQueryValue($inactive_query_str);
                        $inactive_count = (count($inactive_data) > 0) ? $inactive_data[0]->total_inact : 0;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'active_count' => $active_count, 'inactive_count' => $inactive_count);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        //ACTIVE USER
                        $active_query_str = "SELECT COUNT(u.userid) as total_act FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' AND  status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status=1 AND u.is_deleted=0 " . $user_reg_cond;
                        $active_data = $conn->getQueryValue($active_query_str);
                        $active_count = (count($active_data) > 0) ? $active_data[0]->total_act : 0;

                        //INACTIVE USER
                        $inactive_query_str = "SELECT COUNT(u.userid) as total_inact FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' AND  status=1 AND is_deleted='0' ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status!=1  AND u.is_deleted=0 " . $user_reg_cond;
                        $inactive_data = $conn->getQueryValue($inactive_query_str);
                        $inactive_count = (count($inactive_data) > 0) ? $inactive_data[0]->total_inact : 0;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'active_count' => $active_count, 'inactive_count' => $inactive_count);
                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0' " . $user_cond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        //ACTIVE USER
                        $active_query_str = "SELECT COUNT(u.userid) as total_act FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code='" . $val->dealer_code . "' AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status=1 AND u.is_deleted=0 " . $user_reg_cond;
                        $active_data = $conn->getQueryValue($active_query_str);
                        $active_count = (count($active_data) > 0) ? $active_data[0]->total_act : 0;

                        //INACTIVE USER
                        $inactive_query_str = "SELECT COUNT(u.userid) as total_inact FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code='" . $val->dealer_code . "' AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status!=1 " . $user_reg_cond;
                        $inactive_data = $conn->getQueryValue($inactive_query_str);
                        $inactive_count = (count($inactive_data) > 0) ? $inactive_data[0]->total_inact : 0;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'active_count' => $active_count, 'inactive_count' => $inactive_count);
                    }
                }
            } else if ($type == 'dealer') {
                //GET ALL DEALER

                $duser_cond = str_replace("dealer_code", "d.dealer_code", $user_cond);
                $getdealerquery_str = "SELECT u.userid,u.smpin,u.fname,u.lname,u.dealer_code,u.status FROM " . TABLE_USER . " u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code " . $tablejoin . " WHERE u.is_deleted=0 AND u.role>7 AND u.role<16 AND u.role!='14' AND d.status=1 AND d.is_deleted='0' " . $duser_cond . $user_reg_cond . " GROUP BY u.userid";
                $getdse_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdse_data) > 0) {
                    foreach ($getdse_data as $val) {

                        if ($val->status == '1') {
                            $active_count = 1;
                            $inactive_count = 0;
                            $active_txt = "Active";
                        } else {
                            $active_count = 0;
                            $inactive_count = 1;
                            $active_txt = "Inactive";
                        }

                        $returnarr[] = array("userid" => $val->userid, "smpin" => $val->smpin, "name" => trim($val->fname . " " . $val->lname), 'dealer_code' => $val->dealer_code, 'active_count' => $active_count, 'inactive_count' => $inactive_count, 'active_txt' => $active_txt);
                    }
                }
            }

            $active_query_str = "SELECT COUNT(DISTINCT u.userid) as total_act FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . "  ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status=1 AND u.is_deleted=0 " . $user_reg_cond;
            $active_data = $conn->getQueryValue($active_query_str);
            $active_count = (count($active_data) > 0) ? $active_data[0]->total_act : 0;

            //INACTIVE USER
            $inactive_query_str = "SELECT COUNT(DISTINCT u.userid) as total_inact FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.is_deleted=0 AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $user_cond . "  ) AND u.role>7 AND u.role<16 AND u.role!='14' AND u.status!=1  AND u.is_deleted=0 " . $user_reg_cond;
            $inactive_data = $conn->getQueryValue($inactive_query_str);
            $inactive_count = (count($inactive_data) > 0) ? $inactive_data[0]->total_inact : 0;

            $total_arr = array('active_count' => $active_count, 'inactive_count' => $inactive_count);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// page_access_v2
function page_access_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        //$_SESSION["userid"]='1';
        if (isset($_SESSION["userid"])) {
            //$page = escape_str($_REQUEST["page"]);

            //if(!($page!=''))  $conn->message(false,$conn->getMessage($lang,'report','error6'));

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND d.am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND d.rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND d.zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            $tablejoin = '';
            if ($regtype == '1') {
                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= " AND e.is_current=1 ";
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            //$query_str="SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,MAX(ac.created_on) as last_access,d.dealer_name FROM ".TABLE_USER_ACCESSCOUNT." as ac JOIN ".TABLE_USER." as u ON ac.smpin=u.smpin JOIN ".DMS_DEALER." as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  AND ac.page='".$page."' ".$user_cond." GROUP BY ac.smpin ORDER BY last_access DESC ".$limitcond;
            //Dashboard
            //Learning
            //Assessment
            //Feedback
            //JOIN ".TABLE_USER_ACCESSCOUNT." as ac ON ac.smpin=u.smpin
            $query_str = "SELECT SQL_CALC_FOUND_ROWS u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,COALESCE(acd.last_access,'-') as dashboard_last_access,COALESCE( acl.last_access,'-') as learning_last_access,COALESCE(aca.last_access,'-') as assessment_last_access,COALESCE(acf.last_access,'-') as feedback_last_access,d.dealer_name FROM " . TABLE_USER . " as u " . $tablejoin . " LEFT JOIN (SELECT smpin,MAX(created_on) as last_access FROM " . TABLE_USER_ACCESSCOUNT . " WHERE page='Dashboard' GROUP BY smpin  )as acd ON u.smpin=acd.smpin  LEFT JOIN (SELECT smpin,MAX(created_on) as last_access FROM " . TABLE_USER_ACCESSCOUNT . " WHERE page='Learning' GROUP BY smpin  )as acl ON u.smpin=acl.smpin LEFT JOIN (SELECT smpin,MAX(created_on) as last_access FROM " . TABLE_USER_ACCESSCOUNT . " WHERE page='Assessment' GROUP BY smpin  )as aca ON u.smpin=aca.smpin LEFT JOIN (SELECT smpin,MAX(created_on) as last_access FROM " . TABLE_USER_ACCESSCOUNT . " WHERE page='Feedback' GROUP BY smpin  )as acf ON u.smpin=acf.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  " . $user_cond . " GROUP BY u.userid ORDER BY dashboard_last_access DESC,u.userid ASC " . $limitcond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}
// points_report_v2
function points_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            $emp_cond = '';
            if ($regtype == '1') {
                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= ' AND e.is_current=1 ';
                $emp_cond = ' AND u.is_current=1 ';
            }

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $accumulation_point = 0;
                        $redemption_point = 0;
                        $approved_point = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND  status=1 AND is_deleted='0' )";

                        //ACCUMULATION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //REDEMPTION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " " . $manager_cond . "  LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //APPROVED POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND  zm_email='" . $val->zm_email . "' ";
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //REG DSE COUNT
                        $query_str = "SELECT COUNT(u.userid) emp_count  FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 AND u.role<14  AND u.is_deleted=0 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $regdse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'accumulation_point' => $accumulation_point, 'redemption_point' => $redemption_point, "approved_point" => $approved_point, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'regdse_count' => $regdse_count);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $accumulation_point = 0;
                        $redemption_point = 0;
                        $approved_point = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

                        //ACCUMULATION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //REDEMPTION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " " . $manager_cond . "  LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //APPROVED POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND rm_email='" . $val->rm_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //REG DSE COUNT
                        $query_str = "SELECT COUNT(u.userid) emp_count  FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 AND u.role<14  AND u.is_deleted=0 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $regdse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'accumulation_point' => $accumulation_point, 'redemption_point' => $redemption_point, "approved_point" => $approved_point, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'regdse_count' => $regdse_count);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0'  " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $accumulation_point = 0;
                        $redemption_point = 0;
                        $approved_point = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $mcond . " AND status=1 AND is_deleted='0'  )";

                        //ACCUMULATION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //REDEMPTION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " " . $manager_cond . "  LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //APPROVED POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND am_email='" . $val->am_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //REG DSE COUNT
                        $query_str = "SELECT COUNT(u.userid) emp_count  FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 AND u.role<14  AND u.is_deleted=0 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $regdse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'accumulation_point' => $accumulation_point, 'redemption_point' => $redemption_point, "approved_point" => $approved_point, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'regdse_count' => $regdse_count);

                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0'  " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $accumulation_point = 0;
                        $redemption_point = 0;
                        $approved_point = 0;
                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";

                        //ACCUMULATION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //REDEMPTION POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " " . $manager_cond . "  LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                        //APPROVED POINT
                        $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';

                        $dealer_count = "1";

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //REG DSE COUNT
                        $query_str = "SELECT COUNT(u.userid) emp_count  FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 AND u.role<14  AND u.is_deleted=0 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $regdse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'accumulation_point' => $accumulation_point, 'redemption_point' => $redemption_point, "approved_point" => $approved_point, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'regdse_count' => $regdse_count);

                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' " . $mcond . " AND status=1 AND is_deleted='0'   )" : " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted='0'   )";
            //ACCUMULATION POINT
            $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE  u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='C' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
            //REDEMPTION POINT
            $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_EPOINTS_HISTORY . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.transaction_type='D' " . $user_cond . " " . $manager_cond . "  LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
            //APPROVED POINT
            $query_str = "SELECT SUM(h.points) as sumpoints FROM " . TABLE_USER . " u " . $tablejoin . " JOIN " . TABLE_USER_WALLET_APPROVE . " h ON u.smpin=h.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND h.is_approved='1' " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';

            //DEALER COUNT
            $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 " . $mcond;
            $getdealercount_data = $conn->getQueryValue($getdealercount_str);
            $dealer_count = $getdealercount_data[0]->dealer_count;

            //DSE COUNT
            $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
            $dsecount_arr = $conn->getQueryValue($query_str);
            $dse_count = $dsecount_arr[0]->emp_count;

            //REG DSE COUNT
            $query_str = "SELECT COUNT(u.userid) emp_count  FROM " . TABLE_USER . " u " . $tablejoin . " WHERE u.role>7 AND u.role<14  AND u.is_deleted=0 " . $user_cond . $manager_cond . " LIMIT 1 ";
            $dsecount_arr = $conn->getQueryValue($query_str);
            $regdse_count = $dsecount_arr[0]->emp_count;

            $total_arr = array('accumulation_point' => $accumulation_point, 'redemption_point' => $redemption_point, "approved_point" => $approved_point, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'regdse_count' => $regdse_count);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// sales_report_v2
function sales_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $month = escape_str($_REQUEST["month"]);
            $year = escape_str($_REQUEST["year"]);

            if (!($month != '' && $year != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $from_date = date('Y-m', strtotime($year . "-" . $month . "-01")) . "-01";
            $to_date = date('Y-m-t', strtotime($year . "-" . $month . "-01"));

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1' || !isset($_REQUEST["regtype"])) ? "1" : "0";

            if ($regtype == '1') {
                //$tablejoin=" JOIN ".DMS_EMPLOYEE." e ON u.smpin=e.smpin";
                $user_cond .= ' AND u.is_current=1 ';
            }

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0'  GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $total_target = 0;
                        $total_sales = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0'   )";

                        //TOTAL SALES
                        $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $q1 = $query_str;
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_sales = (count($result_arr) > 0 && $result_arr[0]->total_sales) ? $result_arr[0]->total_sales : '0';
                        //TOTAL TARGET
                        $query_str = "SELECT SUM(s.target) as total_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $q2 = $query_str;
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($result_arr) > 0 && $result_arr[0]->total_target) ? $result_arr[0]->total_target : '0';

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'target' => $total_target, 'sales' => $total_sales, 'q1' => $q1, 'q2' => $q2);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0'  " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $total_target = 0;
                        $total_sales = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

                        //TOTAL SALES
                        $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_sales = (count($result_arr) > 0 && $result_arr[0]->total_sales) ? $result_arr[0]->total_sales : '0';
                        //TOTAL TARGET
                        $query_str = "SELECT SUM(s.target) as total_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($result_arr) > 0 && $result_arr[0]->total_target) ? $result_arr[0]->total_target : '0';

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'target' => $total_target, 'sales' => $total_sales);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0'  " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $total_target = 0;
                        $total_sales = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $mcond . " AND status=1 AND is_deleted='0'  )";

                        //TOTAL SALES
                        $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_sales = (count($result_arr) > 0 && $result_arr[0]->total_sales) ? $result_arr[0]->total_sales : '0';
                        //TOTAL TARGET
                        $query_str = "SELECT SUM(s.target) as total_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "') s ON u.smpin=s.smpin WHERE  u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($result_arr) > 0 && $result_arr[0]->total_target) ? $result_arr[0]->total_target : '0';

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'target' => $total_target, 'sales' => $total_sales);

                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0'  " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $total_target = 0;
                        $total_sales = 0;
                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";

                        //TOTAL SALES
                        $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_sales = (count($result_arr) > 0 && $result_arr[0]->total_sales) ? $result_arr[0]->total_sales : '0';
                        //TOTAL TARGET
                        $query_str = "SELECT SUM(s.target) as total_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
                        $result_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($result_arr) > 0 && $result_arr[0]->total_target) ? $result_arr[0]->total_target : '0';

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'target' => $total_target, 'sales' => $total_sales);

                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' " . $mcond . " AND status=1 AND is_deleted='0'   )" : " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted='0'   )";
            //TOTAL SALES
            $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $total_sales = (count($result_arr) > 0 && $result_arr[0]->total_sales) ? $result_arr[0]->total_sales : '0';
            //TOTAL TARGET
            $query_str = "SELECT SUM(s.target) as total_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "') s ON u.smpin=s.smpin WHERE u.status=1 " . $user_cond . " " . $manager_cond . " LIMIT 1 ";
            $result_arr = $conn->getQueryValue($query_str);
            $total_target = (count($result_arr) > 0 && $result_arr[0]->total_target) ? $result_arr[0]->total_target : '0';

            $total_arr = array('target' => $total_target, 'sales' => $total_sales);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// certification_report_v2
function certification_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            $emp_cond = '';
            if ($regtype == '1') {
                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_cond .= ' AND e.is_current=1 ';
                $emp_cond = ' AND u.is_current=1 ';
            }
            $basel1_count = 0;
            $bronze1_count = 0;
            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0'  GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $base_count = 0;
                        $bronze_count = 0;
                        $silver_count = 0;
                        $gold_count = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0'   )";
//                        //BASE L1
                        //                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=1 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $base_arr=$conn->getQueryValue($query_str);
                        //                        $basel1_count=(count($base_arr)>0)?$base_arr[0]->learn_count:'0';
                        //BASE L2
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $base_arr = $conn->getQueryValue($query_str);
                        $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';
//                        //BRONZE L1
                        //                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=2 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $bronze_arr=$conn->getQueryValue($query_str);
                        //                        $bronze1_count=(count($bronze_arr)>0)?$bronze_arr[0]->learn_count:'0';
                        //BRONZE L2
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $bronze_arr = $conn->getQueryValue($query_str);
                        $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';
                        //SILVER L1
						//lvl_id=3 AND status IN (7,4,1,2,3)
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=3 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver1_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';
                        //SILVER L2
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';
                        //GOLD L1
						//lvl_id=4 AND status IN (7,4,1,2,3)
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=5 AND status=3  ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold1_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';
                        //GOLD L2
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND  zm_email='" . $val->zm_email . "' ";
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'base1' => $basel1_count, 'base' => $base_count, 'bronze1' => $bronze1_count, 'bronze' => $bronze_count, 'silver1' => $silver1_count, 'silver' => $silver_count, 'gold1' => $gold1_count, 'gold' => $gold_count, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE    status=1 AND is_deleted='0' " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $base_count = 0;
                        $bronze_count = 0;
                        $silver_count = 0;
                        $gold_count = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=1 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $base_arr=$conn->getQueryValue($query_str);
                        //                        $base1_count=(count($base_arr)>0)?$base_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $base_arr = $conn->getQueryValue($query_str);
                        $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=2 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $bronze_arr=$conn->getQueryValue($query_str);
                        //                        $bronze1_count=(count($bronze_arr)>0)?$bronze_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $bronze_arr = $conn->getQueryValue($query_str);
                        $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';
//lvl_id=3 AND status IN (7,4,1,2,3)
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=3 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver1_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';
//lvl_id=4 AND status IN (7,4,1,2,3)
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=5 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold1_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND rm_email='" . $val->rm_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;
                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'base1' => $basel1_count, 'base' => $base_count, 'bronze1' => $bronze1_count, 'bronze' => $bronze_count, 'silver1' => $silver1_count, 'silver' => $silver_count, 'gold1' => $gold1_count, 'gold' => $gold_count, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0' " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {
                        $base_count = 0;
                        $bronze_count = 0;
                        $silver_count = 0;
                        $gold_count = 0;
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=1 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $base_arr=$conn->getQueryValue($query_str);
                        //                        $base1_count=(count($base_arr)>0)?$base_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $base_arr = $conn->getQueryValue($query_str);
                        $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=2  AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $bronze_arr=$conn->getQueryValue($query_str);
                        //                        $bronze1_count=(count($bronze_arr)>0)?$bronze_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $bronze_arr = $conn->getQueryValue($query_str);
                        $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';
//lvl_id=3 AND status IN (7,4,1,2,3)
                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin  FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=3 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver1_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin  FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=5 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold1_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND am_email='" . $val->am_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'base1' => $basel1_count, 'base' => $base_count, 'bronze1' => $bronze1_count, 'bronze' => $bronze_count, 'silver1' => $silver1_count, 'silver' => $silver_count, 'gold1' => $gold1_count, 'gold' => $gold_count, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count);

                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $base_count = 0;
                        $bronze_count = 0;
                        $silver_count = 0;
                        $gold_count = 0;
                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=1 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $base_arr=$conn->getQueryValue($query_str);
                        //                        $base1_count=(count($base_arr)>0)?$base_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $base_arr = $conn->getQueryValue($query_str);
                        $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';

//                        $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=2 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
                        //                        $bronze_arr=$conn->getQueryValue($query_str);
                        //                        $bronze1_count=(count($bronze_arr)>0)?$bronze_arr[0]->learn_count:'0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $bronze_arr = $conn->getQueryValue($query_str);
                        $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=3 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver1_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $silver_arr = $conn->getQueryValue($query_str);
                        $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin  FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=5 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold1_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin  FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
                        $gold_arr = $conn->getQueryValue($query_str);
                        $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

                        $dealer_count = "1";

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'base1' => $basel1_count, 'base' => $base_count, 'bronze1' => $bronze1_count, 'bronze' => $bronze_count, 'silver1' => $silver1_count, 'silver' => $silver_count, 'gold1' => $gold1_count, 'gold' => $gold_count, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count);

                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' " . $mcond . " AND status=1 AND is_deleted='0' )" : " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted='0' )";

//            $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=1 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
            //            $base_arr=$conn->getQueryValue($query_str);
            //            $base1_count=(count($base_arr)>0)?$base_arr[0]->learn_count:'0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $base_arr = $conn->getQueryValue($query_str);
            $base_count = (count($base_arr) > 0) ? $base_arr[0]->learn_count : '0';

//            $query_str="SELECT COUNT(l.id) as learn_count FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin FROM ".TABLE_USER_LEARNING." WHERE lvl_id=2 AND status IN (7,4,1,2,3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0".$user_cond.$manager_cond." LIMIT 1 ";
            //            $bronze_arr=$conn->getQueryValue($query_str);
            //            $bronze1_count=(count($bronze_arr)>0)?$bronze_arr[0]->learn_count:'0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $bronze_arr = $conn->getQueryValue($query_str);
            $bronze_count = (count($bronze_arr) > 0) ? $bronze_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=3 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $silver_arr = $conn->getQueryValue($query_str);
            $silver1_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $silver_arr = $conn->getQueryValue($query_str);
            $silver_count = (count($silver_arr) > 0) ? $silver_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=5 AND status=3) ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $gold_arr = $conn->getQueryValue($query_str);
            $gold1_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

            $query_str = "SELECT COUNT(l.id) as learn_count FROM " . TABLE_USER . " u " . $tablejoin . " JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3 ) l ON u.smpin=l.smpin WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0" . $user_cond . $manager_cond . " LIMIT 1 ";
            $gold_arr = $conn->getQueryValue($query_str);
            $gold_count = (count($gold_arr) > 0) ? $gold_arr[0]->learn_count : '0';

            //DEALER COUNT
            $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 " . $mcond;
            $getdealercount_data = $conn->getQueryValue($getdealercount_str);
            $dealer_count = $getdealercount_data[0]->dealer_count;

            //DSE COUNT
            $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $emp_cond . $manager_cond . " LIMIT 1 ";
            $dsecount_arr = $conn->getQueryValue($query_str);
            $dse_count = $dsecount_arr[0]->emp_count;

            $total_arr = array('base1' => $basel1_count, 'base' => $base_count, 'bronze1' => $bronze1_count, 'bronze' => $bronze_count, 'silver1' => $silver1_count, 'silver' => $silver_count, 'gold1' => $gold1_count, 'gold' => $gold_count, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// enquiry_report_v2
function enquiry_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $type = escape_str($_REQUEST["type"]);
            if (!empty($type) && $type != '') {
                switch ($type) {
                    case '0':$user_cond .= ' AND e.customerid=0';
                        break;
                    case '1':$user_cond .= ' AND e.customerid!=0';
                        break;
                }
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS e.id,CASE WHEN e.customerid=0 THEN 'HYPERM App' WHEN e.customerid!=0 THEN 'Customer Experience App' END as source_type,CASE WHEN e.customerid=0 THEN u.smpin WHEN e.customerid!=0 THEN c.smpin END as enq_smpin,CASE WHEN e.customerid=0 THEN CONCAT(u.fname,u.lname) WHEN e.customerid!=0 THEN c.dsename END as enq_dsename , m.model_name,v.title as variant_name ,d.`dealer_code`, d.`dealer_name`,d.zone,d.`zm_name`, d.`rm_name`, d.`am_name`, e.`name`, e.`mobile`, e.`email`, e.is_brochure, e.created_on
FROM " . TABLE_EXPLORE_ENQUIRY . " as e
JOIN " . TABLE_USER . " as u ON e.user_id=u.userid
JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
JOIN " . TABLE_COMPARE_MODEL . " as m ON  m.model_id=e.model_id
JOIN " . TABLE_MODEL_VARIANT . " v ON e.variant_id=v.id
LEFT JOIN " . TABLE__CUSTOMEREXP . " c ON c.id=e.customerid
WHERE e.created_on>='" . $from_date . " 00:00:00' AND e.created_on<='" . $to_date . " 23:59:59' " . $user_cond . " ORDER BY e.id ASC " . $limitcond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// customerexperience_report_v2
function customerexperience_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $query_str = "SELECT SQL_CALC_FOUND_ROWS c.`salutation`, c.`firstname`, c.`lastname`, c.`email`, c.`mobile`, c.`age`, c.`smpin`, c.`dsename` ,c.`created_on`,d.`dealer_code`, d.`dealer_name`,d.zone,d.`zm_name`, d.`rm_name`, d.`am_name` FROM " . TABLE__CUSTOMEREXP . " as c JOIN " . TABLE_USER . " as u ON c.userid=u.userid JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE c.created_on>='" . $from_date . " 00:00:00' AND c.created_on<='" . $to_date . " 23:59:59' " . $user_cond . " ORDER BY c.id ASC " . $limitcond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;

            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// assessment_report_v2
function assessment_report_v2()
{

    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $assessmentid = escape_str($_REQUEST["assessmentid"]);

            $query_str = "SELECT `assessmentid`,`atype`, `lvl_id`, `assessmentname`,user_type  FROM " . TABLE_ASSESSMENT . " WHERE status=1 AND assettypeid=1 AND  assessmentid='" . $assessmentid . "'";
            $assessment_arr = $conn->getQueryValue($query_str);

            if (!($assessmentid != '' && count($assessment_arr) > 0)) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $score_arr = array();
            $score_arr[] = array("from" => 0, 'to' => 49);
            $score_arr[] = array("from" => 50, 'to' => 79);
            $score_arr[] = array("from" => 80, 'to' => 100);

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0' )";
                        $rarr = array("name" => $val->zm_name, 'email' => $val->zm_email);
                        $m = 1;
                        if (count($score_arr) > 0) {
                            foreach ($score_arr as $res_score) {
                                $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                                $result_arr = $conn->getQueryValue($query_str);
                                $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                                $rarr['level_' . ($m++)] = $usercount;
                            }
                        }
                        //TOTAL
                        $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                        $result_arr = $conn->getQueryValue($query_str);
                        $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                        $rarr['total'] = $usercount;
                        $returnarr[] = $rarr;
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE    status=1 AND is_deleted='0' " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";
                        $rarr = array("name" => $val->rm_name, 'email' => $val->rm_email);
                        $m = 1;
                        if (count($score_arr) > 0) {
                            foreach ($score_arr as $res_score) {
                                $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                                $result_arr = $conn->getQueryValue($query_str);
                                $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                                $rarr['level_' . ($m++)] = $usercount;
                            }
                        }
                        //TOTAL
                        $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                        $result_arr = $conn->getQueryValue($query_str);
                        $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                        $rarr['total'] = $usercount;

                        $returnarr[] = $rarr;
                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE    status=1 AND is_deleted='0' " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $mcond . " AND status=1 AND is_deleted='0'  )";
                        $rarr = array("name" => $val->am_name, 'email' => $val->am_email);
                        $m = 1;
                        if (count($score_arr) > 0) {
                            foreach ($score_arr as $res_score) {
                                $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                                $result_arr = $conn->getQueryValue($query_str);
                                $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                                $rarr['level_' . ($m++)] = $usercount;
                            }
                        }
                        //TOTAL
                        $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                        $result_arr = $conn->getQueryValue($query_str);
                        $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                        $rarr['total'] = $usercount;
                        $returnarr[] = $rarr;
                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {
                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";
                        $rarr = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code);
                        $m = 1;
                        if (count($score_arr) > 0) {
                            foreach ($score_arr as $res_score) {
                                $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                                $result_arr = $conn->getQueryValue($query_str);
                                $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                                $rarr['level_' . ($m++)] = $usercount;
                            }
                        }
                        //TOTAL
                        $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                        $result_arr = $conn->getQueryValue($query_str);
                        $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                        $rarr['total'] = $usercount;
                        $returnarr[] = $rarr;
                    }
                }
            }

            $rarr = array();
            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' " . $mcond . " AND status=1 AND is_deleted='0' )" : " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted='0' )";
            if (count($score_arr) > 0) {
                $m = 1;
                foreach ($score_arr as $res_score) {
                    $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND au.highscore>='" . $res_score['from'] . "' AND au.highscore<='" . $res_score['to'] . "' " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
                    $result_arr = $conn->getQueryValue($query_str);
                    $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
                    $rarr['level_' . ($m++)] = $usercount;
                }
            }
            //TOTAL
            $query_str = "SELECT count(au.userid) as usercount FROM `" . TABLE_ASSESSMENT_USER . "` as au JOIN " . TABLE_USER . " as u ON au.userid=u.userid WHERE au.assessmentid='" . $assessmentid . "' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 " . $manager_cond . " GROUP BY au.assessmentid LIMIT 1";
            $result_arr = $conn->getQueryValue($query_str);
            $usercount = (count($result_arr) > 0 && $result_arr[0]->usercount) ? $result_arr[0]->usercount : '0';
            $rarr['total'] = $usercount;
            $total_arr = $rarr;
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);

        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// setsalestarget_report_v2
function setsalestarget_report_v2()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $month = escape_str($_REQUEST["month"]);
            $year = escape_str($_REQUEST["year"]);

            if (!($month != '' && $year != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $from_date = date('Y-m-d', strtotime($year . "-" . $month . "-01"));
            $to_date = date('Y-m-t', strtotime($year . "-" . $month . "-01"));

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1' || !isset($_REQUEST["regtype"])) ? "1" : "0";

            if ($regtype == '1') {
                //$tablejoin=" JOIN ".DMS_EMPLOYEE." e ON u.smpin=e.smpin";
                $user_cond .= ' AND u.is_current=1 ';
            }

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE   status=1 AND is_deleted='0' GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted='0' )";

                        //$query_str="SELECT SUM(t.target) as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin   WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
                        $query_str = "SELECT SUM(t.target) as tot_target FROM " . DMS_EMPLOYEE . " u  JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";

                        $count_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

                        //$query_str="SELECT SUM(t.target)as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM ".TABLE_USER_SALESTARGET_STATUS."  WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."' AND status=1  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM ".TABLE_USER_SALESTARGET_STATUS." WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status=1  AND roleid=14  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $dealer_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "'  ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status IN(2,3) AND roleid=7 ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $am_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'total_target' => $total_target, 'dealer_target' => $dealer_target, 'am_target' => $am_target, 'auto_target' => $auto_target);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

                        $query_str = "SELECT SUM(t.target) as tot_target FROM " . DMS_EMPLOYEE . " u  JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";

                        $count_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

                        //$query_str="SELECT SUM(t.target)as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM ".TABLE_USER_SALESTARGET_STATUS."  WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."' AND status=1  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM ".TABLE_USER_SALESTARGET_STATUS." WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status=1  AND roleid=14  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $dealer_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "'  ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status IN(2,3) AND roleid=7 ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $am_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'total_target' => $total_target, 'dealer_target' => $dealer_target, 'am_target' => $am_target, 'auto_target' => $auto_target);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted='0' " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "' " . $mcond . " AND status=1 AND is_deleted='0' )";

                        $query_str = "SELECT SUM(t.target) as tot_target FROM " . DMS_EMPLOYEE . " u  JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";

                        $count_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

                        //$query_str="SELECT SUM(t.target)as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM ".TABLE_USER_SALESTARGET_STATUS."  WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."' AND status=1  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM ".TABLE_USER_SALESTARGET_STATUS." WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status=1  AND roleid=14  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $dealer_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "'  ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status IN(2,3) AND roleid=7 ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $am_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'total_target' => $total_target, 'dealer_target' => $dealer_target, 'am_target' => $am_target, 'auto_target' => $auto_target);

                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {

                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";

                        $query_str = "SELECT SUM(t.target) as tot_target FROM " . DMS_EMPLOYEE . " u  JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";

                        $count_arr = $conn->getQueryValue($query_str);
                        $total_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';

                        //$query_str="SELECT SUM(t.target)as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM ".TABLE_USER_SALESTARGET_STATUS."  WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."' AND status=1  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM ".TABLE_USER_SALESTARGET_STATUS." WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status=1  AND roleid=14  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $dealer_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "'  ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status IN(2,3) AND roleid=7 ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $am_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'total_target' => $total_target, 'dealer_target' => $dealer_target, 'am_target' => $am_target, 'auto_target' => $auto_target);
                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' " . $mcond . " AND status=1 AND is_deleted='0'  )" : " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted='0' )";

            //$query_str="SELECT SUM(t.target) as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN ".TABLE_USER_SALETARGET." t ON u.smpin=t.smpin   WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND t.sale_month>='".$from_date."' AND t.sale_month<='".$to_date."' ".$user_cond.$manager_cond." LIMIT 1 ";
            $query_str = "SELECT SUM(t.target) as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN " . TABLE_USER_SALETARGET . " t ON u.smpin=t.smpin   WHERE u.status=1 AND t.sale_month>='" . $from_date . "' AND t.sale_month<='" . $to_date . "' " . $user_cond . $manager_cond . " LIMIT 1 ";
            $count_arr = $conn->getQueryValue($query_str);
            $total_target = (count($count_arr) > 0 && $count_arr[0]->tot_target) ? $count_arr[0]->tot_target : '0';
            $t_q = $query_str;
            //$query_str="SELECT SUM(t.target)as tot_target FROM ".TABLE_USER." u ".$tablejoin." JOIN (SELECT id,smpin,target,sale_month FROM ".TABLE_USER_SALETARGET." WHERE sale_month>='".$from_date."' AND sale_month<='".$to_date."' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM ".TABLE_USER_SALESTARGET_STATUS."  WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."' AND status=1  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM ".TABLE_USER_SALESTARGET_STATUS." WHERE sale_month>='".$from_date."' AND  sale_month<='".$to_date."'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0  ".$user_cond.$manager_cond." LIMIT 1 ";
            $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status=1 AND roleid=14  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE u.status=1  " . $user_cond . $manager_cond . " LIMIT 1 ";
            $sales_arr = $conn->getQueryValue($query_str);
            $dealer_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

            $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN  (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "'  ) t ON u.smpin=t.smpin JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND status IN(2,3) AND roleid=7  ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month JOIN (SELECT max(id) as maxid  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "'  GROUP by smpin ) as a ON a.maxid=s.id  WHERE  u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
            $sales_arr = $conn->getQueryValue($query_str);
            $am_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

            $query_str = "SELECT SUM(t.target)as tot_target FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
            $sales_arr = $conn->getQueryValue($query_str);
            $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->tot_target) ? $sales_arr[0]->tot_target : '0';

            $total_arr = array('total_target' => $total_target, 'dealer_target' => $dealer_target, 'am_target' => $am_target, 'auto_target' => $auto_target);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// enquiry_report_v3
function enquiry_report_v3()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = escape_str($_REQUEST["from_date"]);
            $to_date = escape_str($_REQUEST["to_date"]);

            if (!($from_date != '' && $to_date != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            if (!(isValidDateTimeString($from_date, 'Y-m-d', 'Asia/Kolkata') && isValidDateTimeString($to_date, 'Y-m-d', 'Asia/Kolkata'))) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error7'));
            }

            $user_cond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND d.am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND d.rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND d.zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                //$user_cond='';
            }

            //PAGINATION
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limitcond = " limit " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $type = escape_str($_REQUEST["type"]);
//            if(!empty($type)&& $type!='' )
            //            {
            //               switch($type)
            //               {
            //                   case '0':$user_cond.=' AND e.customerid=0'; break;
            //                   case '1':$user_cond.=' AND e.customerid!=0';break;
            //               }
            //            }

            $query_str = "SELECT c.id as cust_id,coalesce(enq.id,'0') as enq_id,IF(ISNULL(enq.created_on),'0', '1') enquiry_status,c.created_on,'' as model_name,'' as variant_name,d.`dealer_code`, d.`dealer_name`,d.`city`,d.`state`,d.`zm_name`, d.`rm_name`, d.`am_name` ,'Customer Experience App' as source_type,CONCAT(CONCAT(c.salutation, ' ', c.firstname), ' ', c.lastname)as name,c. `mobile`, c.`email`,c.`age`,0 as is_brochure, CASE WHEN c.smpin='' THEN u.smpin ELSE c.smpin END AS smpin,CASE WHEN c.dsename='' THEN CONCAT(u.fname, ' ', u.lname) ELSE c.dsename END AS dsename
FROM " . TABLE__CUSTOMEREXP . " as c
JOIN " . TABLE_USER . "  as u ON c.userid=u.userid
JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
LEFT JOIN " . TABLE_EXPLORE_ENQUIRY . " enq ON enq.customerid=c.id
WHERE c.created_on>='" . $from_date . "' AND  c.created_on<='" . $to_date . "' " . $user_cond . "
UNION
SELECT '0' as cust_id,e.id as enq_id,'1' as enquiry_status,e.created_on,m.model_name,v.title as variant_name ,d.`dealer_code`, d.`dealer_name`,d.`city`,d.`state`,d.`zm_name`, d.`rm_name`, d.`am_name` ,'DSE App' as source_type, e.`name`, e.`mobile`, e.`email`,'' as age, e.is_brochure,u.smpin as smpin, CONCAT(u.fname, ' ', u.lname) as dsename
FROM " . TABLE_EXPLORE_ENQUIRY . " as e
JOIN " . TABLE_USER . " as u ON e.user_id=u.userid
JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
JOIN " . TABLE_COMPARE_MODEL . " as m ON  m.model_id=e.model_id
JOIN " . TABLE_MODEL_VARIANT . " v ON e.variant_id=v.id
WHERE e.customerid=0 AND  e.created_on>='" . $from_date . "' AND  e.created_on<='" . $to_date . "' " . $user_cond . "  ORDER BY created_on ASC " . $limitcond;

            $query = $conn->query($query_str);
            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
                foreach ($data_arr as $key => $res_val) {
                    $data_arr[$key]->is_feedback = 0;
                    $data_arr[$key]->feedback_id = 0;
                    $data_arr[$key]->created_on_str = '';
                    if ($res_val->enquiry_status == 0) {
                        $data_arr[$key]->brochure_status = ($res_val->is_brochure == '1') ? "Shared" : "Not Shared";
                        $data_arr[$key]->created_on_str = date('d-m-Y h:i A', strtotime($res_val->created_on));
                    } else {
                        $get_enq_data = $conn->getQueryValue("SELECT  e.id as enq_id, e.created_on,m.model_name,v.title as variant_name ,d.`dealer_code`, d.`dealer_name`,d.`city`,d.`state`,d.`zm_name`, d.`rm_name`, d.`am_name` ,'DSE App' as source_type, e.`name`, e.`mobile`, e.`email`,  e.is_brochure    FROM " . TABLE_EXPLORE_ENQUIRY . " as e
    JOIN " . TABLE_USER . " as u ON e.user_id=u.userid
    JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
    JOIN " . TABLE_COMPARE_MODEL . " as m ON  m.model_id=e.model_id
    JOIN " . TABLE_MODEL_VARIANT . " v ON e.variant_id=v.id
    WHERE e.id='" . $res_val->enq_id . "' LIMIT 1");

                        if (count($get_enq_data) > 0) {
                            $data_arr[$key]->brochure_status = ($get_enq_data[0]->is_brochure == '1') ? "Shared" : "Not Shared";
                            $data_arr[$key]->model_name = $get_enq_data[0]->model_name;
                            $data_arr[$key]->variant_name = $get_enq_data[0]->variant_name;
                            $data_arr[$key]->created_on = $get_enq_data[0]->created_on;
                            $data_arr[$key]->created_on_str = date('d-m-Y h:i A', strtotime($get_enq_data[0]->created_on));
                            $data_arr[$key]->name = $get_enq_data[0]->name;
                            $data_arr[$key]->email = $get_enq_data[0]->email;
                            $data_arr[$key]->mobile = $get_enq_data[0]->mobile;
                        }
                        if ($res_val->source_type == 'Customer Experience App' && $res_val->cust_id > 0) {
                            //TABLE__FEEDBACKUSER
                            $feed_data = $conn->getQueryValue("SELECT id FROM " . TABLE__FEEDBACKUSER . " WHERE customerid='" . $res_val->cust_id . "' LIMIT 1 ");
                            if (count($feed_data) > 0) {
                                $data_arr[$key]->is_feedback = "1";
                                $data_arr[$key]->feedback_id = $feed_data[0]->id;
                            }
                        }
                    }
                }

            }
            // Total records
            $rows_query = $conn->query("SELECT COUNT(id)total_records FROM (SELECT c.id as id
FROM " . TABLE__CUSTOMEREXP . " as c
JOIN " . TABLE_USER . "  as u ON c.userid=u.userid
JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
LEFT JOIN " . TABLE_EXPLORE_ENQUIRY . " enq ON enq.customerid=c.id
WHERE c.created_on>='" . $from_date . "' AND  c.created_on<='" . $to_date . "' " . $user_cond . "
UNION
SELECT e.id
FROM " . TABLE_EXPLORE_ENQUIRY . " as e
JOIN " . TABLE_USER . " as u ON e.user_id=u.userid
JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code
JOIN " . TABLE_COMPARE_MODEL . " as m ON  m.model_id=e.model_id
JOIN " . TABLE_MODEL_VARIANT . " v ON e.variant_id=v.id
WHERE e.customerid=0 AND  e.created_on>='" . $from_date . "' AND  e.created_on<='" . $to_date . "' " . $user_cond . " ) x");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;
            $arr['report'] = $data_arr;
            //$arr['q']=$query_str;
            if (count($arr) != 0) {
                $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
            } else {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// setsalestarget_report_v3
function setsalestarget_report_v3()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        // $_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $month = escape_str($_REQUEST["month"]);
            $year = escape_str($_REQUEST["year"]);

            if (!($month != '' && $year != '')) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error6'));
            }

            $from_date = date('Y-m-d', strtotime($year . "-" . $month . "-01"));
            $to_date = date('Y-m-t', strtotime($year . "-" . $month . "-01"));

            $user_cond = '';
            if ($am_email != 'all' && $am_email != '') {
                //AM
                $type = "am";
            } else if ($rm_email != 'all' && $rm_email != '') {
                //RM
                $type = "rm";
            } else if ($zm_email != 'all' && $zm_email != '') {
                //ZM
                $type = "zm";
            } else {
                //PAN
                $user_cond = '';
                $type = "pan";
            }

            $mcond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';

            }

            $returnarr = array();
            $tablejoin = '';
            $regtype = (escape_str($_REQUEST["regtype"]) == '1' || !isset($_REQUEST["regtype"])) ? "1" : "0";

            if ($regtype == '1') {
                //$tablejoin=" JOIN ".DMS_EMPLOYEE." e ON u.smpin=e.smpin";
                $user_cond .= ' AND u.is_current=1 ';
            }

            if ($type == 'pan') {
                //GET ALL ZM
                $getzmquery_str = "SELECT zm_name,zm_email FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 GROUP BY zm_email";
                $getzm_data = $conn->getQueryValue($getzmquery_str);

                if (count($getzm_data) > 0) {
                    foreach ($getzm_data as $val) {
                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $val->zm_email . "' AND status=1 AND is_deleted=0  )";

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND zm_email='" . $val->zm_email . "' ";
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //Target Set by Dealers
                        $get_dealer_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=14)as s ON e.smpin=s.smpin LIMIT 1 ";
                        $get_dealerupdated_arr = $conn->getQueryValue($get_dealer_updated_str);
                        $dealer_target_set_dse_count = $get_dealerupdated_arr[0]->emp_count;

                        //Target Approved by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=2)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_set_dse_count = $get_am_arr[0]->emp_count;

                        //Target Modified by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=3)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_mod_dse_count = $get_am_arr[0]->emp_count;

                        //Auto Target Set
                        $query_str = "SELECT COUNT(DISTINCT u.id) emp_count FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->emp_count) ? $sales_arr[0]->emp_count : '0';

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'dealer_target_set_dse_count' => $dealer_target_set_dse_count, 'am_target_set_dse_count' => $am_target_set_dse_count, 'am_target_mod_dse_count' => $am_target_mod_dse_count, 'auto_target' => $auto_target);
                    }
                }
            } else if ($type == 'zm') {
                //GET ALL RM
                $getrmquery_str = "SELECT rm_name,rm_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted=0 " . $mcond . " GROUP BY rm_email";
                $getrm_data = $conn->getQueryValue($getrmquery_str);

                if (count($getrm_data) > 0) {
                    foreach ($getrm_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $val->rm_email . "'   AND status=1 AND is_deleted=0  " . $mcond . " )";

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND rm_email='" . $val->rm_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //Target Set by Dealers
                        $get_dealer_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=14)as s ON e.smpin=s.smpin LIMIT 1 ";
                        $get_dealerupdated_arr = $conn->getQueryValue($get_dealer_updated_str);
                        $dealer_target_set_dse_count = $get_dealerupdated_arr[0]->emp_count;

                        //Target Approved by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=2)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_set_dse_count = $get_am_arr[0]->emp_count;

                        //Target Modified by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=3)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_mod_dse_count = $get_am_arr[0]->emp_count;

                        //Auto Target Set
                        $query_str = "SELECT COUNT(DISTINCT u.id) emp_count FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->emp_count) ? $sales_arr[0]->emp_count : '0';

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'dealer_target_set_dse_count' => $dealer_target_set_dse_count, 'am_target_set_dse_count' => $am_target_set_dse_count, 'am_target_mod_dse_count' => $am_target_mod_dse_count, 'auto_target' => $auto_target);

                    }
                }
            } else if ($type == 'rm') {
                //GET ALL AM
                $getamquery_str = "SELECT am_name,am_email FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted=0 " . $mcond . " GROUP BY am_email";
                $getam_data = $conn->getQueryValue($getamquery_str);

                if (count($getam_data) > 0) {
                    foreach ($getam_data as $val) {

                        $manager_cond = " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $val->am_email . "'   AND status=1 AND is_deleted=0 " . $mcond . "  )";

                        //DEALER COUNT
                        $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND am_email='" . $val->am_email . "' " . $mcond;
                        $getdealercount_data = $conn->getQueryValue($getdealercount_str);
                        $dealer_count = $getdealercount_data[0]->dealer_count;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //Target Set by Dealers
                        $get_dealer_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=14)as s ON e.smpin=s.smpin LIMIT 1 ";
                        $get_dealerupdated_arr = $conn->getQueryValue($get_dealer_updated_str);
                        $dealer_target_set_dse_count = $get_dealerupdated_arr[0]->emp_count;

                        //Target Approved by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=2)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_set_dse_count = $get_am_arr[0]->emp_count;

                        //Target Modified by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=3)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_mod_dse_count = $get_am_arr[0]->emp_count;

                        //Auto Target Set
                        $query_str = "SELECT COUNT(DISTINCT u.id) emp_count FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->emp_count) ? $sales_arr[0]->emp_count : '0';

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'dealer_target_set_dse_count' => $dealer_target_set_dse_count, 'am_target_set_dse_count' => $am_target_set_dse_count, 'am_target_mod_dse_count' => $am_target_mod_dse_count, 'auto_target' => $auto_target);

                    }
                }
            } else if ($type == 'am') {
                //GET ALL DEALER
                $getdealerquery_str = "SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted=0 " . $mcond . "";
                $getdealer_data = $conn->getQueryValue($getdealerquery_str);

                if (count($getdealer_data) > 0) {
                    foreach ($getdealer_data as $val) {

                        $manager_cond = " AND u.dealer_code='" . $val->dealer_code . "' ";

                        //DEALER COUNT
                        //$getdealercount_str="SELECT COUNT(id) dealer_count FROM ".DMS_DEALER." WHERE status=1 AND is_deleted=0 AND dealer_code='".$val->dealer_code."' ";
                        //$getdealercount_data=$conn->getQueryValue($getdealercount_str);
                        $dealer_count = 1;

                        //DSE COUNT
                        $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $dsecount_arr = $conn->getQueryValue($query_str);
                        $dse_count = $dsecount_arr[0]->emp_count;

                        //Target Set by Dealers
                        $get_dealer_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=14)as s ON e.smpin=s.smpin LIMIT 1 ";
                        $get_dealerupdated_arr = $conn->getQueryValue($get_dealer_updated_str);
                        $dealer_target_set_dse_count = $get_dealerupdated_arr[0]->emp_count;

                        //Target Approved by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=2)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_set_dse_count = $get_am_arr[0]->emp_count;

                        //Target Modified by AMs
                        $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=3)as s ON e.smpin=s.smpin  LIMIT 1  ";
                        $get_am_arr = $conn->getQueryValue($get_am_updated_str);
                        $am_target_mod_dse_count = $get_am_arr[0]->emp_count;

                        //Auto Target Set
                        $query_str = "SELECT COUNT(DISTINCT u.id) emp_count FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
                        $sales_arr = $conn->getQueryValue($query_str);
                        $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->emp_count) ? $sales_arr[0]->emp_count : '0';

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'dealer_target_set_dse_count' => $dealer_target_set_dse_count, 'am_target_set_dse_count' => $am_target_set_dse_count, 'am_target_mod_dse_count' => $am_target_mod_dse_count, 'auto_target' => $auto_target);
                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND dealer_code!='' " . $mcond . "  )" : " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND dealer_code!=''  )";

            //DEALER COUNT
            $getdealercount_str = "SELECT COUNT(id) dealer_count FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 " . $mcond . " ";
            $getdealercount_data = $conn->getQueryValue($getdealercount_str);
            $dealer_count = $getdealercount_data[0]->dealer_count;

            //DSE COUNT
            $query_str = "SELECT COUNT(u.id) emp_count  FROM " . DMS_EMPLOYEE . " u WHERE u.role IN (SELECT rolename FROM `" . TABLE_ROLE . "` WHERE id>7 AND id<14) AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
            $dsecount_arr = $conn->getQueryValue($query_str);
            $dse_count = $dsecount_arr[0]->emp_count;

            //Target Set by Dealers
            $get_dealer_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=14)as s ON e.smpin=s.smpin LIMIT 1 ";
            $get_dealerupdated_arr = $conn->getQueryValue($get_dealer_updated_str);
            $dealer_target_set_dse_count = $get_dealerupdated_arr[0]->emp_count;

            //Target Approved by AMs
            $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=2)as s ON e.smpin=s.smpin  LIMIT 1  ";
            $get_am_arr = $conn->getQueryValue($get_am_updated_str);
            $am_target_set_dse_count = $get_am_arr[0]->emp_count;

            //Target Modified by AMs
            $get_am_updated_str = "SELECT COUNT(DISTINCT e.id) emp_count FROM (SELECT id,dealer_code,smpin FROM " . DMS_EMPLOYEE . " u WHERE  status=1  " . $user_cond . $manager_cond . " )as e JOIN (SELECT id,smpin FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' AND roleid=7 AND status=3)as s ON e.smpin=s.smpin  LIMIT 1  ";
            $get_am_arr = $conn->getQueryValue($get_am_updated_str);
            $am_target_mod_dse_count = $get_am_arr[0]->emp_count;

            //Auto Target Set
            $query_str = "SELECT COUNT(DISTINCT u.id) emp_count FROM " . DMS_EMPLOYEE . " u " . $tablejoin . " JOIN (SELECT id,smpin,target,sale_month FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' ) t ON u.smpin=t.smpin LEFT JOIN (SELECT id,smpin,sale_month,status FROM " . TABLE_USER_SALESTARGET_STATUS . "  WHERE sale_month>='" . $from_date . "' AND  sale_month<='" . $to_date . "' ) s ON s.smpin=t.smpin AND t.sale_month=s.sale_month WHERE s.id IS NULL AND u.status=1 " . $user_cond . $manager_cond . " LIMIT 1 ";
            $sales_arr = $conn->getQueryValue($query_str);
            $auto_target = (count($sales_arr) > 0 && $sales_arr[0]->emp_count) ? $sales_arr[0]->emp_count : '0';

            $total_arr = array('dealer_count' => $dealer_count, 'dse_count' => $dse_count, 'dealer_target_set_dse_count' => $dealer_target_set_dse_count, 'am_target_set_dse_count' => $am_target_set_dse_count, 'am_target_mod_dse_count' => $am_target_mod_dse_count, 'auto_target' => $auto_target);
            $list_arr = $returnarr;
            $returnarr = array('total' => $total_arr, 'list' => $list_arr);
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

//reward_point_dse
function reward_point_dse()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $dealer_code = escape_str($_REQUEST["dealer_code"]);

            $returnarr = array();
            $tablejoin = '';
            if (!isset($_REQUEST["regtype"])) {
                $_REQUEST["regtype"] = 1;
            }

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            $emp_cond = '';
            if ($regtype == '1') {
                $user_cond .= ' AND e.is_current=1 ';
            }

            //CHECK DEALER
            $query_str = "SELECT * FROM " . DMS_DEALER . " WHERE  status=1 AND is_deleted='0' AND dealer_code='" . $dealer_code . "'";
            $dealer_data = $conn->getQueryValue($query_str);
            if (!count($dealer_data) > 0) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error9'));
            }

            $user_cond .= " AND e.dealer_code='" . $dealer_code . "'";
            $query_str = "SELECT e.fname,e.lname,e.smpin,e.dealer_code,COALESCE(p.points,0) as points FROM " . DMS_EMPLOYEE . " e  LEFT JOIN " . TABLE_USER_EPOINTS . " p ON e.smpin=p.smpin WHERE e.status=1 " . $user_cond . " order by p.points DESC,e.id DESC";
            $data_arr = $conn->getQueryValue($query_str);
            $returnarr = (count($data_arr) > 0) ? $data_arr : array();

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

//rewardpoint_data
function rewardpoint_data()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $smpin = escape_str($_REQUEST["smpin"]);

            //CHECK DEALER
            $query_str = "SELECT * FROM " . DMS_EMPLOYEE . " WHERE  status=1  AND smpin='" . $smpin . "' LIMIT 1";
            $smpin_data = $conn->getQueryValue($query_str);
            if (!count($smpin_data) > 0) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error8'));
            }

            $query_str = "SELECT *, CASE WHEN transaction_type='C' THEN 'Credit' ELSE 'Debit' END AS transaction_type_str FROM " . TABLE_USER_EPOINTS_HISTORY . "  WHERE smpin='" . $smpin . "' ORDER BY  hid DESC ";
            $data_arr = $conn->getQueryValue($query_str);
            $returnarr = (count($data_arr) > 0) ? $data_arr : array();

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $returnarr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// useraccess_data
function useraccess_data()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);

        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $from_date = date('Y-m-d', strtotime(CUR_DATE));
            //$to_date=date('Y-m-t',strtotime(CUR_DATE));
            $to_date = date('Y-m-d', strtotime(CUR_DATE));

            $user_cond = '';
            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
                //AM
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $returnarr = array();

            $tablejoin = '';
            $user_reg_cond = '';

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";

            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $tablejoin = " JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin";
                $user_reg_cond = " AND e.is_current=1 ";

            }
            $returnarr = array();

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            // Search
            if (!empty($key)) {
                $ucond .= " and (smpin like '%" . $key . "%' OR fname like '%" . $key . "%' OR lname like '%" . $key . "%'   ) ";
            }
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $limit_cond .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }

            $dsequery_str = "SELECT SQL_CALC_FOUND_ROWS u.*,l.created_on,d.dealer_name,d.zone,d.zm_name,d.rm_name,d.am_name FROM (SELECT userid,MAX(created_on)as created_on FROM " . TABLE_USER_LOG . " WHERE created_on>='" . $from_date . " 00:00:00' AND created_on<='" . $to_date . " 23:59:59' GROUP BY userid)l JOIN  (SELECT userid,smpin,fname,lname,dealer_code FROM  " . TABLE_USER . " WHERE  is_deleted=0 AND role>7 AND role<14 AND status=1 " . $ucond . " )u ON l.userid=u.userid " . $tablejoin . "   JOIN (SELECT  dealer_code,dealer_name,zone,zm_name,rm_name,am_name FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 AND dealer_code!='' " . $user_cond . "   )d ON u.dealer_code=d.dealer_code GROUP BY u.userid ORDER BY l.created_on,u.userid " . $limit_cond;
            $dse_data = $conn->getQueryValue($dsequery_str);

            // Total records
            $rows_query = $conn->query("SELECT FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;

            $arr['total_records'] = $total_records;

            if (count($dse_data) > 0) {
                foreach ($dse_data as $res) {
//                    $res->dealer_name=$dealer_data[0]->dealer_name;
                    //                    $res->zm_name=$dealer_data[0]->zm_name;
                    //                    $res->rm_name=$dealer_data[0]->rm_name;
                    //                    $res->am_name=$dealer_data[0]->am_name;
                    $returnarr[] = $res;
                }
            }
            $arr['report'] = $returnarr;
            $arr['q'] = $dsequery_str;
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);

        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// bulletin_report
function bulletin_report()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        $arr = array('report' => array(), 'total' => array('total_view' => 0));
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND d.am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND d.rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND d.zm_email='" . $zm_email . "'" : '';
                //AM
                //$user_cond=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE is_deleted='0' ".$mcond.")";
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

//            $pn = escape_str($_REQUEST["pn"]);
            //            $nr = escape_str($_REQUEST["nr"]);
            //            $key = escape_str($_REQUEST["key"]);

            //GET ALL BULLETIN
            $query_str = "SELECT `bulletin_id`, `bulletin_title`, `bulletin_sdesc`, `bulletin_desc` FROM  " . TABLE_BULLETIN . " WHERE `status`='1' AND `is_deleted`=0 ORDER BY bulletin_id ASC ";
            $result_arr = $conn->getQueryValue($query_str);

            $total_view = 0;

            if (count($result_arr)) {
                foreach ($result_arr as $res_content) {
                    $count_str = "SELECT COUNT(DISTINCT c.userid) as total_view FROM (SELECT userid,bulletin_id FROM `" . TABLE_BULLETINSTATUS . "` WHERE bulletin_id='" . $res_content->bulletin_id . "'  ) c JOIN " . TABLE_USER . " u ON u.userid=c.userid  JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.role>7 AND u.role<14 AND u.is_deleted=0 AND e.is_current=1 " . $user_cond . " GROUP BY c.bulletin_id LIMIT 1";
                    $count_arr = $conn->getQueryValue($count_str);
                    $res_content->total_view = (count($count_arr) > 0) ? $count_arr[0]->total_view : 0;
                    $total_view += $res_content->total_view;

                    $data_arr[] = $res_content;
                }
                $arr['report'] = $data_arr;
                $arr['total'] = array('total_view' => $total_view);
            }

            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}

// bulletin_report_list
function bulletin_report_list()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"] = 1;
        if (isset($_SESSION["userid"])) {

            $bulletin_id = escape_str($_REQUEST["bulletin_id"]);

            $zm_email = escape_str($_REQUEST["zm_email"]);
            $rm_email = escape_str($_REQUEST["rm_email"]);
            $am_email = escape_str($_REQUEST["am_email"]);

            $user_cond = '';

            if (($am_email != 'all' && $am_email != '') || ($rm_email != 'all' && $rm_email != '') || ($zm_email != 'all' && $zm_email != '')) {
                $mcond = '';
                $mcond = ($am_email != 'all' && $am_email != '') ? " AND d.am_email='" . $am_email . "'" : '';
                $mcond .= ($rm_email != 'all' && $rm_email != '') ? " AND d.rm_email='" . $rm_email . "'" : '';
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND d.zm_email='" . $zm_email . "'" : '';
                //AM
                //$user_cond=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE is_deleted='0' ".$mcond.")";
                $user_cond = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            $key = escape_str($_REQUEST["key"]);

            //BULLETIN
            $query_str = "SELECT `bulletin_id`, `bulletin_title`, `bulletin_sdesc`, `bulletin_desc` FROM  " . TABLE_BULLETIN . " WHERE `status`='1' AND `is_deleted`=0  AND bulletin_id='" . $bulletin_id . "' LIMIT 1 ";
            $content_arr = $conn->getQueryValue($query_str);
            if (!count($content_arr) > 0) {
                $conn->message(false, $conn->getMessage($lang, 'report', 'error2'));
            }

            $arr['content'] = $content_arr;

            //GET ALL USER
            $content_user_str = "SELECT SQL_CALC_FOUND_ROWS u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.status,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,c.created_on FROM (SELECT id,userid,created_on FROM `" . TABLE_BULLETINSTATUS . "` WHERE bulletin_id='" . $bulletin_id . "') c JOIN " . TABLE_USER . " u ON u.userid=c.userid  JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE e.is_current=1 " . $user_cond . " ORDER BY c.id DESC ";
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $content_user_str .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }
            $query = $conn->query($content_user_str);

            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}
// dse_remark
function dse_remark()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"] = 1;
        if (isset($_SESSION["userid"])) {

            $userid = escape_str($_REQUEST["userid"]);

            $pn = escape_str($_REQUEST["pn"]);
            $nr = escape_str($_REQUEST["nr"]);
            //$key = escape_str($_REQUEST["key"]);

            //GET ALL USER
            $content_user_str = "SELECT SQL_CALC_FOUND_ROWS h.userid,h.action_user,h.status,h.remarks,h.created_on,CONCAT(u.fname,u.lname) action_username FROM " . TABLE_USER_STATUS_HISTORY . " h LEFT JOIN " . TABLE_USER . " u ON u.userid=h.action_user WHERE h.userid='" . $userid . "' ORDER BY h.id DESC ";
            // Pagination
            if (!empty($pn) and !empty($nr)) {
                // Navigation
                $content_user_str .= " LIMIT " . (($pn - 1) * $nr) . ", " . $nr;
            }
            $query = $conn->query($content_user_str);

            $data_arr = array();
            if ($query->rowCount() > 0) {
                $data_arr = $query->fetchAll(PDO::FETCH_OBJ);
            }
            $arr['report'] = $data_arr;

            // Total records
            $rows_query = $conn->query("select FOUND_ROWS() as total_records");
            $rows_result = $rows_query->fetchAll(PDO::FETCH_OBJ);
            $total_records = $rows_result[0]->total_records;
            $arr['total_records'] = $total_records;
            $conn->message(true, $conn->getMessage($lang, 'report', 'success1'), $arr);
        } else {
            $conn->message(false, $conn->getMessage($lang, 'report', 'error1'));
        }
    } catch (PDOException $e) {
        $conn->message(false, $conn->getMessage($lang, 'report', 'error4'));
    }
}
