<?php

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

include "Connection.php";
$conn = new Connection();
$_SESSION["userid"] = 1;

if (isset($_REQUEST["funName"])) {
    $db = $conn->getConnection();
    $fun = $_REQUEST["funName"];
    switch ($fun) {
        case "user_registration":
        case "inactive_user_registration":
        case "page_access":
        case "content_report":
        case "accumulation_report":
        case "redemption_report":
        case "redemptionapproved_report":
        case "assessment_report":
        case "sales_report":
        case "certification_report":
        case "setsalestarget_report":
        case "attrition_report":
        case "activeinactive_report":
        case "customerexperiencelogin":
        case "enquiry":
        case "user_registration_mail":
        case "emp_activeinactive_mail":
        case "customerexperiencefeedback":

        case "user_registration_percent":
        case "inactive_user_table":
        case "page_access_v2":

        case "points_report_v2":
        case "points_report_list_v2":
        case 'sales_report_v2':
        case 'sales_report_list_v2':
        case 'certification_report_v2':
        case 'certification_report_list_v2':
        case 'enquiry_report_v2':
        case 'customerexperience_report_v2':
        case 'assessment_report_v2':
        case 'assessment_report_list_v2':
        case "setsalestarget_report_v2":
        case "setsalestarget_report_list_v2":
        case 'enquiry_report_v3':
        case "setsalestarget_report_v3":
        case "useraccess_data":

        case "bulletin_report":
        case "bulletin_report_list":
        case "rewardpoint_data":

            $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_download.php";
            $fun();
            break;

        // case 'enquirystatusexport':
        // case 'overallratioexport':
        // case 'convertedsalesexport':
        // case 'achievedvstargetexport':
        // case 'productivityexport':

        case 'enquirystatusdseexport':
        case 'enquirytestridedseexport':
        case 'enquiryconversionratiodseexport':
        case 'productivitydseexport':
        case 'enquiry_dealer_report_terms_condition':   
        case 'temp_suspension':       
            // case 'overallratiodseexport':
            // case 'convertedsalesdseexport':
            // case 'achievedvstargetdseexport':
            // case 'productivitydseexport':
            // case 'enquiry_report_terms_condition':
            // case 'enquiry_dealer_report_terms_condition':
            require_once "report_enquiry_download.php";
            $fun();
            break;
        default:
            $conn->message(false, "PAGE NOT FOUND");exit;
            break;
    }
}
//setCellBorder
function setCellBorder($columns, $objPHPExcel)
{

    for ($i = 0; $i < count($columns); $i++) {
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getBorders()->getLeft()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getBorders()->getRight()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("0A64B5");
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getFont()->setSize(12)->getColor()->setRGB("FFFFFF");
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle($columns[$i])->getAlignment()->setWrapText(true);
    }
}
//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;
}
//isValidDateTimeString
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;
}

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)) . "-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)) - 1) . "-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)) - 1) . "-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)) - 1) . "-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();
            $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 != '') || ($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 e.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE is_deleted='0' AND status=1  " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER REGISTRATION");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":Q" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);
            if ($datetype != 4) {
                $row++;

                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
                $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
                $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));
            }

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row, 'N' . $row, 'O' . $row, 'P' . $row, 'Q' . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Register Date & Time', 'Status', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM', 'DOJ', '(DOJ - Star Reg Date)in days', '(DOJ - Current Date)in days', '(HYPER-M App Date - DOJ)in days', 'Segment');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            if ($datetype != 4) {
                $user_cond .= " AND u.created_on>='" . $from_date . " 00:00:00' AND u.created_on<='" . $to_date . " 23:59:59'";
            }
            if ($regtype == '1') {
                //$query_str="SELECT modified_on FROM ".DMS_EMPLOYEE."  ORDER BY modified_on DESC LIMIT 1  ";
                //$data_arr=$conn->getQueryValue($query_str);

                $user_cond .= " AND e.is_current=1";

                //$query_str="SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.doj,u.created_on,u.status,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on,e.segment FROM ".TABLE_USER." u JOIN ".DMS_EMPLOYEE." e ON u.smpin=e.smpin 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   ".$user_cond."  ";
                //$query_str="SELECT e.smpin,e.fname,e.lname,e.mobile as mobileno,e.dealer_code,e.date_of_join as doj,e.created_on as emp_created_on,e.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,u.created_on,u.status,u.userid FROM ".DMS_EMPLOYEE." e LEFT JOIN ".TABLE_USER." u ON u.smpin=e.smpin AND u.is_deleted=0 LEFT JOIN ".DMS_DEALER." d ON u.dealer_code=d.dealer_code WHERE ".$user_cond."  ";

            } else {
                //$query_str="SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.doj,u.created_on,u.status,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on,e.segment FROM ".TABLE_USER." u JOIN ".DMS_EMPLOYEE." e ON u.smpin=e.smpin 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   ".$user_cond."  ";

            }

            $query_str = "SELECT e.smpin,e.fname,e.lname,e.mobile as mobileno,e.dealer_code,e.date_of_join as doj,e.created_on as emp_created_on,e.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,u.created_on,u.status,u.userid FROM " . DMS_EMPLOYEE . " e LEFT JOIN " . TABLE_USER . " u ON u.smpin=e.smpin AND u.is_deleted=0 LEFT JOIN " . DMS_DEALER . " d ON e.dealer_code=d.dealer_code WHERE e.status=1 " . $user_cond . " ORDER BY e.id ASC";

            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                    $reg_date = '';
                    if ($res_user->userid > 0) {
                        $reg_date = date('d-m-Y h:i A', strtotime($res_user->created_on));
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $reg_date);

                    $status = ($res_user->status == '1') ? "Active" : "Inactive";

                    if ($status == 'Active') {
                        $color = '008000';
                    } else if ($status == 'Inactive') {
                        $color = 'FF0000';
                        //$objPHPExcel->getActiveSheet()->getStyle('F'.$row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                    }
                    if (!$res_user->userid > 0) {
                        $color = 'FF0000';
                        $status = "Not Registered";
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status);
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB($color);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->doj);

                    $diff = 0;
                    if ($res_user->userid > 0) {
                        $doj_date = $res_user->doj;
                        $reg_date = $res_user->created_on;
                        $emp_created_date = $res_user->emp_created_on;

                        $doj_date_str = new DateTime($doj_date);
                        $reg_date_str = new DateTime($reg_date);
                        $diff = $reg_date_str->diff($doj_date_str)->format("%a");
                    }

                    $column_row = getColumnFromNumber($i++) . $row;
                    $objPHPExcel->getActiveSheet()->SetCellValue($column_row, $diff);
                    if ($diff > 7) {
                        $objPHPExcel->getActiveSheet()->getStyle($column_row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                        //$objPHPExcel->getActiveSheet()->getStyle($column_row)->getFont()->setSize(12)->getColor()->setRGB("FFFFFF");
                    }

                    $cur_date_str = new DateTime(CUR_DATE);
                    $diff2 = $cur_date_str->diff($doj_date_str)->format("%a");

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $diff2);

                    $doj_date_str = new DateTime($doj_date);
                    $emp_created_date_str = new DateTime($emp_created_date);
                    $diff3 = $emp_created_date_str->diff($doj_date_str)->format("%a");

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $diff3);
                    $segment_str = '';
                    switch ($res_user->segment) {

                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('REGISTRATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_registration_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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   status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "INACTIVE USER REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Register Date & Time', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }
            $tablejoin = '';
            $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 ";
            }

            //$query_str="SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,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 u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.created_on,u.segment,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 . "  ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('INACTIVE USER REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "inactive_user_registration_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
