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

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

class managerexport
{

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

            switch ($parameter_val) {

                case "salesreportexport":
                case "certifieddsereportexport":
                case "assessmentreportexport":
                case "salesleaderboardexport":
                case "assessmentleaderboardexport":
                case "registrationreportexport":
                case "coursereportexport":
                case "salestargetreportexport":
                case "surveyuserreportexport":
                case "managertargetsample":

                case "newregistrationreportexport":
                case "newcoursereportexport":
                case "newsalesreportexport":
                case "newcertifieddsereportexport":
                case "newassessmentreportexport":
                case "newsalestargetreportexport":

                case "newsalesleaderboardexport":
                case "newassessmentleaderboardexport":

                case "asmassessmentreport":
                case "rmassessmentreport":
                case "zmassessmentreport":
                case "trainerassessmentreport":

                case "surveyreportexport":
                case "dealersalestargetreportexport":
                case "managersalestargetreportexport":

                case "zmcoursereport":
                case "trainercoursereport":
                case "rmcoursereport":
                case "amcoursereport":
                case "dealercoursereport":

                case "ilearnreport":

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

                default:
                    break;
            }

        }

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

    private function getmanagerconditon_arr($parent)
    {
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7', '14'));
        $zmemail = escape_str($parent->_request["zmemail"]);
        $rmemail = escape_str($parent->_request["rmemail"]);
        $amemail = escape_str($parent->_request["amemail"]);
        $dealer_cond = array();

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

            if ($zmemail != 'all' && $zmemail != '') {
                $dealer_cond[] = "zm_email='" . $zmemail . "'";
            }

            if ($rmemail != 'all' && $rmemail != '') {
                $dealer_cond[] = "rm_email='" . $rmemail . "'";
            }

            if ($amemail != 'all' && $amemail != '') {
                $dealer_cond[] = "am_email='" . $amemail . "'";
            }

        } else if ($parent->current_user_role == '4') {
            $REQUEST_KEYS = array('rmemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

            $dealer_cond[] = "zm_email='" . $parent->current_user_data->emailid . "'";
            if ($rmemail != 'all' && $rmemail != '') {
                $dealer_cond[] = "rm_email='" . $rmemail . "'";
            }

            if ($amemail != 'all' && $amemail != '') {
                $dealer_cond[] = "am_email='" . $amemail . "'";
            }

        } else if ($parent->current_user_role == '5') {
            $REQUEST_KEYS = array('rmemail' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

            $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
            $trainer_arr = $parent->getQueryValue($get_trainer_str);
            $dealer_cond[] = (count($trainer_arr) > 0) ? "zone='" . $trainer_arr[0]->zone . "'" : "zone='0'";
            if ($rmemail != 'all' && $rmemail != '') {
                $dealer_cond[] = "rm_email='" . $rmemail . "'";
            }

            if ($amemail != 'all' && $amemail != '') {
                $dealer_cond[] = "am_email='" . $amemail . "'";
            }

        } else if ($parent->current_user_role == '6') {
            //$REQUEST_KEYS=array('amemail'=>'string');
            //globalfunc::mandatoryKey($parent,$REQUEST_KEYS);

            $dealer_cond[] = "rm_email='" . $parent->current_user_data->emailid . "'";
            if ($amemail != 'all' && $amemail != '') {
                $dealer_cond[] = "am_email='" . $amemail . "'";
            }

        } else if ($parent->current_user_role == '7') {
            $dealer_cond[] = "am_email='" . $parent->current_user_data->emailid . "'";
        } else if ($parent->current_user_role == '14') {
            $dealer_cond[] = "dealer_code='" . $parent->current_user_data->dealer_code . "'";
        }

        return $dealer_cond;
    }

    /* /////////////////////////////// SALES REPORT EXPORT //////////////////////////////////////////////// */
    private function salesreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //$limit_str=globalfunc::setPageLimit($parent,$parent->params[2]);
        //$limit_cond=($limit_str)?" LIMIT ".$limit_str:'';

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Month & Year: " . date('M Y', strtotime($year_data . "-" . $month_data . "-01")));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES REPORT");
        $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);

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

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Target', 'Achieved', 'Percentage');

        //GET MODEL
        $query_modelarr = $parent->selectQuery(TABLE_MODEL, "model_id, model_name", "model_is_target=1 AND user_type=0 ORDER BY model_id", "");
        if ($query_modelarr['nr']) {
            foreach ($query_modelarr['result'] as $res_model) {
                //$header_arr[]=$res_model->model_name." (Achieved/Target)";
                $header_arr[] = $res_model->model_name . " \n Target";
                $header_arr[] = $res_model->model_name . " \n Achieved";
                $header_arr[] = $res_model->model_name . " \n %";

            }
        }

        $header_arr[] = 'ZM';
        $header_arr[] = 'RM';
        $header_arr[] = '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('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');

        if ($query_modelarr['nr']) {
            foreach ($query_modelarr['result'] as $res_model) {
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
                $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
            }
        }

        $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);
            $m = $i - 1;
            $bordercells_arr[] = getColumnFromNumber($m) . $row;
        }
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //AREA MANAGER SELF
        if ($parent->current_user_role == '7') {
            $role = $parent->current_user_role;
            $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);

            $emailid = $userdata->emailid;
        } else {
            //AREA MANAGER LEVEL REPORT
            $role = '7';
            //FIELDS VALIDATION
            $REQUEST_KEYS = array('area' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $area_manager = escape_str($parent->_request["area"]);
            $emailid = $area_manager;
        }

        $user_cond = "";
        switch ($role) {

            case '3': //HO Manager
                $user_cond .= "";
                break;
            case '4': //Zonal Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '5': //Trainer
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.emailid='" . $emailid . "' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                break;
            case '6': //Regional Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '7': //Area Manager
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "' " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;

//            case '9': break;
            //            case '10': break;
            //            case '11': break;
            //            case '12': break;
            //            case '13': break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[2]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE u.status='1' AND u.is_deleted='0' AND u.role>7  AND u.role<14 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //TARGET
                $user_target = projectfunc::userSalesTarget($parent, $res_user->userid, '', $year_data, $month_data);
                //ACHIEVED
                $user_achieved = projectfunc::userSalesAchieved($parent, $res_user->userid, '', $year_data, $month_data);
                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_target);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_achieved);
                $percentage = ($user_target > 0) ? round(($user_achieved / $user_target) * 100) : 0;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage);

                if ($query_modelarr['nr']) {
                    foreach ($query_modelarr['result'] as $res_model) {
                        $model_user_target = projectfunc::userSalesTarget($parent, $res_user->userid, $res_model->model_name, $year_data, $month_data);
                        //ACHIEVED
                        $model_user_achieved = projectfunc::userSalesAchieved($parent, $res_user->userid, $res_model->model_name, $year_data, $month_data);

                        //$objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$model_user_achieved."/".$model_user_target);

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

                        $modelpercentage = ($model_user_target > 0) ? round(($model_user_achieved / $model_user_target) * 100) : 0;
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $modelpercentage);
                    }
                }

                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'MANAGER SALES REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// CERTIFIED DSE REPORT EXPORT //////////////////////////////////////////////// */
    private function certifieddsecolor($parent, $value)
    {

        switch ($value) {
            case '0':$color_value = 'DCDCDC';
                break;
            case '1':$color_value = 'FFA500';
                break;
            case '2':$color_value = 'FF0000';
                break;
            case '3':$color_value = '008000';
                break;
            default:$color_value = 'DCDCDC';
                break;
        }
        return $color_value;
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// CERTIFIED DSE REPORT EXPORT //////////////////////////////////////////////// */
    private function certifieddsereportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CERTIFIED DSE 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 += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Certification', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('5');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('5');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('5');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('5');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth('30');

        $i = 0;
        foreach ($header_arr as $val) {
            $i = ($i == '5') ? '8' : $i;
            if ($i == 4) {
                $objPHPExcel->getActiveSheet()->mergeCells("E" . $row . ":H" . $row);
                $objPHPExcel->getActiveSheet()->getStyle("E" . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            }
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
        }

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

        $user_cond = "";
        switch ($role) {

            case '3': //HO Manager
                $user_cond .= "";
                break;
            case '4': //Zonal Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "' AND status='1' AND is_deleted='0')";
                break;
            case '5': //Trainer
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.emailid='" . $emailid . "' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                break;
            case '6': //Regional Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '7': //Area Manager
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "'" . $dealer_code_cond . "  AND status='1' AND is_deleted='0' )";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[0]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE u.status='1' AND u.is_deleted='0' AND u.role>7 AND u.role<14 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                $learning_arr = projectfunc::userLearningLevel($parent, $res_user->userid);
//0-Not Started Grey
                //1-Current Orange
                //2-Failed Red,
                //3-Completed Green
                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }

                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent, $learning_arr['basic']));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, 'B');

                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent, $learning_arr['bronze']));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, 'B');
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent, $learning_arr['silver']));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, 'S');
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setBold(true);
                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i) . $row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent, $learning_arr['gold']));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, 'G');

                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'CERTIFIED DSE REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Score', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";
        //AREA MANAGER SELF
        if ($parent->current_user_role == '7') {
            $role = $parent->current_user_role;
            $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
            $emailid = $userdata->emailid;
        } else {
            //AREA MANAGER LEVEL REPORT
            $role = '7';
            //FIELDS VALIDATION
            $REQUEST_KEYS = array('area' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $area_manager = escape_str($parent->_request["area"]);
            $emailid = $area_manager;
        }
        if ($parent->current_user_role == '14') {
            $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
        }
        $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "'" . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[0]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR  d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE u.status='1' AND u.is_deleted='0' AND u.role>7 AND u.role<14 AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                $learning_arr = projectfunc::userLearningLevel($parent, $res_user->userid);
//0-Not Started Grey
                //1-Current Orange
                //2-Failed Red,
                //3-Completed Green
                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");

                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// SALES LEADERBOARD REPORT EXPORT //////////////////////////////////////////////// */
    private function salesleaderboardexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Month & Year: " . date('M Y', strtotime($year_data . "-" . $month_data . "-01")));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES LEADERBOARD REPORT");
        $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);

        $row += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Sales', 'Rank', 'OutOf', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');

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

        $user_cond = "";
        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[2]);
        $order_by_cond = " ORDER BY f.rank,d.dealer_name ASC";

        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Rank

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = " ORDER BY d.dealer_name,f.rank ASC ";
                    break;
                case '2':$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
                default:$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
            }
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR  d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        if ($parent->current_user_role == '14') {
            $user_cond .= " AND u.dealer_code='" . $dealer_code . "'";
        }

        //GET MONTH RANGE
        $start_date = date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01"));
        $end_date = date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01"));

        $parent->executeQuery("SET @rownum := 0");
        $get_user_str = "SELECT f.*,u.fname,u.lname,u.mobileno,d.* FROM (SELECT @rownum := @rownum + 1 AS rank, salecount,targetcount,smpin,userid,last_sale,first_sale,sale_per FROM (SELECT c.*, CASE WHEN targetcount=0 THEN 0 ELSE (salecount/targetcount)*100 END as 'sale_per' FROM ( SELECT a.userid,a.smpin,a.salecount,b.targetcount,a.last_sale,a.first_sale FROM (SELECT COALESCE(COUNT(s.id),'0') as scount, COALESCE(SUM(s.sales_count),'0') as salecount, u.smpin,u.userid,MAX(s.sale_date) last_sale,MIN(s.sale_date) first_sale FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $start_date . "' AND sale_date<='" . $end_date . "') as s ON s.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND d.am_email='" . $email . "' " . $area_cond . " GROUP BY u.smpin ) as a JOIN  (SELECT  COALESCE(SUM(t.target),'0') as targetcount,u.smpin,u.userid FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $start_date . "' AND sale_month<='" . $end_date . "')as t ON t.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND d.am_email='" . $email . "' " . $area_cond . "  GROUP BY u.smpin ) as b ON a.userid=b.userid )c ORDER BY cast(sale_per as SIGNED) DESC,cast(salecount as SIGNED) DESC,cast(targetcount as SIGNED) DESC,last_sale ASC,first_sale ASC,userid ASC)as a )as f JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        $get_user_count_str = "SELECT COUNT(u.userid)as user_count FROM (SELECT  salecount,targetcount,smpin,userid,last_sale,first_sale,sale_per FROM (SELECT c.*, CASE WHEN targetcount=0 THEN 0 ELSE (salecount/targetcount)*100 END as 'sale_per' FROM ( SELECT a.userid,a.smpin,a.salecount,b.targetcount,a.last_sale,a.first_sale FROM (SELECT COALESCE(COUNT(s.id),'0') as scount, COALESCE(SUM(s.sales_count),'0') as salecount, u.smpin,u.userid,MAX(s.sale_date) last_sale,MIN(s.sale_date) first_sale FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $start_date . "' AND sale_date<='" . $end_date . "') as s ON s.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND d.am_email='" . $email . "' " . $area_cond . " GROUP BY u.smpin ) as a JOIN  (SELECT  COALESCE(SUM(t.target),'0') as targetcount,u.smpin,u.userid FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $start_date . "' AND sale_month<='" . $end_date . "')as t ON t.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND d.am_email='" . $email . "' " . $area_cond . "  GROUP BY u.smpin ) as b ON a.userid=b.userid )c ORDER BY cast(sale_per as SIGNED) DESC,cast(salecount as SIGNED) DESC,cast(targetcount as SIGNED) DESC,last_sale ASC,first_sale ASC,userid ASC)as a )as f JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1   " . $user_cond . " LIMIT 1";
        if (count($user_arr) > 0) {
            $user_count_arr = $parent->getQueryValue($get_user_count_str);
            $outof = (count($user_count_arr) > 0) ? $user_count_arr[0]->user_count : '0';
            foreach ($user_arr as $mkey => $res_user) {
                $learning_arr = projectfunc::userLearningLevel($parent, $res_user->userid);

                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salecount . "/" . $res_user->targetcount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rank);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $outof);
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'SALES LEADERBOARD REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ASSESSMENT LEADERBOARD REPORT EXPORT //////////////////////////////////////////////// */
    private function assessmentleaderboardexportold($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT LEADERBOARD REPORT");
        $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);

        $row += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Score', 'Position', 'Outof', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');

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

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

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[2]);
        $order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = " ORDER BY d.dealer_name,f.rank ASC ";
                    break;
                case '2':$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
                default:$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
            }
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR  d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

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

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

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

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

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

        } else {
            $query_cond .= " AND (filter_dealer='' AND filter_region='' AND filter_zone=''  ) ";
        }

        $outof = 0;
        //GET ALL ASSESSMENT LIST WHICH PUBLISHED
        $assessment_str = "SELECT a.* FROM `" . TABLE_ASSESSMENT . "` as a  WHERE a.atype='0' AND a.assettypeid='1' AND a.publisheddate<='" . NOW . "' " . $query_cond;
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $data_arr = array();
        if (count($assessment_arr) > 0) {
            foreach ($assessment_arr as $res_assess) {
                $assessmentid_arr[] = $res_assess->assessmentid;
            }

            $parent->executeQuery("SET @rownum := 0");
            $get_user_rank_str = "SELECT f.*,u.userid,u.fname,u.lname,u.mobileno,u.smpin,d.* FROM ( SELECT @rownum := @rownum + 1 AS rank,COALESCE(user_score,'0')as user_score,CASE WHEN user_score IS NULL THEN 0 ELSE (user_score/" . count($assessment_arr) . ") END as percentage_score,userid,total_time,last_modified FROM (SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified  FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code JOIN (SELECT * FROM  " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") GROUP BY userid )as ar ON u.userid=ar.userid  WHERE u.role>7 AND  u.role<14 AND  u.is_deleted=0 AND d.am_email='" . $email . "'  GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC )as a )as f  JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1  " . $user_cond . $order_by_cond;
            $user_arr = $parent->getQueryValue($get_user_rank_str);

            $get_user_count_str = "SELECT COUNT(u.userid)as user_count FROM ( SELECT  COALESCE(user_score,'0')as user_score,CASE WHEN user_score IS NULL THEN 0 ELSE (user_score/" . count($assessment_arr) . ") END as percentage_score,userid,total_time,last_modified FROM (SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified  FROM " . TABLE_USER . " as u JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code JOIN (SELECT * FROM  " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") GROUP BY userid )as ar ON u.userid=ar.userid  WHERE u.role>7 AND  u.role<14 AND  u.is_deleted=0 AND d.am_email='" . $email . "'    GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC )as a )as f  JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1 " . $user_cond . " LIMIT 1";

            if (count($user_arr) > 0) {
                $user_count_arr = $parent->getQueryValue($get_user_count_str);
                $outof = (count($user_count_arr) > 0) ? $user_count_arr[0]->user_count : '0';
                foreach ($user_arr as $res_user) {
                    $i = 0;
                    $row++;
                    if (empty($res_user->smpin)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                    }
                    if (empty($res_user->fname)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                    }
                    if (empty($res_user->mobileno)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                    }
                    if (empty($res_user->dealer_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                    }
                    if (empty($res_user->city)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->percentage_score . "%");
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rank);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $outof);
                    if (empty($res_user->zm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                    }
                    if (empty($res_user->rm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                    }
                    if (empty($res_user->am_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                    }
                }
            }
        }

        //LOG
        $logstr = 'ASSESSMENT LEADERBOARD REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ASSESSMENT LEADERBOARD REPORT EXPORT //////////////////////////////////////////////// */
    private function assessmentleaderboardexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        if (count($parent->params) == 1) {
            $get_dealer_str = "SELECT dealer_code,city,zone FROM " . DMS_DEALER . "  WHERE am_email='" . $email . "'  AND status='1' AND is_deleted='0' GROUP BY dealer_code ";
            $dealer_arr = $parent->getQueryValue($get_dealer_str);

            //AREA LEADERBOARD COND
            //$dealer_code=$parent->current_user_data->dealer_code;
            // $query_allareadealerarr=$parent->selectQuery(DMS_DEALER,"`dealer_code`,am_email ","  dealer_code='".$dealer_code."'  AND status='1' AND is_deleted='0'","1");
            /*
            $dealer_arr=array();
            if($query_allareadealerarr['nr'])
            {
            $join_cond="JOIN ".DMS_DEALER." as d ON d.dealer_code=u.dealer_code ";
            $area_cond=" and d.am_email ='".$query_allareadealerarr['result'][0]->am_email."'";
            }else
            {
            $area_cond=" and u.dealer_code IN ('')";
            }
             */

            $start_date = date('Y-m', strtotime(CUR_DATE)) . "-01";
            $end_date = date('Y-m-t', strtotime(CUR_DATE));
            $date_cond = " AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59'";

        } else if (count($parent->params) == 2) {
            //FOR PANINDIA,ZONE,REGION WISE LEADER BOARD
            $dealer_code = $parent->current_user_data->dealer_code;
            $type_data = explode('?', strtolower(escape_str($parent->params[1])));
            switch ($type_data[0]) {
                case "panindia":
                    $start_date = date('Y', strtotime(CUR_DATE)) . "-01-01";
                    $end_date = date('Y', strtotime(CUR_DATE)) . "-12-31";
                    //COND
                    $join_cond = 'JOIN ".DMS_DEALER." as d ON d.dealer_code=u.dealer_code ';
                    $area_cond = '';

                    $date_cond = " AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59'";
                    break;
                case "zone":
                    $cur_month = date('m', strtotime(CUR_DATE));
                    /*if(in_array($cur_month,array('01','02','03','04','05','06')))
                    {
                    $start_date=date('Y', strtotime(CUR_DATE))."-01-01";
                    $end_date=date('Y',strtotime(CUR_DATE))."-06-30";
                    }else if(in_array($cur_month,array('07','08','09','10','11','12')))
                    {
                    $start_date=date('Y', strtotime(CUR_DATE))."-07-01";
                    $end_date=date('Y', strtotime(CUR_DATE))."-12-31";
                    }
                     */
                    $start_date = date('Y-m', strtotime(CUR_DATE)) . "-01";
                    $end_date = date('Y-m-t', strtotime(CUR_DATE));
                    $date_cond = " AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59'";

                    $get_dealer_str = "SELECT dealer_code,city,zone FROM " . DMS_DEALER . "  WHERE zm_email='" . $email . "'  AND status='1' AND is_deleted='0' GROUP BY dealer_code ";
                    $dealer_arr = $parent->getQueryValue($get_dealer_str);
                    if (count($dealer_arr) > 0) {
                        $join_cond = "JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code ";
                        $area_cond = " and d.zm_email ='" . $email . "'";
                    } else {
                        $area_cond = " and u.dealer_code IN ('')";
                    }

                    break;
                case "region":
                    $cur_month = date('m', strtotime(CUR_DATE));
                    /* if(in_array($cur_month,array('01','02','03')))
                    {
                    $start_date=date('Y', strtotime(CUR_DATE))."-01-01";
                    $end_date=date('Y',strtotime(CUR_DATE))."-03-31";
                    }else if(in_array($cur_month,array('04','05','06')))
                    {
                    $start_date=date('Y', strtotime(CUR_DATE))."-04-01";
                    $end_date=date('Y',strtotime(CUR_DATE))."-06-30";

                    }else if(in_array($cur_month,array('07','08','09')))
                    {

                    $start_date=date('Y', strtotime(CUR_DATE))."-07-01";
                    $end_date=date('Y',strtotime(CUR_DATE))."-09-30";
                    }else if(in_array($cur_month,array('10','11','12')))
                    {
                    $start_date=date('Y', strtotime(CUR_DATE))."-10-01";
                    $end_date=date('Y', strtotime(CUR_DATE))."-12-31";
                    }
                     *
                     */
                    $start_date = date('Y-m', strtotime(CUR_DATE)) . "-01";
                    $end_date = date('Y-m-t', strtotime(CUR_DATE));
                    $date_cond = " AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59'";

                    $get_dealer_str = "SELECT dealer_code,city,zone FROM " . DMS_DEALER . "  WHERE rm_email='" . $email . "'  AND status='1' AND is_deleted='0' GROUP BY dealer_code ";
                    $dealer_arr = $parent->getQueryValue($get_dealer_str);
                    if (count($dealer_arr) > 0) {
                        $join_cond = "JOIN " . DMS_DEALER . " as d ON d.dealer_code=u.dealer_code ";
                        $area_cond = " and d.rm_email ='" . $email . "'";
                    } else {
                        $area_cond = " and u.dealer_code IN ('')";
                    }

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT LEADERBOARD REPORT");
        $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);

        $row += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Score', 'Position', 'Outof', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');

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

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

//        $filter_arr=globalfunc::getFilterValues($parent,$parent->params[2]);
        //        $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC ";
        //        if(isset($filter_arr) && isset($filter_arr['sort']))
        //        {
        //            //1-Dealer
        //            //2-Location
        //            switch($filter_arr['sort'])
        //            {
        //                case '1': $order_by_cond=" ORDER BY d.dealer_name,f.rank ASC "; break;
        //                case '2': $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC "; break;
        //                default: $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC "; break;
        //            }
        //        }

//         if(isset($parent->_request["search"]))
        //        {
        //            $search_txt = escape_str($parent->_request["search"]);
        //            $user_cond.=" AND (u.fname LIKE '%".$search_txt."%' OR u.lname LIKE '%".$search_txt."%' OR u.emp_code LIKE '%".$search_txt."%' OR  d.dealer_name LIKE '%".$search_txt."%' OR d.zm_name LIKE '%".$search_txt."%'  OR d.rm_name LIKE '%".$search_txt."%' OR d.am_name LIKE '%".$search_txt."%' OR d.city LIKE '%".$search_txt."%'  )";
        //        }
        $new_cond = 'AND a.user_type=0 ';

        //GET ALL ASSESSMENT LIST WHICH PUBLISHED
        $assessment_str = "SELECT a.* FROM `" . TABLE_ASSESSMENT . "` as a  WHERE  a.status='1' AND a.atype='0' AND a.assettypeid='1' " . $date_cond . $new_cond;
        $assessment_arr = $parent->getQueryValue($assessment_str);
        //ASSESSMENT COUNT
        $assessment_count = (count($assessment_arr) > 0) ? count($assessment_arr) : '0';

//        $outof=0;
        //GET ALL ASSESSMENT LIST WHICH PUBLISHED
        //        $assessment_str = "SELECT a.* FROM `".TABLE_ASSESSMENT."` as a  WHERE a.atype='0' AND a.assettypeid='1' AND a.publisheddate<='".NOW."' ".$query_cond;
        //        $assessment_arr=$parent->getQueryValue($assessment_str);
        //        $data_arr=array();
        if (count($assessment_arr) > 0) {
            foreach ($assessment_arr as $res_assess) {
                $assessmentid_arr[] = $res_assess->assessmentid;
            }

//            $parent->executeQuery("SET @rownum := 0");
            //            $get_user_rank_str = "SELECT f.*,u.userid,u.fname,u.lname,d.* FROM ( SELECT @rownum := @rownum + 1 AS rank,COALESCE(user_score,'0')as user_score,CASE WHEN user_score IS NULL THEN 0 ELSE (user_score/".count($assessment_arr).") END as percentage_score,userid,total_time,last_modified FROM (SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified  FROM ".TABLE_USER." as u JOIN (SELECT * FROM  ".TABLE_ASSESSMENT_USER."  WHERE assessmentid IN (".@implode(',',$assessmentid_arr).") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM ".TABLE_ASSESSMENTRESULT." WHERE assessmentid IN (".@implode(',',$assessmentid_arr).") GROUP BY userid )as ar ON u.userid=ar.userid  WHERE u.role>='3' AND  u.is_deleted=0  GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC )as a )as f  LEFT JOIN ".TABLE_USER." as u ON f.userid=u.userid LEFT JOIN ".DMS_DEALER." d ON u.dealer_code=d.dealer_code WHERE 1=1 AND d.am_email='".$email."'  ".$user_cond.$order_by_cond;
            //            $user_arr=$parent->getQueryValue($get_user_rank_str);
            //
            //
            //             $get_user_count_str = "SELECT COUNT(u.userid)as user_count FROM ( SELECT  COALESCE(user_score,'0')as user_score,CASE WHEN user_score IS NULL THEN 0 ELSE (user_score/".count($assessment_arr).") END as percentage_score,userid,total_time,last_modified FROM (SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified  FROM ".TABLE_USER." as u JOIN (SELECT * FROM  ".TABLE_ASSESSMENT_USER."  WHERE assessmentid IN (".@implode(',',$assessmentid_arr).") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM ".TABLE_ASSESSMENTRESULT." WHERE assessmentid IN (".@implode(',',$assessmentid_arr).") GROUP BY userid )as ar ON u.userid=ar.userid  WHERE u.role>='3' AND  u.is_deleted=0  GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC )as a )as f  LEFT JOIN ".TABLE_USER." as u ON f.userid=u.userid LEFT JOIN ".DMS_DEALER." d ON u.dealer_code=d.dealer_code WHERE 1=1 AND d.am_email='".$email."'  ".$user_cond." LIMIT 1";
            if ($parent->current_user_role == '14') {
                $area_cond .= " AND u.dealer_code='" . $dealer_code . "'";
            }
            $get_user_rank_str = "SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified,u.dealer_code,u.role,d.*  FROM " . TABLE_USER . " as u JOIN (SELECT * FROM  " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") GROUP BY userid )as ar ON u.userid=ar.userid " . $join_cond . "  WHERE u.role>'7' AND  u.role<14 AND  u.is_deleted=0 " . $area_cond . " GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC   ";
            $get_user_rank_arr = $parent->getQueryValue($get_user_rank_str);
            if ($get_user_rank_arr) {
                foreach ($get_user_rank_arr as $res_user) {
                    $user_data_arr = new stdClass();
                    $get_time_str = "SELECT * FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND userid='" . $res_user->userid . "'";
                    $get_time_arr = $parent->getQueryValue($get_time_str);

                    $user_data_arr->user = projectfunc::getuserdata($parent, $res_user->userid);
                    $user_data_arr->rank = '0';
                    $user_data_arr->tot_user_score = (count($get_time_arr) > 0) ? $res_user->user_score : '-';
                    $user_data_arr->percentage_score = (count($get_time_arr) > 0) ? '0' : '-';

                    $user_data_arr->total_time = $res_user->total_time;
                    $user_data_arr->last_time = (count($get_time_arr) > 0) ? $res_user->last_modified : '-';

                    //GET EACH USER COND
                    $getuser_data = projectfunc::getuserdata($parent, $res_user->userid);
                    if ($getuser_data->role) {
                        $user_cond[] = " (a.filter_role LIKE '%" . $getuser_data->role . "%' OR a.filter_role='')";
                    }
                    if ($getuser_data->zone) {
                        $user_cond[] = " (a.filter_zone LIKE '%" . $getuser_data->zone . "%' OR a.filter_zone='')";
                    }
                    if ($getuser_data->city) {
                        $user_cond[] = " (a.filter_region LIKE '%" . $getuser_data->city . "%' OR a.filter_region='')";
                    }
                    if ($getuser_data->dealer_code) {
                        $user_cond[] = " (a.filter_dealer LIKE '%" . $getuser_data->dealer_code . "%'  OR a.filter_dealer='')";
                    }
                    $assess_user_cond = " AND " . @implode(' AND ', $user_cond);

                    $count_assessment_str = "SELECT COUNT(a.assessmentid) as acount FROM `" . TABLE_ASSESSMENT . "` as a  WHERE a.status='1' AND a.atype='0' AND a.assettypeid='1'  AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59' AND a.enddate>='" . $user_data_arr->user->user_created_on . "' AND  assessmentid IN (" . @implode(',', $assessmentid_arr) . ") " . $assess_user_cond . " LIMIT 1";
                    $count_assessment_arr = $parent->getQueryValue($count_assessment_str);
                    $user_data_arr->percentage_score = (count($assessment_arr) > 0) ? round($res_user->user_score / $count_assessment_arr[0]->acount) : '0';
                    $user_data_arr->percentage = $user_data_arr->percentage_score;

                    if ((($assessment_count / $count_assessment_arr[0]->acount) * 100) >= MIN_ASSESSMENT_PERCENT) {
                        $data_arr[] = $user_data_arr;
                    }
                }
            }

            if (count($data_arr) > 0) {
                array_multisort(array_column($data_arr, 'percentage_score'), SORT_DESC, array_column($rank_arr, 'total_time'), SORT_ASC, $rank_arr);
                $rank = 1;
                foreach ($data_arr as $key => $res) {
                    $data_arr[$key]->rank = $rank;
                    $rank++;
                }
            }

            if (count($data_arr) > 0) {
                $outof = count($data_arr);
                //$user_count_arr=$parent->getQueryValue($get_user_count_str);
                //$outof=(count($user_count_arr)>0)?$user_count_arr[0]->user_count:'0';
                foreach ($data_arr as $res_user) {
                    $i = 0;
                    $row++;
                    if (empty($res_user->smpin)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->smpin);
                    }
                    if (empty($res_user->fname)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->fname . " " . $res_user->user->lname);
                    }
                    if (empty($res_user->mobileno)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->mobileno);
                    }
                    if (empty($res_user->dealer_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->dealer_name);
                    }
                    if (empty($res_user->city)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->city);
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->percentage_score . "%");
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rank);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $outof);
                    if (empty($res_user->zm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->zm_name);
                    }
                    if (empty($res_user->rm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->rm_name);
                    }
                    if (empty($res_user->am_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->am_name);
                    }
                }
            }
        }

        //LOG
        $logstr = 'ASSESSMENT LEADERBOARD REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "REGISTRATION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":I" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Status', 'Registered On', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');

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

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

            case '3': //HO Manager
                $user_cond .= "";
                break;
            case '4': //Zonal Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '5': //Trainer
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.emailid='" . $emailid . "' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                break;
            case '6': //Regional Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '7': //Area Manager
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "' " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[0]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT  e.`smpin`, e.`emp_code`,e. `fname`,e.`lname`,e.mobile,e.emp_code,u.userid,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name,l.created_on as register_date,u.userid 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 LEFT JOIN (SELECT * FROM " . TABLE_USER_LOG . " WHERE module='REGISTER' )as l ON u.userid=l.userid  WHERE e.status='1' AND e.smpin!='' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                $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->mobile);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                $status = ($res_user->userid) ? 'Active' : '';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status);
                $register_date = ($res_user->register_date) ? date('d-m-Y', strtotime($res_user->register_date)) : '-';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $register_date);
                $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);
            }
        }

        //LOG
        $logstr = 'REGISTRATION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Overall Progress %', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');

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

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

            case '3': //HO Manager
                $user_cond .= "";
                break;
            case '4': //Zonal Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '5': //Trainer
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.emailid='" . $emailid . "' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                break;
            case '6': //Regional Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                break;
            case '7': //Area Manager
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "' " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[0]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE u.status='1' AND u.is_deleted='0' AND u.role>7 AND  u.role<14 " . $user_cond . $order_by_cond . $limit_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }
                $content_arr = projectfunc::userCourseCompleteStatus($parent, $res_user->userid);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// SALES TARGET REPORT EXPORT //////////////////////////////////////////////// */
    private function salestargetreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 3);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES TARGET REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Sales Target', 'ZM', 'RM', 'AM');

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');

        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');

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

        //AREA MANAGER SELF
        if ($parent->current_user_role == '7') {
            $role = $parent->current_user_role;
            $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
            $emailid = $userdata->emailid;
        } else {
            //AREA MANAGER LEVEL REPORT
            $role = '7';
            //FIELDS VALIDATION
            $REQUEST_KEYS = array('area' => 'string');
            globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
            $area_manager = escape_str($parent->_request["area"]);
            $emailid = $area_manager;
        }
        $user_cond = "";
        switch ($role) {

            case '3': //HO Manager
                $user_cond .= "";
                break;
            case '4': //Zonal Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "' AND status='1' AND is_deleted='0')";
                break;
            case '5': //Trainer
                $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.emailid='" . $emailid . "' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                break;
            case '6': //Regional Manager
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "' AND status='1' AND is_deleted='0')";
                break;
            case '7': //Area Manager
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " AND dealer_code='" . $parent->current_user_data->dealer_code . "'";
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE am_email='" . $emailid . "' " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[2]);

        //SORT
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT  e.`smpin`, e.`emp_code`,e. `fname`,e.`lname`,e.emp_code,u.userid,e.mobileno,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name  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.status='1' AND e.smpin!='' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }
                //$content_arr=projectfunc::userCourseCompleteStatus($parent,$res_user->userid);
                //$percentage=($content_arr['total']>0)?round(($content_arr['completed']/$content_arr['total'])*100):'0';
                $user_target = projectfunc::userSmpinSalesTarget($parent, $res_user->smpin, '', $year_data, $month_data);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_target);
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'SALES TARGET EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// SURVEY REPORT EXPORT //////////////////////////////////////////////// */
    private function surveyuserreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('GET'));

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

        //FIELDS VALIDATION
        globalfunc::checkParamCount($parent, 3);
        $assessmentid = escape_str($parent->params[1]);
        $userid = escape_str($parent->params[2]);

        $query_str = "SELECT assessmentid,assessmentname  FROM " . TABLE_ASSESSMENT . " WHERE status='1'  AND  assettypeid='2' AND  assessmentid='" . $assessmentid . "' AND  publisheddate<='" . NOW . "' LIMIT 1";
        $query_arr = $parent->getQueryValue($query_str);
        if (!count($query_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error9"]), 406);
        }

        //GET USER ATTENTED
        $query_assess_str = "SELECT *  FROM " . TABLE_ASSESSMENT_USER . " WHERE userid='" . $userid . "' AND assessmentid='" . $assessmentid . "' AND highscorearid!='0' LIMIT 1";
        $query_assess_arr = $parent->getQueryValue($query_assess_str);
        if (!count($query_assess_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error10"]), 406);
        }

        //GET USER RESULT
        $query_res_str = "SELECT au.*  FROM " . TABLE_ASSESSMENT_USERRESULT . " as au JOIN " . TABLE_ASSESSMENTRESULT . " as ar ON au.arid=ar.arid WHERE au.userid='" . $userid . "' AND au.assessmentid='" . $assessmentid . "' AND au.arid='" . $query_assess_arr[0]->highscorearid . "' GROUP BY au.arid,au.aqid";
        $query_assess_res_arr = $parent->getQueryValue($query_res_str);
        if (!count($query_assess_res_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error10"]), 406);
        }

        //GET USER
        $user_data = projectfunc::getuserdata($parent, $userid);

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FEEDBACK REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(12);
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Name : " . $user_data->fname . " " . $user_data->lname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $objPHPExcel->getActiveSheet()->getStyle("D" . $row)->getFont()->setSize(12);
        $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, "SMPIN : " . $user_data->smpin);
        $objPHPExcel->getActiveSheet()->getStyle("D" . $row)->getFont()->setBold(true);

        $dealer = (isset($user_data->dealer) && count($user_data->dealer) > 0) ? $user_data->dealer->dealer_name . " " . $user_data->dealer->dealer_code : '';

        $row += 1;
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setSize(12);
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Dealer : " . $dealer);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $objPHPExcel->getActiveSheet()->getStyle("D" . $row)->getFont()->setSize(12);
        $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, "Date : " . date('d-m-Y h:i A', strtotime($query_assess_res_arr[0]->created_on)));
        $objPHPExcel->getActiveSheet()->getStyle("D" . $row)->getFont()->setBold(true);

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

        //SET HEADER
        $header_arr = array('Sno', 'Question', "Answer");
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('25');

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

        $survey_str = "SELECT * FROM " . TABLE_SURVEY_RATING . " ORDER BY id ASC";
        $survey_arr = $parent->getQueryValue($survey_str);

        if (count($survey_arr) > 0) {
            foreach ($survey_arr as $skey => $res_survey) {
                $rating_arr[$res_survey->id] = $res_survey->rating_text;
            }
        }

        if (count($query_assess_res_arr) > 0) {
            $sno = 1;
            foreach ($query_assess_res_arr as $res) {
                $ques_str = "SELECT q.id as qid, q.`questiontypeid`, q.`questiontext`, q.`qimage`, q.`qvideo`  FROM " . TABLE_QUESTION . " as q WHERE id='" . $res->qid . "' LIMIT 1";
                $ques_arr = $parent->getQueryValue($ques_str);

                if (count($ques_arr) > 0) {
                    //GET USER ANSWERED OPTIONS
                    $user_answer_str = "SELECT * FROM " . TABLE_ASSESSMENT_USERRESULT . "  WHERE userid='" . $userid . "' AND arid='" . $query_assess_arr[0]->highscorearid . "' AND aqid='" . $res->aqid . "'";
                    $user_answer_arr = $parent->getQueryValue($user_answer_str);
                    $ques_arr[0]->answer = array();
                    if (count($user_answer_arr) > 0) {

                        foreach ($user_answer_arr as $res_answer) {
                            //GET RATING
                            $ques_arr[0]->survey_text = '';
                            if ($ques_arr[0]->questiontypeid == '3') {
                                if ($res_answer->survey_rating == $res_survey->score) {
                                    $rating = $res_answer->survey_rating;
                                    $ques_arr[0]->answer[] = $rating_arr[$rating];
                                }

                            } elseif ($ques_arr[0]->questiontypeid == '5' || $ques_arr[0]->questiontypeid == '6') {
                                //GET ALL QUESTION OPTIONS
                                if ($res_answer->choiseid) {
                                    $ques_option_str = "SELECT op.optid,op.opttext,'0' as selected  FROM " . TABLE_QUESTIONOPTION . " as op WHERE op.qid='" . $res->qid . "' AND op.optid='" . $res_answer->choiseid . "' LIMIT 1";
                                    $ques_option_arr = $parent->getQueryValue($ques_option_str);

                                    if (count($ques_option_arr) > 0) {
                                        $ques_arr[0]->answer[] = $ques_option_arr[0]->opttext;
                                    }
                                }
                            } else {
                                $ques_arr[0]->answer[] = ($res_answer->survey_text != '') ? $res_answer->survey_text : '';
                            }
                        }
                    }

                    $i = 0;
                    $row++;
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $ques_arr[0]->questiontext);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, @implode(',', $ques_arr[0]->answer));

                }

            }
        }

        //LOG
        $logstr = 'FEEDBACK REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// MANAGER TARGET SAMPLE //////////////////////////////////////////////// */
    private function managertargetsample($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        switch ($parent->current_user_role) {
            case "14": //Dealer
            case "7": //Area Manager
            case "6": //Regional Manager
            case "5": //Trainer
            case "4": //Zonal Manager
            case "3": //HO Manager

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //GET ALL MODELS
        $query_str = "SELECT *  FROM " . TABLE_MODEL . " WHERE model_is_target='1' AND user_type='0'  ORDER BY model_id";
        $query_arr = $parent->getQueryValue($query_str);
        $column_name = getColumnFromNumber(count($query_arr));

        //EXCEL HEADER
        $row = 1;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "TARGET SALES");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":" . strtoupper($column_name) . $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(12);

        $REQUEST_KEYS = array('area' => 'string');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $area_manager = escape_str($parent->_request["area"]);
        if ($parent->current_user_role == '14') {
            $dealer_code = $parent->current_user_data->dealer_code;
            $dealer_code_cond = " dealer_code='" . $dealer_code . "'";
            $query_user_str = "SELECT * FROM `" . DMS_DEALER . "` WHERE  " . $dealer_code_cond . "  AND status='1' AND is_deleted='0'  LIMIT 1";
        } else {
            $query_user_str = "SELECT * FROM `" . DMS_DEALER . "` WHERE am_email='" . $area_manager . "' " . $dealer_code_cond . "  AND status='1' AND is_deleted='0'  LIMIT 1";
        }

        $query_user_arr = $parent->getQueryValue($query_user_str);
        if (count($query_user_arr) > 0) {
            $area_manger_str = $query_user_arr[0]->am_name . ", " . $query_user_arr[0]->am_mobile . " - " . $query_user_arr[0]->am_email;
        }

        $row++;
        if ($parent->current_user_role == '14') {
            $area_manger_str .= " Dealer : " . $query_user_arr[0]->dealer_name . " " . $query_user_arr[0]->dealer_code;
        }
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "AREA MANAGER : " . $area_manger_str);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":" . strtoupper($column_name) . $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(12);

        $row++;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row);
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('30');

        //SET HEADER
        $header_arr = array('SMPIN', "NAME", "MOBILE", "DEALER");
        if (count($query_arr) > 0) {
            $i = 4;
            foreach ($query_arr as $res_val) {
                $header_arr[] = strtoupper(trim($res_val->model_name));
                $column_name = getColumnFromNumber($i++);
                $column_row = $column_name . $row;
                $bordercells_arr[] = $column_row;
                $objPHPExcel->getActiveSheet()->getColumnDimension($column_name)->setWidth('20');
            }
        }

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

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

        if ($parent->current_user_role == '14') {
            $dealer_code = $parent->current_user_data->dealer_code;
            $dealer_code_cond = " AND e.dealer_code='" . $dealer_code . "'";
        }

        //GET ALL SMPIN OF
        $query_user_str = "SELECT e.smpin,e.fname,e.lname,e.mobile,e.dealer_code,d.dealer_name FROM " . DMS_EMPLOYEE . " e JOIN " . TABLE_USER . " u ON u.smpin=e.smpin AND u.role>7 JOIN (SELECT dealer_code,dealer_name FROM " . DMS_DEALER . " WHERE am_email='" . $area_manager . "'  AND status='1' AND is_deleted='0' )as d ON d.dealer_code=e.dealer_code WHERE e.status='1' " . $dealer_code_cond;
        $query_user_arr = $parent->getQueryValue($query_user_str);
        if (count($query_user_arr) > 0) {
            foreach ($query_user_arr as $res_user) {
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, $res_user->smpin);
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                $objPHPExcel->getActiveSheet()->SetCellValue('B' . $row, $res_user->fname . " " . $res_user->lname);

                $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, $res_user->mobile);
                $objPHPExcel->getActiveSheet()->getStyle('C' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);

                $objPHPExcel->getActiveSheet()->SetCellValue('D' . $row, $res_user->dealer_code . " - " . $res_user->dealer_name);
                $objPHPExcel->getActiveSheet()->getStyle('D' . $row)->getAlignment()->setWrapText(true);
            }
        }

        //LOG
        $logstr = 'SAMPLE SALES TARGET';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

    /* /////////////////////////////// NEW REGISTRATION REPORT EXPORT //////////////////////////////////////////////// */
    private function newregistrationreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "REGISTRATION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":I" . $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 += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);
        $i = 0;
        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', "Segment", 'Dealer Code', 'Dealer Name', 'Status', 'Registered On', 'ZM', 'RM', 'AM');
        $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('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30'); // SMPIN

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

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

        $user_cond = "";
        //MANAGER WISE DEALER CONDITION
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND d.status='1' AND d.is_deleted='0' AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY d.dealer_name,e.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,e.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,e.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,e.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,e.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,e.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,e.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }
        $user_cond .= " AND e.is_current='1' ";
        //AND u.status=1
        //$get_user_str="SELECT  e.`smpin`, e.`emp_code`,e. `fname`,e.`lname`,e.mobile,e.emp_code,u.userid,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name,l.created_on as register_date,u.userid 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 LEFT JOIN (SELECT * FROM ".TABLE_USER_LOG." WHERE module='REGISTER' )as l ON u.userid=l.userid  WHERE e.status='1' AND u.is_deleted=0 AND e.smpin!='' ".$user_cond.$order_by_cond;
        $get_user_str = "SELECT  e.`smpin`, e.`emp_code`,e. `fname`,e.`lname`,e.mobile,e.emp_code,e.segment,u.userid,d.city,d.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name,u.created_on as register_date,u.userid,u.status 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' AND e.smpin!='' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin); // SMPIN
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);

                if (empty($res_user->mobile)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                }
                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()->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);

                if ($res_user->userid > 0) {
                    $status = ($res_user->status == '1') ? 'Active' : 'Inactive';
                } else {
                    $status = "Not registered";
                }
                if (empty($status)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $status);
                }
                $register_date = ($res_user->register_date) ? date('d-m-Y', strtotime($res_user->register_date)) : '-';
                if ($register_date == '-') {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $register_date);
                }
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
            //projectfunc::highlighRows($bordercells_arr,$objPHPExcel);
        }

        //LOG
        $logstr = 'REGISTRATION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// NEW COURSE REPORT EXPORT //////////////////////////////////////////////// */
    private function newcoursereportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row, "J" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Segment', 'Mobile', 'Dealer Code', 'Dealer Name', 'Overall Progress %', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //seg
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //mob
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //dealer
        $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);
        }

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

        $user_cond = "";
        //MANAGER WISE DEALER CONDITION
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND d.status='1' AND d.is_deleted='0' AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        $user_cond .= " AND e.is_current='1' ";
        // u.status='1' AND
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,u.segment,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name,e.segment FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE u.is_deleted='0' AND u.role>7 AND  u.role<14 " . $user_cond . $order_by_cond . $limit_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $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);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $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);

                $content_arr = projectfunc::userSegmentCourseCompleteStatus($parent, $res_user->userid, $res_user->segment);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// NEW SALES REPORT EXPORT //////////////////////////////////////////////// */
    private function newsalesreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //$limit_str=globalfunc::setPageLimit($parent,$parent->params[2]);
        //$limit_cond=($limit_str)?" LIMIT ".$limit_str:'';

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Month & Year: " . date('M Y', strtotime($year_data . "-" . $month_data . "-01")));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":C" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES REPORT");
        $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);

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

        //SET HEADER
        //$header_arr=array('SMPIN', 'DSE Name','Mobile','Dealer Name','Location','Target','Achieved','Percentage');
        $header_arr = array('SMPIN', 'DSE Name', 'Segment', "Mobile", 'Dealer Code', 'Dealer Name', 'Target', 'Achieved', 'Percentage');
        //GET MODEL
        $query_modelarr = $parent->selectQuery(TABLE_MODEL, "model_id, model_name,segment", "model_is_target=1 AND user_type=0 ORDER BY model_id", "");
        if ($query_modelarr['nr']) {
            foreach ($query_modelarr['result'] as $res_model) {
                //$header_arr[]=$res_model->model_name." (Achieved/Target)";
                $header_arr[] = $res_model->model_name . " \n Target";
                $header_arr[] = $res_model->model_name . " \n Achieved";
                $header_arr[] = $res_model->model_name . " \n %";

            }
        }

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

        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //smpin
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //segm
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //mo
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //dealer
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20'); //dealer name

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

        if ($query_modelarr['nr']) {
            foreach ($query_modelarr['result'] as $res_model) {
                $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('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);
            $m = $i - 1;
            $bordercells_arr[] = getColumnFromNumber($m) . $row;
        }
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

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

        //MANAGER WISE DEALER CONDITION
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND d.status='1' AND d.is_deleted='0' AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[3]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        //GET ROLE
        $role_data = array();
        $role_arr = $parent->getQueryValue("SELECT rolename FROM " . TABLE_ROLE . " WHERE status=1 AND id>7 AND id<14 ");
        if (count($role_arr) > 0) {
            foreach ($role_arr as $res) {
                $role_data[] = "'" . $res->rolename . "'";
            }
        } else {
            $role_data[] = '';
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }
        //   u.status='1' AND
        $is_history = (isset($parent->_request["is_history"]) && $parent->_request["is_history"] == 1) ? "1" : "0";
        if ($is_history == 0) {
            $user_cond .= " AND e.is_current='1' AND e.status=1 ";
        } else {
            $user_cond .= " AND e.status=1 ";
        }

        $get_user_str = "SELECT e.fname,e.lname,e.smpin,e.emp_code,e.mobile,e.mobile AS mobileno,e.segment,d.city,e.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . DMS_EMPLOYEE . " e LEFT JOIN " . TABLE_USER . " u ON e.smpin=u.smpin LEFT JOIN " . DMS_DEALER . " d ON e.dealer_code=d.dealer_code  WHERE e.role IN (" . @implode(',', $role_data) . ")  " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //'SMPIN', 'DSE Name','Segment',"Mobile",'Dealer Code','Dealer Name','Target','Achieved','Percentage'
                //TARGET
                $user_target = projectfunc::userSmpinSalesTarget($parent, $res_user->smpin, '', $year_data, $month_data);
                //ACHIEVED
                $user_achieved = projectfunc::userSmpinSalesAchieved($parent, $res_user->smpin, '', $year_data, $month_data);
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $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);
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $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, $user_target);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_achieved);
                $percentage = ($user_target > 0) ? round(($user_achieved / $user_target) * 100) : 0;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage);

                if ($query_modelarr['nr']) {
                    foreach ($query_modelarr['result'] as $res_model) {

                        $model_user_target = projectfunc::userSmpinSalesTarget($parent, $res_user->smpin, $res_model->model_name, $year_data, $month_data);
                        //ACHIEVED
                        $model_user_achieved = projectfunc::userSmpinSalesAchieved($parent, $res_user->smpin, $res_model->model_name, $year_data, $month_data);

                        //$objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,$model_user_achieved."/".$model_user_target);

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

                        $modelpercentage = ($model_user_target > 0) ? round(($model_user_achieved / $model_user_target) * 100) : 0;
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $modelpercentage);

                    }
                }

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

        //LOG
        $logstr = 'MANAGER SALES REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// NEW CERTIFIED DSE REPORT EXPORT //////////////////////////////////////////////// */
    private function newcertifieddsereportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "CERTIFIED DSE 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 += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        //$header_arr=array('SMPIN','DSE Name','Mobile','Dealer Name','Location','Certification','ZM','RM','AM');
        $header_arr = array('SMPIN', 'DSE Name', "Segment", 'Mobile', 'Dealer Code', 'Dealer Name', 'Base', 'Bronze', 'Bronze Level 2 Status', 'Silver Level 1 Status', 'Silver Level 2 Status', 'Gold Level 1 Status', 'Gold Level 2 Status', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');

        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('15');

        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth('15');

        $objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth('30');

        $i = 0;
        foreach ($header_arr as $val) {
//            $i=($i=='6')?'9':$i;
            //            if($i==5)
            //            {
            //                $objPHPExcel->getActiveSheet()->mergeCells("F".$row.":I".$row);
            //                $objPHPExcel->getActiveSheet()->getStyle("F".$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
            //            }
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
        }

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

        $user_cond = "";
        //MANAGER WISE DEALER CONDITION
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND d.status='1' AND d.is_deleted='0' AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }
        $user_cond .= " AND e.is_current='1' ";
        //u.status='1' ANDu
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,u.segment,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name FROM " . TABLE_USER . " u LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE  u.is_deleted='0' AND u.role>7 AND u.role<14 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                $learning_arr = projectfunc::userLearningLevel($parent, $res_user->userid);
//0-Not Started Grey
                //1-Current Orange
                //2-Failed Red,
                //3-Completed Green
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $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);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                //'SMPIN','DSE Name',"Segment",'Mobile','Dealer Code','Dealer Name','Base','Bronze','Bronze Level 2 Status','Silver Level 1 Status','Silver Level 2 Status','Gold Level 1 Status','Gold Level 2 Status','ZM','RM','AM'
                $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);

                switch ($learning_arr['basic']) {
                    case "0":$base_lvl1_str = 'Not Started';
                        $base_lvl2_str = 'Not Started';
                        break;
                    case "6":$base_lvl1_str = 'Failed';
                        $base_lvl2_str = 'Not Started';
                        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';
                        break;
                    case "3":$base_lvl1_str = 'Completed';
                        $base_lvl2_str = 'Completed';
                        break;
                    default:$base_lvl1_str = 'Not Started';
                        $base_lvl2_str = 'Not Started';
                        break;
                }
                switch ($learning_arr['bronze']) {
                    case "0":$bronze_lvl1_str = 'Not Started';
                        $bronze_lvl2_str = 'Not Started';
                        break;
                    case "6":$bronze_lvl1_str = 'Failed';
                        $bronze_lvl2_str = 'Not Started';
                        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';
                        break;
                    case "3":$bronze_lvl1_str = 'Completed';
                        $bronze_lvl2_str = 'Completed';
                        break;
                    default:$bronze_lvl1_str = 'Not Started';
                        $bronze_lvl2_str = 'Not Started';
                        break;
                }
                switch ($learning_arr['silver']) {
                    case "0":$silver_lvl1_str = 'Not Started';
                        $silver_lvl2_str = 'Not Started';
                        break;
                    case "6":$silver_lvl1_str = 'Failed';
                        $silver_lvl2_str = 'Not Started';
                        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';
                        break;
                    case "3":$silver_lvl1_str = 'Completed';
                        $silver_lvl2_str = 'Completed';
                        break;
                    default:$silver_lvl1_str = 'Not Started';
                        $silver_lvl2_str = 'Not Started';
                        break;
                }
                switch ($learning_arr['gold']) {
                    case "0":$gold_lvl1_str = 'Not Started';
                        $gold_lvl2_str = 'Not Started';
                        break;
                    case "6":$gold_lvl1_str = 'Failed';
                        $gold_lvl2_str = 'Not Started';
                        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';
                        break;
                    case "3":$gold_lvl1_str = 'Completed';
                        $gold_lvl2_str = 'Completed';
                        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()->getStyle(getColumnFromNumber($i).$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setBold(true);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent,$learning_arr['basic']));
                //                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,'B');
                //
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setBold(true);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent,$learning_arr['bronze']));
                //                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,'B');
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setBold(true);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent,$learning_arr['silver']));
                //                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,'S');
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setBold(true);
                //                $objPHPExcel->getActiveSheet()->getStyle(getColumnFromNumber($i).$row)->getFont()->setSize(14)->getColor()->setRGB($this->certifieddsecolor($parent,$learning_arr['gold']));
                //                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++).$row,'G');

                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'CERTIFIED DSE REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ASSESSMENT REPORT EXPORT //////////////////////////////////////////////// */
    private function newassessmentreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6', '7', '14'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $assessment_str = "SELECT assessmentname,pass_score FROM " . TABLE_ASSESSMENT . "  WHERE assessmentid='" . $assessmentid . "'   LIMIT 1";
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessmentname = (count($assessment_arr) > 0) ? $assessment_arr[0]->assessmentname : '';
        $pass_score = $assessment_arr[0]->pass_score;

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Assessment : " . $assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Passing Percentage : " . $pass_score . "%");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT 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 += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', "Segment", 'Mobile', 'Dealer Code', 'Dealer Name', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result', 'ZM', 'RM', 'AM', 'Zone');
        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //seg
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15'); //mon
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10'); //score
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $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('15');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";

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

        $user_cond = "";
        $zm_email = escape_str($parent->_request["zmemail"]);
        $rm_email = escape_str($parent->_request["rmemail"]);
        $am_email = escape_str($parent->_request["amemail"]);

        //MANAGER WISE DEALER CONDITION
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND d.status='1' AND d.is_deleted='0' AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY au.highscore DESC,au.modified_on ASC,d.dealer_name,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR  d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }
        $dse_cond = (isset($parent->_request["is_idse"]) && $parent->_request["is_idse"] !== '1') ? " AND u.role='15'" : "AND u.role>7 AND u.role<14";
        $user_cond .= " AND e.is_current='1' ";
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.smpin,u.emp_code,u.mobileno,d.city,u.dealer_code,u.segment,d.dealer_name,d.zm_name,d.rm_name,d.am_name,d.zone FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid LEFT JOIN " . DMS_EMPLOYEE . " e ON u.smpin=e.smpin LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code  WHERE  u.is_deleted='0' " . $dse_cond . " AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $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);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $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);

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");
                // $header_arr=array('SMPIN','DSE Name','Mobile','Dealer Name','Location','Score','Timetaken in Minutes','Result','Completed Time','ZM','RM','AM','Zone');
                $timetaken = '';
                $completedtime = '';
                if (count($exam_highscore_arr) > 0) {
                    $exam_result_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENTRESULT . "  WHERE assessmentid='" . $assessmentid . "' AND arid='" . $exam_highscore_arr[0]->highscorearid . "' LIMIT 1";
                    $exam_result_highscore_arr = $parent->getQueryValue($exam_result_highscore_str);

                    $timetaken = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->time_taken)) ? round($exam_result_highscore_arr[0]->time_taken / 60) : "0";
                    $completedtime = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->modified_on)) ? date('d-m-Y h:i A', strtotime($exam_result_highscore_arr[0]->modified_on)) : "-";
                }

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

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

                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
                if (empty($res_user->zone)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zone);
                }
            }
        }

        //LOG
        $logstr = 'ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

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

        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// NEW SALES TARGET REPORT EXPORT //////////////////////////////////////////////// */
    private function newsalestargetreportexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES TARGET REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row, "I" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Sales Target', 'ZM', 'RM', 'AM');

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');

        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');

        $i = 0;
        foreach ($header_arr as $val) {
            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $val);
        }
        $dealer_code_cond = '';
        $qry_params = explode("?", $parent->params[3]);
        $type = strtolower($qry_params[0]);

        $user_cond = "";

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

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

                        $emailid = escape_str($parent->_request["rmemail"]);
                        $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";

                        if ($emailid == 'all') {
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE  " . $zone_cond . " AND  status=1 AND is_deleted=0)";
                        } else {
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "' AND " . $zone_cond . "  AND  status=1 AND is_deleted=0)";
                        }
                    } else {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

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

                            $emailid = escape_str($parent->_request["zmemail"]);
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE zm_email='" . $emailid . "' AND  status=1 AND is_deleted=0)";
                        } else {
                            //ZM EMAIL
                            $zmemail = escape_str($parent->_request["zmemail"]);
                            $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND zm_email='" . $zmemail . "'" : '';
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "' " . $zmcond . " AND  status=1 AND is_deleted=0)";
                        }
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "am":
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " dealer_code='" . $parent->current_user_data->dealer_code . "'";
                } else {
                    $REQUEST_KEYS = array('amemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = escape_str($parent->_request["amemail"]);
                    if ($emailid == 'all') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $emailid = escape_str($parent->_request["rmemail"]);
                        $dealer_code_cond = " rm_email='" . $emailid . "'";

                    } else {
                        $dealer_code_cond = "  am_email='" . $emailid . "'";
                    }
                    if ($parent->current_user_role != '5') {
                        //ZM EMAIL
                        $zmemail = escape_str($parent->_request["zmemail"]);
                        $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND zm_email='" . $zmemail . "'" : '';
                        $dealer_code_cond .= $zmcond;
                    }
                    //RM EMAIL
                    $rmemail = escape_str($parent->_request["rmemail"]);
                    $rmcond = ($rmemail != 'all' && $rmemail != '') ? " AND rm_email='" . $rmemail . "'" : '';
                    $dealer_code_cond .= $rmcond;
                    //TRAINER
                    if ($parent->current_user_role == '5') {
                        $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $dealer_code_cond .= (count($trainer_arr) > 0) ? "AND zone='" . $trainer_arr[0]->zone . "'" : "AND zone='0'";
                    }
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE  " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[3]);

        //SORT
        $order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Location
            //3-AM
            //4-RM
            //5-ZM
            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;
                case '2':$order_by_cond = "ORDER BY d.city,u.fname ASC ";
                    break;
                case '3':$order_by_cond = "ORDER BY d.am_email,u.fname ASC ";
                    break;
                case '4':$order_by_cond = "ORDER BY d.rm_email,u.fname ASC ";
                    break;
                case '5':$order_by_cond = "ORDER BY d.zm_email,u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY d.dealer_name,u.fname ASC ";
                    break;

            }
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (e.fname LIKE '%" . $search_txt . "%' OR e.lname LIKE '%" . $search_txt . "%' OR e.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }
        $user_cond .= " AND e.is_current='1' ";

        $get_user_str = "SELECT  e.`smpin`, e.`emp_code`,e. `fname`,e.`lname`,e.emp_code,e.mobile,u.userid,d.city,u.dealer_code,d.dealer_name,d.zm_name,d.rm_name,d.am_name  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.status='1' AND e.smpin!='' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobile)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobile);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }
                //$content_arr=projectfunc::userCourseCompleteStatus($parent,$res_user->userid);
                //$percentage=($content_arr['total']>0)?round(($content_arr['completed']/$content_arr['total'])*100):'0';
                $user_target = projectfunc::userSmpinSalesTarget($parent, $res_user->smpin, '', $year_data, $month_data);

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_target);
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'SALES TARGET EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// SALES LEADERBOARD REPORT EXPORT //////////////////////////////////////////////// */
    private function newsalesleaderboardexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 4);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

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

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

        $dealer_cond = "";

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

            case "rm":
                if ($parent->current_user_role < '7') {
                    if ($parent->current_user_role == '5') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $zone_cond .= (count($trainer_arr) > 0) ? " d.zone='" . $trainer_arr[0]->zone . "'" : " d.zone='0'";
                        $emailid = escape_str($parent->_request["rmemail"]);
                        if ($emailid == 'all') {

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

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

                            $emailid = escape_str($parent->_request["zmemail"]);
                            //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE zm_email='". $emailid."')";
                            $dealer_cond .= " AND d.zm_email='" . $emailid . "'";
                        } else {
                            //ZM EMAIL
                            $zmemail = escape_str($parent->_request["zmemail"]);
                            $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';
                            //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE rm_email='".$emailid."')";
                            $dealer_cond .= " AND d.rm_email='" . $emailid . "' " . $zmcond . "";
                        }
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "am":
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " d.dealer_code='" . $parent->current_user_data->dealer_code . "'";
                } else {
                    $REQUEST_KEYS = array('amemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = escape_str($parent->_request["amemail"]);
                    if ($emailid == 'all') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $emailid = escape_str($parent->_request["rmemail"]);
                        $dealer_code_cond = " d.rm_email='" . $emailid . "'";

                    } else {
                        $dealer_code_cond = "  d.am_email='" . $emailid . "'";
                    }
                    if ($parent->current_user_role != '5') {
                        //ZM EMAIL
                        $zmemail = escape_str($parent->_request["zmemail"]);
                        $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';
                        $dealer_code_cond .= $zmcond;
                    }
                    //RM EMAIL
                    $rmemail = escape_str($parent->_request["rmemail"]);
                    $rmcond = ($rmemail != 'all' && $rmemail != '') ? " AND d.rm_email='" . $rmemail . "'" : '';
                    $dealer_code_cond .= $rmcond;
                    //TRAINER
                    if ($parent->current_user_role == '5') {
                        $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $dealer_code_cond .= (count($trainer_arr) > 0) ? "AND d.zone='" . $trainer_arr[0]->zone . "'" : "AND d.zone='0'";
                    }
                }
                //$user_cond.=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE  ".$dealer_code_cond.")";
                $dealer_cond .= " AND " . $dealer_code_cond;
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Month & Year: " . date('M Y', strtotime($year_data . "-" . $month_data . "-01")));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":D" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "SALES LEADERBOARD REPORT");
        $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 += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Sales', 'Sales Target', 'Sales Achvt', 'Rank', 'OutOf', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth('30');

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

        $user_cond = "";
        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[2]);
        $order_by_cond = " ORDER BY f.rank,d.dealer_name ASC";

        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //1-Dealer
            //2-Rank

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = " ORDER BY d.dealer_name,f.rank ASC ";
                    break;
                case '2':$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
                default:$order_by_cond = " ORDER BY f.rank,d.dealer_name ASC ";
                    break;
            }
        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR  d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        if ($parent->current_user_role == '14') {
            $user_cond .= " AND u.dealer_code='" . $dealer_code . "'";
        }

        //GET MONTH RANGE
        $start_date = date('Y-m-d', strtotime($year_data . "-" . $month_data . "-01"));
        $end_date = date('Y-m-t', strtotime($year_data . "-" . $month_data . "-01"));

        $parent->executeQuery("SET @rownum := 0");
        $get_user_str = "SELECT f.*,u.fname,u.lname,u.mobileno,d.* FROM (SELECT @rownum := @rownum + 1 AS rank, salecount,targetcount,smpin,userid,last_sale,first_sale,sale_per FROM (SELECT c.*, CASE WHEN targetcount=0 THEN 0 ELSE (salecount/targetcount)*100 END as 'sale_per' FROM ( SELECT a.userid,a.smpin,a.salecount,b.targetcount,a.last_sale,a.first_sale FROM (SELECT COALESCE(COUNT(s.id),'0') as scount, COALESCE(SUM(s.sales_count),'0') as salecount, u.smpin,u.userid,MAX(s.sale_date) last_sale,MIN(s.sale_date) first_sale FROM " . TABLE_USER . " as u LEFT JOIN " . DMS_EMPLOYEE . " e  ON u.smpin=e.smpin  JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $start_date . "' AND sale_date<='" . $end_date . "') as s ON s.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND e.is_current='1' " . $dealer_cond . "  GROUP BY u.smpin ) as a JOIN  (SELECT  COALESCE(SUM(t.target),'0') as targetcount,u.smpin,u.userid FROM " . TABLE_USER . " as u LEFT JOIN " . DMS_EMPLOYEE . " e  ON u.smpin=e.smpin  JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $start_date . "' AND sale_month<='" . $end_date . "')as t ON t.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14 AND e.is_current='1' " . $dealer_cond . "  GROUP BY u.smpin ) as b ON a.userid=b.userid )c ORDER BY cast(sale_per as SIGNED) DESC,cast(salecount as SIGNED) DESC,cast(targetcount as SIGNED) DESC,last_sale ASC,first_sale ASC,userid ASC)as a )as f JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        $get_user_count_str = "SELECT COUNT(u.userid)as user_count FROM (SELECT  salecount,targetcount,smpin,userid,last_sale,first_sale,sale_per FROM (SELECT c.*, CASE WHEN targetcount=0 THEN 0 ELSE (salecount/targetcount)*100 END as 'sale_per' FROM ( SELECT a.userid,a.smpin,a.salecount,b.targetcount,a.last_sale,a.first_sale FROM (SELECT COALESCE(COUNT(s.id),'0') as scount, COALESCE(SUM(s.sales_count),'0') as salecount, u.smpin,u.userid,MAX(s.sale_date) last_sale,MIN(s.sale_date) first_sale FROM " . TABLE_USER . " as u LEFT JOIN " . DMS_EMPLOYEE . " e  ON u.smpin=e.smpin JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,sale_date,sales_count FROM " . DMS_EMPLOYEE_SALES . " WHERE sale_date>='" . $start_date . "' AND sale_date<='" . $end_date . "') as s ON s.smpin=u.smpin WHERE u.is_deleted=0   AND u.role>7 AND u.role<14   AND e.is_current='1' " . $dealer_cond . "  GROUP BY u.smpin ) as a JOIN  (SELECT  COALESCE(SUM(t.target),'0') as targetcount,u.smpin,u.userid FROM " . TABLE_USER . " as u LEFT JOIN " . DMS_EMPLOYEE . " e  ON u.smpin=e.smpin JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code LEFT JOIN (SELECT id,smpin,target FROM " . TABLE_USER_SALETARGET . " WHERE sale_month>='" . $start_date . "' AND sale_month<='" . $end_date . "')as t ON t.smpin=u.smpin WHERE u.is_deleted=0 AND u.role>7 AND u.role<14   AND e.is_current='1' " . $dealer_cond . "  GROUP BY u.smpin ) as b ON a.userid=b.userid )c ORDER BY cast(sale_per as SIGNED) DESC,cast(salecount as SIGNED) DESC,cast(targetcount as SIGNED) DESC,last_sale ASC,first_sale ASC,userid ASC)as a )as f JOIN " . TABLE_USER . " as u ON f.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE 1=1   " . $user_cond . " LIMIT 1";
        if (count($user_arr) > 0) {
            $user_count_arr = $parent->getQueryValue($get_user_count_str);
            $outof = (count($user_count_arr) > 0) ? $user_count_arr[0]->user_count : '0';
            foreach ($user_arr as $mkey => $res_user) {
                $learning_arr = projectfunc::userLearningLevel($parent, $res_user->userid);

                $i = 0;
                $row++;
                if (empty($res_user->smpin)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->smpin);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->dealer_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                }
                if (empty($res_user->city)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->city);
                }

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salecount . "/" . $res_user->targetcount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->salecount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->targetcount);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rank);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $outof);
                if (empty($res_user->zm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->zm_name);
                }
                if (empty($res_user->rm_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rm_name);
                }
                if (empty($res_user->am_name)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->am_name);
                }
            }
        }

        //LOG
        $logstr = 'SALES LEADERBOARD REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ASSESSMENT LEADERBOARD REPORT EXPORT //////////////////////////////////////////////// */
    private function newassessmentleaderboardexport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));

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

        //DATE CONDITION
        //$start_date="2018-".date('m',strtotime(CUR_DATE))."-01";
        $start_date = date('Y-m', strtotime(CUR_DATE)) . "-01";
        $end_date = date('Y-m-t', strtotime(CUR_DATE));
        $date_cond = " AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59'";

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

        $user_cond = "";
        switch ($type) {
            case "zm":
                if ($parent->current_user_role == '3') {
                    $REQUEST_KEYS = array('zmemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = escape_str($parent->_request["zmemail"]);
                    if ($emailid != 'all') {
                        $user_cond .= " AND d.zm_email='" . $emailid . "'";
                    } else {
                        $user_cond .= "";
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "rm":
                if ($parent->current_user_role < '7') {
                    if ($parent->current_user_role == '5') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $zone_cond .= (count($trainer_arr) > 0) ? " d.zone='" . $trainer_arr[0]->zone . "'" : " d.zone='0'";
                        $emailid = escape_str($parent->_request["rmemail"]);
                        if ($emailid == 'all') {

                            $user_cond .= " AND " . $zone_cond;
                        } else {
                            $user_cond .= " AND d.rm_email='" . $emailid . "'  AND " . $zone_cond . "";
                        }
                    } else {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

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

                            $emailid = escape_str($parent->_request["zmemail"]);
                            $user_cond .= " AND d.zm_email='" . $emailid . "'";
                        } else {
                            //ZM EMAIL
                            $zmemail = escape_str($parent->_request["zmemail"]);
                            $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';
                            $user_cond .= " AND d.rm_email='" . $emailid . "' " . $zmcond;
                        }
                    }
                } else {
                    $parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                }
                break;

            case "am":
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " d.dealer_code='" . $parent->current_user_data->dealer_code . "'";
                } else {
                    $REQUEST_KEYS = array('amemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = escape_str($parent->_request["amemail"]);
                    if ($emailid == 'all') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $emailid = escape_str($parent->_request["rmemail"]);
                        $dealer_code_cond = " d.rm_email='" . $emailid . "'";

                    } else {
                        $dealer_code_cond = "  d.am_email='" . $emailid . "'";
                    }
                    if ($parent->current_user_role != '5') {
                        //ZM EMAIL
                        $zmemail = escape_str($parent->_request["zmemail"]);
                        $zmcond = ($zmemail != 'all' && $zmemail != '') ? " AND d.zm_email='" . $zmemail . "'" : '';
                        $dealer_code_cond .= $zmcond;
                    }
                    //RM EMAIL
                    $rmemail = escape_str($parent->_request["rmemail"]);
                    $rmcond = ($rmemail != 'all' && $rmemail != '') ? " AND d.rm_email='" . $rmemail . "'" : '';
                    $dealer_code_cond .= $rmcond;
                    //TRAINER
                    if ($parent->current_user_role == '5') {
                        $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                        $trainer_arr = $parent->getQueryValue($get_trainer_str);
                        $dealer_code_cond .= (count($trainer_arr) > 0) ? "AND d.zone='" . $trainer_arr[0]->zone . "'" : "AND d.zone='0'";
                    }
                }
                $user_cond .= "AND " . $dealer_code_cond . "";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        $join_cond = 'JOIN ' . DMS_DEALER . ' as d ON d.dealer_code=u.dealer_code ';

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT LEADERBOARD REPORT");
        $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);

        $row += 2;
        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Location', 'Score', 'Position', 'Outof', 'ZM', 'RM', 'AM');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth('30');

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

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