function page_access()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $page = escape_str($_REQUEST["page"]);

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER ACCESS REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "PAGE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $page);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Last access', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT  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,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 LEFT 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 ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Last access','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->last_access)));
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ACCESS REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_access_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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'));
            }

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CONTENT ACCESS REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":Z" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $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'));
            }

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CATEGORY :");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":B" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, $cat_arr[0]->catename);

            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 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);

            $row += 2;
            $i = 0;
            if (count($contentresult_arr)) {
                //SETTING HEADER
                $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row);
                $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Dealer Code', 'Dealer Name', 'Zone');

                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');

                $BStyle = array('borders' => array('outline' => array('style' => PHPExcel_Style_Border::BORDER_THIN)));

                // print_r($contentresult_arr);
                foreach ($contentresult_arr as $res_content) {
                    $column = $i++;
                    $bordercells_arr[] = getColumnFromNumber($column) . $row;
                    $header_arr[] = $res_content->title;
                    $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($column))->setWidth('25');
                    $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($column) . $row)->getAlignment()->setWrapText(true);
                    $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($column) . $row)->applyFromArray($BStyle);
                }

                $objPHPExcel->getActiveSheet()->getStyle("I" . ($row - 1) . ":" . getColumnFromNumber($column) . ($row - 1))->applyFromArray($BStyle);

                $objPHPExcel->getActiveSheet()->SetCellValue('I' . ($row - 1), "CONTENT ACCESS COUNT");
                $objPHPExcel->getActiveSheet()->mergeCells("I" . ($row - 1) . ":" . getColumnFromNumber($column) . ($row - 1));
                $objPHPExcel->getActiveSheet()->getStyle("I" . ($row - 1))->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->getStyle("I" . ($row - 1))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);

                $column = $i++;
                $bordercells_arr[] = getColumnFromNumber($column) . $row; //ZM
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($column))->setWidth('25');
                //$objPHPExcel->getActiveSheet()->mergeCells(getColumnFromNumber($column).($row-1).":".getColumnFromNumber($column).$row);

                $column = $i++;
                $bordercells_arr[] = getColumnFromNumber($column) . $row; //RM
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($column))->setWidth('25');
                //$objPHPExcel->getActiveSheet()->mergeCells(getColumnFromNumber($column).($row-1).":".getColumnFromNumber($column).$row);

                $column = $i;
                $bordercells_arr[] = getColumnFromNumber($column) . $row; //AM
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($column))->setWidth('25');
                //$objPHPExcel->getActiveSheet()->mergeCells(getColumnFromNumber($column).($row-1).":".getColumnFromNumber($column).$row);

                $header_arr[] = "ZM";
                $header_arr[] = "RM";
                $header_arr[] = "AM";

                setCellBorder(array("I" . ($row - 1)), $objPHPExcel);
                //$objPHPExcel->getStyle("H".($row-1).":".getColumnFromNumber($column).($row-1))->applyFromArray(array('borders' => array('allborders' => array('style' => PHPExcel_Style_Border::BORDER_THIN,'color' => array('rgb' => 'DDDDDD')))));

                setCellBorder($bordercells_arr, $objPHPExcel);
                $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

                $i = 0;
                foreach ($header_arr as $val) {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
                }

                $query_str = "SELECT  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 LEFT 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 ";
                $user_arr = $conn->getQueryValue($query_str);

                if (count($user_arr) > 0) {
                    $sno = 1;
                    foreach ($user_arr as $mkey => $res_user) {
                        //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Dealer Code','Dealer Name','Zone');
                        $i = 0;
                        $row++;
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                        $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                        $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                        //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';
                            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $count);
                        }

                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                    }
                }
            } else {

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "NO RECORD FOUND ");

            }

            $objPHPExcel->getActiveSheet()->setTitle('USER CONTENT REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_content_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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'));
            }

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ACCUMULATION REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Points', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

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

            $limit_cond = '';
            if ($top_50 == '1') {
                $limit_cond = " LIMIT 0,50";
            }

            $query_str = "SELECT  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 LEFT 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;
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Points','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $sum_points = ($res_user->sum_points) ? $res_user->sum_points : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sum_points);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ACCUMULATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_accumulation_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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'));
            }

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "REDEMPTION REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Points', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $limit_cond = '';
            if ($top_50 == '1') {
                $limit_cond = " LIMIT 0,50";
            }

            $query_str = "SELECT  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 LEFT 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='D' 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;
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Points','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $sum_points = ($res_user->sum_points) ? $res_user->sum_points : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sum_points);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('REDEMPTION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_redemption_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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'));
            }

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "APPROVAL REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Points', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT  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 LEFT 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.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 ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Last access','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $sum_points = ($res_user->sum_points) ? $res_user->sum_points : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sum_points);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('APPROVAL REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_approval_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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,pass_score  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"]);

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

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":P" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT :" . $assessment_arr[0]->assessmentname);
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, '');

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, 'O' . $row, "P" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result Category', 'Result', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }
            $limit_cond = '';
            if ($top_50 == '1') {
                $limit_cond = " LIMIT 0,50";
            }

            $query_str = "SELECT  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`,ar.time_taken,ar.modified_on as completed_time FROM " . TABLE_ASSESSMENT_USER . " as au JOIN " . TABLE_ASSESSMENTRESULT . " as ar ON au.highscorearid=ar.arid JOIN " . TABLE_USER . " as u ON au.userid=u.userid LEFT 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,ar.time_taken ASC,au.modified_on ASC " . $limit_cond;
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Score','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $score = ($res_user->score) ? $res_user->score : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $score);

                    $timetaken = (isset($res_user->time_taken)) ? round($res_user->time_taken / 60) : "0";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $timetaken);

                    $completed_time = (isset($res_user->completed_time)) ? date('d-m-Y h:i A', strtotime($res_user->completed_time)) : "-";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $completed_time);

                    switch (true) {
                        case ($score < 50): $cat_str = 'Below 50%';
                            break;
                        case ($score >= 50 && $score < 80): $cat_str = '50% to 79%';
                            break;
                        case ($score >= 80): $cat_str = '80% to 100%';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $cat_str);

                    $result_str = ($score >= $assessment_arr[0]->pass_score) ? "Pass" : "Fail";
                    $column_str = getColumnFromNumber($i++) . $row;
                    if ($result_str == 'Pass') {
                        $objPHPExcel->getActiveSheet()->getStyle($column_str)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("008000");
                    } else {
                        $objPHPExcel->getActiveSheet()->getStyle($column_str)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue($column_str, $result_str);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ASSESSMENT REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "assessment_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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'));
            }

            $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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Total Sales', 'Total Target', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

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

            $limit_cond = '';
            if ($top_50 == '1') {
                $limit_cond = " LIMIT 0,50";
            }

            //$query_str="SELECT 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 LEFT 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_str = "SELECT 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 . " as u   LEFT 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  u.userid ASC  " . $limit_cond;

            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Total Sales','Total Target','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $getsales_str = "SELECT COALESCE(SUM(sales_count),0) as tot_sales FROM  " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "' AND smpin='" . $res_user->smpin . "' GROUP BY smpin ";
                    $getsales_arr = $conn->getQueryValue($getsales_str);

                    $tot_sales = (count($getsales_arr) > 0 && $getsales_arr[0]->tot_sales > 0) ? $getsales_arr[0]->tot_sales : '0';
                    //$tot_sales=($res_user->tot_sales)?$res_user->tot_sales:'0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_sales);

                    $gettarget_str = "SELECT COALESCE(SUM(target),0) as tot_target FROM  " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' AND smpin='" . $res_user->smpin . "' GROUP BY smpin ";
                    $gettarget_arr = $conn->getQueryValue($gettarget_str);

                    $tot_target = (count($gettarget_arr) > 0 && $gettarget_arr[0]->tot_target > 0) ? $gettarget_arr[0]->tot_target : '0';

                    //$tot_target=($res_user->tot_target)?$res_user->tot_target:'0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_target);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('SALES REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "sales_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
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 status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CERTIFICATION REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Base Status', 'Bronze Status', 'Silver Status', 'Gold Status', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $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 u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,CASE WHEN a.id iS NULL THEN 'Not completed' ELSE  'Completed' END as base_status,CASE WHEN b.id iS NULL THEN 'Not completed' ELSE 'Completed' END as bronze_status,CASE WHEN c.id iS NULL THEN 'Not completed' ELSE 'Completed' END as silver_status,CASE WHEN e.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 " . $tablejoin . " LEFT JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code  LEFT JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1 AND status=3) as a ON a.smpin=u.smpin LEFT JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2 AND status=3) as b ON b.smpin=u.smpin LEFT JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4 AND status=3) as c ON c.smpin=u.smpin LEFT JOIN (SELECT id,smpin FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6 AND status=3) as e ON e.smpin=u.smpin  WHERE u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0 AND u.status=1 " . $user_cond . " GROUP BY u.smpin ORDER BY gold_status ASC,silver_status ASC,bronze_status ASC,base_status ASC,userid DESC ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Total Sales','Total Target','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->base_status);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->bronze_status);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->silver_status);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->gold_status);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('CERTIFICATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "certification_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
function setsalestarget_report()
{
    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  status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SET SALES REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('M-Y', strtotime($from_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Email', 'Total Target', 'Target Status', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT 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 LEFT 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  ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Total Sales','Total Target','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $tot_target = ($res_user->tot_target) ? $res_user->tot_target : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_target);

                    $statusquery_str = "SELECT * FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE  smpin='" . $res_user->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':

                                $sale_status_str = 'Waiting';
                                break;
                            case '2':
                                $sale_status_str = 'Approval';
                                break;
                            case '3':
                                $sale_status_str = 'Modification';
                                break;
                        }

                    } else {
                        $data_arr[$ukey]->sale_status = '0';
                        $data_arr[$ukey]->sale_status_str = 'No Action';
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sale_status_str);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('SALES REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "setsales_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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()
{
    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 != '') && ($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 . "'" : '';
                //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 = '';
            }

            $regtype = (escape_str($_REQUEST["regtype"]) == '1') ? "1" : "0";
            $user_reg_cond = '';
            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 ";

            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER ACTIVE/INACTIVE");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);
            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Register Date & Time', 'Status', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //$query_str="SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.created_on,u.status,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_str = "SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.created_on,u.status,u.segment,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 " . $tablejoin . " WHERE  d.status=1 AND d.is_deleted='0' AND u.role>7 and u.role!=14 and u.role!=16 AND u.is_deleted=0   " . $user_cond . $user_reg_cond . "  ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                    $status_str = ($res_user->status == 1) ? "Active" : "Inactive";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status_str);
                    if ($status_str == 'Active') {
                        $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("008000");
                    } else if ($status_str == 'Inactive') {
                        $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                    }

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('USER ACTIVE & INACTIVE REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_status_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

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;
}
// attrition_report
function attrition_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 = 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  status=1 AND is_deleted='0' " . $mcond . ")";
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER ACTIVE/INACTIVE");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);
//
            //            $row++;
            //
            //            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"FROM DATE :");
            //            $objPHPExcel->getActiveSheet()->getStyle("A".$row)->getFont()->setBold(true);
            //            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,date('d-M-Y',strtotime($from_date)));
            //
            //
            //            $objPHPExcel->getActiveSheet()->SetCellValue('C'.$row,"TO DATE :");
            //            $objPHPExcel->getActiveSheet()->getStyle("C".$row)->getFont()->setBold(true);
            //            $objPHPExcel->getActiveSheet()->SetCellValue('D'.$row,date('d-M-Y',strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Month & year', 'Active user', 'Inactive user', 'Attrition');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $label_arr = array();
            $valarr = array();
            $sno = 1;
            //INCATIVE COND
            $inactivecond = " AND (module='AUTOMATIC INACTIVE' OR  module='MANUAL INACTIVE')";
            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;

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $la_str);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $activecount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $inactivecount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $rate);

            }

            $objPHPExcel->getActiveSheet()->setTitle('ATTRITION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "attrition_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);
            $returnarr = array("report_url" => $url);

            $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"])) {
            $page = escape_str($_REQUEST["page"]);

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CUSTOMER EXPERIENCE REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":N" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Name', 'Email', 'Mobile', 'Age', 'SMPIN', 'DSE Name', 'Created on', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM'); //12

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10'); //smpin
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //dse
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT u.`userid`,c.`salutation`,c. `firstname`, c.`lastname`, c.`email`,c. `mobile`,c. `age`,c.smpin,c.dsename,c.created_on,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  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 ASC ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {

                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salutation . " " . $res_user->firstname . " " . $res_user->lastname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->email);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                    $objPHPExcel->getActiveSheet()->getStyle('D' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->age);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dsename);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('CUSTOMER EXPERIENCE REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "customer_experience_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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
function enquiry()
{
    global $conn;
    try {
        $lang = "en_us";
        $pageData = $conn->loadJSON($lang);
        //$_SESSION["userid"]=1;
        if (isset($_SESSION["userid"])) {
            $page = escape_str($_REQUEST["page"]);

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CUSTOMER EXPERIENCE REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":L" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Name', 'Email', 'Mobile', 'Model', "Variant", 'Created on', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM'); //12

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT  c.`name`, c.`mobile`, c.`email`,c.`model_id`, c.`variant_id`, d.`dealer_code`, d.`dealer_name`,d. `city`, d.`state`, 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 ASC ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {

                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->model_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->variant_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ENQUIRY REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "enquiry_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_mail
function user_registration_mail()
{

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

        $user_cond = '';

        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER REGISTRATION");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":Q" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

        $row++;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row, 'N' . $row, 'O' . $row, 'P' . $row, 'Q' . $row);
        setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Register Date & Time', 'Status', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM', 'DOJ', '(DOJ - HYPER-M Reg Date)in days', '(DOJ - Current Date)in days', '(HYPER-M App Date - DOJ)in days', 'Segment');

        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

        $i = 0;
        foreach ($header_arr as $val) {
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
        }

        $user_cond .= " AND e.is_current=1";

        $query_str = "SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.doj,u.status,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on,e.segment FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin 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 " . $user_cond . "  ";

        $user_arr = $conn->getQueryValue($query_str);

        if (count($user_arr) > 0) {
            $sno = 1;
            foreach ($user_arr as $mkey => $res_user) {
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));
                $status = ($res_user->status == '1') ? "Active" : "Inactive";
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status);
                if ($status == 'Active') {
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("008000");
                } else if ($status == 'Inactive') {
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                }

                $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->doj);

                $doj_date = $res_user->doj;
                $reg_date = $res_user->created_on;
                $emp_created_date = $res_user->emp_created_on;

                $doj_date_str = new DateTime($doj_date);
                $reg_date_str = new DateTime($reg_date);
                $diff = $reg_date_str->diff($doj_date_str)->format("%a");

                $column_row = getColumnFromNumber($i++) . $row;
                $objPHPExcel->getActiveSheet()->SetCellValue($column_row, $diff);
                if ($diff > 7) {
                    $objPHPExcel->getActiveSheet()->getStyle($column_row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                    //$objPHPExcel->getActiveSheet()->getStyle($column_row)->getFont()->setSize(12)->getColor()->setRGB("FFFFFF");
                }

                $cur_date_str = new DateTime(CUR_DATE);
                $diff2 = $cur_date_str->diff($doj_date_str)->format("%a");

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $diff2);

                $doj_date_str = new DateTime($doj_date);
                $emp_created_date_str = new DateTime($emp_created_date);
                $diff3 = $emp_created_date_str->diff($doj_date_str)->format("%a");

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $diff3);
                $segment_str = '';
                switch ($res_user->segment) {

                    case '1':$segment_str = 'Parents/Guardian';
                        break;
                    case '2':$segment_str = 'Instructor';
                        break;
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);

            }
        }

        $objPHPExcel->getActiveSheet()->setTitle('REGISTRATION REPORT');
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $fpath = "../" . S3_REPORT_URL;
        $filename = "user_registration_report_" . uniqid() . ".xlsx";
        $objWriter->save($fpath . $filename);

        //S3 UPLOAD
        //S3 LIB
        require dirname(__FILE__) . '/awsfileserver.php';

        //S3 OBJ
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

        //DELETE AFTER MOVE TO S3
        @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

        $obj = new stdClass();
        $obj->key_name = S3_REPORT_URL . $filename;
        $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

        $returnarr = array("report_url" => $url);

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

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

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

        $user_cond = '';

        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
        require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "EMPLOYEE STATUS REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":N" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

        $row++;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row, 'N' . $row, 'O' . $row);
        setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Created on', 'Register Date & Time', 'Status', 'Inactivated On', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

        $i = 0;
        foreach ($header_arr as $val) {
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
        }

        $query_str = "SELECT e.smpin,e.fname,e.lname,e.mobile,e.dealer_code,e.segment,u.userid,u.status,u.is_deleted,u.inactivated_on,u.created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on FROM " . DMS_EMPLOYEE . " e LEFT JOIN " . TABLE_USER . " u ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON e.dealer_code=d.dealer_code WHERE e.is_current=1 GROUP BY e.id ";

        $user_arr = $conn->getQueryValue($query_str);

        if (count($user_arr) > 0) {
            $sno = 1;
            foreach ($user_arr as $mkey => $res_user) {
                // $header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Created on','Register Date & Time','Status','Inactivated On','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);

                $segment_str = '-';
                switch ($res_user->segment) {
                    case '1':$segment_str = 'Parents/Guardian';
                        break;
                    case '2':$segment_str = 'Instructor';
                        break;
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);

                $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->emp_created_on)));

                $reg_created_on = ($res_user->userid) ? date('d-m-Y h:i A', strtotime($res_user->created_on)) : '';
                $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $reg_created_on);

                $inactive_on = '';
                if ($res_user->userid) {
                    $status = ($res_user->status == '1') ? "Active" : "Inactive";
                    if ($status == 'Active' && $res_user->is_deleted == '1') {
                        $status = "Deleted";
                    }

                    $inactive_on = ($status == 'Inactive') ? date('d-m-Y h:i A', strtotime($res_user->inactivated_on)) : "";

                } else {
                    $status = "Not registered";
                }

                if ($status == 'Active') {
                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("008000");
                } else if ($status == 'Inactive') {
                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                } else if ($status == 'Deleted') {
                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                } else {
                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("7676FF");
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $inactive_on);
                $objPHPExcel->getActiveSheet()->getStyle('I' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);

            }
        }

        $objPHPExcel->getActiveSheet()->setTitle('EMPLOYEE STATUS REPORT');
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $fpath = "../" . S3_REPORT_URL;
        $filename = "employee_status_report_" . uniqid() . ".xlsx";
        $objWriter->save($fpath . $filename);

        //S3 UPLOAD
        //S3 LIB
        require dirname(__FILE__) . '/awsfileserver.php';

        //S3 OBJ
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

        //DELETE AFTER MOVE TO S3
        @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

        $obj = new stdClass();
        $obj->key_name = S3_REPORT_URL . $filename;
        $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

        $returnarr = array("report_url" => $url);

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

    } 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"])) {
            $user_cond = '';

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

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

            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'));
            }

            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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CUSTOMER EXPERIENCE FEEDBACK");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":O" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row, 'N' . $row);

            $query_str = "SELECT q.id,r.survey_rating,q.questiontext,q.questiontypeid FROM " . TABLE__FEEDBACKRESULT . " r JOIN " . TABLE__EXPQUESTION . " as q ON r.qid=q.id GROUP BY r.qid";
            $ques_arr = $conn->getQueryValue($query_str);
            if (count($ques_arr) > 0) {
                $i = 14;
                foreach ($ques_arr as $mkey => $res_user) {
                    $bordercells_arr[] = getColumnFromNumber($i++) . $row;
                }
            }

            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Name', 'Email', 'Mobile', 'Age', 'SMPIN', 'DSE Name', 'Created on', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM'); //12
            if (count($ques_arr) > 0) {
                $i = 13;
                foreach ($ques_arr as $mkey => $res_ques) {
                    $header_arr[] = $res_ques->questiontext;
                }
            }

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            if (count($ques_arr) > 0) {
                foreach ($ques_arr as $mkey => $res_user) {
                    $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('40');
                }
            }

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $query_str = "SELECT 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,f.created_on  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";

            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {

                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salutation . " " . $res_user->firstname . " " . $res_user->lastname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->email);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                    $objPHPExcel->getActiveSheet()->getStyle('D' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->age);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dsename);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->getStyle('I' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);

                    if (count($ques_arr) > 0) {
                        foreach ($ques_arr as $res_ques) {
                            $query_str = "SELECT r.survey_rating FROM " . TABLE__FEEDBACKRESULT . " r WHERE r.arid='" . $res_user->id . "' AND r.qid='" . $res_ques->id . "' LIMIT 1";
                            $feedresult_arr = $conn->getQueryValue($query_str);
                            $rating = (count($feedresult_arr) > 0) ? $feedresult_arr[0]->survey_rating : '';
                            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $rating);
                        }
                    }

                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('FEEDBACK REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "customer_experience_feedback_report_" . uniqid() . ".xlsx";

            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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();
            $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 = '';
            $returnarr = array();
            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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER REGISTRATION PERCENTAGE");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);
            if ($datetype != 4) //FOR ALL LIST
            {
                $row++;

                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
                $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
                $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));
            }

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Employee Count', 'Registered Count', 'Percentage %');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //$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) //FOR ALL LIST
            {
                $user_reg_cond .= " AND u.created_on>='" . $from_date . " 00:00:00' AND  u.created_on<='" . $to_date . " 23:59:59' ";
            }
            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 ($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 . ") " . $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' " . $mcond . " 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 . ") " . $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 . "  ) 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);

            //print_r($returnarr);

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    // $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$sno++);
                    //$objPHPExcel->getActiveSheet()->getStyle('B'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['emp_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['reg_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['percent']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['emp_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['reg_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['percent']);
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('REGISTRATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_registration_percentange_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

// inactive_user_table
function inactive_user_table()
{
    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 = '';
            $returnarr = array();
            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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER ACTIVE INCATIVE REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Active Count', 'Inactive Count');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //$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 ";
            }

            //$user_reg_cond.=" AND u.created_on>='".$from_date." 00:00:00' AND  u.created_on<='".$to_date." 23:59:59' ";
            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 ($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  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->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'active_count' => $active_count, 'inactive_count' => $inactive_count);
                    }
                }
            }

            $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   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(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);

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    // $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$sno++);
                    //$objPHPExcel->getActiveSheet()->getStyle('B'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['active_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['inactive_count']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['active_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['inactive_count']);
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('USER ACTIVE REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_active_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);
            $returnarr = array("report_url" => $url);

            $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"]);

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER ACCESS REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

//            $row++;
            //            $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"PAGE :");
            //            $objPHPExcel->getActiveSheet()->getStyle("A".$row)->getFont()->setBold(true);
            //            $objPHPExcel->getActiveSheet()->SetCellValue('B'.$row,$page);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row, "P" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            //Dashboard
            //Learning
            //Assessment
            //Feedback

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Email', 'Dashboard Last access', 'Learning Last access', 'Assessment Last access', 'Feedback Last access', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $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 ";
            }

            //$query_str="SELECT  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,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 LEFT 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 ";
            $query_str = "SELECT  u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,u.segment,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,d.zone,d.`zm_name`,d.`rm_name`,d.`am_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 ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Last access','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $dashboard_last_access = ($res_user->dashboard_last_access != '') ? date('d-m-Y h:i A', strtotime($res_user->dashboard_last_access)) : '';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $dashboard_last_access);

                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $learning_last_access = ($res_user->learning_last_access != '') ? date('d-m-Y h:i A', strtotime($res_user->learning_last_access)) : '';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $learning_last_access);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $assessment_last_access = ($res_user->assessment_last_access != '') ? date('d-m-Y h:i A', strtotime($res_user->assessment_last_access)) : '';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $assessment_last_access);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $feedback_last_access = ($res_user->feedback_last_access != '') ? date('d-m-Y h:i A', strtotime($res_user->feedback_last_access)) : '';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $feedback_last_access);

                    $objPHPExcel->getActiveSheet()->getStyle('I' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ACCESS REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_access_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

// 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 = '';
            $returnarr = array();
            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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER REWARD POINTS");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Accumulation Points', 'Redemption Points', 'Approved Points', 'Count of DLR/BR', 'Total DSEs', 'HYPER-M App Registered DSE');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

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

            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";
            }
            $emp_cond = '';
            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 ";
                $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 . "' " . $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  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!='' AND status=1 AND is_deleted='0'  " . $mcond . "  )" : " 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);

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['accumulation_point']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['redemption_point']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['approved_point']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dealer_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dse_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['regdse_count']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['accumulation_point']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['redemption_point']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['approved_point']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dealer_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dse_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['regdse_count']);
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('REWARD POINTS REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_reward_points_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

// points_report_list_v2
function points_report_list_v2()
{
    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 != '') || ($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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER REWARD POINTS");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":M" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row, 'N' . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM', 'Accumulation Points', 'Redemption Point', 'Approved Point');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

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

                $user_cond .= " AND e.is_current=1";

                $query_str = "SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.doj,u.created_on,u.status,u.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin 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  " . $user_cond . "  ";

            } else {
                $query_str = "SELECT u.smpin,u.fname,u.lname,u.mobileno,u.dealer_code,u.doj,u.created_on,u.status,u.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,e.created_on as emp_created_on FROM " . TABLE_USER . " u JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin 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 " . $user_cond . "  ";
            }

            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);

                    //ACCUMULATION POINT
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM  " . TABLE_USER_EPOINTS_HISTORY . " h WHERE h.smpin='" . $res_user->smpin . "' AND h.transaction_type='C' LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);
                    $accumulation_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $accumulation_point);
                    //REDEMPTION POINT
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM  " . TABLE_USER_EPOINTS_HISTORY . " h WHERE h.smpin='" . $res_user->smpin . "' AND h.transaction_type='D' LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);
                    $redemption_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $redemption_point);
                    //APPROVED POINT
                    $query_str = "SELECT SUM(h.points) as sumpoints FROM  " . TABLE_USER_WALLET_APPROVE . " h  WHERE h.is_approved='1' AND h.smpin='" . $res_user->smpin . "'  LIMIT 1 ";
                    $result_arr = $conn->getQueryValue($query_str);
                    $approved_point = (count($result_arr) > 0 && $result_arr[0]->sumpoints) ? $result_arr[0]->sumpoints : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $approved_point);

                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('USER REWARD POINTS REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_reward_points_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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 = '';
            $returnarr = array();
            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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER SALES & TARGET REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH & YEAR :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('F Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":D" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Sales', 'Target');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $tablejoin = '';

            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";
            }

            $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, 'sales' => $total_sales, 'target' => $total_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) {
                        $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 . "' AND status=1 AND is_deleted=0 " . $mcond . " )";

                        //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, 'sales' => $total_sales, 'target' => $total_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) {
                        $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 . "' AND status=1 AND is_deleted=0 " . $mcond . "  )";

                        //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, 'sales' => $total_sales, 'target' => $total_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) {
                        $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, 'sales' => $total_sales, 'target' => $total_target);
                    }
                }
            }

            $manager_cond = ($mcond != '') ? " AND u.dealer_code IN (SELECT  dealer_code FROM " . DMS_DEALER . " WHERE dealer_code!='' AND status=1 AND is_deleted=0 " . $mcond . "  )" : " 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('sales' => $total_sales, 'target' => $total_target);
            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['sales']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['target']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['sales']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['target']);
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('SALES & TARGET REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_sales_target_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_v2
function sales_report_list_v2()
{
    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"]);

            $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 != '') || ($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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER SALE & TARGET REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":L" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH & YEAR :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('F Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":D" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, 'L' . $row, 'M' . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM', 'Sales', 'Target');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $regtype = (escape_str($_REQUEST["regtype"]) == '1' || !isset($_REQUEST["regtype"])) ? "1" : "0";
            if ($regtype == '1') {

                $user_cond .= " AND e.is_current=1";

                $query_str = "SELECT e.smpin,e.fname,e.lname,e.mobile as mobileno,e.dealer_code,e.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . DMS_EMPLOYEE . " e  LEFT JOIN " . DMS_DEALER . " d ON e.dealer_code=d.dealer_code WHERE e.status=1  " . $user_cond . "  ";

            } else {
                $query_str = "SELECT e.smpin,e.fname,e.lname,e.mobile as mobileno,e.dealer_code,e.segment,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . DMS_EMPLOYEE . " e  LEFT JOIN " . DMS_DEALER . " d ON e.dealer_code=d.dealer_code WHERE e.status=1  " . $user_cond . "  ";
            }

            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);

                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);

                    //TOTAL SALES
                    $query_str = "SELECT SUM(s.sales_count) as total_sales FROM " . DMS_EMPLOYEE_SALES . " s WHERE sale_date>='" . $from_date . "' AND sale_date<='" . $to_date . "' AND s.smpin='" . $res_user->smpin . "' 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';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_sales);

                    //TOTAL TARGET
                    $query_str = "SELECT SUM(s.target) as total_target FROM  " . TABLE_USER_SALETARGET . " s WHERE s.smpin='" . $res_user->smpin . "' AND sale_month>='" . $from_date . "' AND sale_month<='" . $to_date . "' 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';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_target);

                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('USER SALE & TARGET REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_sales_target_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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 = '';
            $returnarr = array();
            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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER CERTIFICATION REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Base Completed', 'Bronze Completed', 'Silver Completed', 'Gold Completed');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $tablejoin = '';
            $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 ";
            }

            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";
            }
            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 )";

                        $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=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=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=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';

                        $returnarr[] = array("name" => $val->zm_name, 'email' => $val->zm_email, 'base' => $base_count, 'bronze' => $bronze_count, 'silver' => $silver_count, 'gold' => $gold_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 . "' AND status=1 AND is_deleted=0 " . $mcond . " )";

                        $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=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=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=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';

                        $returnarr[] = array("name" => $val->rm_name, 'email' => $val->rm_email, 'base' => $base_count, 'bronze' => $bronze_count, 'silver' => $silver_count, 'gold' => $gold_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 . "' AND status=1 AND is_deleted=0 " . $mcond . "  )";

                        $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=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=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=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';

                        $returnarr[] = array("name" => $val->am_name, 'email' => $val->am_email, 'base' => $base_count, 'bronze' => $bronze_count, 'silver' => $silver_count, 'gold' => $gold_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=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=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=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=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';

                        $returnarr[] = array("name" => $val->dealer_name . " - " . $val->dealer_code, 'dealer_code' => $val->dealer_code, 'base' => $base_count, 'bronze' => $bronze_count, 'silver' => $silver_count, 'gold' => $gold_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=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=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=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=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';

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

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['base']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['bronze']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['silver']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['gold']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['base']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['bronze']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['silver']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['gold']);

            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('CERTIFICATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_certification_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_v2
function certification_report_list_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 != '') || ($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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CERTIFICATION REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row, "P" . $row, "Q" . $row, "R" . $row, "S" . $row, "T" . $row, "U" . $row, "V" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Email', 'Base', 'Bronze', 'Silver Level 1 Status', 'Silver Level 2 Status', 'Gold Level 2 Status', 'Gold Level 2 Status', 'Base Completed date', 'Bronze Completed date', 'Silver Completed date', 'Gold Completed date', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');

            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $tablejoin = '';
            $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 ";
            }

            $query_str = "SELECT u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.`dealer_code`,u.segment,a.status as base_status,b.status as bronze_status,c.status as silver_status,d.status as  gold_status,a.created_on as base_created_on,b.created_on as bronze_created_on,c.created_on as silver_created_on,d.created_on as gold_created_on,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name` FROM " . TABLE_USER . " as u " . $tablejoin . " LEFT JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code  LEFT JOIN (SELECT id,smpin,status,created_on FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=1) as a ON a.smpin=u.smpin LEFT JOIN (SELECT id,smpin,status,created_on FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=2) as b ON b.smpin=u.smpin LEFT JOIN (SELECT id,smpin,status,created_on FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=4) as c ON c.smpin=u.smpin LEFT JOIN (SELECT id,smpin,status,created_on FROM " . TABLE_USER_LEARNING . " WHERE lvl_id=6) as f ON f.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 gold_status ASC,silver_status ASC,bronze_status ASC,base_status ASC,u.userid DESC ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Total Sales','Total Target','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $base_date_str = '';
                    $bronze_date_str = '';
                    $silver_date_str = '';
                    $gold_date_str = '';
                    //,a.created_on as base_created_on,b.created_on as bronze_created_on,c.created_on as silver_created_on,d.created_on as gold_created_on
                    switch ($res_user->base_status) {
                        case "0":$base_lvl1_str = 'Not Started';
                            $base_lvl2_str = 'Not Started';
                            break;
                        case "6":$base_lvl1_str = 'Failed';
                            $base_lvl2_str = 'Not Started';
                            $base_date_str = date('d-m-Y', strtotime($res_user->base_created_on));
                            break;
                        case "7":$base_lvl1_str = 'Completed';
                            $base_lvl2_str = 'Not Started';
                            break; // same
                        case "4":$base_lvl1_str = 'Completed';
                            $base_lvl2_str = 'Not Started';
                            break; // same
                        case "2":$base_lvl1_str = 'Completed';
                            $base_lvl2_str = 'Failed';
                            $base_date_str = date('d-m-Y', strtotime($res_user->base_created_on));
                            break;
                        case "3":$base_lvl1_str = 'Completed';
                            $base_lvl2_str = 'Completed';
                            $base_date_str = date('d-m-Y', strtotime($res_user->base_created_on));
                            break;
                        default:$base_lvl1_str = 'Not Started';
                            $base_lvl2_str = 'Not Started';
                            break;
                    }
                    switch ($res_user->bronze_status) {
                        case "0":$bronze_lvl1_str = 'Not Started';
                            $bronze_lvl2_str = 'Not Started';
                            break;
                        case "6":$bronze_lvl1_str = 'Failed';
                            $bronze_lvl2_str = 'Not Started';
                            $bronze_date_str = date('d-m-Y', strtotime($res_user->bronze_created_on));
                            break;
                        case "7":$bronze_lvl1_str = 'Completed';
                            $bronze_lvl2_str = 'Not Started';
                            break; // same
                        case "4":$bronze_lvl1_str = 'Completed';
                            $bronze_lvl2_str = 'Not Started';
                            break; // same
                        case "2":$bronze_lvl1_str = 'Completed';
                            $bronze_lvl2_str = 'Failed';
                            $bronze_date_str = date('d-m-Y', strtotime($res_user->bronze_created_on));
                            break;
                        case "3":$bronze_lvl1_str = 'Completed';
                            $bronze_lvl2_str = 'Completed';
                            $bronze_date_str = date('d-m-Y', strtotime($res_user->bronze_created_on));
                            break;
                        default:$bronze_lvl1_str = 'Not Started';
                            $bronze_lvl2_str = 'Not Started';
                            break;
                    }
                    switch ($res_user->silver_status) {
                        case "0":$silver_lvl1_str = 'Not Started';
                            $silver_lvl2_str = 'Not Started';
                            break;
                        case "6":$silver_lvl1_str = 'Failed';
                            $silver_lvl2_str = 'Not Started';
                            $silver_date_str = date('d-m-Y', strtotime($res_user->silver_created_on));
                            break;
                        case "7":$silver_lvl1_str = 'Completed';
                            $silver_lvl2_str = 'Not Started';
                            break; // same
                        case "4":$silver_lvl1_str = 'Completed';
                            $silver_lvl2_str = 'Not Started';
                            break; // same
                        case "2":$silver_lvl1_str = 'Completed';
                            $silver_lvl2_str = 'Failed';
                            $silver_date_str = date('d-m-Y', strtotime($res_user->silver_created_on));
                            break;
                        case "3":$silver_lvl1_str = 'Completed';
                            $silver_lvl2_str = 'Completed';
                            $silver_date_str = date('d-m-Y', strtotime($res_user->silver_created_on));
                            break;
                        default:$silver_lvl1_str = 'Not Started';
                            $silver_lvl2_str = 'Not Started';
                            break;
                    }
                    switch ($res_user->gold_status) {
                        case "0":$gold_lvl1_str = 'Not Started';
                            $gold_lvl2_str = 'Not Started';
                            break;
                        case "6":$gold_lvl1_str = 'Failed';
                            $gold_lvl2_str = 'Not Started';
                            $gold_date_str = date('d-m-Y', strtotime($res_user->gold_created_on));
                            break;
                        case "7":$gold_lvl1_str = 'Completed';
                            $gold_lvl2_str = 'Not Started';
                            break; // same
                        case "4":$gold_lvl1_str = 'Completed';
                            $gold_lvl2_str = 'Not Started';
                            break; // same
                        case "2":$gold_lvl1_str = 'Completed';
                            $gold_lvl2_str = 'Failed';
                            $gold_date_str = date('d-m-Y', strtotime($res_user->gold_created_on));
                            break;
                        case "3":$gold_lvl1_str = 'Completed';
                            $gold_lvl2_str = 'Completed';
                            $gold_date_str = date('d-m-Y', strtotime($res_user->gold_created_on));
                            break;
                        default:$gold_lvl1_str = 'Not Started';
                            $gold_lvl2_str = 'Not Started';
                            break;
                    }

                    //$objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$base_lvl1_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $base_lvl2_str);
                    //$objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$bronze_lvl1_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $bronze_lvl2_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $silver_lvl1_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $silver_lvl2_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $gold_lvl1_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $gold_lvl2_str);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $base_date_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $bronze_date_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $silver_date_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $gold_date_str);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('CERTIFICATION REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "certification_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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"])) {

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ENQUIRY REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":Q" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row, "P" . $row, "Q" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Source', 'SMPIN', 'DSE', 'Model Name', 'Variant Name', 'Name', 'Mobile', 'Email', 'Created On', 'Brochure', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }
            $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";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->source_type);
                    $objPHPExcel->getActiveSheet()->getStyle('C' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->enq_smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->enq_dsename);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->model_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->variant_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->email);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $brochure_status = ($res_user->is_brochure == '1') ? "Shared" : "Not Shared";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $brochure_status);

                    $objPHPExcel->getActiveSheet()->getStyle('L' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ENQUIRY REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "enquiry_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}
// customerexperience_report_v2
function customerexperience_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"]);

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CUSTOMER EXPERIENCE FORM REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":N" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'Name', 'Mobile', 'Email', 'Age', 'SMPIN', 'DSE', 'Created On', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $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";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','Name','Mobile','Email','Age','SMPIN','DSE','Created On','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salutation . " " . $res_user->firstname . " " . $res_user->lastname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->email);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->age);
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dsename);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $objPHPExcel->getActiveSheet()->getStyle('I' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('CUSTOMER EXPERIENCE REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "customer_experience_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_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,pass_score  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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT :" . $assessment_arr[0]->assessmentname);
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":B" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, '');

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Below 50%', '50% to 79%', '80% to 100%', 'Total Participant');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            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";
            }

            $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 . "' AND status=1 AND is_deleted=0 " . $mcond . " )";
                        $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 . "' AND status=1 AND is_deleted=0 " . $mcond . "  )";
                        $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!='' AND status=1 AND is_deleted=0 " . $mcond . "  )" : " 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;

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['level_1']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['level_2']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['level_3']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['total']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['level_1']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['level_2']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['level_3']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['total']);

            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('ASSESSMENT REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "assessment_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_v2
function assessment_report_list_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,pass_score  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"]);

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

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":P" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT :" . $assessment_arr[0]->assessmentname);
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, '');

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, 'O' . $row, "P" . $row, "Q" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result Category', 'Result', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }
            $limit_cond = '';
            if ($top_50 == '1') {
                $limit_cond = " LIMIT 0,50";
            }

            $query_str = "SELECT  u.`userid`,u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u. `mobileno`,u.segment,u.`dealer_code`,highscore as score,d.dealer_name,d.zone,d.`zm_name`,d.`rm_name`,d.`am_name`,ar.time_taken,ar.modified_on as completed_time FROM " . TABLE_ASSESSMENT_USER . " as au JOIN " . TABLE_ASSESSMENTRESULT . " as ar ON au.highscorearid=ar.arid JOIN " . TABLE_USER . " as u ON au.userid=u.userid LEFT 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,ar.time_taken ASC,au.modified_on ASC " . $limit_cond;
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Email','Score','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                    $score = ($res_user->score) ? $res_user->score : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $score);

                    $timetaken = (isset($res_user->time_taken)) ? round($res_user->time_taken / 60) : "0";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $timetaken);

                    $completed_time = (isset($res_user->completed_time)) ? date('d-m-Y h:i A', strtotime($res_user->completed_time)) : "-";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $completed_time);

                    switch (true) {
                        case ($score < 50): $cat_str = 'Below 50%';
                            break;
                        case ($score >= 50 && $score < 80): $cat_str = '50% to 79%';
                            break;
                        case ($score >= 80): $cat_str = '80% to 100%';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $cat_str);

                    $result_str = ($score >= $assessment_arr[0]->pass_score) ? "Pass" : "Fail";
                    $column_str = getColumnFromNumber($i++) . $row;
                    if ($result_str == 'Pass') {
                        $objPHPExcel->getActiveSheet()->getStyle($column_str)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("008000");
                    } else {
                        $objPHPExcel->getActiveSheet()->getStyle($column_str)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID)->getStartColor()->setRGB("FF0000");
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue($column_str, $result_str);

                    $objPHPExcel->getActiveSheet()->getStyle('G' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ASSESSMENT REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "assessment_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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 = '';
            $returnarr = array();
            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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SET SALES TARGET REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH & YEAR :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('F Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":D" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Target Set by Dealer', 'Target Approved by AM', "Auto Approved Target", "Total");

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $tablejoin = '';

            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 . "'" : '';

            }

            $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 " . 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';

                        $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';

                        $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 . "' AND status=1 AND is_deleted=0 " . $mcond . " )";

                        $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';

                        $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';

                        $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 . "' AND status=1 AND is_deleted=0 " . $mcond . "  )";

                        $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';

                        $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';

                        $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 " . $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';

                        $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';

                        $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!='' AND status=1 AND is_deleted=0 " . $mcond . "  )" : " 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 " . 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';

            $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);

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dealer_target']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['am_target']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['auto_target']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['total_target']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dealer_target']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['am_target']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['auto_target']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['total_target']);

            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('SET SALES TARGET REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "setsales_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_list_v2
function setsalestarget_report_list_v2()
{
    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 d.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE status=1 AND is_deleted=0 " . $mcond . ")";
            } else {
                //PAN
                $user_cond = " AND d.is_deleted=0 AND d.status=1";
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SET SALES REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":K" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH & YEAR :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('M-Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":D" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Total Target', 'Target Status -Dealer', 'Target Status -AM', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //#
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //Segment
            //$objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //mobile
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //total
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //Target Status -Dealer
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //Target Status -AM
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //u.role>7 and u.role!=14  and u.role!=16 AND u.is_deleted=0
            $_REQUEST["regtype"] = isset($_REQUEST["regtype"]) ? $_REQUEST["regtype"] : 1;
            $regtype = (escape_str($_REQUEST["regtype"]) == '1' || !isset($_REQUEST["regtype"])) ? "1" : "0";

            if ($regtype == '1') {
                $user_cond .= " AND u.is_current=1 ";
            }

            //$query_str="SELECT 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 LEFT 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  ";
            $query_str = "SELECT  u. `smpin`, u. `fname`,u. `lname`,u. `emailid`,u.mobile as 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`,u.segment FROM " . DMS_EMPLOYEE . " 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 LEFT JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code WHERE u.status=1 " . $user_cond . " GROUP BY u.smpin ORDER BY  tot_target DESC,u.id ASC  ";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $mkey => $res_user) {
                    //'#','SMPIN','Segment', 'DSE Name','Mobile','Email','Total Target','Target Status -Dealer','Target Status -AM','Dealer Code','Dealer Name','Zone','ZM','RM','AM'
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $segment_str = '-';
                    switch ($res_user->segment) {
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    //$objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$res_user->emailid);

                    $tot_target = ($res_user->tot_target) ? $res_user->tot_target : '0';
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_target);

                    $statusquery_str = "SELECT id FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE  smpin='" . $res_user->smpin . "' AND  sale_month='" . $from_date . "' AND roleid=14 LIMIT 1";
                    $sales_arr = $conn->getQueryValue($statusquery_str);
                    $dealer_sale_status_str = (count($sales_arr) > 0) ? "Target Set by dealer" : "Target Auto set";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $dealer_sale_status_str);

                    $statusquery_str = "SELECT id,status  FROM " . TABLE_USER_SALESTARGET_STATUS . " WHERE  smpin='" . $res_user->smpin . "' AND  sale_month='" . $from_date . "' AND roleid=7 LIMIT 1";
                    $sales_arr = $conn->getQueryValue($statusquery_str);

                    $am_sale_status_str = (count($sales_arr) > 0) ? "Target Approved by AM" : "Target Auto set";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $am_sale_status_str);

                    $objPHPExcel->getActiveSheet()->getStyle('H' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('SET SALES REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "setsales_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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"])) {

            $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 = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ENQUIRY REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":Q" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;

            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FROM DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('d-M-Y', strtotime($from_date)));

            $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "TO DATE :");
            $objPHPExcel->getActiveSheet()->getStyle("C" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, date('d-M-Y', strtotime($to_date)));

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row, "L" . $row, "M" . $row, "N" . $row, "O" . $row, "P" . $row, "Q" . $row, 'R' . $row, 'S' . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', "Name", "Email", "Mobile", "Age", "Smpin", "Dse Name", "Model", "Variant", "Source", "Created On", "Brochure", "Dealer code", "Dealer", "State", "City", "ZM", "RM", "AM");

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }
            $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";
            $user_arr = $conn->getQueryValue($query_str);

            if (count($user_arr) > 0) {
                $sno = 1;
                foreach ($user_arr as $key => $res_user) {
                    if ($res_user->enquiry_status == 0) {
                        $user_arr[$key]->brochure_status = ($res_user->is_brochure == '1') ? "Shared" : "Not Shared";
                        $user_arr[$key]->created_on_str = date('d-m-Y h:i A', strtotime($res_user->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_user->enq_id . "' LIMIT 1");

                        if (count($get_enq_data) > 0) {
                            $user_arr[$key]->brochure_status = ($get_enq_data[0]->is_brochure == '1') ? "Shared" : "Not Shared";
                            $user_arr[$key]->model_name = $get_enq_data[0]->model_name;
                            $user_arr[$key]->variant_name = $get_enq_data[0]->variant_name;
                            $user_arr[$key]->created_on = $get_enq_data[0]->created_on;
                            $user_arr[$key]->created_on_str = date('d-m-Y h:i A', strtotime($get_enq_data[0]->created_on));
                            $user_arr[$key]->name = $get_enq_data[0]->name;
                            $user_arr[$key]->email = $get_enq_data[0]->email;
                            $user_arr[$key]->mobile = $get_enq_data[0]->mobile;
                        }

                    }

                    $i = 0;
                    //'#',"Name","Email","Mobile","Age","Smpin","Dse Name","Model","Variant","Source","Created On","Brochure","Dealer code","Dealer","State","City","ZM","RM","AM"
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_arr[$key]->name);
                    //$objPHPExcel->getActiveSheet()->getStyle('C'.$row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_arr[$key]->email);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_arr[$key]->mobile);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->age);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dsename);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->model_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->variant_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->source_type);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_arr[$key]->brochure_status);

                    $objPHPExcel->getActiveSheet()->getStyle('L' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->state);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('ENQUIRY REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "enquiry_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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_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 = '';
            $returnarr = array();
            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' ".$mcond.")";
            } else {
                //PAN
                //$user_cond='';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SET SALES TARGET REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "MONTH & YEAR :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, date('F Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":E" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            $header_arr = array('Users', 'Dealer Count', 'DSE count', "Target Set by Dealers", "Target Approved by AMs", "Target Modified by AMs", "Auto Target Set");
            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            $tablejoin = '';

            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 . "'" : '';

            }

            $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->zm_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."' ".$mcond." ";
                        //$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, '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);

            if (count($returnarr) > 0) {
                $sno = 1;
                foreach ($returnarr as $mkey => $res_user) {
                    // $header_arr=array('Users', 'Employee Count','Registered Count','Percentage %');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['name']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dealer_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dse_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['dealer_target_set_dse_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['am_target_set_dse_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['am_target_mod_dse_count']);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user['auto_target']);
                }
            }
            //TOTAL
            $row += 2;
            $i = 0;
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, "Total");
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dealer_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dse_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['dealer_target_set_dse_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['am_target_set_dse_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['am_target_mod_dse_count']);
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $total_arr['auto_target']);

            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);

            $objPHPExcel->getActiveSheet()->setTitle('SET SALES TARGET REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "setsales_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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"])) {
            $page = escape_str($_REQUEST["page"]);

            $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 = $mcond;
            } else {
                //PAN
                $user_cond = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $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));

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "USER LOGIN REPORT - " . date('d-M-Y', strtotime($from_date)));
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":J" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $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 ";

            }

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

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, 'K' . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Segment', 'last Access', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //GET ALL USER
            $user_str = "SELECT u.*,l.created_on,d.dealer_name,d.zone,d.zm_name,d.rm_name,d.am_name FROM (SELECT userid,smpin,fname,lname,dealer_code,segment FROM  " . TABLE_USER . " WHERE  is_deleted=0 AND role>7 AND role<14 AND status=1 " . $ucond . " )u " . $tablejoin . " JOIN (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 ON l.userid=u.userid 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 ";
            $result_arr = $conn->getQueryValue($user_str);

            if (count($result_arr)) {
                $sno = 1;
                foreach ($result_arr as $res_user) {
                    //'#','SMPIN', 'DSE Name','last Access','Dealer Code','Dealer Name','Zone','ZM','RM','AM'
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    switch ($res_user->segment) {
                        case '0':$segment_str = 'Student Learner';
                            break;
                        case '1':$segment_str = 'Parents/Guardian';
                            break;
                        case '2':$segment_str = 'Instructor';
                            break;
                        default:$segment_str = 'Student Learner';
                            break;
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $segment_str);

                    $time_str = date('d-m-Y h:i A', strtotime($res_user->created_on));
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $time_str);
                    $objPHPExcel->getActiveSheet()->getStyle('E' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('USER LOGIN REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_login_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

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

            $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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "BULLETIN VIEW REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":L" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

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

            //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'));
            }

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "BULLETIN : " . $content_arr[0]->bulletin_title);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row, "K" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

            $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Viewed on', 'Dealer Code', 'Dealer Name', 'Zone', 'ZM', 'RM', 'AM');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('25');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //GET ALL USER
            $content_user_str = "SELECT 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";
            $contentresult_arr = $conn->getQueryValue($content_user_str);

            if (count($contentresult_arr)) {
                $sno = 1;
                foreach ($contentresult_arr as $res_user) {
                    //$header_arr=array('S.No','SMPIN', 'DSE Name','Mobile','Completed on','Dealer Code','Dealer Name','Zone','ZM','RM','AM');
                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->getStyle('B' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                    $objPHPExcel->getActiveSheet()->getStyle('F' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('BULLETIN REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "user_bulletin_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}

// bulletin_report
function bulletin_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 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 = '';
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "BULLETIN REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            //Dashboard
            //Learning
            //Assessment
            //Feedback

            $header_arr = array('S.No', 'Title', 'No. of DSE Viewed');

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('45');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //GET ALL CONTENTS OF CATEGORY
            $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 ";
            $contentresult_arr = $conn->getQueryValue($query_str);

            if (count($contentresult_arr)) {
                $sno = 1;
                foreach ($contentresult_arr as $res_content) {
                    //'#','Topic', 'Content','Content type','DSE Completed'
                    $i = 0;
                    $row++;

                    $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;

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_content->bulletin_title);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_content->total_view);

                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('BULLETIN REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "bulletin_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
            }

            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel.php';
            require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'PHPExcel/Writer/Excel2007.php';
            $objPHPExcel = new PHPExcel();
            $objPHPExcel->setActiveSheetIndex(0);

            $row += 2;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "REWARD POINT HISTORY REPORT");
            $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(14);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "EMPLOYE :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $smpin_data[0]->fname . " " . $smpin_data[0]->lname . " - " . $smpin_data[0]->smpin);
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":E" . $row);

            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SMPIN :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $smpin_data[0]->smpin);
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":E" . $row);

            $segment_str = '';
            switch ($smpin_data[0]->segment) {

                    case '1':$segment_str = 'Parents/Guardian';
                        break;
                    case '2':$segment_str = 'Instructor';
                        break;
                    default:$segment_str = 'Student Learner';
                    break;
            }
            $row++;
            $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SEGMENT :");
            $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);
            $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $segment_str);
            $objPHPExcel->getActiveSheet()->mergeCells("B" . $row . ":E" . $row);

            $row++;
            $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
            setCellBorder($bordercells_arr, $objPHPExcel);
            $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
            //Dashboard
            //Learning
            //Assessment
            //Feedback

            $header_arr = array('S.No', 'Description', "Points", "Type", "Date");

            $i = 0;
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
            $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');

            $i = 0;
            foreach ($header_arr as $val) {
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
            }

            //HISTORY
            $query_str = "SELECT * FROM " . TABLE_USER_EPOINTS_HISTORY . "  WHERE smpin='" . $smpin . "' ORDER BY  hid DESC ";
            $result_arr = $conn->getQueryValue($query_str);

            if (count($result_arr)) {
                $sno = 1;
                foreach ($result_arr as $res) {
                    //'#','Topic', 'Content','Content type','DSE Completed'
                    $i = 0;
                    $row++;

                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res->description);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res->points);
                    $type_str = ($res->transaction_type == 'C') ? "Credit" : "Debit";
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $type_str);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res->created_on)));

                }
            }

            $objPHPExcel->getActiveSheet()->setTitle('REWARD POINT REPORT');
            $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
            $fpath = "../" . S3_REPORT_URL;
            $filename = "reward_report_" . uniqid() . ".xlsx";
            $objWriter->save($fpath . $filename);

            //S3 UPLOAD
            //S3 LIB
            require dirname(__FILE__) . '/awsfileserver.php';

            //S3 OBJ
            $s3 = awsfileserver::connectS3();
            awsfileserver::checkupload($s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../" . S3_REPORT_URL . $filename);

            //DELETE AFTER MOVE TO S3
            @unlink(dirname(__FILE__) . '/../' . S3_REPORT_URL . $filename);

            $obj = new stdClass();
            $obj->key_name = S3_REPORT_URL . $filename;
            $url = awsfileserver::getS3Url($s3, S3_BUCKET, $obj);

            $returnarr = array("report_url" => $url);

            $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'));
    }
}