//        $filter_arr=globalfunc::getFilterValues($parent,$parent->params[2]);
        //        $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC ";
        //        if(isset($filter_arr) && isset($filter_arr['sort']))
        //        {
        //            //1-Dealer
        //            //2-Location
        //            switch($filter_arr['sort'])
        //            {
        //                case '1': $order_by_cond=" ORDER BY d.dealer_name,f.rank ASC "; break;
        //                case '2': $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC "; break;
        //                default: $order_by_cond=" ORDER BY f.rank,d.dealer_name ASC "; break;
        //            }
        //        }

//         if(isset($parent->_request["search"]))
        //        {
        //            $search_txt = escape_str($parent->_request["search"]);
        //            $user_cond.=" AND (u.fname LIKE '%".$search_txt."%' OR u.lname LIKE '%".$search_txt."%' OR u.emp_code LIKE '%".$search_txt."%' OR  d.dealer_name LIKE '%".$search_txt."%' OR d.zm_name LIKE '%".$search_txt."%'  OR d.rm_name LIKE '%".$search_txt."%' OR d.am_name LIKE '%".$search_txt."%' OR d.city LIKE '%".$search_txt."%'  )";
        //        }
        $new_cond = 'AND a.user_type=0 ';
        //GET ALL ASSESSMENT LIST WHICH PUBLISHED
        $assessment_str = "SELECT a.* FROM `" . TABLE_ASSESSMENT . "` as a  WHERE  a.status='1' AND a.atype='0' AND a.assettypeid='1' " . $date_cond . $new_cond;
        $assessment_arr = $parent->getQueryValue($assessment_str);
        //ASSESSMENT COUNT
        $assessment_count = (count($assessment_arr) > 0) ? count($assessment_arr) : '0';

//        $outof=0;
        //GET ALL ASSESSMENT LIST WHICH PUBLISHED
        //        $assessment_str = "SELECT a.* FROM `".TABLE_ASSESSMENT."` as a  WHERE a.atype='0' AND a.assettypeid='1' AND a.publisheddate<='".NOW."' ".$query_cond;
        //        $assessment_arr=$parent->getQueryValue($assessment_str);
        //        $data_arr=array();
        if (count($assessment_arr) > 0) {
            foreach ($assessment_arr as $res_assess) {
                $assessmentid_arr[] = $res_assess->assessmentid;
            }

            if ($parent->current_user_role == '14') {
                $area_cond .= " AND u.dealer_code='" . $dealer_code . "'";
            }
            $get_user_rank_str = "SELECT SUM(highscore)as user_score,u.userid,COALESCE(ar.total_time,'0')as total_time,MAX(au.modified_on) as last_modified,u.dealer_code,u.role,d.*  FROM " . TABLE_USER . " as u LEFT JOIN " . DMS_EMPLOYEE . " e  ON u.smpin=e.smpin JOIN (SELECT * FROM  " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND highscorearid!=0 ) as au ON u.userid=au.userid  LEFT JOIN (SELECT SUM(time_taken)as total_time,assessmentid,userid FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") GROUP BY userid )as ar ON u.userid=ar.userid " . $join_cond . "  WHERE u.role>'7' AND  u.role<14 AND  u.is_deleted=0 AND e.is_current='1'  " . $user_cond . " GROUP BY u.userid ORDER BY user_score DESC,ar.total_time ASC,last_modified ASC,u.userid ASC   ";
            $get_user_rank_arr = $parent->getQueryValue($get_user_rank_str);
            if ($get_user_rank_arr) {
                foreach ($get_user_rank_arr as $res_user) {
                    $user_data_arr = new stdClass();
                    $get_time_str = "SELECT * FROM " . TABLE_ASSESSMENTRESULT . " WHERE assessmentid IN (" . @implode(',', $assessmentid_arr) . ") AND userid='" . $res_user->userid . "'";
                    $get_time_arr = $parent->getQueryValue($get_time_str);

                    $user_data_arr->user = projectfunc::getuserdata($parent, $res_user->userid);
                    $user_data_arr->rank = '0';
                    $user_data_arr->tot_user_score = (count($get_time_arr) > 0) ? $res_user->user_score : '-';
                    $user_data_arr->percentage_score = (count($get_time_arr) > 0) ? '0' : '-';

                    $user_data_arr->total_time = $res_user->total_time;
                    $user_data_arr->last_time = (count($get_time_arr) > 0) ? $res_user->last_modified : '-';

                    //GET EACH USER COND
                    $getuser_data = projectfunc::getuserdata($parent, $res_user->userid);
//                    if($getuser_data->role)
                    //                    {
                    //                        $user_cond[]=" (a.filter_role LIKE '%".$getuser_data->role."%' OR a.filter_role='')";
                    //                    }
                    if ($getuser_data->zone) {
                        $user_cond[] = " (a.filter_zone LIKE '%" . $getuser_data->zone . "%' OR a.filter_zone='')";
                    }
                    if ($getuser_data->city) {
                        $user_cond[] = " (a.filter_region LIKE '%" . $getuser_data->city . "%' OR a.filter_region='')";
                    }
                    if ($getuser_data->dealer_code) {
                        $user_cond[] = " (a.filter_dealer LIKE '%" . $getuser_data->dealer_code . "%'  OR a.filter_dealer='')";
                    }
                    $assess_user_cond = " AND " . @implode(' AND ', $user_cond);

                    $count_assessment_str = "SELECT COUNT(a.assessmentid) as acount FROM `" . TABLE_ASSESSMENT . "` as a  WHERE a.status='1' AND a.atype='0' AND a.assettypeid='1'  AND a.publisheddate>='" . $start_date . " 00:00:00'  AND a.publisheddate<='" . $end_date . " 23:59:59' AND a.enddate>='" . $user_data_arr->user->user_created_on . "' AND  assessmentid IN (" . @implode(',', $assessmentid_arr) . ") " . $assess_user_cond . " LIMIT 1";
                    $count_assessment_arr = $parent->getQueryValue($count_assessment_str);
                    $user_data_arr->percentage_score = (count($assessment_arr) > 0) ? round($res_user->user_score / $count_assessment_arr[0]->acount) : '0';
                    $user_data_arr->percentage = $user_data_arr->percentage_score;

                    if ((($assessment_count / $count_assessment_arr[0]->acount) * 100) >= MIN_ASSESSMENT_PERCENT) {
                        $data_arr[] = $user_data_arr;
                    }
                }
            }

            if (count($data_arr) > 0) {
                array_multisort(array_column($data_arr, 'percentage_score'), SORT_DESC, array_column($rank_arr, 'total_time'), SORT_ASC, $rank_arr);
                $rank = 1;
                foreach ($data_arr as $key => $res) {
                    $data_arr[$key]->rank = $rank;
                    $rank++;
                }
            }

            if (count($data_arr) > 0) {
                $outof = count($data_arr);
                //$user_count_arr=$parent->getQueryValue($get_user_count_str);
                //$outof=(count($user_count_arr)>0)?$user_count_arr[0]->user_count:'0';
                foreach ($data_arr as $res_user) {
                    $i = 0;
                    $row++;
                    if (empty($res_user->smpin)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->smpin);
                    }
                    if (empty($res_user->fname)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->fname . " " . $res_user->user->lname);
                    }
                    if (empty($res_user->mobileno)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->mobileno);
                    }
                    if (empty($res_user->dealer_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->dealer_name);
                    }
                    if (empty($res_user->city)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->city);
                    }
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->percentage_score . "%");
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->rank);
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $outof);
                    if (empty($res_user->zm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->zm_name);
                    }
                    if (empty($res_user->rm_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->rm_name);
                    }
                    if (empty($res_user->am_name)) {
                        $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                        projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                    } else {
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->user->dealer->am_name);
                    }
                }
            }
        }

        //LOG
        $logstr = 'ASSESSMENT LEADERBOARD REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ASM ASSESSMENT REPORT EXPORT //////////////////////////////////////////////// */
    private function asmassessmentreport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5', '6'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $assessment_str = "SELECT assessmentname,pass_score FROM " . TABLE_ASSESSMENT . "  WHERE assessmentid='" . $assessmentid . "'   LIMIT 1";
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessmentname = (count($assessment_arr) > 0) ? $assessment_arr[0]->assessmentname : '';
        $pass_score = $assessment_arr[0]->pass_score;

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Assessment : " . $assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Passing Percentage : " . $pass_score . "%");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Emp code', 'Name', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";

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

        $user_cond = "";
        $zm_email = escape_str($parent->_request["zmemail"]);
        $rm_email = escape_str($parent->_request["rmemail"]);
        //$am_email = escape_str($parent->_request["amemail"]);

        if (($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 . "'" : '';
            if ($parent->current_user_role != '5') {
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
            }
            if ($parent->current_user_role == '5') {
                $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $mcond .= (count($trainer_arr) > 0) ? "AND zone='" . $trainer_arr[0]->zone . "'" : "AND zone='0'";
            }

            //MANAGER
            $query_all_amarr = $parent->selectQuery(DMS_DEALER, "`dealer_code`,am_email ", " status='1' AND is_deleted='0' " . $mcond . "  GROUP BY am_email", "");
            $emailid_arr = array();
            if ($query_all_amarr['nr'] > 0) {
                foreach ($query_all_amarr['result'] as $res) {
                    $emailid_arr[] = "'" . $res->am_email . "'";
                }
                $user_cond .= " AND u.emailid IN (" . implode(',', $emailid_arr) . ")";
            } else {
                $user_cond .= " AND u.emailid IN ('')";
            }
        } else {
            //PAN
            $user_cond = '';
            if ($parent->current_user_role == '5') {
                $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $mcond .= (count($trainer_arr) > 0) ? "AND zone='" . $trainer_arr[0]->zone . "'" : "AND zone='0'";
                //$user_cond=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE is_deleted='0' ".$mcond.")";
                $query_all_amarr = $parent->selectQuery(DMS_DEALER, "`dealer_code`,am_email ", " status='1' AND is_deleted='0' " . $mcond . " GROUP BY am_email", "");
                $emailid_arr = array();
                if ($query_all_amarr['nr'] > 0) {
                    foreach ($query_all_amarr['result'] as $res) {
                        $emailid_arr[] = "'" . $res->am_email . "'";
                    }
                    $user_cond .= " AND u.emailid IN (" . implode(',', $emailid_arr) . ")";
                } else {
                    $user_cond .= " AND u.emailid IN ('')";
                }
            }
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //0-Score
            //1-Name

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY au.highscore DESC,au.modified_on ASC,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR u.emailid LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.emailid,u.mobileno FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid   WHERE u.status='1' AND u.is_deleted='0' AND u.role=7 AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                //$header_arr=array('Emp code','Name','Mobile','Email','Score','Timetaken in Minutes','Completed Time');

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                if (empty($res_user->emp_code)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->emailid)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);
                }

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");
                $timetaken = '';
                $completedtime = '';
                if (count($exam_highscore_arr) > 0) {
                    $exam_result_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENTRESULT . "  WHERE assessmentid='" . $assessmentid . "' AND arid='" . $exam_highscore_arr[0]->highscorearid . "' LIMIT 1";
                    $exam_result_highscore_arr = $parent->getQueryValue($exam_result_highscore_str);

                    $timetaken = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->time_taken)) ? round($exam_result_highscore_arr[0]->time_taken / 60) : "0";
                    $completedtime = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->modified_on)) ? date('d-m-Y h:i A', strtotime($exam_result_highscore_arr[0]->modified_on)) : "-";
                }

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

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

            }
        }

        //LOG
        $logstr = 'AM ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// RM ASSESSMENT REPORT EXPORT //////////////////////////////////////////////// */
    private function rmassessmentreport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 2);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3', '4', '5'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $assessment_str = "SELECT assessmentname,pass_score FROM " . TABLE_ASSESSMENT . "  WHERE assessmentid='" . $assessmentid . "'   LIMIT 1";
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessmentname = (count($assessment_arr) > 0) ? $assessment_arr[0]->assessmentname : '';
        $pass_score = $assessment_arr[0]->pass_score;

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Assessment : " . $assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Passing Percentage : " . $pass_score . "%");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Emp code', 'Name', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";

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

        $user_cond = "";
        $zm_email = escape_str($parent->_request["zmemail"]);
        //$rm_email = escape_str($parent->_request["rmemail"]);
        //$am_email = escape_str($parent->_request["amemail"]);

        if ($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."'":'';
            if ($parent->current_user_role != '5') {
                $mcond .= ($zm_email != 'all' && $zm_email != '') ? " AND zm_email='" . $zm_email . "'" : '';
            }
            if ($parent->current_user_role == '5') {
                $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $mcond .= (count($trainer_arr) > 0) ? "AND zone='" . $trainer_arr[0]->zone . "'" : "AND zone='0'";
            }

            //MANAGER
            $query_all_amarr = $parent->selectQuery(DMS_DEALER, "`dealer_code`,rm_email ", " status='1' AND is_deleted='0' " . $mcond . " GROUP BY rm_email", "");
            $emailid_arr = array();
            if ($query_all_amarr['nr'] > 0) {
                foreach ($query_all_amarr['result'] as $res) {
                    $emailid_arr[] = "'" . $res->rm_email . "'";
                }
                $user_cond .= " AND u.emailid IN (" . implode(',', $emailid_arr) . ")";
            } else {
                $user_cond .= " AND u.emailid IN ('')";
            }
        } else {
            //PAN
            $user_cond = '';
            if ($parent->current_user_role == '5') {
                $get_trainer_str = "SELECT h.zone FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                $trainer_arr = $parent->getQueryValue($get_trainer_str);
                $mcond .= (count($trainer_arr) > 0) ? "AND zone='" . $trainer_arr[0]->zone . "'" : "AND zone='0'";
                //$user_cond=" AND u.dealer_code IN (SELECT dealer_code FROM ".DMS_DEALER." WHERE is_deleted='0' ".$mcond.")";
                $query_all_amarr = $parent->selectQuery(DMS_DEALER, "`dealer_code`,rm_email ", " status='1' AND is_deleted='0' " . $mcond . " GROUP BY rm_email", "");
                $emailid_arr = array();
                if ($query_all_amarr['nr'] > 0) {
                    foreach ($query_all_amarr['result'] as $res) {
                        $emailid_arr[] = "'" . $res->rm_email . "'";
                    }
                    $user_cond .= " AND u.emailid IN (" . implode(',', $emailid_arr) . ")";
                } else {
                    $user_cond .= " AND u.emailid IN ('')";
                }
            }
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //0-Score
            //1-Name

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY au.highscore DESC,au.modified_on ASC,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR u.emailid LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.emailid,u.mobileno FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid   WHERE u.status='1' AND u.is_deleted='0' AND u.role=6 AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                if (empty($res_user->emp_code)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->emailid)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);
                }

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");
                $timetaken = '';
                $completedtime = '';
                if (count($exam_highscore_arr) > 0) {
                    $exam_result_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENTRESULT . "  WHERE assessmentid='" . $assessmentid . "' AND arid='" . $exam_highscore_arr[0]->highscorearid . "' LIMIT 1";
                    $exam_result_highscore_arr = $parent->getQueryValue($exam_result_highscore_str);

                    $timetaken = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->time_taken)) ? round($exam_result_highscore_arr[0]->time_taken / 60) : "0";
                    $completedtime = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->modified_on)) ? date('d-m-Y h:i A', strtotime($exam_result_highscore_arr[0]->modified_on)) : "-";
                }

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

                $result_str = ($highscore >= $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);
            }
        }

        //LOG
        $logstr = 'RM ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// ZM ASSESSMENT REPORT EXPORT //////////////////////////////////////////////// */
    private function zmassessmentreport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $assessment_str = "SELECT assessmentname,pass_score FROM " . TABLE_ASSESSMENT . "  WHERE assessmentid='" . $assessmentid . "'   LIMIT 1";
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessmentname = (count($assessment_arr) > 0) ? $assessment_arr[0]->assessmentname : '';
        $pass_score = $assessment_arr[0]->pass_score;

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Assessment : " . $assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Passing Percentage : " . $pass_score . "%");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Emp code', 'Name', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";

        $dealer_code_cond = '';
        $user_cond = "";

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //0-Score
            //1-Name

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY au.highscore DESC,au.modified_on ASC,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR u.emailid LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.emailid,u.mobileno FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid   WHERE u.status='1' AND u.is_deleted='0' AND u.role=4 AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                //$header_arr=array('Emp code','Name','Mobile','Email','Score','Timetaken in Minutes','Completed Time');

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                if (empty($res_user->emp_code)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->emailid)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);
                }

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");
                $timetaken = '';
                $completedtime = '';
                if (count($exam_highscore_arr) > 0) {
                    $exam_result_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENTRESULT . "  WHERE assessmentid='" . $assessmentid . "' AND arid='" . $exam_highscore_arr[0]->highscorearid . "' LIMIT 1";
                    $exam_result_highscore_arr = $parent->getQueryValue($exam_result_highscore_str);

                    $timetaken = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->time_taken)) ? round($exam_result_highscore_arr[0]->time_taken / 60) : "0";
                    $completedtime = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->modified_on)) ? date('d-m-Y h:i A', strtotime($exam_result_highscore_arr[0]->modified_on)) : "-";
                }

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

                $result_str = ($highscore >= $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);
            }
        }

        //LOG
        $logstr = 'ZM ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// SURVEY REPORT EXPORT //////////////////////////////////////////////// */
    private function surveyreportexport($parent)
    {

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

        //FIELDS VALIDATION
        globalfunc::checkParamCount($parent, 2);
        //FIELDS VALIDATION
        $REQUEST_KEYS = array('assessmentid' => 'integer');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
        $assessmentid = escape_str($parent->_request["assessmentid"]);

        $query_str = "SELECT assessmentid,assessmentname  FROM " . TABLE_ASSESSMENT . " WHERE status='1'  AND  assettypeid='2' AND  assessmentid='" . $assessmentid . "' AND  publisheddate<='" . NOW . "' LIMIT 1";
        $query_arr = $parent->getQueryValue($query_str);
        if (!count($query_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error9"]), 406);
        }

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "FEEDBACK REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":F" . $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 += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Survey : " . $query_arr[0]->assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":F" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row += 2;
        $header_arr = array('S.No', 'SMPIN', 'DSE Name', 'Mobile', 'Dealer Name', 'Date & Time');
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row);

        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('20');

        $ques_str = "SELECT q.id as qid, q.`questiontypeid`, q.`questiontext`, q.`qimage`, q.`qvideo`  FROM " . TABLE_QUESTION . " as q WHERE id IN (SELECT qid FROM " . TABLE_ASSESSMENT_QUESTION . " WHERE assessmentid='" . $assessmentid . "')  ORDER BY id ASC ";
        $ques_arr = $parent->getQueryValue($ques_str);

        if (count($ques_arr)) {
            $i = 6;
            foreach ($ques_arr as $res_ques) {
                $column = getColumnFromNumber($i++);
                $bordercells_arr[] = $column . $row;
                $header_arr[] = $res_ques->questiontext;
                $objPHPExcel->getActiveSheet()->getColumnDimension($column)->setWidth('40');
            }
        }

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

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

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

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

        $user_cond = "";

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

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

                        $emailid = escape_str($parent->_request["rmemail"]);
                        if ($emailid == 'all') {
                            $get_trainer_str = "SELECT h.* FROM " . DMS_HOUSER . " h JOIN " . TABLE_USER . " u ON u.emp_code=h.emp_code WHERE u.userid='" . $parent->current_user_id . "' AND u.role='5' LIMIT 1";
                            $trainer_arr = $parent->getQueryValue($get_trainer_str);
                            $zone_cond .= (count($trainer_arr) > 0) ? " zone='" . $trainer_arr[0]->zone . "'" : " zone='0'";
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE " . $zone_cond . "  AND status='1' AND is_deleted='0')";
                        } else {
                            $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE rm_email='" . $emailid . "'  AND status='1' AND is_deleted='0')";
                        }
                    } else {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

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

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

            case "am":
                if ($parent->current_user_role == '14') {
                    $dealer_code_cond = " dealer_code='" . $parent->current_user_data->dealer_code . "'";
                } else {
                    $REQUEST_KEYS = array('amemail' => 'string');
                    globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                    $emailid = escape_str($parent->_request["amemail"]);
                    if ($emailid == 'all') {
                        $REQUEST_KEYS = array('rmemail' => 'string');
                        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);
                        $emailid = escape_str($parent->_request["rmemail"]);
                        $dealer_code_cond = " rm_email='" . $emailid . "'";

                    } else {
                        $dealer_code_cond = "  am_email='" . $emailid . "'";
                    }
                }
                $user_cond .= " AND u.dealer_code IN (SELECT dealer_code FROM " . DMS_DEALER . " WHERE  " . $dealer_code_cond . "  AND status='1' AND is_deleted='0')";
                break;
            default:$parent->response(array('response' => 'failed', 'message' => 'Sorry No access'), 401);
                break;
        }

        if (isset($parent->_request["search"]) && $parent->_request["search"] != '') {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.smpin LIKE '%" . $search_txt . "%' OR d.dealer_name LIKE '%" . $search_txt . "%' OR d.zm_name LIKE '%" . $search_txt . "%'  OR d.rm_name LIKE '%" . $search_txt . "%' OR d.am_name LIKE '%" . $search_txt . "%' OR d.city LIKE '%" . $search_txt . "%'  )";
        }

        //GET USER ATTENTED
        $query_assess_str = "SELECT a.* FROM " . TABLE_ASSESSMENT_USER . " as a JOIN " . TABLE_USER . " u ON u.userid=a.userid WHERE a.assessmentid='" . $assessmentid . "' AND a.highscorearid!='0' AND u.is_deleted=0 " . $user_cond;
        $query_assess_arr = $parent->getQueryValue($query_assess_str);
        if (!count($query_assess_arr) > 0) {
            $parent->response(array('response' => 'failed', 'message' => $this->page_data["error10"]), 406);
        }

        if (count($query_assess_arr) > 0) {
            $survey_str = "SELECT * FROM " . TABLE_SURVEY_RATING . " ORDER BY id ASC";
            $survey_arr = $parent->getQueryValue($survey_str);

            if (count($survey_arr) > 0) {
                foreach ($survey_arr as $skey => $res_survey) {
                    $rating_arr[$res_survey->id] = $res_survey->rating_text;
                }
            }

            $sno = 1;
            foreach ($query_assess_arr as $res_user) {
                $userid = $res_user->userid;
                $user_data = projectfunc::getuserdata($parent, $userid);
                $row++;
                $i = 0;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $sno++);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_data->smpin);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_data->fname . " " . $user_data->lname);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_data->mobileno);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $user_data->dealer->dealer_name . " " . $user_data->dealer->dealer_code);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_user->created_on)));

                //GET USER RESULT
                $query_res_str = "SELECT au.*  FROM " . TABLE_ASSESSMENT_USERRESULT . " as au JOIN " . TABLE_ASSESSMENTRESULT . " as ar ON au.arid=ar.arid WHERE au.userid='" . $userid . "' AND au.assessmentid='" . $assessmentid . "' AND au.arid='" . $res_user->highscorearid . "' GROUP BY au.arid,au.aqid ORDER by qid ASC";

                $query_assess_res_arr = $parent->getQueryValue($query_res_str);
                if (!count($query_assess_res_arr) > 0) {
                    $parent->response(array('response' => 'failed', 'message' => $this->page_data["error10"]), 406);
                }

                if (count($query_assess_res_arr) > 0) {
                    // $sno=1;
                    foreach ($query_assess_res_arr as $res) {
                        $ques_str = "SELECT q.id as qid, q.`questiontypeid`, q.`questiontext`, q.`qimage`, q.`qvideo`  FROM " . TABLE_QUESTION . " as q WHERE id='" . $res->qid . "' LIMIT 1";
                        $ques_arr = $parent->getQueryValue($ques_str);

                        if (count($ques_arr) > 0) {
                            //GET USER ANSWERED OPTIONS
                            $user_answer_str = "SELECT * FROM " . TABLE_ASSESSMENT_USERRESULT . "  WHERE userid='" . $userid . "' AND arid='" . $res_user->highscorearid . "' AND aqid='" . $res->aqid . "'";

                            $user_answer_arr = $parent->getQueryValue($user_answer_str);
                            $ques_arr[0]->answer = array();
                            if (count($user_answer_arr) > 0) {

                                foreach ($user_answer_arr as $res_answer) {

                                    //GET RATING
                                    $ques_arr[0]->survey_text = '';
                                    if ($ques_arr[0]->questiontypeid == '3') {
                                        if ($res_answer->survey_rating == $res_survey->score) {
                                            $rating = $res_answer->survey_rating;
                                            $ques_arr[0]->answer[] = isset($rating_arr[$rating]) ? $rating_arr[$rating] : '';
                                        }

                                    } elseif ($ques_arr[0]->questiontypeid == '5' || $ques_arr[0]->questiontypeid == '6') {
                                        //GET ALL QUESTION OPTIONS
                                        if ($res_answer->choiseid) {
                                            $ques_option_str = "SELECT op.optid,op.opttext,'0' as selected  FROM " . TABLE_QUESTIONOPTION . " as op WHERE op.qid='" . $res->qid . "' AND op.optid='" . $res_answer->choiseid . "' LIMIT 1";
                                            $ques_option_arr = $parent->getQueryValue($ques_option_str);

                                            if (count($ques_option_arr) > 0) {
                                                $ques_arr[0]->answer[] = $ques_option_arr[0]->opttext;
                                            }
                                        }
                                    } else {
                                        $ques_arr[0]->answer[] = ($res_answer->survey_text != '') ? $res_answer->survey_text : '';
                                    }
                                }
                            }

                            $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, @implode(',', $ques_arr[0]->answer));

                        }

                    }
                }
            }
        }

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// TRAINER ASSESSMENT REPORT EXPORT //////////////////////////////////////////////// */
    private function trainerassessmentreport($parent)
    {
        globalfunc::checkMethodType($parent, array('POST'));
        globalfunc::checkParamCount($parent, 1);
        globalfunc::checkAuthrozation($parent, array('type' => 'user'));
        projectfunc::authorizedRole($parent, array('3'));

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        $assessment_str = "SELECT assessmentname,pass_score FROM " . TABLE_ASSESSMENT . "  WHERE assessmentid='" . $assessmentid . "'   LIMIT 1";
        $assessment_arr = $parent->getQueryValue($assessment_str);
        $assessmentname = (count($assessment_arr) > 0) ? $assessment_arr[0]->assessmentname : '';
        $pass_score = $assessment_arr[0]->pass_score;

        //EXCEL HEADER
        $row = 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Assessment : " . $assessmentname);
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        $row++;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "Passing Percentage : " . $pass_score . "%");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":E" . $row);
        $objPHPExcel->getActiveSheet()->getStyle("A" . $row)->getFont()->setBold(true);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "ASSESSMENT REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row, "F" . $row, "G" . $row, "H" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Emp code', 'Name', 'Mobile', 'Email', 'Score', 'Timetaken in Minutes', 'Completed Time', 'Result');
        $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth('10');
        $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth('20');
        $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth('10');

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

        $role = $parent->current_user_role;
        $userdata = projectfunc::getuserdata($parent, $parent->current_user_id);
        $user_cond = "";

        $dealer_code_cond = '';
        $user_cond = "";

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC ";
        if (isset($filter_arr) && isset($filter_arr['sort'])) {
            //0-Score
            //1-Name

            switch ($filter_arr['sort']) {
                case '1':$order_by_cond = "ORDER BY u.fname ASC ";
                    break;
                default:$order_by_cond = "ORDER BY au.highscore DESC,au.modified_on ASC,u.fname ASC ";
                    break;

            }

        }

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' OR u.emp_code LIKE '%" . $search_txt . "%' OR u.emailid LIKE '%" . $search_txt . "%'  )";
        }

        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.emailid,u.mobileno FROM " . TABLE_USER . " u  JOIN " . TABLE_ASSESSMENT_USER . " as au ON u.userid=au.userid   WHERE u.status='1' AND u.is_deleted='0' AND u.role=5 AND au.assessmentid='" . $assessmentid . "' " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {

                //$header_arr=array('Emp code','Name','Mobile','Email','Score','Timetaken in Minutes','Completed Time');

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                if (empty($res_user->emp_code)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);
                }
                if (empty($res_user->fname)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->fname . " " . $res_user->lname);
                }
                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                if (empty($res_user->emailid)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);
                }

                $exam_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENT_USER . "  WHERE assessmentid='" . $assessmentid . "' AND userid='" . $res_user->userid . "' LIMIT 1";
                $exam_highscore_arr = $parent->getQueryValue($exam_highscore_str);
                $highscore = (count($exam_highscore_arr) > 0) ? $exam_highscore_arr[0]->highscore : '0';

                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $highscore . "%");
                $timetaken = '';
                $completedtime = '';
                if (count($exam_highscore_arr) > 0) {
                    $exam_result_highscore_str = "SELECT  * FROM " . TABLE_ASSESSMENTRESULT . "  WHERE assessmentid='" . $assessmentid . "' AND arid='" . $exam_highscore_arr[0]->highscorearid . "' LIMIT 1";
                    $exam_result_highscore_arr = $parent->getQueryValue($exam_result_highscore_str);

                    $timetaken = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->time_taken)) ? round($exam_result_highscore_arr[0]->time_taken / 60) : "0";
                    $completedtime = (count($exam_result_highscore_arr) > 0 && isset($exam_result_highscore_arr[0]->modified_on)) ? date('d-m-Y h:i A', strtotime($exam_result_highscore_arr[0]->modified_on)) : "-";
                }

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

                $result_str = ($highscore >= $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);
            }
        }

        //LOG
        $logstr = 'TRAINER ASSESSMENT REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

    /* /////////////////////////////// DEALER SALES TARGET REPORT EXPORT //////////////////////////////////////////////// */
    private function dealersalestargetreportexport($parent)
    {
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

    /* /////////////////////////////// MANAGER SALESTARGET  REPORT EXPORT //////////////////////////////////////////////// */
    private function managersalestargetreportexport($parent)
    {
    }
    /* //////////////////////////////////////////////////////////////////////////////////////////// */

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Name', 'Emp Code', 'Mobile', 'EmailID', 'Overall Progress %');
        $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('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

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

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

        $user_cond = "";

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC,u.lname ASC,u.userid ASC ";

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' )";
        }

        //$user_cond.=" AND e.is_current='1' ";
        // u.status='1' AND
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.mobileno,u.emailid FROM " . TABLE_USER . " u JOIN " . DMS_DEALER . " d ON u.emailid=d.zm_email  WHERE u.is_deleted='0' AND u.role=4 " . $user_cond . "  GROUP BY u.userid  " . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //'Name','Emp Code','Mobile','EmailID','Overall Progress %'
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                $content_arr = projectfunc::managerCourseCompleteStatus($parent, $res_user->userid, 4);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");

            }
        }

        //LOG
        $logstr = 'ZM COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Name', 'Emp Code', 'Mobile', 'EmailID', 'Overall Progress %');
        $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('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

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

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

        $user_cond = "";

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC,u.lname ASC,u.userid ASC ";

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' )";
        }

        //$user_cond.=" AND e.is_current='1' ";
        // u.status='1' AND
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.mobileno,u.emailid FROM " . TABLE_USER . " u  WHERE u.is_deleted='0' AND u.role=5 " . $user_cond . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //'Name','Emp Code','Mobile','EmailID','Overall Progress %'
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                $content_arr = projectfunc::managerCourseCompleteStatus($parent, $res_user->userid, 5);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");

            }
        }

        //LOG
        $logstr = 'TRAINER COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Name', 'Emp Code', 'Mobile', 'EmailID', 'Overall Progress %');
        $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('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

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

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

        $user_cond = "";
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC,u.lname ASC,u.userid ASC ";

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' )";
        }

        //$user_cond.=" AND e.is_current='1' ";
        // u.status='1' AND
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.mobileno,u.emailid FROM " . TABLE_USER . " u JOIN " . DMS_DEALER . " d ON u.emailid=d.rm_email  WHERE u.is_deleted='0' AND u.role=6 " . $user_cond . " GROUP BY u.userid  " . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //'Name','Emp Code','Mobile','EmailID','Overall Progress %'
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                $content_arr = projectfunc::managerCourseCompleteStatus($parent, $res_user->userid, 6);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");

            }
        }

        //LOG
        $logstr = 'RM COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "COURSE COMPLETION REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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 += 2;
        $bordercells_arr = array("A" . $row, "B" . $row, "C" . $row, "D" . $row, "E" . $row);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Name', 'Emp Code', 'Mobile', 'EmailID', 'Overall Progress %');
        $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('25');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');

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

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

        $user_cond = "";
        $dealer_cond_arr = $this->getmanagerconditon_arr($parent);

        if (count($dealer_cond_arr) > 0) {
            //APPEND prefix
            array_walk($dealer_cond_arr, function (&$value, $key) {$value = 'd.' . $value;});
            $user_cond .= " AND " . @implode(" AND ", $dealer_cond_arr);
        }

        $filter_arr = globalfunc::getFilterValues($parent, $parent->params[1]);
        $order_by_cond = "ORDER BY u.fname ASC,u.lname ASC,u.userid ASC ";

        if (isset($parent->_request["search"])) {
            $search_txt = escape_str($parent->_request["search"]);
            $user_cond .= " AND (u.fname LIKE '%" . $search_txt . "%' OR u.lname LIKE '%" . $search_txt . "%' )";
        }

        //$user_cond.=" AND e.is_current='1' ";
        // u.status='1' AND
        $get_user_str = "SELECT u.userid,u.fname,u.lname,u.emp_code,u.emailid FROM " . TABLE_USER . " u JOIN " . DMS_DEALER . " d ON u.emailid=d.am_email  WHERE u.is_deleted='0' AND u.role=7 " . $user_cond . " GROUP BY u.userid  " . $order_by_cond;
        $user_arr = $parent->getQueryValue($get_user_str);

        if (count($user_arr) > 0) {

            foreach ($user_arr as $mkey => $res_user) {
                //'Name','Emp Code','Mobile','EmailID','Overall Progress %'
                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emp_code);

                if (empty($res_user->mobileno)) {
                    $bordercells_arr = array(getColumnFromNumber($i++) . $row);
                    projectfunc::highlighRows($bordercells_arr, $objPHPExcel);
                } else {
                    $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->mobileno);
                }
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->emailid);

                $content_arr = projectfunc::managerCourseCompleteStatus($parent, $res_user->userid, 7);
                $percentage = ($content_arr['total'] > 0) ? round(($content_arr['completed'] / $content_arr['total']) * 100) : '0';
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $percentage . "%");

            }
        }

        //LOG
        $logstr = 'AM COURSE COMPLETION REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        $REQUEST_KEYS = array('fromdate' => 'date|Y-m-d', 'todate' => 'date|Y-m-d');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        $fromdate = escape_str($parent->_request["fromdate"]);
        $todate = escape_str($parent->_request["todate"]);

        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "i-Learn REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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('d-m-Y', strtotime($fromdate)));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":B" . $row);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "To :" . date('d-m-Y', strtotime($todate)));
        $objPHPExcel->getActiveSheet()->mergeCells("C" . $row . ":D" . $row);

        //GET ALL TRAINING

        $ilearn_arr = $parent->getQueryValue("SELECT s.*,CONCAT(u.fname,' ',u.lname) as organiser_name,u.emp_code FROM " . TABLE_VIDEOTRAININGSCHEDULE . " s JOIN " . TABLE_USER . " u ON u.userid=s.organiser WHERE s.fromdate>='" . $fromdate . " 00:00:00' AND s.fromdate<='" . $todate . " 23:59:59' AND s.is_deleted=0 ORDER BY s.fromdate ASC,s.id ASC");

        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        $header_arr = array('Title', 'Organiser', 'From', 'To', 'SMPIN', 'Name', 'Mobile', 'EmailID', 'Dealer code', 'Dealer', 'Start time', 'End Time', 'Time Taken');
        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('30');
        $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('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('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);
        }

        if (count($ilearn_arr) > 0) {

            foreach ($ilearn_arr as $res_ilearn) {

//                $row+=2;
                //                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"Title :".$res_ilearn->title);
                //                $objPHPExcel->getActiveSheet()->mergeCells("A".$row.":D".$row);
                //                $row++;
                //
                //                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"Organiser :".$res_ilearn->organiser_name);
                //                $objPHPExcel->getActiveSheet()->mergeCells("A".$row.":B".$row);
                //                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$row,"From :".date('d-m-Y h:i A',strtotime($res_ilearn->fromdate)));
                //                $objPHPExcel->getActiveSheet()->mergeCells("C".$row.":D".$row);
                //                $objPHPExcel->getActiveSheet()->SetCellValue('E'.$row,"To :".date('d-m-Y h:i A',strtotime($res_ilearn->todate)));
                //                $objPHPExcel->getActiveSheet()->mergeCells("E".$row.":F".$row);

                $user_arr = $parent->getQueryValue("SELECT u.userid,u.fname,u.lname,u.smpin,u.dealer_code, u.role,u.emp_code,u.mobileno,u.emailid,CASE WHEN u.role IN (8,9,10,11,12,13) THEN d.dealer_name ELSE '' END as dealer_name,CASE WHEN u.segment=0 THEN 'Student Learner' WHEN u.segment=1 THEN 'Parents/Guardian' WHEN u.segment=2 THEN 'Instructor' END as segment_str FROM " . TABLE_VIDEOSCHEDULEUSER . " vu JOIN  " . TABLE_USER . " u ON vu.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE u.status='1' AND u.is_deleted='0' AND vu.sid='" . $res_ilearn->id . "' ORDER BY  u.dealer_code ASC,u.fname ASC,u.userid ASC");
                if (count($user_arr) > 0) {
                    foreach ($user_arr as $res_user) {
                        $i = 0;
                        $row++;
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_ilearn->title);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_ilearn->organiser_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_ilearn->fromdate)));
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y h:i A', strtotime($res_ilearn->todate)));

                        $smpin = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->smpin : $res_user->emp_code;
                        $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $smpin);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                        $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->dealer_code);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);

                        $time_arr = $parent->getQueryValue("SELECT MIN(fromtime)fromtime,MAX(totime)totime,SUM(timetaken) tottime FROM `" . TABLE_VIDEOTRAININGTIMER . "` WHERE sid='" . $res_ilearn->id . "' AND userid='" . $res_user->userid . "' GROUP BY userid");

                        //$start_arr=$parent->getQueryValue("SELECT * FROM ".TABLE_VIDEOTRAININGTIMER." WHERE sid='".$res_ilearn->id."' AND userid='".$res_user->userid."' ORDER BY id ASC LIMIT 1");
                        $start_time = (count($time_arr) > 0 && $time_arr[0]->fromtime) ? date('d-m-Y h:i A', strtotime($time_arr[0]->fromtime)) : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $start_time);

                        //$end_arr=$parent->getQueryValue("SELECT * FROM ".TABLE_VIDEOTRAININGTIMER." WHERE sid='".$res_ilearn->id."' AND userid='".$res_user->userid."' ORDER BY id DESC  LIMIT 1");
                        $end_time = (count($time_arr) > 0 && $time_arr[0]->totime) ? date('d-m-Y h:i A', strtotime($time_arr[0]->totime)) : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $end_time);

                        //$tot_arr=$parent->getQueryValue("SELECT SUM(timetaken) tottime FROM ".TABLE_VIDEOTRAININGTIMER." WHERE sid='".$res_ilearn->id."' AND userid='".$res_user->userid."' GROUP BY userid");
                        $timetaken = (count($time_arr) > 0 && $time_arr[0]->tottime) ? $time_arr[0]->tottime : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $timetaken);
                    }
                }

            }
        }

        //LOG
        $logstr = 'ILEARN REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

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

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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

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

        $REQUEST_KEYS = array('fromdate' => 'date|Y-m-d', 'todate' => 'date|Y-m-d');
        globalfunc::mandatoryKey($parent, $REQUEST_KEYS);

        $fromdate = escape_str($parent->_request["fromdate"]);
        $todate = escape_str($parent->_request["todate"]);

        $row = 0;
        //Excel class library
        require_once dirname(__FILE__) . '/../../PHPExcel.php';
        require_once dirname(__FILE__) . '/../../PHPExcel/Writer/Excel2007.php';

        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getActiveSheet()->setTitle("Dashboard");

        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "i-Learn REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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('d-m-Y', strtotime($fromdate)));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":B" . $row);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "To :" . date('d-m-Y', strtotime($todate)));
        $objPHPExcel->getActiveSheet()->mergeCells("C" . $row . ":D" . $row);

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

        //SET HEADER
        $header_arr = array('Trainer Name', 'No.of Sessions', 'Total Duration (Mins)', 'Total Participants', 'Total Participants planned', 'Total Participants Attended', 'Avg. Batch Size');
        $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);
        }

        $trainer_arr = $parent->getQueryValue("SELECT userid,fname,lname FROM " . TABLE_USER . "  WHERE role=5 AND is_deleted=0 AND status=1 ORDER BY userid");

        if (count($trainer_arr) > 0) {
            foreach ($trainer_arr as $res_user) {

                $schdeule_arr = $parent->getQueryValue("SELECT COUNT(id) no_of_session FROM " . TABLE_VIDEOTRAININGSCHEDULE . " WHERE organiser='" . $res_user->userid . "' AND is_deleted=0 AND fromdate>='" . $fromdate . " 00:00:00' AND fromdate<='" . $todate . " 23:59:59' LIMIT 1");
                $no_of_session = (count($schdeule_arr) > 0) ? $schdeule_arr[0]->no_of_session : '';

                $time_arr = $parent->getQueryValue("SELECT SUM(t.timetaken) tottime FROM `" . TABLE_VIDEOTRAININGTIMER . "` t JOIN " . TABLE_VIDEOTRAININGSCHEDULE . " s ON t.sid=s.id WHERE s.organiser='" . $res_user->userid . "' AND s.is_deleted=0 AND s.fromdate>='" . $fromdate . " 00:00:00' AND s.fromdate<='" . $todate . " 23:59:59'");
                $tot_duration = (count($time_arr) > 0) ? $time_arr[0]->tottime : '0';
                $tot_duration = ($tot_duration > 0) ? round($tot_duration / 60) : 0;

                $participant_arr = $parent->getQueryValue("SELECT COUNT(DISTINCT u.id) tot_part FROM `" . TABLE_VIDEOSCHEDULEUSER . "` u JOIN " . TABLE_VIDEOTRAININGSCHEDULE . " s ON u.sid=s.id WHERE u.status=1 AND s.organiser='" . $res_user->userid . "' AND s.is_deleted=0 AND s.fromdate>='" . $fromdate . " 00:00:00' AND s.fromdate<='" . $todate . " 23:59:59'");
                $tot_participant = (count($participant_arr) > 0) ? $participant_arr[0]->tot_part : '0';

                $participant_arr = $parent->getQueryValue("SELECT COUNT(DISTINCT t.userid,t.sid) attend_participant FROM `" . TABLE_VIDEOTRAININGTIMER . "` t JOIN " . TABLE_VIDEOTRAININGSCHEDULE . " s ON t.sid=s.id WHERE s.organiser='" . $res_user->userid . "' AND s.is_deleted=0 AND s.fromdate>='" . $fromdate . " 00:00:00' AND s.fromdate<='" . $todate . " 23:59:59'");
                $tot_attend_participant = (count($participant_arr) > 0) ? $participant_arr[0]->attend_participant : '0';

                $avg_bacth = ($tot_attend_participant > 0) ? round($tot_participant / $tot_attend_participant) : 0;

                $i = 0;
                $row++;
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->lname));
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $no_of_session);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_duration);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_participant);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_participant);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $tot_attend_participant);
                $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $avg_bacth);
            }
        }

        $row = 0;
        $objPHPExcel->createSheet(1);
        $objPHPExcel->setActiveSheetIndex(1);
        $objPHPExcel->getActiveSheet()->setTitle('Report');

        //EXCEL HEADER
        $row += 2;
        $objPHPExcel->getActiveSheet()->SetCellValue('A' . $row, "i-Learn REPORT");
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":H" . $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('d-m-Y', strtotime($fromdate)));
        $objPHPExcel->getActiveSheet()->mergeCells("A" . $row . ":B" . $row);
        $objPHPExcel->getActiveSheet()->SetCellValue('C' . $row, "To :" . date('d-m-Y', strtotime($todate)));
        $objPHPExcel->getActiveSheet()->mergeCells("C" . $row . ":D" . $row);

        //GET ALL TRAINING

        $ilearn_arr = $parent->getQueryValue("SELECT s.*,CONCAT(u.fname,' ',u.lname) as organiser_name,u.emp_code,s.fromdate FROM " . TABLE_VIDEOTRAININGSCHEDULE . " s JOIN " . TABLE_USER . " u ON u.userid=s.organiser WHERE s.fromdate>='" . $fromdate . " 00:00:00' AND s.fromdate<='" . $todate . " 23:59:59' AND s.is_deleted=0 ORDER BY s.fromdate ASC,s.id ASC");

        $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);
        projectfunc::setCellBorder($bordercells_arr, $objPHPExcel);
        $objPHPExcel->getActiveSheet()->getRowDimension(1)->setRowHeight(-1);

        //SET HEADER
        //S.no.    Date    Trainer Name    Participant Name(Planned)    Participant Name(Attended)    smpin    from time    to time    time taken    Dealer Name    Dealer Code    Zone    RM    AM
        $header_arr = array('S.no.', 'Date', 'Trainer Name', 'Participant Name(Planned)', 'Participant Name(Attended)', 'SMPIN', 'From time', 'To time', 'Time taken (Min)', 'Dealer Name', 'Dealer Code', 'Zone', 'ZM', 'RM', 'AM');
        //$header_arr=array('Title','Organiser','From','To','SMPIN','Name','Mobile','EmailID','Dealer code','Dealer','Start time','End Time','Time Taken');
        $i = 0;
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $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('15');
        $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('30');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('15');
        $objPHPExcel->getActiveSheet()->getColumnDimension(getColumnFromNumber($i++))->setWidth('20');
        $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);
        }

        if (count($ilearn_arr) > 0) {
            $sn = 0;
            foreach ($ilearn_arr as $res_ilearn) {

//                $row+=2;
                //                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"Title :".$res_ilearn->title);
                //                $objPHPExcel->getActiveSheet()->mergeCells("A".$row.":D".$row);
                //                $row++;
                //
                //                $objPHPExcel->getActiveSheet()->SetCellValue('A'.$row,"Organiser :".$res_ilearn->organiser_name);
                //                $objPHPExcel->getActiveSheet()->mergeCells("A".$row.":B".$row);
                //                $objPHPExcel->getActiveSheet()->SetCellValue('C'.$row,"From :".date('d-m-Y h:i A',strtotime($res_ilearn->fromdate)));
                //                $objPHPExcel->getActiveSheet()->mergeCells("C".$row.":D".$row);
                //                $objPHPExcel->getActiveSheet()->SetCellValue('E'.$row,"To :".date('d-m-Y h:i A',strtotime($res_ilearn->todate)));
                //                $objPHPExcel->getActiveSheet()->mergeCells("E".$row.":F".$row);

                $user_arr = $parent->getQueryValue("SELECT u.userid,u.fname,u.lname,u.smpin,u.dealer_code, u.role,u.emp_code,u.mobileno,u.emailid,CASE WHEN u.role IN (8,9,10,11,12,13) THEN d.dealer_name ELSE '' END as dealer_name,CASE WHEN u.segment=0 THEN 'Student Learner' WHEN u.segment=1 THEN 'Parents/Guardian' WHEN u.segment=2 THEN 'Instructor' END as segment_str,d.zm_name,d.rm_name,d.am_name,d.zone FROM " . TABLE_VIDEOSCHEDULEUSER . " vu JOIN  " . TABLE_USER . " u ON vu.userid=u.userid LEFT JOIN " . DMS_DEALER . " d ON u.dealer_code=d.dealer_code WHERE vu.status=1 AND u.status='1' AND u.is_deleted='0' AND vu.sid='" . $res_ilearn->id . "' GROUP BY vu.sid,vu.userid  ORDER BY  u.dealer_code ASC,u.fname ASC,u.userid ASC");
                if (count($user_arr) > 0) {
                    foreach ($user_arr as $res_user) {
                        $i = 0;
                        $row++;
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, ++$sn);
                        // $header_arr=array('S.no.','Date','Trainer Name','Participant Name(Planned)','Participant Name(Attended)','SMPIN','From time','To time','Time taken','Dealer Name','Dealer Code','Zone','ZM','RM','AM');
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, date('d-m-Y', strtotime($res_ilearn->fromdate)));
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_ilearn->organiser_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, trim($res_user->fname . " " . $res_user->fname));
                        $check_arr = $parent->getQueryValue("SELECT id FROM `" . TABLE_VIDEOTRAININGTIMER . "` WHERE sid='" . $res_ilearn->id . "' AND userid='" . $res_user->userid . "' LIMIT 1");
                        $attend_name = (count($check_arr) > 0) ? trim($res_user->fname . " " . $res_user->fname) : '-';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $attend_name);

                        $smpin = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->smpin : $res_user->emp_code;
                        $objPHPExcel->getActiveSheet()->getStyle('A' . $row)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_TEXT);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $smpin);

                        $time_arr = $parent->getQueryValue("SELECT MIN(fromtime)fromtime,MAX(totime)totime,SUM(timetaken) tottime FROM `" . TABLE_VIDEOTRAININGTIMER . "` WHERE sid='" . $res_ilearn->id . "' AND userid='" . $res_user->userid . "' GROUP BY userid");
                        $start_time = (count($time_arr) > 0 && $time_arr[0]->fromtime) ? date('d-m-Y h:i A', strtotime($time_arr[0]->fromtime)) : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $start_time);

                        $end_time = (count($time_arr) > 0 && $time_arr[0]->totime) ? date('d-m-Y h:i A', strtotime($time_arr[0]->totime)) : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $end_time);

                        $timetaken = (count($time_arr) > 0 && $time_arr[0]->tottime) ? round($time_arr[0]->tottime / 60) : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $timetaken);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_name);
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $res_user->dealer_code);

                        $zone = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->zone : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $zone);

                        $zm_name = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->zm_name : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $zm_name);

                        $rm_name = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->rm_name : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $rm_name);

                        $am_name = ($res_user->role > 7 && $res_user->role < 14) ? $res_user->am_name : '';
                        $objPHPExcel->getActiveSheet()->SetCellValue(getColumnFromNumber($i++) . $row, $am_name);

                    }
                }

            }
        }

        //LOG
        $logstr = 'ILEARN REPORT EXPORT';
        globalfunc::insertUserLog($parent, $logstr);

        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
        $fpath = "../../" . S3_REPORT_URL;
        $filename = "ilearn_report_" . uniqid() . ".xlsx";
        $objWriter->save($fpath . $filename);

        //CONNECT S3
        $s3 = awsfileserver::connectS3();
        awsfileserver::checkupload($parent, $s3, S3_BUCKET, S3_REPORT_URL . $filename, dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        //delete report
        @unlink(dirname(__FILE__) . "/../../../" . S3_REPORT_URL . $filename);
        $filename = awsfileserver::getS3FileUrl($s3, S3_BUCKET, S3_REPORT_URL . $filename);

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

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