<?php

namespace App\Http\Controllers;

use App\User;
use DB;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;

class CounterReportR5Controller extends BenthamController
{
    public function index(Request $request)
    {
        return View::make('admin.counter_report.indexr5');
    }
    public function CheckUser($user_name)
    {
        $user_obj = User::getUserByName($user_name);
        if ($user_obj) {
            echo $user_obj->id;
            die;
        } else {
            echo '0';
            die;
        }
    }
    public function getMonthsInRange5($startDate, $endDate)
    {
        $months = array();
        while (strtotime($startDate) <= strtotime($endDate)) {
            $months[]  = array('year' => date('Y', strtotime($startDate)), 'month' => date('m', strtotime($startDate)));
            $startDate = date('d M Y', strtotime($startDate . '+ 1 month'));
        }
        return $months;
    }
    public function Export(Request $request)
    {
        $input      = $request->all();
        $error_flag = "";
        $validator  = Validator::make($request->all(), [
            'user_id'       => 'required',
            'user_name'     => 'required',
            'reporttype'    => 'required',
            'frommonthwise' => 'required',
            'tomonthwise'   => 'required',
            'toyearwise'    => 'required',
        ]);
        if ($validator->fails()) {
            $messages = $validator->messages();
            if ($request->get("user") == 'userEx') {
                return redirect()->back()->withErrors($validator)->with('input', $input);
            } else {
                return View::make('admin.counter_report.indexr5')
                    ->withErrors($validator)
                    ->with('input', $input);
            }
        } else {
            $gentype       = $request->get("exportrange");
            $user_id       = $request->get("user_id");
            $reptype       = $request->get("reporttype");
            $frommonthwise = $request->get('frommonthwise');
            $fromyearwise  = $request->get('fromyearwise');
            $tomonthwise   = $request->get('tomonthwise');
            $toyearwise    = $request->get('toyearwise');
            $exporttype    = $request->get('exporttype');
            $eureka_ingenta = $request->get('eureka_ingenta');
            $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
            $todate        = $toyearwise . '-' . $tomonthwise . '-28';
            $UDRBegin   = date('Y-m-01', strtotime($fromdate));
            $UDREnd     = date('Y-m-t', strtotime($todate));
            
            $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $year      = $monthsyear[0]['year'];
                $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
            }

            //if partial data return
            if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
                $error_flag = 3031;
                $UDREnd     = date('Y-m-t');
            }
            
            $resultuser = User::getUserById($user_id);
            $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
            if ($eureka_ingenta == 0) {
                $result = $this->getqueryReportTypeWise($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd);
            } elseif ($eureka_ingenta == 1 && ($reptype == 1 || $reptype == 2)) {
                $result = $this->getqueryReportTypeWiseIngenta($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd);
            } elseif ($eureka_ingenta == 2  && ($reptype == 1 || $reptype == 2)) {
                $result = $this->getqueryReportTypeWiseMerged($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd);
            }
            if (empty($result)) {
                $error_flag = 3030;
            }
            $year = substr($year, 2, 2);
            //---------------------Book Report Table format------------------------//
            if ($gentype == "0" and $reptype == "6") {
                $table = $this->get_TR_J1($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag);
            }
            if ($gentype == "0" and $reptype == "7") {
                $table = $this->get_TR_J2($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag);
            }
            if ($gentype == "0" and $reptype == "8") {
                $table = $this->get_TR_B3($request, $error_flag);
            }
            //------------------------End------------------------//
            //---------------------Journal Report Table Format------------------------//
            if ($gentype == "2" and $reptype == "1") {
                $table = $this->get_TR_J1($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag);
            }
            if ($gentype == "2" and $reptype == "2") {
                $table = $this->get_TR_J2($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag);
            }
            if ($gentype == "2" and $reptype == "3") {
                $table = $this->get_TR_J3($request, $error_flag);
            }
            if ($gentype == "2" and $reptype == "4") {
                $table = $this->get_TR_J4($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag);
            }
            if ($gentype == "2" and $reptype == "5") {
                $table = $this->get_TR($request, $error_flag);
            }
            if ($gentype == "3" and $reptype == "8") {
                $table = $this->get_PR_P1($request, $error_flag);
            }
            if ($gentype == "3" and $reptype == "9") {
                $table = $this->get_PR($request, $error_flag);
            }
            if ($exporttype == 1) {
                //export in Excel
                header("Content-type: application/vnd.ms-excel");
                header("Content-Disposition: attachment; filename=countercompliant_report.xls");
                print $table;
                exit;
            } else {
                $tsv = $this->getTableTSV($table);
                header("Content-type: text/tsv");
                header("Content-Disposition: attachment; filename=countercompliant_report.tsv");
                // BOM header UTF-8
                echo "\xEF\xBB\xBF";
                print $tsv;
                exit;
            }
        }
    }
    public function getTopHeader($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $colspanccnt, $error_flag)
    {
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $exceptions_data = '';
        $exception_data = "";
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];
            if($error_flag==3031){                   
                $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        
        
        $tomonthwise1   = \Request::get('tomonthwise');
        $toyearwise1    = \Request::get('toyearwise');                
        $todate1        = $toyearwise1 . '-' . $tomonthwise1 . '-28';        
        $orginal_UDREnd = date('Y-m-t', strtotime($todate1));

        //-------------------Book Report Heading Start--------------------
        if ($gentype == "0" && $reptype == "6") {
            $Report_Name       = "Book Requests (Excluding OA_Gold)";
            $Report_ID         = "TR_B1";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = "Total_Item_Requests; Unique_Title_Requests";
            $Report_Filters    = "Data_Type=Book; Access_Type=Controlled; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        if ($gentype == "0" && $reptype == "7") {
            $Report_Name       = "Book Access Denied";
            $Report_ID         = "TR_B2";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = "Limit_Exceeded; No_License";
            $Report_Filters    = "Data_Type=Book; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        //-------------------------------End-----------------------------
        //-------------Journal Report Heading Start----------------------
        if ($gentype == "2" && $reptype == "1") {
            $Report_Name       = "Journal Requests (Excluding OA_Gold)";
            $Report_ID         = "TR_J1";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = "Total_Item_Requests; Unique_Item_Requests";
            $Report_Filters    = "Data_Type=Journal; Access_Type=Controlled; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        if ($gentype == "2" && $reptype == "2") {
            $Report_Name       = "Journal Access Denied";
            $Report_ID         = "TR_J2";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = "No_License; Limit_Exceeded";
            $Report_Filters    = "Data_Type=Journal; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        if ($gentype == "2" && $reptype == "4") {
            $Report_Name       = "Journal Requests by YOP (Excluding OA_Gold)";
            $Report_ID         = "TR_J4";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = "Total_Item_Requests; Unique_Item_Requests";
            $Report_Filters    = "Data_Type=Journal; Access_Type=Controlled; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        //-------------End--------------------
        $blankTDs    = "";
        $monthsyear  = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));
        if (($gentype == "2" && $reptype == 3) || ($gentype == "0")) {
            $YOP = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>YOP</b></td>";
        }
        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td ><b>Reporting_Period</b></td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";
        return $tableTopHeader;
    }
    public function getTableHeader($gentype, $reptype, $monthsyear)
    {
        $YOP               = "";
        $tableHeader = "";
        $ISBN               = "";
        $tableHeader_years = "";
        if ($gentype == "0") {
            $ISBN = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>ISBN</b></td>";
        }
        if (($gentype == "2" && $reptype == 4) || ($gentype == "0")) {
            $YOP = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>YOP</b></td>";
        }
        $tableHeader .= "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Title</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Platform</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>DOI</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Proprietary_ID</b></td>
                                " . $ISBN . "
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Print_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Online_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>URI</b></td>
                                " . $YOP . "
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[$j]['year'];
            $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
            $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
        }
        $tableHeader .= $tableHeader_years;
        $tableHeader .= " </tr>   ";
        return $tableHeader;
    }
    public function getTableRow($gentype, $reptype, $row, $monthsyear, $Total_Item_Requests = 'Total_Item_Requests')
    {
        $tr_data = "";
        $jan     = 0;
        $feb     = 0;
        $mar     = 0;
        $apr     = 0;
        $may     = 0;
        $jun     = 0;
        $july    = 0;
        $aug     = 0;
        $sep     = 0;
        $oct     = 0;
        $nov     = 0;
        $dec     = 0;
        $tr_data    = "";
        $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
        $jan  = $jan + round($row->January, 2);
        $feb  = $feb + round($row->February, 2);
        $mar  = $mar + round($row->March, 2);
        $apr  = $apr + round($row->April, 2);
        $may  = $may + round($row->May, 2);
        $jun  = $jun + round($row->June, 2);
        $july = $july + round($row->July, 2);
        $aug  = $aug + round($row->August, 2);
        $sep  = $sep + round($row->September, 2);
        $oct  = $oct + round($row->October, 2);
        $nov  = $nov + round($row->November, 2);
        $dec  = $dec + round($row->December, 2);
        $tr_data .= "<tr align='center'> ";
        $tr_data .= "<td align='left'>" . trim($row->title) . "</td>
                    <td>Bentham Science Publishers</td>
                    <td></td>
                    <td>Eurekaselect</td>
                    <td>" . trim($row->doi) . "</td>
                    <td>&nbsp;</td>";
        if ($gentype == "0") {
            $tr_data .= "<td>" . $row->isbn . "</td>";
        }
        $tr_data .= "<td>" . trim($row->issn) . "</td>
                        <td>" . trim($row->eissn) . "</td>
                        <td>https://www.eurekaselect.com/node/" . $row->nid . "</td>";
        if (($gentype == "2" && $reptype == 4) || ($gentype == "0")) {
            $tr_data .= "<td>" . $row->volume_year . "</td>";
        }
        $tr_data .= "<td>" . $Total_Item_Requests . "</td>
                    <td>" . $total_sale . "</td>
                    ";
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
            //$tr_data .="<td>".round($row->$monthName,2)."</td>";
            if ($monthName == "January") {
                $tr_data .= "<td>" . round($row->January, 2) . "</td>";
            }
            if ($monthName == "February") {
                $tr_data .= "<td>" . round($row->February, 2) . "</td>";
            }
            if ($monthName == "March") {
                $tr_data .= "<td>" . round($row->March, 2) . "</td>";
            }
            if ($monthName == "April") {
                $tr_data .= "<td>" . round($row->April, 2) . "</td>";
            }
            if ($monthName == "May") {
                $tr_data .= "<td>" . round($row->May, 2) . "</td>";
            }
            if ($monthName == "June") {
                $tr_data .= "<td>" . round($row->June, 2) . "</td>";
            }
            if ($monthName == "July") {
                $tr_data .= "<td>" . round($row->July, 2) . "</td>";
            }
            if ($monthName == "August") {
                $tr_data .= "<td>" . round($row->August, 2) . "</td>";
            }
            if ($monthName == "September") {
                $tr_data .= "<td>" . round($row->September, 2) . "</td>";
            }
            if ($monthName == "October") {
                $tr_data .= "<td>" . round($row->October, 2) . "</td>";
            }
            if ($monthName == "November") {
                $tr_data .= "<td>" . round($row->November, 2) . "</td>";
            }
            if ($monthName == "December") {
                $tr_data .= "<td>" . round($row->December, 2) . "</td>";
            }
        }
        $tr_data .= "</tr>";
        return $tr_data;
    }
    public function getTableUniqueRow($gentype, $reptype, $row, $monthsyear, $Unique_Item_Requests = 'Unique_Item_Requests')
    {
        $tr_unique_data = "";
        $tr_unique_data = "";
        $unique_jan     = 0;
        $unique_feb     = 0;
        $unique_mar     = 0;
        $unique_apr     = 0;
        $unique_may     = 0;
        $unique_jun     = 0;
        $unique_july    = 0;
        $unique_aug     = 0;
        $unique_sep     = 0;
        $unique_oct     = 0;
        $unique_nov     = 0;
        $unique_dec     = 0;
        $tr_unique_data    = "";
        $total_unique_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
        $unique_jan  = $unique_jan + round($row->unique_January, 2);
        $unique_feb  = $unique_feb + round($row->unique_February, 2);
        $unique_mar  = $unique_mar + round($row->unique_March, 2);
        $unique_apr  = $unique_apr + round($row->unique_April, 2);
        $unique_may  = $unique_may + round($row->unique_May, 2);
        $unique_jun  = $unique_jun + round($row->unique_June, 2);
        $unique_july = $unique_july + round($row->unique_July, 2);
        $unique_aug  = $unique_aug + round($row->unique_August, 2);
        $unique_sep  = $unique_sep + round($row->unique_September, 2);
        $unique_oct  = $unique_oct + round($row->unique_October, 2);
        $unique_nov  = $unique_nov + round($row->unique_November, 2);
        $unique_dec  = $unique_dec + round($row->unique_December, 2);
        $tr_unique_data .= "<tr align='center'> ";
        $tr_unique_data .= "<td align='left'>" . trim($row->title) . "</td>
                    <td>Bentham Science Publishers</td>
                    <td></td>
                    <td>Eurekaselect</td>
                    <td>" . trim($row->doi) . "</td>
                    <td>&nbsp;</td>";
        if ($gentype == "0") {
            $tr_unique_data .= "<td>" . $row->isbn . "</td>";
        }
        $tr_unique_data .= "<td>" . trim($row->issn) . "</td>
                                <td>" . trim($row->eissn) . "</td>
                                <td>https://www.eurekaselect.com/node/" . $row->nid . "</td>";
        if (($gentype == "2" && $reptype == 4) || ($gentype == "0" && $reptype == 6)) {
            $tr_unique_data .= "<td>" . $row->volume_year . "</td>";
        }
        $tr_unique_data .= "<td>" . $Unique_Item_Requests . "</td>
                    <td>" . $total_unique_sale . "</td>
                    ";
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
            //$tr_data .="<td>".round($row->$monthName,2)."</td>";
            if ($monthName == "January") {
                $tr_unique_data .= "<td>" . round($row->unique_January, 2) . "</td>";
            }
            if ($monthName == "February") {
                $tr_unique_data .= "<td>" . round($row->unique_February, 2) . "</td>";
            }
            if ($monthName == "March") {
                $tr_unique_data .= "<td>" . round($row->unique_March, 2) . "</td>";
            }
            if ($monthName == "April") {
                $tr_unique_data .= "<td>" . round($row->unique_April, 2) . "</td>";
            }
            if ($monthName == "May") {
                $tr_unique_data .= "<td>" . round($row->unique_May, 2) . "</td>";
            }
            if ($monthName == "June") {
                $tr_unique_data .= "<td>" . round($row->unique_June, 2) . "</td>";
            }
            if ($monthName == "July") {
                $tr_unique_data .= "<td>" . round($row->unique_July, 2) . "</td>";
            }
            if ($monthName == "August") {
                $tr_unique_data .= "<td>" . round($row->unique_August, 2) . "</td>";
            }
            if ($monthName == "September") {
                $tr_unique_data .= "<td>" . round($row->unique_September, 2) . "</td>";
            }
            if ($monthName == "October") {
                $tr_unique_data .= "<td>" . round($row->unique_October, 2) . "</td>";
            }
            if ($monthName == "November") {
                $tr_unique_data .= "<td>" . round($row->unique_November, 2) . "</td>";
            }
            if ($monthName == "December") {
                $tr_unique_data .= "<td>" . round($row->unique_December, 2) . "</td>";
            }
        }
        $tr_unique_data .= "</tr>";
        return $tr_unique_data;
    }
    public function getQueryReportTypeWise($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd)
    {
		if (strpos($user_id, ',') !== false) {
            $a = explode(",", $user_id);
            $user_id_list = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_id_list);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        } else {
            $user = User::where('id', $user_id)->first();
            $user_clause = " and a.user_id ='" . $user->id . "' ";
            $user_clause_denail = " and a.userid ='" . $user->id . "' ";
            $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
            $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        }

        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        //-------------------------Book Report Start---------------------------------
        if ($gentype == "0") {
            //$query = $_SESSION['query'];
            if ($reptype == "6") { //TR_B1
                $query = "SELECT *,
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'January' THEN ebook_volume_id END) AS 'January',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'February' THEN ebook_volume_id END) AS 'February',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'March' THEN ebook_volume_id END) AS 'March',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'April' THEN ebook_volume_id END) AS 'April',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'May' THEN ebook_volume_id END) AS 'May',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'June' THEN ebook_volume_id END) AS 'June',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'July' THEN ebook_volume_id END) AS 'July',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'August' THEN ebook_volume_id END) AS 'August',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'September' THEN ebook_volume_id END) AS 'September',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'October' THEN ebook_volume_id END) AS 'October',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'November' THEN ebook_volume_id END) AS 'November',
                    COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'December' THEN ebook_volume_id END) AS 'December',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'January' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_January',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'February' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_February',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'March' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_March',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'April' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_April',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'May' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_May',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'June' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_June',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'July' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_July',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'August' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_August',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'September' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_September',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'October' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_October',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'November' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_November',
                    COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'December' THEN CONCAT(session_id, ',', ebook_volume_id) END) AS 'unique_December'
                FROM
                    (
                    SELECT
                        dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year
                    FROM download_count_volume dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                            and dcv.content_download_type!='FO'
                            " . $user_clause_volume . "
                    UNION
                SELECT
                    dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year
                FROM download_count_chapter dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and dcv.content_download_type!='FO'
                " . $user_clause_volume . "
                ) AS tmp
                GROUP BY ebook_volume_id";

                //print($query); exit;
                $result = DB::select($query);
                return $result;
            } elseif ($reptype == "7") { //TR_B2
                $query = "select *,
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='January' THEN ev_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='February' THEN ev_id END) AS 'February', COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='March' THEN ev_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='April' THEN ev_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='May' THEN ev_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='June' THEN ev_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='July' THEN ev_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='August' THEN ev_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='September' THEN ev_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='October' THEN ev_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='November' THEN ev_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='December' THEN ev_id END) AS 'December'
                from (
                    SELECT
                    ev.ebook_volume_id,    e.title,    ev.nid,    ev.doi,    ev.eissn,    ev.issn,    r.created_on,    r.ev_id,    ev.isbn,    ev.eisbn,    r.cat_id AS category,    c.cat_type cat_type,    Y.year AS volume_year
                FROM access_denied_ebook_report r
                INNER JOIN access_denied_report_category c ON r.cat_id = c.id
                INNER JOIN ebook_volume ev ON r.ev_id = ev.ebook_volume_id
                INNER JOIN ebook e ON e.ebook_id = ev.ebook_id
                INNER JOIN year Y ON Y.id = ev.year_id
                and to_days(from_unixtime(created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and r.user_id='" . $user_id . "'
                 UNION
                 SELECT
                    ev.ebook_volume_id,    e.title,    ev.nid,    ev.doi,    ev.eissn,    ev.issn,    r.created_on,    r.ev_id,    ev.isbn,    ev.eisbn,    r.cat_id AS category,    c.cat_type cat_type,    Y.year AS volume_year
                FROM access_denied_chapter_report r
                INNER JOIN access_denied_report_category c ON r.cat_id = c.id
                INNER JOIN ebook_volume ev ON r.ev_id = ev.ebook_volume_id
                INNER JOIN ebook e ON e.ebook_id = ev.ebook_id
                INNER JOIN year Y ON Y.id = ev.year_id
                and to_days(from_unixtime(created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and r.user_id='" . $user_id . "' ) as tmp
                group by ebook_volume_id order by title ";

                // print($query); exit;
                $result = DB::select($query);
                return $result;
            }
        }
        //-------------------------End---------------------------------
        //-------------------------Journal Report Start---------------------------------
        if ($gentype == "2") {
            //$query = $_SESSION['query'];
            if ($reptype == "1") {
                $query = "select j.nid,j.title,j.issn ,j.eissn, j.journal_id, j.doi,
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN article_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN article_id END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN article_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN article_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN article_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN article_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN article_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN article_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN article_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN article_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN article_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN article_id END) AS 'December' ,
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',article_id) END) AS 'unique_December'
                from download_count_articles a inner join journal j on a.journal_id = j.journal_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and a.content_download_type!='FO'
                and a.download_file_type!=0
                " . $user_clause . "
                group by title";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            } elseif ($reptype == "2") {
                $query = "select j.nid,j.title,j.doi,a.jid,j.issn,j.eissn,a.catid as category, c.cat_type cat_type,
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='January' THEN aid END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='February' THEN aid END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='March' THEN aid END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='April' THEN aid END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='May' THEN aid END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='June' THEN aid END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='July' THEN aid END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='August' THEN aid END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='September' THEN aid END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='October' THEN aid END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='November' THEN aid END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='December' THEN aid END) AS 'December'
                from access_denied_report a
                inner join access_denied_report_category c on c.id=a.catid
                inner join journal j on a.jid = j.journal_id
                 and to_days(from_unixtime(created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                 " . $user_clause_denail . "  group by title order by title";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            } elseif ($reptype == "4") {
                $query = "select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi,a.issue_id,a.article_id,
                CASE WHEN a.issue_id=0 THEN '9999' ELSE CASE WHEN y.year!=''  THEN y.year  ELSE '0001' END END AS 'volume_year',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN article_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN article_id END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN article_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN article_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN article_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN article_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN article_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN article_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN article_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN article_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN article_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN article_id END) AS 'December' ,
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',article_id) END) AS 'unique_December'
                from download_count_articles a
                inner join journal j on a.journal_id = j.journal_id
                inner join volume v on v.volume_id = a.volume_id
                left join year y on y.id = v.year_id
                where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and a.content_download_type!='FO'
                and a.download_file_type!=0
                " . $user_clause . "
                group by a.journal_id,volume_year
                order by j.title ASC,y.year DESC";
                $result = DB::select($query);
                return $result;
            }
        }
        //-------------------------End---------------------------------
    }
    public function getQueryReportTypeWiseIngenta($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd)
    {
        $user = User::where('id', $user_id)->first();
        $user_clause = " and u.id ='" . $user->id . "' ";
        $user_clause_denail = " and u.id ='" . $user->id . "' ";
        $user_clause_volume = " and u.id ='" . $user->id . "' ";
        $user_clause_chapter = " and u.id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and u.id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and u.id in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and u.id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and u.id in (" . $rec->user_associations . ") ";
        }
        //-------------------------Journal Report Start---------------------------------
        if ($gentype == "2") {
            //$query = $_SESSION['query'];
            if ($reptype == "1") {
                $query = "select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_December'
                from users u 
                inner join ingenta_views i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  i.total_downloads > 0 and  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause . "
                group by title";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            } elseif ($reptype == "2") {
                $query = "select j.nid,j.title,j.doi,i.jid,j.issn,j.eissn,'1' as category,'no_license' as cat_type,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN total END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN total END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN total END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN total END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN total END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN total END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN total END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN total END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN total END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN total END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN total END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN total END) AS 'December'
                from users u
                inner join ingenta_access_denied i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  i.total > 0 and  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                 " . $user_clause_denail . "  group by j.title order by j.title";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            }
        }
        //-------------------------End---------------------------------
    }
    public function getQueryReportTypeWiseMerged($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd)
    {
        $user = User::where('id', $user_id)->first();
        $ingenta_user_clause = " and u.id ='" . $user->id . "' ";
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $ingenta_user_clause = " and u.id  in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        //-------------------------Journal Report Start---------------------------------
        if ($gentype == "2") {
            //$query = $_SESSION['query'];
            if ($reptype == "1") {
                $query = "
                Select  nid, title, issn, eissn, journal_id, doi, 
                sum(January) as January,
                sum(February) as February,
                sum(March) as March,
                sum(April) as April,
                sum(May) as May,
                sum(June) as June,
                sum(July) as July,
                sum(August) as August,
                sum(September) as September,
                sum(October) as October,
                sum(November) as November,
                sum(December) as December,
                sum(unique_January) as unique_January,
                sum(unique_February) as unique_February,
                sum(unique_March) as unique_March,
                sum(unique_April) as unique_April,
                sum(unique_May) as unique_May,
                sum(unique_June) as unique_June,
                sum(unique_July) as unique_July,
                sum(unique_August) as unique_August,
                sum(unique_September) as unique_September,
                sum(unique_October) as unique_October,
                sum(unique_November) as unique_November,
                sum(unique_December) as unique_December
                from (
                SELECT j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi, 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'January' THEN article_id END) AS 'January',
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'February' THEN article_id END) AS 'February',
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'March' THEN article_id END ) AS 'March', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'April' THEN article_id END ) AS 'April', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'May' THEN article_id END ) AS 'May', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'June' THEN article_id END ) AS 'June', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'July' THEN article_id END ) AS 'July', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'August' THEN article_id END ) AS 'August', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'September' THEN article_id END ) AS 'September', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'October' THEN article_id END ) AS 'October', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'November' THEN article_id END ) AS 'November', 
                COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'December' THEN article_id END ) AS 'December', 
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'January' THEN CONCAT(session_id, ',', article_id) END ) AS 'unique_January', 
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'February' THEN CONCAT(session_id, ',', article_id) END ) AS 'unique_February', 
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'March' THEN CONCAT(session_id, ',', article_id) END ) AS 'unique_March', 
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'April' THEN CONCAT(session_id, ',', article_id) END ) AS 'unique_April', 
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'May' THEN CONCAT(session_id, ',', article_id) END ) AS 'unique_May',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'June' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_June',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'July' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_July',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'August' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_August',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'September' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_September',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'October' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_October',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'November' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_November',
                COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(TIMESTAMP), '%M') = 'December' THEN CONCAT(session_id, ',', article_id) END) AS 'unique_December'
                FROM download_count_articles a
                INNER JOIN journal j ON a.journal_id = j.journal_id  
                where  TO_DAYS(from_unixtime(timestamp)) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause . "
                AND a.content_download_type != 'FO' AND a.download_file_type != 0 
                GROUP BY title 
                UNION
                select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_December'
                from users u 
                inner join ingenta_views i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where   i.total_downloads > 0 and TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $ingenta_user_clause . "
                group by i.jid 
                ) as tmp group by nid order by nid
                ";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            } elseif ($reptype == "2") {
                $query = "Select  nid,title,doi,jid,issn,eissn,category,cat_type,
                sum(January) as January,
                sum(February) as February,
                sum(March) as March,
                sum(April) as April,
                sum(May) as May,
                sum(June) as June,
                sum(July) as July,
                sum(August) as August,
                sum(September) as September,
                sum(October) as October,
                sum(November) as November,
                sum(December) as December
                from (
                select j.nid,j.title,j.doi,a.jid,j.issn,j.eissn,a.catid as category, c.cat_type cat_type,
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='January' THEN aid END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='February' THEN aid END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='March' THEN aid END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='April' THEN aid END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='May' THEN aid END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='June' THEN aid END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='July' THEN aid END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='August' THEN aid END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='September' THEN aid END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='October' THEN aid END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='November' THEN aid END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='December' THEN aid END) AS 'December'
                from access_denied_report a
                inner join access_denied_report_category c on c.id=a.catid
                inner join journal j on a.jid = j.journal_id
                where  TO_DAYS(from_unixtime(created_on)) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause_denail . "  group by j.title
                
                union 
                
                select j.nid,j.title,j.doi,i.jid,j.issn,j.eissn,'1' as category,'no_license' as cat_type,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN total END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN total END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN total END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN total END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN total END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN total END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN total END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN total END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN total END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN total END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN total END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN total END) AS 'December'
                from users u 
                inner join ingenta_access_denied i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  i.total > 0 and   TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $ingenta_user_clause . "
                group by i.jid 
                
                ) as tmp group by nid order by nid
              ";
                //print($query); exit;
                $result = DB::select($query);
                return $result;
            }
        }
        //-------------------------End---------------------------------
    }
    public function get_TR_J1($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag)
    {
        $colspanccnt = 22;
        //print_r($years); die;
        $tableHeader = "<table width='800' align='center' >";
        $tableHeader .= $this->getTopHeader($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $colspanccnt, $error_flag);
        $tableHeader .= $this->getTableHeader($gentype, $reptype, $monthsyear);
        $tr_data = "";
        $tr_unique_data = "";
        $jan     = 0;
        $feb     = 0;
        $mar     = 0;
        $apr     = 0;
        $may     = 0;
        $jun     = 0;
        $july    = 0;
        $aug     = 0;
        $sep     = 0;
        $oct     = 0;
        $nov     = 0;
        $dec     = 0;
        $unique_jan     = 0;
        $unique_feb     = 0;
        $unique_mar     = 0;
        $unique_apr     = 0;
        $unique_may     = 0;
        $unique_jun     = 0;
        $unique_july    = 0;
        $unique_aug     = 0;
        $unique_sep     = 0;
        $unique_oct     = 0;
        $unique_nov     = 0;
        $unique_dec     = 0;
        if ($result) {
            foreach ($result as $row) {
                $tr_data .= $this->getTableRow($gentype, $reptype, $row, $monthsyear);
                if ($gentype == "0" and ($reptype == "1" or $reptype == "6")) {
                    $Unique_Item_Requests = 'Unique_Title_Requests';
                    $tr_data .= $this->getTableUniqueRow($gentype, $reptype, $row, $monthsyear, $Unique_Item_Requests);
                } else {
                    $tr_data .= $this->getTableUniqueRow($gentype, $reptype, $row, $monthsyear);
                }
            }
        }
        $table = $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function get_TR_J2($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag)
    {
        $colspanccnt = 22;
        //print_r($years); die;
        $tableHeader = "<table width='800' align='center' >";
        $tableHeader .= $this->getTopHeader($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $colspanccnt, $error_flag);
        $tableHeader .= $this->getTableHeader($gentype, $reptype, $monthsyear);
        $tr_data        = "";
        $tr_unique_data = "";
        $jan            = 0;
        $feb            = 0;
        $mar            = 0;
        $apr            = 0;
        $may            = 0;
        $jun            = 0;
        $july           = 0;
        $aug            = 0;
        $sep            = 0;
        $oct            = 0;
        $nov            = 0;
        $dec            = 0;
        if ($result) {
            foreach ($result as $row) {
                $tr_data .= $this->getTableRow($gentype, $reptype, $row, $monthsyear, 'No_License');
            }
        }
        $table = $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function get_TR_J3($request)
    {
        $error_flag  = "";
        $exception_data = "";
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $gentype       = $request->get("exportrange");
        $user_id       = $request->get("user_id");
        $reptype       = $request->get("reporttype");
        $frommonthwise = $request->get('frommonthwise');
        $fromyearwise  = $request->get('toyearwise');
        $tomonthwise   = $request->get('tomonthwise');
        $toyearwise    = $request->get('toyearwise');
        $exporttype    = $request->get('exporttype');
        $eureka_ingenta = $request->get('eureka_ingenta');
        $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
        $todate        = $toyearwise . '-' . $tomonthwise . '-28';
        $UDRBegin = date('Y-m-01', strtotime($fromdate));
        $UDREnd   = $orginal_UDREnd = date(format: 'Y-m-t', timestamp: strtotime($todate));
        
        $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[0]['year'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
        }
        //if partial data return
        if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
            $error_flag = 3031;
            $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
            $UDREnd     = date('Y-m-t');            
        }

        $resultuser = User::getUserById($user_id);
        $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
        $user = User::where('id', $user_id)->first();
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $ingenta_user_clause = " and u.id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $ingenta_user_clause = " and u.id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        //-------------------------Book Report Start---------------------------------
        if ($eureka_ingenta == 0) {
            $query = " select * from (
            select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi, a.issue_id,a.article_id,
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN article_id END) AS 'January',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN  article_id END) AS 'February',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN  article_id END) AS 'March',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN  article_id END) AS 'April',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN  article_id END) AS 'May',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN  article_id END) AS 'June',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN  article_id END) AS 'July',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN  article_id END) AS 'August',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN  article_id END) AS 'September',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN  article_id END) AS 'October',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN  article_id END) AS 'November',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN  article_id END) AS 'December' ,
            
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_January',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_February',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_March',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_April',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_May',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_June',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_July',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_August',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_September',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_October',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_November',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_December',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
            from download_count_articles a
            inner join journal j on a.journal_id = j.journal_id
            inner join volume v on v.volume_id = a.volume_id
            left join year y on y.id = v.year_id
            where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "')  and to_days('" . $UDREnd . "')
             and a.download_file_type!=0
           " . $user_clause . " group by a.journal_id
            ) as tmp ";
            $result = DB::select($query);
            $query_investigation = "
            select * from (
            select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi, a.issue_id,a.article_id,
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN article_id END) AS 'January',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN  article_id END) AS 'February',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN  article_id END) AS 'March',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN  article_id END) AS 'April',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN  article_id END) AS 'May',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN  article_id END) AS 'June',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN  article_id END) AS 'July',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN  article_id END) AS 'August',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN  article_id END) AS 'September',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN  article_id END) AS 'October',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN  article_id END) AS 'November',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN  article_id END) AS 'December' ,
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_January',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_February',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_March',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_April',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_May',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_June',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_July',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_August',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_September',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_October',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_November',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_December',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
            COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
            COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
            from access_investigation_report a
            inner join journal j on a.journal_id = j.journal_id
            inner join volume v on v.volume_id = a.volume_id
            left join year y on y.id = v.year_id
            where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
            and a.investigation_type!=0
            " . $user_clause . " group by a.journal_id ) as tmp ";
            $result_investigation = DB::select($query_investigation);
        } elseif ($eureka_ingenta == 1) {
            $query = " 
            select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN open_access_downloads END) AS 'oa_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN open_access_downloads END) AS 'oa_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN open_access_downloads END) AS 'oa_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN open_access_downloads END) AS 'oa_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN open_access_downloads END) AS 'oa_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN open_access_downloads END) AS 'oa_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN open_access_downloads END) AS 'oa_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN open_access_downloads END) AS 'oa_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN open_access_downloads END) AS 'oa_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN open_access_downloads END) AS 'oa_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN open_access_downloads END) AS 'oa_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN open_access_downloads END) AS 'oa_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN open_access_downloads END) AS 'oa_unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN open_access_downloads END) AS 'oa_unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN open_access_downloads END) AS 'oa_unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN open_access_downloads END) AS 'oa_unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN open_access_downloads END) AS 'oa_unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN open_access_downloads END) AS 'oa_unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN open_access_downloads END) AS 'oa_unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN open_access_downloads END) AS 'oa_unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN open_access_downloads END) AS 'oa_unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN open_access_downloads END) AS 'oa_unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN open_access_downloads END) AS 'oa_unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN open_access_downloads END) AS 'oa_unique_December'
                
                from users u 
                inner join ingenta_views i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $ingenta_user_clause . "
                group by i.jid";
            $result = DB::select($query);
            $query_investigation = "
    
            select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'January',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'February',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'March',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'April',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'May',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'June',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'July',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'August',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'September',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'October',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'November',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'December',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'unique_January',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'unique_February',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'unique_March',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'unique_April',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'unique_May',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'unique_June',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'unique_July',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'unique_August',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'unique_September',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'unique_October',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'unique_November',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'unique_December',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'oa_January',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'oa_February',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'oa_March',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'oa_April',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'oa_May',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'oa_June',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'oa_July',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'oa_August',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'oa_September',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'oa_October',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'oa_November',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'oa_December',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'oa_unique_January',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'oa_unique_February',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'oa_unique_March',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'oa_unique_April',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'oa_unique_May',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'oa_unique_June',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'oa_unique_July',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'oa_unique_August',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'oa_unique_September',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'oa_unique_October',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'oa_unique_November',
            SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'oa_unique_December'
            
            from users u 
            inner join ingenta_views i on i.identity_id=u.ingenta_id
            inner join journal j on j.journal_id=i.jid
            where  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
            " . $ingenta_user_clause . "
            group by i.jid";
            $result_investigation = DB::select($query_investigation);
        } elseif ($eureka_ingenta == 2) {
            $query = " 
            Select  nid, title, issn, eissn, journal_id, doi, 
                sum(January) as January,
                sum(February) as February,
                sum(March) as March,
                sum(April) as April,
                sum(May) as May,
                sum(June) as June,
                sum(July) as July,
                sum(August) as August,
                sum(September) as September,
                sum(October) as October,
                sum(November) as November,
                sum(December) as December,
                sum(unique_January) as unique_January,
                sum(unique_February) as unique_February,
                sum(unique_March) as unique_March,
                sum(unique_April) as unique_April,
                sum(unique_May) as unique_May,
                sum(unique_June) as unique_June,
                sum(unique_July) as unique_July,
                sum(unique_August) as unique_August,
                sum(unique_September) as unique_September,
                sum(unique_October) as unique_October,
                sum(unique_November) as unique_November,
                sum(unique_December) as unique_December,
                sum(oa_January) as oa_January,
                sum(oa_February) as oa_February,
                sum(oa_March) as oa_March,
                sum(oa_April) as oa_April,
                sum(oa_May) as oa_May,
                sum(oa_June) as oa_June,
                sum(oa_July) as oa_July,
                sum(oa_August) as oa_August,
                sum(oa_September) as oa_September,
                sum(oa_October) as oa_October,
                sum(oa_November) as oa_November,
                sum(oa_December) as oa_December,
                sum(oa_unique_January) as oa_unique_January,
                sum(oa_unique_February) as oa_unique_February,
                sum(oa_unique_March) as oa_unique_March,
                sum(oa_unique_April) as oa_unique_April,
                sum(oa_unique_May) as oa_unique_May,
                sum(oa_unique_June) as oa_unique_June,
                sum(oa_unique_July) as oa_unique_July,
                sum(oa_unique_August) as oa_unique_August,
                sum(oa_unique_September) as oa_unique_September,
                sum(oa_unique_October) as oa_unique_October,
                sum(oa_unique_November) as oa_unique_November,
                sum(oa_unique_December) as oa_unique_December
                from (
                select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi,
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN article_id END) AS 'January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN  article_id END) AS 'February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN  article_id END) AS 'March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN  article_id END) AS 'April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN  article_id END) AS 'May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN  article_id END) AS 'June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN  article_id END) AS 'July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN  article_id END) AS 'August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN  article_id END) AS 'September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN  article_id END) AS 'October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN  article_id END) AS 'November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN  article_id END) AS 'December' ,
                
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_December',
    
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
    
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
    
                from download_count_articles a
                inner join journal j on a.journal_id = j.journal_id
                inner join volume v on v.volume_id = a.volume_id
                left join year y on y.id = v.year_id
                where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "')  and to_days('" . $UDREnd . "')
                 and a.download_file_type!=0
               " . $user_clause . " group by a.journal_id
               
                
                UNION 
                select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN (subscriber_Downloads+free_Downloads) END) AS 'unique_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN open_access_downloads END) AS 'oa_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN open_access_downloads END) AS 'oa_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN open_access_downloads END) AS 'oa_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN open_access_downloads END) AS 'oa_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN open_access_downloads END) AS 'oa_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN open_access_downloads END) AS 'oa_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN open_access_downloads END) AS 'oa_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN open_access_downloads END) AS 'oa_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN open_access_downloads END) AS 'oa_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN open_access_downloads END) AS 'oa_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN open_access_downloads END) AS 'oa_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN open_access_downloads END) AS 'oa_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN open_access_downloads END) AS 'oa_unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN open_access_downloads END) AS 'oa_unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN open_access_downloads END) AS 'oa_unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN open_access_downloads END) AS 'oa_unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN open_access_downloads END) AS 'oa_unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN open_access_downloads END) AS 'oa_unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN open_access_downloads END) AS 'oa_unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN open_access_downloads END) AS 'oa_unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN open_access_downloads END) AS 'oa_unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN open_access_downloads END) AS 'oa_unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN open_access_downloads END) AS 'oa_unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN open_access_downloads END) AS 'oa_unique_December'
                
                from users u 
                inner join ingenta_views i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $ingenta_user_clause . "
                group by i.jid
                
                ) as tmp group by nid order by nid
                ";
            $result = DB::select($query);
            $query_investigation = "
                Select  nid, title, issn, eissn, journal_id, doi, 
                sum(January) as January,
                sum(February) as February,
                sum(March) as March,
                sum(April) as April,
                sum(May) as May,
                sum(June) as June,
                sum(July) as July,
                sum(August) as August,
                sum(September) as September,
                sum(October) as October,
                sum(November) as November,
                sum(December) as December,
                sum(unique_January) as unique_January,
                sum(unique_February) as unique_February,
                sum(unique_March) as unique_March,
                sum(unique_April) as unique_April,
                sum(unique_May) as unique_May,
                sum(unique_June) as unique_June,
                sum(unique_July) as unique_July,
                sum(unique_August) as unique_August,
                sum(unique_September) as unique_September,
                sum(unique_October) as unique_October,
                sum(unique_November) as unique_November,
                sum(unique_December) as unique_December,
                sum(oa_January) as oa_January,
                sum(oa_February) as oa_February,
                sum(oa_March) as oa_March,
                sum(oa_April) as oa_April,
                sum(oa_May) as oa_May,
                sum(oa_June) as oa_June,
                sum(oa_July) as oa_July,
                sum(oa_August) as oa_August,
                sum(oa_September) as oa_September,
                sum(oa_October) as oa_October,
                sum(oa_November) as oa_November,
                sum(oa_December) as oa_December,
                sum(oa_unique_January) as oa_unique_January,
                sum(oa_unique_February) as oa_unique_February,
                sum(oa_unique_March) as oa_unique_March,
                sum(oa_unique_April) as oa_unique_April,
                sum(oa_unique_May) as oa_unique_May,
                sum(oa_unique_June) as oa_unique_June,
                sum(oa_unique_July) as oa_unique_July,
                sum(oa_unique_August) as oa_unique_August,
                sum(oa_unique_September) as oa_unique_September,
                sum(oa_unique_October) as oa_unique_October,
                sum(oa_unique_November) as oa_unique_November,
                sum(oa_unique_December) as oa_unique_December
                from (
                select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi, 
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN article_id END) AS 'January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN  article_id END) AS 'February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN  article_id END) AS 'March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN  article_id END) AS 'April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN  article_id END) AS 'May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN  article_id END) AS 'June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN  article_id END) AS 'July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN  article_id END) AS 'August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN  article_id END) AS 'September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN  article_id END) AS 'October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN  article_id END) AS 'November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN  article_id END) AS 'December' ,
    
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_December',
    
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
    
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
                from access_investigation_report a
                inner join journal j on a.journal_id = j.journal_id
                inner join volume v on v.volume_id = a.volume_id
                left join year y on y.id = v.year_id
                where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and a.investigation_type!=0
                " . $user_clause . " group by a.journal_id 
                
                UNION
                select j.nid, j.title, j.issn, j.eissn, j.journal_id, j.doi,
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'unique_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'oa_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'oa_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'oa_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'oa_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'oa_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'oa_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'oa_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'oa_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'oa_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'oa_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'oa_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'oa_December',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'January' THEN round(article_abstract_views/2) END) AS 'oa_unique_January',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'February' THEN round(article_abstract_views/2) END) AS 'oa_unique_February',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'March' THEN round(article_abstract_views/2) END) AS 'oa_unique_March',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'April' THEN round(article_abstract_views/2) END) AS 'oa_unique_April',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'May' THEN round(article_abstract_views/2) END) AS 'oa_unique_May',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'June' THEN round(article_abstract_views/2) END) AS 'oa_unique_June',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'July' THEN round(article_abstract_views/2) END) AS 'oa_unique_July',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'August' THEN round(article_abstract_views/2) END) AS 'oa_unique_August',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'September' THEN round(article_abstract_views/2) END) AS 'oa_unique_September',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'October' THEN round(article_abstract_views/2) END) AS 'oa_unique_October',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'November' THEN round(article_abstract_views/2) END) AS 'oa_unique_November',
                SUM( CASE WHEN DATE_FORMAT(i.created_on, '%M') = 'December' THEN round(article_abstract_views/2) END) AS 'oa_unique_December'
                
                from users u 
                inner join ingenta_views i on i.identity_id=u.ingenta_id
                inner join journal j on j.journal_id=i.jid
                where  TO_DAYS(i.created_on) BETWEEN to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $ingenta_user_clause . "
                group by i.jid
                
                ) as tmp group by nid order by nid
                ";
            $result_investigation = DB::select($query_investigation);
        }
        $table = "<table width='800' align='center' >";
        $tableHeader_years = '';
        $blankTDs = "";
        $colspanccnt = 24;
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));
        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $exceptions_data = '';       
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];
            if($error_flag==3031){                   
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        if ($gentype == "2" && $reptype == "3") {
            $Report_Name       = "Journal Usage by Access Type";
            $Report_ID         = "TR_J3";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Type      = array("Total_Item_Investigations", "Total_Item_Requests", "Unique_Item_Investigations", "Unique_Item_Requests");
            $Metric_Types      = implode("; ", $Metric_Type);
            $Report_Filters    = "Data_Type=Journal; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td >Reporting_Period</td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";

        $tableHeader = "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Title</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Platform</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>DOI</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Proprietary_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Print_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Online_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>URI</b></td>
                                 <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Type</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[$j]['year'];
            $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
            $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
        }
        $tableHeader .= $tableHeader_years;
        $tableHeader .= " </tr>   ";
        $table_rows      = "";
        $tr_data         = "";
        $tr_unique_data  = "";
        $jan             = 0;
        $feb             = 0;
        $mar             = 0;
        $apr             = 0;
        $may             = 0;
        $jun             = 0;
        $july            = 0;
        $aug             = 0;
        $sep             = 0;
        $oct             = 0;
        $nov             = 0;
        $dec             = 0;
        $unique_jan      = 0;
        $unique_feb      = 0;
        $unique_mar      = 0;
        $unique_apr      = 0;
        $unique_may      = 0;
        $unique_jun      = 0;
        $unique_july     = 0;
        $unique_aug      = 0;
        $unique_sep      = 0;
        $oct             = 0;
        $unique_nov      = 0;
        $unique_dec      = 0;
        $prev_journal_id = 0;
        $TR              = array();
        $cnt = 0;
        if ($result) {
            foreach ($result as $row) {
                if (in_array('Total_Item_Requests', $Metric_Type)) {
                    $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    if ($total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['Access_Type'] = 'Controlled';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale']  = $total_sale;
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                    if ($oa_total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['Access_Type'] = 'OA_Gold';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale'] = $oa_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_December, 2);
                        $cnt++;
                    }
                }
                if (in_array('Unique_Item_Requests', $Metric_Type)) {
                    $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                    if ($unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = $unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);
                        $cnt++;
                    }
                    $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                    if ($oa_unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                        $cnt++;
                    }
                }
            } //while
        } //if

        if ($result_investigation) {
            foreach ($result_investigation as $row) {
                if (in_array('Total_Item_Investigations', $Metric_Type)) {
                    $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    if ($total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['Access_Type'] = 'Controlled';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale']  = $total_sale;
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                    if ($oa_total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['Access_Type'] = 'OA_Gold';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $oa_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_December, 2);
                        $cnt++;
                    }
                }
                if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                    $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                    if ($unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);
                        $cnt++;
                    }
                    $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                    if ($oa_unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                        $cnt++;
                    }
                }
            } //while
        } //if
        //print_r($TR); die;
        usort($TR, function ($a, $b) {
            //$c = $a['title'] - $b['title'];
            //$c .= $b['volume_year'] - $a['volume_year'];
            //return $c;
            if ($a['title'] == $b['title']) {
                return $b['Access_Type'];
            }
            return strcmp($a['title'], $b['title']);
        });
        //print_r($TR); die;
        //ksort($TR);
        for ($i = 0; $i < count($TR); $i++) {

            $tr_data .= "<tr align='center'> ";
            $tr_data .= "<td align='left'>" . trim($TR[$i]['title']) . "</td>
                        <td>Bentham Science Publishers</td>
                        <td></td>
                        <td>Eurekaselect</td>
                        <td>" . $TR[$i]['doi'] . "</td>
                        <td>&nbsp;</td>
                        <td>" . $TR[$i]['issn'] . "</td>
                        <td>" . $TR[$i]['eissn'] . "</td>
                        <td>https://www.eurekaselect.com/node/" . $TR[$i]['nid'] . "</td>";

            $tr_data .= "<td>" . $TR[$i]['Access_Type'] . "</td>";
            $tr_data .= "<td>" . $TR[$i]['Metric_Type'] . "</td>";
            $tr_data .= "<td>" . $TR[$i]['total_sale'] . "</td>";

            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
                //$tr_data .="<td>".round($row->$monthName,2)."</td>";
                if ($monthName == "January") {
                    $tr_data .= "<td>" . round($TR[$i]['jan'], 2) . "</td>";
                }
                if ($monthName == "February") {
                    $tr_data .= "<td>" . round($TR[$i]['feb'], 2) . "</td>";
                }
                if ($monthName == "March") {
                    $tr_data .= "<td>" . round($TR[$i]['mar'], 2) . "</td>";
                }
                if ($monthName == "April") {
                    $tr_data .= "<td>" . round($TR[$i]['apr'], 2) . "</td>";
                }
                if ($monthName == "May") {
                    $tr_data .= "<td>" . round($TR[$i]['may'], 2) . "</td>";
                }
                if ($monthName == "June") {
                    $tr_data .= "<td>" . round($TR[$i]['jun'], 2) . "</td>";
                }
                if ($monthName == "July") {
                    $tr_data .= "<td>" . round($TR[$i]['july'], 2) . "</td>";
                }
                if ($monthName == "August") {
                    $tr_data .= "<td>" . round($TR[$i]['aug'], 2) . "</td>";
                }
                if ($monthName == "September") {
                    $tr_data .= "<td>" . round($TR[$i]['sep'], 2) . "</td>";
                }
                if ($monthName == "October") {
                    $tr_data .= "<td>" . round($TR[$i]['oct'], 2) . "</td>";
                }
                if ($monthName == "November") {
                    $tr_data .= "<td>" . round($TR[$i]['nov'], 2) . "</td>";
                }
                if ($monthName == "December") {
                    $tr_data .= "<td>" . round($TR[$i]['dec'], 2) . "</td>";
                }
            }
            $tr_data .= "</tr>";
        } //while
        $table .= $tableTopHeader . $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function get_TR_J4($result, $gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $monthsyear, $error_flag)
    {
        $colspanccnt = 23;
        //print_r($years); die;
        $tableHeader = "<table width='800' align='center'>";
        $tableHeader .= $this->getTopHeader($gentype, $reptype, $org_name, $user_id, $UDRBegin, $UDREnd, $colspanccnt, $error_flag);
        $tableHeader .= $this->getTableHeader($gentype, $reptype, $monthsyear);
        $tr_data        = "";
        $tr_unique_data = "";
        $jan            = 0;
        $feb            = 0;
        $mar            = 0;
        $apr            = 0;
        $may            = 0;
        $jun            = 0;
        $july           = 0;
        $aug            = 0;
        $sep            = 0;
        $oct            = 0;
        $nov            = 0;
        $dec            = 0;
        $unique_jan     = 0;
        $unique_feb     = 0;
        $unique_mar     = 0;
        $unique_apr     = 0;
        $unique_may     = 0;
        $unique_jun     = 0;
        $unique_july    = 0;
        $unique_aug     = 0;
        $unique_sep     = 0;
        $oct            = 0;
        $unique_nov     = 0;
        $unique_dec     = 0;
        if ($result) {
            foreach ($result as $row) {
                $tr_data .= $this->getTableRow($gentype, $reptype, $row, $monthsyear);
                $tr_data .= $this->getTableUniqueRow($gentype, $reptype, $row, $monthsyear);
            }
        }
        $table = $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function findKeyVal($array, $keySearch, $doi,$Access_Type)
    {
        for ($i = 0; $i < count($array); $i++) {
            // print($array[$i]['Metric_Type'].' == '.$keySearch.' && '.$array[$i]['doi'].'=='.$doi.'<br>');				
            if ($array[$i]['Metric_Type'] == $keySearch && $array[$i]['doi'] == $doi && $array[$i]['Access_Type'] == $Access_Type) {
                return true;
            }
        }
        return false;
    }
    public function get_TR($request)
    {
        $error_flag  = "";
        $exception_data = "";
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $gentype       = $request->get("exportrange");
        $user_id       = $request->get("user_id");
        $reptype       = $request->get("reporttype");
        $frommonthwise = $request->get('frommonthwise');
        $fromyearwise  = $request->get('toyearwise');
        $tomonthwise   = $request->get('tomonthwise');
        $toyearwise    = $request->get('toyearwise');
        $exporttype    = $request->get('exporttype');
        $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
        $todate        = $toyearwise . '-' . $tomonthwise . '-28';
        $journal_result        = false;
        $journal_result_no_license  = false;
        $Metric_Type = $request->get("Metric_Type");
        //$mtcolumn = $_REQUEST['mtcolumn'];
        $Data_Type = $request->get("Data_Type");
        $dtcolumn  = $request->get("dtcolumn");
        $Access_Type = $request->get("Access_Type");
        $atcolumn    = $request->get("atcolumn");
        $Access_Method = $request->get("Access_Method");
        $amcolumn      = $request->get("amcolumn");
        $YOP       = $request->get("YOP");
        $yopcolumn = $request->get("yopcolumn");
        $Exclude_Monthly = $request->get("Exclude_Monthly");
        $UDRBegin = date('Y-m-01', strtotime($fromdate));
        $UDREnd   = $orginal_UDREnd = date('Y-m-t', strtotime($todate));
        if (empty($Access_Type)) {
            $Access_Type = array('Controlled');
        }
        
        $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[0]['year'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
        }
        //if partial data return
        if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
            $error_flag = 3031;
            $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
            $UDREnd     = date('Y-m-t');
        }

        $resultuser = User::getUserById($user_id);
        $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
        $user = User::where('id', $user_id)->first();
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        $user_clause_denail_book = " and r.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail_book = " and r.user_id in (" . $rec->user_associations . ") ";
        }
        if (in_array('Journal', $Data_Type)) {

            //$journal_result='';
            if (in_array('Total_Item_Requests', $Metric_Type) || in_array('Unique_Item_Requests', $Metric_Type)) {
                $query = " select * from (
                    select j.nid,j.title,j.issn ,j.eissn, j.journal_id,j.doi, a.issue_id,a.article_id, a.content_download_type,
                    CASE WHEN a.content_download_type='FO' THEN 'OA_Gold' ELSE 'Controlled' END AS access_type,
                    CASE WHEN a.issue_id=0 THEN '9999' ELSE CASE WHEN y.year!=''  THEN y.year  ELSE '0001' END END AS 'volume_year',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN article_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN  article_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN  article_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN  article_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN  article_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN  article_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN  article_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN  article_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN  article_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN  article_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN  article_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN  article_id END) AS 'December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO') THEN concat(session_id,',',article_id) END) AS 'unique_December',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
                    from download_count_articles a
                    inner join journal j on a.journal_id = j.journal_id
                    inner join volume v on v.volume_id = a.volume_id
                    left join year y on y.id = v.year_id
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')";
                $query .= " and a.download_file_type!=0
                    " . $user_clause . " group by a.journal_id,volume_year ) as tmp ";

                if ($YOP != '') {
                    $query .= " where volume_year='$YOP' ";
                }
                $query .= " order by title ASC,volume_year DESC ";
                //echo $query; die;
                $journal_result = DB::select($query);
            }
            $journal_result_no_license = '';
            if (in_array('No_License', $Metric_Type)) {
                $query_no_license = "select * from (select j.nid,j.title,j.issn,j.eissn,a.jid journal_id,j.doi doi,ar.issue_id,ar.article_id,
                    (CASE WHEN ar.issue_id=0 THEN '9999' ELSE CASE WHEN y.year!='' THEN y.year ELSE '0001' END END) AS 'volume_year',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='January' THEN aid END) AS 'January',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='February' THEN aid END) AS 'February',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='March' THEN aid END) AS 'March',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='April' THEN aid END) AS 'April',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='May' THEN aid END) AS 'May',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='June' THEN aid END) AS 'June',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='July' THEN aid END) AS 'July',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='August' THEN aid END) AS 'August',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='September' THEN aid END) AS 'September',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='October' THEN aid END) AS 'October',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='November' THEN aid END) AS 'November',
                    COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='December' THEN aid END) AS 'December'
                    from access_denied_report a
                    inner join article ar on ar.article_id = a.aid
                    inner join volume v on v.volume_id = ar.volume_id
                    inner join journal j on a.jid = j.journal_id
                    inner join year y on y.id = v.year_id
                    where to_days(from_unixtime(a.created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause_denail . " group by a.jid,volume_year ) as tmp ";
                if ($YOP != '') {
                    $query_no_license .= " where volume_year='$YOP' ";
                }
                $query_no_license .= " order by title ASC,volume_year DESC ";
                $journal_result_no_license = DB::select($query_no_license);
            }
            $journal_result_investigation = '';
            if (in_array('Total_Item_Investigations', $Metric_Type) || in_array('Unique_Item_Investigations', $Metric_Type)) {
                $query_investigation = " select * from (
                select j.nid,j.title,j.issn,j.eissn,a.journal_id journal_id,j.doi,a.issue_id,a.article_id, a.content_download_type,
                CASE WHEN a.content_download_type='FO' THEN 'OA_Gold' ELSE 'Controlled' END AS access_type,
                CASE WHEN a.issue_id=0 THEN '9999' ELSE CASE WHEN y.year!=''  THEN y.year  ELSE '0001' END END AS 'volume_year',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN article_id END) AS 'January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN  article_id END) AS 'February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN  article_id END) AS 'March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN  article_id END) AS 'April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN  article_id END) AS 'May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN  article_id END) AS 'June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN  article_id END) AS 'July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN  article_id END) AS 'August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN  article_id END) AS 'September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN  article_id END) AS 'October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN  article_id END) AS 'November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN  article_id END) AS 'December' ,
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type!='FO' ) THEN concat(session_id,',',article_id) END) AS 'unique_December',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and a.content_download_type='FO') THEN article_id END) AS 'oa_January',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN  article_id END) AS 'oa_February',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN  article_id END) AS 'oa_March',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN  article_id END) AS 'oa_April',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN  article_id END) AS 'oa_May',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN  article_id END) AS 'oa_June',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN  article_id END) AS 'oa_July',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN  article_id END) AS 'oa_August',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN  article_id END) AS 'oa_September',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN  article_id END) AS 'oa_October',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN  article_id END) AS 'oa_November',
                COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN  article_id END) AS 'oa_December' ,
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_January',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_February',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_March',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_April',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_May',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_June',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_July',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_August',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_September',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_October',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_November',
                COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and a.content_download_type='FO') THEN concat(session_id,',',article_id) END) AS 'oa_unique_December'
                from access_investigation_report a
                    inner join journal j on a.journal_id = j.journal_id
                    inner join volume v on v.volume_id = a.volume_id
                    inner join year y on y.id = v.year_id
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and a.investigation_type!=0 ";

                $query_investigation .= " " . $user_clause . " group by a.journal_id,volume_year
            ) as tmp ";
                if ($YOP != '') {
                    $query_investigation .= " where volume_year='$YOP' ";
                }
                $query_investigation .= " order by title ASC,volume_year DESC ";
                $journal_result_investigation = DB::select($query_investigation);
            }
        }
        if (in_array('Book', $Data_Type)) {
            $book_result = '';
            if (in_array('Total_Item_Requests', $Metric_Type) || in_array('Unique_Item_Requests', $Metric_Type)) {
                
                $query = "SELECT *,
                   
                   COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_December',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and content_download_type='FO') THEN ebook_volume_id END) AS 'oa_January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_December' ,
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_December',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_December'
                    
                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year
                        ,'Book' as Section_Type
                    FROM download_count_volume dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                            
                            " . $user_clause_volume . "
                    UNION
                SELECT
                dcv.chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year
                    ,'Chapter' as Section_Type
                FROM download_count_chapter dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                
                " . $user_clause_volume . "
                ) AS tmp";
                if ($YOP != '') {
                    $query .= " where volume_year='$YOP' ";
                }
                $query .= " GROUP BY ebook_volume_id,Section_Type
							order by ebook_volume_id,Section_Type ";
                //dd($query);
                $book_result = DB::select($query);
            }
            $book_result_investigation = '';
            if (in_array('Total_Item_Investigations', $Metric_Type) || in_array('Unique_Item_Investigations', $Metric_Type)) {
                $query_investigation = "SELECT *,
                   
                   COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_December',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and content_download_type='FO') THEN ebook_volume_id END) AS 'oa_January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_December' ,
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_December',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_December'
                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year
                        ,'Book' as Section_Type
                    FROM ebook_access_investigation_report dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.investigation_type!=0        
                            " . $user_clause_volume . "
                    UNION
                    SELECT
                    dcv.chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year
                        ,'Chapter' as Section_Type
                    FROM chapter_access_investigation_report dcv
                    INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                    INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON  Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.investigation_type!=0 
                    " . $user_clause_volume . "
                    ) AS tmp";
                if ($YOP != '') {
                    $query_investigation .= " where volume_year='$YOP' ";
                }
                $query_investigation .= " GROUP BY ebook_volume_id,Section_Type 
											order by ebook_volume_id,Section_Type ";
                //print($query_investigation); die;          
                $book_result_investigation = DB::select($query_investigation);
            }
            $book_result_no_license = '';
            if (in_array('No_License', $Metric_Type)) {
                $query_no_license = "select *,
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='January' THEN ev_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='February' THEN ev_id END) AS 'February', COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='March' THEN ev_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='April' THEN ev_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='May' THEN ev_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='June' THEN ev_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='July' THEN ev_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='August' THEN ev_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='September' THEN ev_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='October' THEN ev_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='November' THEN ev_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(created_on),'%M')='December' THEN ev_id END) AS 'December'
                from (
                    SELECT
                    ev.ebook_volume_id,    e.title,    ev.nid,    ev.doi,    ev.eissn,    ev.issn,    r.created_on,    r.ev_id,    ev.isbn,    ev.eisbn,    r.cat_id AS category,    c.cat_type cat_type,    Y.year AS volume_year
                    ,'Book' as Section_Type
                FROM access_denied_ebook_report r
                INNER JOIN access_denied_report_category c ON r.cat_id = c.id
                INNER JOIN ebook_volume ev ON r.ev_id = ev.ebook_volume_id
                INNER JOIN ebook e ON e.ebook_id = ev.ebook_id
                INNER JOIN year Y ON Y.id = ev.year_id
                and to_days(from_unixtime(created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause_denail_book . "
                 UNION
                 SELECT
                    ev.ebook_volume_id,    e.title,    ev.nid,    ev.doi,    ev.eissn,    ev.issn,    r.created_on,    r.ev_id,    ev.isbn,    ev.eisbn,    r.cat_id AS category,    c.cat_type cat_type,    Y.year AS volume_year
                    ,'Chapter' as Section_Type
                FROM access_denied_chapter_report r
                INNER JOIN access_denied_report_category c ON r.cat_id = c.id
                INNER JOIN ebook_volume ev ON r.ev_id = ev.ebook_volume_id
                INNER JOIN ebook e ON e.ebook_id = ev.ebook_id
                INNER JOIN year Y ON Y.id = ev.year_id
                and to_days(from_unixtime(created_on)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause_denail_book . "
                 ) as tmp";
                if ($YOP != '') {
                    $query_no_license .= " where volume_year='$YOP' ";
                }
                $query_no_license .= " group by ebook_volume_id order by title ";

                $book_result_no_license = DB::select($query_no_license);
            }
        }
        $tableHeader       = '';
        $tableHeader_years = '';
        $table             = "<table width='800' align='center' >";
        $blankTDs    = "";
        $colspanccnt = 27;
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));
        $Attributes_To_Show[] = 'Section_Type';
        if ($dtcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Data_Type';
        }

        if ($yopcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'YOP';
        }
        if ($atcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Access_Type';
        }

        if ($amcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Access_Method';
        }
        $Exclude_Monthly_Details = '';
        if ($Exclude_Monthly == 1) {
            $colspanccnt             = $colspanccnt - count($monthsyear);
            $Exclude_Monthly_Details = 'Exclude_Monthly_Details=True; ';
        }
        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $exceptions_data = '';
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];
            if($error_flag==3031){                   
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        if ($gentype == "2" && $reptype == "5") {
            $Report_Name       = "Title Master Report";
            $Report_ID         = "TR";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Types      = implode("; ", $Metric_Type);
            if (in_array('Controlled', $Access_Type) && in_array('OA_Gold', $Access_Type)) {
                $Report_Filters    = "Data_Type=" . implode("|", $Data_Type) . "; Access_Method=" . $Access_Method;
            } else if (in_array('Controlled', $Access_Type) || in_array('OA_Gold', $Access_Type)) {
                $Report_Filters    = "Data_Type=" . implode("|", $Data_Type) . "; Access_Type=" . implode("|", $Access_Type) . "; Access_Method=" . $Access_Method;
            }
            $Report_Attributes = $Exclude_Monthly_Details . "Attributes_To_Show=" . implode('|', $Attributes_To_Show);
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td >Reporting_Period</td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";
        $DataTypetd = "";
        if ($dtcolumn == 1) {
            $DataTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Data_Type</b></td>";
        }
        $SectionTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Section_Type</b></td>";
        $YOPtd = "";
        if ($yopcolumn == 1) {
            $YOPtd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>YOP</b></td>";
        }
        $AccessTypetd = "";
        if ($atcolumn == 1) {
            $AccessTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Type</b></td>";
        }
        $AccessMethodtd = "";
        if ($amcolumn == 1) {
            $AccessMethodtd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Method</b></td>";
        }
        $MetricTypetd = "";
        //if($mtcolumn==1){
        $MetricTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>";
        //}
        $tableHeader .= "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Title</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Platform</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>DOI</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Proprietary_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>ISBN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Print_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Online_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>URI</b></td>
                                " . $DataTypetd . $SectionTypetd . $YOPtd . $AccessTypetd . $AccessMethodtd . $MetricTypetd . "
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        if ($Exclude_Monthly == 1) {
        } else {
            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $year      = $monthsyear[$j]['year'];
                $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
                $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
            }
            $tableHeader .= $tableHeader_years;
        }
        $tableHeader .= " </tr>   ";
        $table_rows      = "";
        $tr_data         = "";
        $tr_unique_data  = "";
        $jan             = 0;
        $feb             = 0;
        $mar             = 0;
        $apr             = 0;
        $may             = 0;
        $jun             = 0;
        $july            = 0;
        $aug             = 0;
        $sep             = 0;
        $oct             = 0;
        $nov             = 0;
        $dec             = 0;
        $unique_jan      = 0;
        $unique_feb      = 0;
        $unique_mar      = 0;
        $unique_apr      = 0;
        $unique_may      = 0;
        $unique_jun      = 0;
        $unique_july     = 0;
        $unique_aug      = 0;
        $unique_sep      = 0;
        $oct             = 0;
        $unique_nov      = 0;
        $unique_dec      = 0;
        $prev_journal_id = 0;
        $TR              = array();
        $cnt = 0;
        if (in_array('Journal', $Data_Type)) {
            if ($journal_result) {
                foreach ($journal_result as $row) {
                    if (in_array('Total_Item_Requests', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                                $TR[$cnt]['Access_Type'] = 'Controlled';
                                $TR[$cnt]['total_sale']  = $total_sale;
                                $TR[$cnt]['jan']  = round($row->January, 2);
                                $TR[$cnt]['feb']  = round($row->February, 2);
                                $TR[$cnt]['mar']  = round($row->March, 2);
                                $TR[$cnt]['apr']  = round($row->April, 2);
                                $TR[$cnt]['may']  = round($row->May, 2);
                                $TR[$cnt]['jun']  = round($row->June, 2);
                                $TR[$cnt]['july'] = round($row->July, 2);
                                $TR[$cnt]['aug']  = round($row->August, 2);
                                $TR[$cnt]['sep']  = round($row->September, 2);
                                $TR[$cnt]['oct']  = round($row->October, 2);
                                $TR[$cnt]['nov']  = round($row->November, 2);
                                $TR[$cnt]['dec']  = round($row->December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                                $TR[$cnt]['Access_Type'] = 'OA_Gold';
                                $TR[$cnt]['total_sale'] = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_December, 2);
                                $cnt++;
                            }
                        }
                    }

                    if (in_array('Unique_Item_Requests', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Metric_Type'] = 'Unique_Item_Requests';
                                $TR[$cnt]['Access_Type'] = 'Controlled';
                                $TR[$cnt]['total_sale'] = $total_sale;

                                $TR[$cnt]['jan']  = round($row->unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->unique_April, 2);
                                $TR[$cnt]['may']  = round($row->unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->unique_June, 2);
                                $TR[$cnt]['july'] = round($row->unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->unique_December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Metric_Type'] = 'Unique_Item_Requests';
                                $TR[$cnt]['Access_Type'] = 'OA_Gold';
                                $TR[$cnt]['total_sale'] = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                                $cnt++;
                            }
                        }
                    }
                } //while
            } //if
            if ($journal_result_no_license) {
                foreach ($journal_result_no_license as $row) {
                    $TR[$cnt]['title']       = trim($row->title);
                    $TR[$cnt]['volume_year'] = $row->volume_year;
                    $TR[$cnt]['doi']         = trim($row->doi);
                    $TR[$cnt]['isbn']        = "";
                    $TR[$cnt]['issn']        = trim($row->issn);
                    $TR[$cnt]['eissn']       = trim($row->eissn);
                    $TR[$cnt]['nid']         = $row->nid;
                    $TR[$cnt]['Data_Type'] = 'Journal';
                    $TR[$cnt]['Section_Type'] = 'Article';
                    $TR[$cnt]['Metric_Type'] = 'No_License';
                    $TR[$cnt]['Access_Type'] = 'Controlled';
                    $TR[$cnt]['total_sale'] = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    $TR[$cnt]['jan']  = round($row->January, 2);
                    $TR[$cnt]['feb']  = round($row->February, 2);
                    $TR[$cnt]['mar']  = round($row->March, 2);
                    $TR[$cnt]['apr']  = round($row->April, 2);
                    $TR[$cnt]['may']  = round($row->May, 2);
                    $TR[$cnt]['jun']  = round($row->June, 2);
                    $TR[$cnt]['july'] = round($row->July, 2);
                    $TR[$cnt]['aug']  = round($row->August, 2);
                    $TR[$cnt]['sep']  = round($row->September, 2);
                    $TR[$cnt]['oct']  = round($row->October, 2);
                    $TR[$cnt]['nov']  = round($row->November, 2);
                    $TR[$cnt]['dec']  = round($row->December, 2);
                    $cnt++;
                } //while
            } //if

            if ($journal_result_investigation) {
                foreach ($journal_result_investigation as $row) {
                    if (in_array('Total_Item_Investigations', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Access_Type']  = 'Controlled';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                                $TR[$cnt]['total_sale']  = $total_sale;
                                $TR[$cnt]['jan']  = round($row->January, 2);
                                $TR[$cnt]['feb']  = round($row->February, 2);
                                $TR[$cnt]['mar']  = round($row->March, 2);
                                $TR[$cnt]['apr']  = round($row->April, 2);
                                $TR[$cnt]['may']  = round($row->May, 2);
                                $TR[$cnt]['jun']  = round($row->June, 2);
                                $TR[$cnt]['july'] = round($row->July, 2);
                                $TR[$cnt]['aug']  = round($row->August, 2);
                                $TR[$cnt]['sep']  = round($row->September, 2);
                                $TR[$cnt]['oct']  = round($row->October, 2);
                                $TR[$cnt]['nov']  = round($row->November, 2);
                                $TR[$cnt]['dec']  = round($row->December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Access_Type']  = 'OA_Gold';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                                $TR[$cnt]['total_sale']  = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_December, 2);
                                $cnt++;
                            }
                        }
                    }
                    if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Access_Type']  = 'Controlled';
                                $TR[$cnt]['Metric_Type'] = 'Unique_Item_Investigations';
                                $TR[$cnt]['total_sale'] = $total_sale;

                                $TR[$cnt]['jan']  = round($row->unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->unique_April, 2);
                                $TR[$cnt]['may']  = round($row->unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->unique_June, 2);
                                $TR[$cnt]['july'] = round($row->unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->unique_December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['volume_year'] = $row->volume_year;
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = "";
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Journal';
                                $TR[$cnt]['Section_Type'] = 'Article';
                                $TR[$cnt]['Access_Type']  = 'OA_Gold';
                                $TR[$cnt]['Metric_Type'] = 'Unique_Item_Investigations';
                                $TR[$cnt]['total_sale']  = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                                $cnt++;
                            }
                        }
                    }
                } //while
            } //if    

        }
        if (in_array('Book', $Data_Type)) {
            if ($book_result) {
                foreach ($book_result as $row) {

                    if (in_array('Total_Item_Requests', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = trim($row->isbn);
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type'] = 'Controlled';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                                $TR[$cnt]['total_sale']  = $total_sale;
                                $TR[$cnt]['jan']  = round($row->January, 2);
                                $TR[$cnt]['feb']  = round($row->February, 2);
                                $TR[$cnt]['mar']  = round($row->March, 2);
                                $TR[$cnt]['apr']  = round($row->April, 2);
                                $TR[$cnt]['may']  = round($row->May, 2);
                                $TR[$cnt]['jun']  = round($row->June, 2);
                                $TR[$cnt]['july'] = round($row->July, 2);
                                $TR[$cnt]['aug']  = round($row->August, 2);
                                $TR[$cnt]['sep']  = round($row->September, 2);
                                $TR[$cnt]['oct']  = round($row->October, 2);
                                $TR[$cnt]['nov']  = round($row->November, 2);
                                $TR[$cnt]['dec']  = round($row->December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = trim($row->isbn);
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type'] = 'OA_Gold';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                                $TR[$cnt]['total_sale'] = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_December, 2);
                                $cnt++;
                            }
                        }
                    }
                    if (in_array('Unique_Item_Requests', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                            if ($unique_total_sale > 0) {
                                $TR[$cnt]['title']             = trim($row->title);
                                $TR[$cnt]['doi']               = trim($row->doi);
                                $TR[$cnt]['isbn']              = trim($row->isbn);
                                $TR[$cnt]['issn']              = trim($row->issn);
                                $TR[$cnt]['eissn']             = trim($row->eissn);
                                $TR[$cnt]['nid']               = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type']       = 'Controlled';
                                $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                                $TR[$cnt]['total_sale'] = $unique_total_sale;
                                $TR[$cnt]['jan']  = round($row->unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->unique_April, 2);
                                $TR[$cnt]['may']  = round($row->unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->unique_June, 2);
                                $TR[$cnt]['july'] = round($row->unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->unique_December, 2);
                                $ucnt = $cnt;
                                $cnt++;
                                
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                            if ($oa_unique_total_sale > 0) {
                                $TR[$cnt]['title']             = trim($row->title);
                                $TR[$cnt]['doi']               = trim($row->doi);
                                $TR[$cnt]['isbn']              = trim($row->isbn);
                                $TR[$cnt]['issn']              = trim($row->issn);
                                $TR[$cnt]['eissn']             = trim($row->eissn);
                                $TR[$cnt]['nid']               = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type']       = 'OA_Gold';
                                $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                                $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                                $ucnt = $cnt;
                                $cnt++;
                                
                            }
                        }
                    }
                    if (in_array('Unique_Title_Requests', $Metric_Type)) {                       
                        if (in_array('Controlled', $Access_Type)) {
                            if ($this->findKeyVal($TR, 'Unique_Title_Requests', trim($row->doi),'Controlled')) {
                            } else {
                                $unique_title_total_sale = round($row->unique_title_January, 2) + round($row->unique_title_February, 2) + round($row->unique_title_March, 2) + round($row->unique_title_April, 2) + round($row->unique_title_May, 2) + round($row->unique_title_June, 2) + round($row->unique_title_July, 2) + round($row->unique_title_August, 2) + round($row->unique_title_September, 2) + round($row->unique_title_October, 2) + round($row->unique_title_November, 2) + round($row->unique_title_December, 2);
                                if ($unique_title_total_sale > 0) {
                                    $TR[$cnt]['title']             = trim($row->title);
                                    $TR[$cnt]['doi']               = trim($row->doi);
                                    $TR[$cnt]['isbn']              = trim($row->isbn);
                                    $TR[$cnt]['issn']              = trim($row->issn);
                                    $TR[$cnt]['eissn']             = trim($row->eissn);
                                    $TR[$cnt]['nid']               = $row->nid;
                                    $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                    $TR[$cnt]['Data_Type'] = 'Book';
                                    $TR[$cnt]['Section_Type'] = '';
                                    $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                    $TR[$cnt]['Access_Type']       = 'Controlled';
                                    $TR[$cnt]['Metric_Type']       = 'Unique_Title_Requests';
                                    $TR[$cnt]['total_sale'] = $unique_title_total_sale;
                                    $TR[$cnt]['jan']  = round($row->unique_title_January, 2);
                                    $TR[$cnt]['feb']  = round($row->unique_title_February, 2);
                                    $TR[$cnt]['mar']  = round($row->unique_title_March, 2);
                                    $TR[$cnt]['apr']  = round($row->unique_title_April, 2);
                                    $TR[$cnt]['may']  = round($row->unique_title_May, 2);
                                    $TR[$cnt]['jun']  = round($row->unique_title_June, 2);
                                    $TR[$cnt]['july'] = round($row->unique_title_July, 2);
                                    $TR[$cnt]['aug']  = round($row->unique_title_August, 2);
                                    $TR[$cnt]['sep']  = round($row->unique_title_September, 2);
                                    $TR[$cnt]['oct']  = round($row->unique_title_October, 2);
                                    $TR[$cnt]['nov']  = round($row->unique_title_November, 2);
                                    $TR[$cnt]['dec']  = round($row->unique_title_December, 2);
                                    $ucnt = $cnt;
                                    $cnt++;
                                    
                                }
                            }                           
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            if ($this->findKeyVal($TR, 'Unique_Title_Requests', trim($row->doi),  'OA_Gold')) {
                            } else {
                                $oa_unique_title_total_sale = round($row->oa_unique_title_January, 2) + round($row->oa_unique_title_February, 2) + round($row->oa_unique_title_March, 2) + round($row->oa_unique_title_April, 2) + round($row->oa_unique_title_May, 2) + round($row->oa_unique_title_June, 2) + round($row->oa_unique_title_July, 2) + round($row->oa_unique_title_August, 2) + round($row->oa_unique_title_September, 2) + round($row->oa_unique_title_October, 2) + round($row->oa_unique_title_November, 2) + round($row->oa_unique_December, 2);
                                if ($oa_unique_title_total_sale > 0) {
                                    $TR[$cnt]['title']             = trim($row->title);
                                    $TR[$cnt]['doi']               = trim($row->doi);
                                    $TR[$cnt]['isbn']              = trim($row->isbn);
                                    $TR[$cnt]['issn']              = trim($row->issn);
                                    $TR[$cnt]['eissn']             = trim($row->eissn);
                                    $TR[$cnt]['nid']               = $row->nid;
                                    $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                    $TR[$cnt]['Data_Type'] = 'Book';
                                    $TR[$cnt]['Section_Type'] = '';
                                    $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                    $TR[$cnt]['Access_Type']       = 'OA_Gold';
                                    $TR[$cnt]['Metric_Type']       = 'Unique_Title_Requests';
                                    $TR[$cnt]['total_sale'] = $oa_unique_title_total_sale;
                                    $TR[$cnt]['jan']  = round($row->oa_unique_title_January, 2);
                                    $TR[$cnt]['feb']  = round($row->oa_unique_title_February, 2);
                                    $TR[$cnt]['mar']  = round($row->oa_unique_title_March, 2);
                                    $TR[$cnt]['apr']  = round($row->oa_unique_title_April, 2);
                                    $TR[$cnt]['may']  = round($row->oa_unique_title_May, 2);
                                    $TR[$cnt]['jun']  = round($row->oa_unique_title_June, 2);
                                    $TR[$cnt]['july'] = round($row->oa_unique_title_July, 2);
                                    $TR[$cnt]['aug']  = round($row->oa_unique_title_August, 2);
                                    $TR[$cnt]['sep']  = round($row->oa_unique_title_September, 2);
                                    $TR[$cnt]['oct']  = round($row->oa_unique_title_October, 2);
                                    $TR[$cnt]['nov']  = round($row->oa_unique_title_November, 2);
                                    $TR[$cnt]['dec']  = round($row->oa_unique_title_December, 2);
                                    $ucnt = $cnt;
                                    $cnt++;                             
                                
                                }
                            }
                        }
                    }
                } //while
            } //if
            if ($book_result_no_license) {
                foreach ($book_result_no_license as $row) {
                    $TR[$cnt]['title']       = trim($row->title);
                    $TR[$cnt]['volume_year'] = $row->volume_year;
                    $TR[$cnt]['doi']         = trim($row->doi);
                    $TR[$cnt]['isbn']              = trim($row->isbn);
                    $TR[$cnt]['issn']        = trim($row->issn);
                    $TR[$cnt]['eissn']       = trim($row->eissn);
                    $TR[$cnt]['nid']         = $row->nid;
                    $TR[$cnt]['Data_Type'] = 'Book';
                    $TR[$cnt]['Section_Type'] = $row->Section_Type;
                    $TR[$cnt]['Metric_Type'] = 'No_License';
                    $TR[$cnt]['Access_Type']       = 'Controlled';
                    $TR[$cnt]['total_sale'] = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    $TR[$cnt]['jan']  = round($row->January, 2);
                    $TR[$cnt]['feb']  = round($row->February, 2);
                    $TR[$cnt]['mar']  = round($row->March, 2);
                    $TR[$cnt]['apr']  = round($row->April, 2);
                    $TR[$cnt]['may']  = round($row->May, 2);
                    $TR[$cnt]['jun']  = round($row->June, 2);
                    $TR[$cnt]['july'] = round($row->July, 2);
                    $TR[$cnt]['aug']  = round($row->August, 2);
                    $TR[$cnt]['sep']  = round($row->September, 2);
                    $TR[$cnt]['oct']  = round($row->October, 2);
                    $TR[$cnt]['nov']  = round($row->November, 2);
                    $TR[$cnt]['dec']  = round($row->December, 2);
                    $cnt++;
                } //while
            } //if

            if ($book_result_investigation) {
                foreach ($book_result_investigation as $row) {
                    if (in_array('Total_Item_Investigations', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                            if ($total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = trim($row->isbn);
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type'] = 'Controlled';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                                $TR[$cnt]['total_sale']  = $total_sale;
                                $TR[$cnt]['jan']  = round($row->January, 2);
                                $TR[$cnt]['feb']  = round($row->February, 2);
                                $TR[$cnt]['mar']  = round($row->March, 2);
                                $TR[$cnt]['apr']  = round($row->April, 2);
                                $TR[$cnt]['may']  = round($row->May, 2);
                                $TR[$cnt]['jun']  = round($row->June, 2);
                                $TR[$cnt]['july'] = round($row->July, 2);
                                $TR[$cnt]['aug']  = round($row->August, 2);
                                $TR[$cnt]['sep']  = round($row->September, 2);
                                $TR[$cnt]['oct']  = round($row->October, 2);
                                $TR[$cnt]['nov']  = round($row->November, 2);
                                $TR[$cnt]['dec']  = round($row->December, 2);
                                $cnt++;
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                            if ($oa_total_sale > 0) {
                                $TR[$cnt]['title']       = trim($row->title);
                                $TR[$cnt]['doi']         = trim($row->doi);
                                $TR[$cnt]['isbn']        = trim($row->isbn);
                                $TR[$cnt]['issn']        = trim($row->issn);
                                $TR[$cnt]['eissn']       = trim($row->eissn);
                                $TR[$cnt]['nid']         = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type'] = 'OA_Gold';
                                $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                                $TR[$cnt]['total_sale'] = $oa_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_December, 2);
                                $cnt++;
                            }
                        }
                    }
                    if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                            if ($unique_total_sale > 0) {
                                $TR[$cnt]['title']             = trim($row->title);
                                $TR[$cnt]['doi']               = trim($row->doi);
                                $TR[$cnt]['isbn']              = trim($row->isbn);
                                $TR[$cnt]['issn']              = trim($row->issn);
                                $TR[$cnt]['eissn']             = trim($row->eissn);
                                $TR[$cnt]['nid']               = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type']       = 'Controlled';
                                $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                                $TR[$cnt]['total_sale'] = $unique_total_sale;
                                $TR[$cnt]['jan']  = round($row->unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->unique_April, 2);
                                $TR[$cnt]['may']  = round($row->unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->unique_June, 2);
                                $TR[$cnt]['july'] = round($row->unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->unique_December, 2);
                                $ucnt = $cnt;
                                $cnt++;

                                
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                            if ($oa_unique_total_sale > 0) {
                                $TR[$cnt]['title']             = trim($row->title);
                                $TR[$cnt]['doi']               = trim($row->doi);
                                $TR[$cnt]['isbn']              = trim($row->isbn);
                                $TR[$cnt]['issn']              = trim($row->issn);
                                $TR[$cnt]['eissn']             = trim($row->eissn);
                                $TR[$cnt]['nid']               = $row->nid;
                                $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                $TR[$cnt]['Data_Type'] = 'Book';
                                $TR[$cnt]['Section_Type'] = $row->Section_Type;
                                $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                $TR[$cnt]['Access_Type']       = 'OA_Gold';
                                $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                                $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                                $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                                $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                                $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                                $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                                $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                                $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                                $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                                $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                                $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                                $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                                $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                                $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);
                                $ucnt = $cnt;
                                $cnt++;
                                
                            }
                        }
                    }
                    if (in_array('Unique_Title_Investigations', $Metric_Type)) {
                        if (in_array('Controlled', $Access_Type)) {
                            if ($this->findKeyVal($TR, 'Unique_Title_Investigations', trim($row->doi), 'Controlled')) {
                            } else {
                                $unique_title_total_sale = round($row->unique_title_January, 2) + round($row->unique_title_February, 2) + round($row->unique_title_March, 2) + round($row->unique_title_April, 2) + round($row->unique_title_May, 2) + round($row->unique_title_June, 2) + round($row->unique_title_July, 2) + round($row->unique_title_August, 2) + round($row->unique_title_September, 2) + round($row->unique_title_October, 2) + round($row->unique_title_November, 2) + round($row->unique_title_December, 2);
                                if ($unique_title_total_sale > 0) {
                                    $TR[$cnt]['title']             = trim($row->title);
                                    $TR[$cnt]['doi']               = trim($row->doi);
                                    $TR[$cnt]['isbn']              = trim($row->isbn);
                                    $TR[$cnt]['issn']              = trim($row->issn);
                                    $TR[$cnt]['eissn']             = trim($row->eissn);
                                    $TR[$cnt]['nid']               = $row->nid;
                                    $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                    $TR[$cnt]['Data_Type'] = 'Book';
                                    $TR[$cnt]['Section_Type'] = '';
                                    $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                    $TR[$cnt]['Access_Type']       = 'Controlled';
                                    $TR[$cnt]['Metric_Type']       = 'Unique_Title_Investigations';
                                    $TR[$cnt]['total_sale'] = $unique_title_total_sale;
                                    $TR[$cnt]['jan']  = round($row->unique_title_January, 2);
                                    $TR[$cnt]['feb']  = round($row->unique_title_February, 2);
                                    $TR[$cnt]['mar']  = round($row->unique_title_March, 2);
                                    $TR[$cnt]['apr']  = round($row->unique_title_April, 2);
                                    $TR[$cnt]['may']  = round($row->unique_title_May, 2);
                                    $TR[$cnt]['jun']  = round($row->unique_title_June, 2);
                                    $TR[$cnt]['july'] = round($row->unique_title_July, 2);
                                    $TR[$cnt]['aug']  = round($row->unique_title_August, 2);
                                    $TR[$cnt]['sep']  = round($row->unique_title_September, 2);
                                    $TR[$cnt]['oct']  = round($row->unique_title_October, 2);
                                    $TR[$cnt]['nov']  = round($row->unique_title_November, 2);
                                    $TR[$cnt]['dec']  = round($row->unique_title_December, 2);
                                    $ucnt = $cnt;
                                    $cnt++;                                
                                }
                            }
                        }
                        if (in_array('OA_Gold', $Access_Type)) {
                            if ($this->findKeyVal($TR, 'Unique_Title_Investigations', trim($row->doi), 'OA_Gold')) {
                            } else {
                                $oa_unique_title_total_sale = round($row->oa_unique_title_January, 2) + round($row->oa_unique_title_February, 2) + round($row->oa_unique_title_March, 2) + round($row->oa_unique_title_April, 2) + round($row->oa_unique_title_May, 2) + round($row->oa_unique_title_June, 2) + round($row->oa_unique_title_July, 2) + round($row->oa_unique_title_August, 2) + round($row->oa_unique_title_September, 2) + round($row->oa_unique_title_October, 2) + round($row->oa_unique_title_November, 2) + round($row->oa_unique_December, 2);
                                if ($oa_unique_title_total_sale > 0) {
                                    $TR[$cnt]['title']             = trim($row->title);
                                    $TR[$cnt]['doi']               = trim($row->doi);
                                    $TR[$cnt]['isbn']              = trim($row->isbn);
                                    $TR[$cnt]['issn']              = trim($row->issn);
                                    $TR[$cnt]['eissn']             = trim($row->eissn);
                                    $TR[$cnt]['nid']               = $row->nid;
                                    $TR[$cnt]['content_download_type']         = $row->content_download_type;
                                    $TR[$cnt]['Data_Type'] = 'Book';
                                    $TR[$cnt]['Section_Type'] = '';
                                    $TR[$cnt]['volume_year']       = trim($row->volume_year);
                                    $TR[$cnt]['Access_Type']       = 'OA_Gold';
                                    $TR[$cnt]['Metric_Type']       = 'Unique_Title_Investigations';
                                    $TR[$cnt]['total_sale'] = $oa_unique_title_total_sale;
                                    $TR[$cnt]['jan']  = round($row->oa_unique_title_January, 2);
                                    $TR[$cnt]['feb']  = round($row->oa_unique_title_February, 2);
                                    $TR[$cnt]['mar']  = round($row->oa_unique_title_March, 2);
                                    $TR[$cnt]['apr']  = round($row->oa_unique_title_April, 2);
                                    $TR[$cnt]['may']  = round($row->oa_unique_title_May, 2);
                                    $TR[$cnt]['jun']  = round($row->oa_unique_title_June, 2);
                                    $TR[$cnt]['july'] = round($row->oa_unique_title_July, 2);
                                    $TR[$cnt]['aug']  = round($row->oa_unique_title_August, 2);
                                    $TR[$cnt]['sep']  = round($row->oa_unique_title_September, 2);
                                    $TR[$cnt]['oct']  = round($row->oa_unique_title_October, 2);
                                    $TR[$cnt]['nov']  = round($row->oa_unique_title_November, 2);
                                    $TR[$cnt]['dec']  = round($row->oa_unique_title_December, 2);
                                    $ucnt = $cnt;
                                    $cnt++;
                                    
                                }
                            }
                        }
                    }
                } //while
            } //if
        }
        usort($TR, function ($a, $b) {
            //$c = $a['title'] - $b['title'];
            //$c .= $b['volume_year'] - $a['volume_year'];
            //return $c;
            if ($a['title'] == $b['title']) {
                return $b['volume_year'] - $a['volume_year'];
            }
            return strcmp($a['title'], $b['title']);
        });
        //print_r($TR); die;
        //ksort($TR);
        for ($i = 0; $i < count($TR); $i++) {

            $tr_data .= "<tr align='center'> ";
            $tr_data .= "<td align='left'>" . trim($TR[$i]['title']) . "</td>
                        <td>Bentham Science Publishers</td>
                        <td></td>
                        <td>Eurekaselect</td>
                        <td>" . $TR[$i]['doi'] . "</td>
                        <td>&nbsp;</td>
                        <td>" . $TR[$i]['isbn'] . "</td>
                        <td>" . $TR[$i]['issn'] . "</td>
                        <td>" . $TR[$i]['eissn'] . "</td>
                        <td>https://www.eurekaselect.com/node/" . $TR[$i]['nid'] . "</td>";
            if ($dtcolumn == 1) {
                $tr_data .= "<td>" . $TR[$i]['Data_Type'] . "</td>";
            }
            $tr_data .= "<td>" . $TR[$i]['Section_Type'] . "</td>";
            if ($yopcolumn == 1) {
                $tr_data .= "<td>" . $TR[$i]['volume_year'] . "</td>";
            }
            if ($atcolumn == 1) {
                //     if (isset($TR[$i]['content_download_type']) && $TR[$i]['content_download_type'] == 'FO') {
                //         $tr_data .= "<td>OA_Gold</td>";
                //     } else {
                //         $tr_data .= "<td>Controlled</td>";
                //     }
                $tr_data .= "<td>" . $TR[$i]['Access_Type'] . "</td>";
            }

            if ($amcolumn == 1) {
                $tr_data .= "<td>" . $Access_Method . "</td>";
            }
            //if($mtcolumn==1){
            $tr_data .= "<td>" . $TR[$i]['Metric_Type'] . "</td>";
            //}
            $tr_data .= "<td>" . $TR[$i]['total_sale'] . "</td>";
            if ($Exclude_Monthly == 1) {
            } else {
                for ($j = 0; $j < count($monthsyear); $j++) {
                    $monthNum  = $monthsyear[$j]['month'];
                    $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
                    //$tr_data .="<td>".round($row->$monthName,2)."</td>";
                    if ($monthName == "January") {
                        $tr_data .= "<td>" . round($TR[$i]['jan'], 2) . "</td>";
                    }
                    if ($monthName == "February") {
                        $tr_data .= "<td>" . round($TR[$i]['feb'], 2) . "</td>";
                    }
                    if ($monthName == "March") {
                        $tr_data .= "<td>" . round($TR[$i]['mar'], 2) . "</td>";
                    }
                    if ($monthName == "April") {
                        $tr_data .= "<td>" . round($TR[$i]['apr'], 2) . "</td>";
                    }
                    if ($monthName == "May") {
                        $tr_data .= "<td>" . round($TR[$i]['may'], 2) . "</td>";
                    }
                    if ($monthName == "June") {
                        $tr_data .= "<td>" . round($TR[$i]['jun'], 2) . "</td>";
                    }
                    if ($monthName == "July") {
                        $tr_data .= "<td>" . round($TR[$i]['july'], 2) . "</td>";
                    }
                    if ($monthName == "August") {
                        $tr_data .= "<td>" . round($TR[$i]['aug'], 2) . "</td>";
                    }
                    if ($monthName == "September") {
                        $tr_data .= "<td>" . round($TR[$i]['sep'], 2) . "</td>";
                    }
                    if ($monthName == "October") {
                        $tr_data .= "<td>" . round($TR[$i]['oct'], 2) . "</td>";
                    }
                    if ($monthName == "November") {
                        $tr_data .= "<td>" . round($TR[$i]['nov'], 2) . "</td>";
                    }
                    if ($monthName == "December") {
                        $tr_data .= "<td>" . round($TR[$i]['dec'], 2) . "</td>";
                    }
                }
            }
            $tr_data .= "</tr>";
        } //while
        $table .= $tableTopHeader . $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function get_PR($request)
    {
        $error_flag  = "";
        $exception_data  = "";
        $SectionTypetd = "";
        $tableHeader = "";
        $tableHeader_years = "";
        $result_searches_platform = "";
        $result_investigation = "";
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $gentype       = $request->get("exportrange");
        $user_id       = $request->get("user_id");
        $reptype       = $request->get("reporttype");
        $frommonthwise = $request->get('frommonthwise');
        $fromyearwise  = $request->get('toyearwise');
        $tomonthwise   = $request->get('tomonthwise');
        $toyearwise    = $request->get('toyearwise');
        $exporttype    = $request->get('exporttype');
        $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
        $todate        = $toyearwise . '-' . $tomonthwise . '-28';
        $result        = false;
        $result_no_license  = false;
        $book_result    = false;
        $book_result_investigation = false;
        $Metric_Type = $request->get("Metric_Type");
        $Data_Type = $request->get("Data_Type");
        //$mtcolumn = $_REQUEST['mtcolumn'];
        $dtcolumn  = $request->get("dtcolumn");
        $Access_Type = $request->get("Access_Type");
        $atcolumn    = $request->get("atcolumn");
        $Access_Method = $request->get("Access_Method");
        $amcolumn      = $request->get("amcolumn");
        $YOP       = $request->get("YOP");
        $yopcolumn = $request->get("yopcolumn");
        $Exclude_Monthly = $request->get("Exclude_Monthly");
        $UDRBegin = date('Y-m-01', strtotime($fromdate));
        $UDREnd   = $orginal_UDREnd = date('Y-m-t', strtotime($todate));
        if (empty($Access_Type)) {
            $Access_Type = array('Controlled');
        }
       
        $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[0]['year'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
        }

         //if partial data return
         if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
            $error_flag = 3031;
            $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
            $UDREnd     = date('Y-m-t');
        }
        $resultuser = User::getUserById($user_id);
        $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
        $user = User::where('id', $user_id)->first();
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        if (in_array('Searches_Platform', $Metric_Type)) {
            $query_searches_platform = "select
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN user_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN user_id END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN user_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN user_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN user_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN user_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN user_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN user_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN user_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN user_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN user_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN user_id END) AS 'December'
                from access_searches_report a
                where to_days(from_unixtime(a.timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                " . $user_clause . " group by a.user_id
                ";
            $result_searches_platform =  DB::select($query_searches_platform);
        }
        if (in_array('Journal', $Data_Type)) {
            if (in_array('Total_Item_Requests', $Metric_Type) || in_array('Unique_Item_Requests', $Metric_Type)) {
                $query = " select * from (
                    select
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN article_id END) AS 'January',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN article_id END) AS 'February',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN article_id END) AS 'March',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN article_id END) AS 'April',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN article_id END) AS 'May',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN article_id END) AS 'June',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN article_id END) AS 'July',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN article_id END) AS 'August',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN article_id END) AS 'September',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN article_id END) AS 'October',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN article_id END) AS 'November',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN article_id END) AS 'December' ,
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',article_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',article_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',article_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',article_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',article_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',article_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',article_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',article_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',article_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',article_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',article_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',article_id) END) AS 'unique_December'
                    from download_count_articles a
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    ";
                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query .= " and a.content_download_type!='FO'";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query .= " and a.content_download_type='FO'";
                }
/*                if($Access_Type=='Controlled'){
                    $query .= " and a.content_download_type!='FO'";
                }
                if($Access_Type=='OA_Gold'){
                    $query .= " and a.content_download_type='FO'";
                }
*/
                $query .= " and a.download_file_type!=0
                    " . $user_clause . " group by a.user_id

                ) as tmp ";
                $result = DB::select($query);
            }

            if (in_array('Total_Item_Investigations', $Metric_Type) || in_array('Unique_Item_Investigations', $Metric_Type)) {
                $query_investigation = " select * from (
                    select
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN article_id END) AS 'January',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN article_id END) AS 'February',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN article_id END) AS 'March',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN article_id END) AS 'April',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN article_id END) AS 'May',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN article_id END) AS 'June',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN article_id END) AS 'July',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN article_id END) AS 'August',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN article_id END) AS 'September',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN article_id END) AS 'October',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN article_id END) AS 'November',
                    COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN article_id END) AS 'December' ,
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',article_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',article_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',article_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',article_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',article_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',article_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',article_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',article_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',article_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',article_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',article_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',article_id) END) AS 'unique_December'
                    from access_investigation_report a
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and a.investigation_type!=0
                    ";
                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query_investigation .= " and a.content_download_type!='FO'";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query_investigation .= " and a.content_download_type='FO'";
                }
                /*                if($Access_Type=='Controlled'){
                        $query_investigation .= " and a.content_download_type!='FO' ";
                    }
                    if($Access_Type=='OA_Gold'){
                        $query_investigation .= " and a.content_download_type='FO' ";
                    }
                */
                $query_investigation .= " " . $user_clause . " group by a.user_id

                ) as tmp ";

                $result_investigation = DB::select($query_investigation);
            }
        }
        if (in_array('Book', $Data_Type)) {
            if (in_array('Total_Item_Requests', $Metric_Type) || in_array('Unique_Item_Requests', $Metric_Type)) {
                $book_result = '';
                $query = "SELECT *,                   
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December') THEN  ebook_volume_id END) AS 'December' ,       

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March'   ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April'   ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_December'
                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id,dcv.user_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year,'Book' as Section_Type
                    FROM download_count_volume dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')";
                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query .= " and dcv.content_download_type!='FO' ";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query .= " and dcv.content_download_type='FO' ";
                }

                $query .= $user_clause_volume . "
                    UNION
                SELECT
                dcv.chapter_id,dcv.user_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year,'Chapter' as Section_Type
                FROM download_count_chapter dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')";

                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query .= " and dcv.content_download_type!='FO' ";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query .= " and dcv.content_download_type='FO' ";
                }

                $query .= $user_clause_volume . "
                ) AS tmp
                GROUP BY user_id";
                //dd($query);
                $book_result = DB::select($query);
            }
            $book_result_investigation = '';
            if (in_array('Total_Item_Investigations', $Metric_Type) || in_array('Unique_Item_Investigations', $Metric_Type)) {
                $query_investigation = "SELECT *,                   
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December') THEN  ebook_volume_id END) AS 'December' ,      

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_December'

                    
                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id,dcv.user_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year,CAST('Book'  AS CHAR CHARACTER SET utf8) as Section_Type
                    FROM ebook_access_investigation_report dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.investigation_type!=0";
                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query_investigation .= " and dcv.content_download_type!='FO' ";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query_investigation .= " and dcv.content_download_type='FO' ";
                }

                $query_investigation .= $user_clause_volume . "       
                            
                    UNION
                SELECT
                dcv.chapter_id,dcv.user_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year,CAST('Chapter'  AS CHAR CHARACTER SET utf8) as Section_Type  
                FROM chapter_access_investigation_report dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and dcv.investigation_type!=0 ";
                if (in_array('Controlled', $Access_Type) && !in_array('OA_Gold', $Access_Type)) {
                    $query_investigation .= " and dcv.content_download_type!='FO' ";
                } else if (in_array('OA_Gold', $Access_Type) && !in_array('Controlled', $Access_Type)) {
                    $query_investigation .= " and dcv.content_download_type='FO' ";
                }

                $query_investigation .= $user_clause_volume . "    
                ) AS tmp
                GROUP BY user_id";
                //print($query_investigation); die;          
                $book_result_investigation = DB::select($query_investigation);
            }
        }
        $table = "<table width='800' align='center' >";
        $blankTDs = "";
        $colspanccnt = 27;
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));
        if ($dtcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Data_Type';
        }

        if ($yopcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'YOP';
        }
        if ($atcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Access_Type';
        }

        if ($amcolumn != 1) {
            $colspanccnt = $colspanccnt - 1;
        } else {
            $Attributes_To_Show[] = 'Access_Method';
        }
        $Exclude_Monthly_Details = '';
        if ($Exclude_Monthly == 1) {
            $colspanccnt = $colspanccnt - count($monthsyear);
            $Exclude_Monthly_Details = 'Exclude_Monthly_Details=True; ';
        }
        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $exceptions_data = '';
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];

            if($error_flag==3031){                   
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        if ($gentype == "3" && $reptype == "9") {
            $Report_Name       = "Platform Master Report";
            $Report_ID         = "PR";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";            
            $Metric_Types      = implode("; ", $Metric_Type);

            if (in_array('Searches_Platform', $Metric_Type) && in_array('Total_Item_Requests', $Metric_Type) && in_array('Unique_Item_Requests', $Metric_Type) && in_array('Total_Item_Investigations', $Metric_Type) && in_array('Unique_Item_Investigations', $Metric_Type) && in_array('Unique_Title_Investigations', $Metric_Type) && in_array('Unique_Title_Requests', $Metric_Type) ) {
                $Metric_Types="";
            }
            if (in_array('Searches_Platform', $Metric_Type)) {
                $Data_Type_Head = implode('|', $Data_Type) . "|Platform";
            } else {
                $Data_Type_Head = implode('|', $Data_Type);
            }
            if (in_array('Controlled', $Access_Type) && in_array('OA_Gold', $Access_Type)) {
                $Report_Filters    = "Data_Type=" . $Data_Type_Head . "; Access_Method=" . $Access_Method;
            } else if ((in_array('Controlled', $Access_Type) || in_array('OA_Gold', $Access_Type)) && (isset($atcolumn) && $atcolumn == 1)) {
                $Report_Filters    = "Data_Type=" . $Data_Type_Head . "; Access_Type=" . implode("|", $Access_Type) . "; Access_Method=" . $Access_Method;
            } else if ((in_array('Controlled', $Access_Type) || in_array('OA_Gold', $Access_Type)) && (!isset($atcolumn))) {
                $Report_Filters    = "Data_Type=" . $Data_Type_Head . "; Access_Method=" . $Access_Method;
            }
            // $Report_Filters    = "Data_Type=" . $Data_Type . "; Access_Method=" . $Access_Method;
            $Report_Attributes = $Exclude_Monthly_Details;
            if (!empty($Attributes_To_Show)) {
                $Report_Attributes .= "Attributes_To_Show=" . implode('|', $Attributes_To_Show);
            }
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }

        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td >Reporting_Period</td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";
        $DataTypetd = "";
        if ($dtcolumn == 1) {
            $DataTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Data_Type</b></td>";
        }
        $YOPtd = "";
        if ($yopcolumn == 1) {
            $YOPtd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>YOP</b></td>";
        }
        $AccessTypetd = "";
        if ($atcolumn == 1) {
            $AccessTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Type</b></td>";
        }
        $AccessMethodtd = "";
        if ($amcolumn == 1) {
            $AccessMethodtd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Method</b></td>";
        }
        $MetricTypetd = "";
        //if($mtcolumn==1){
        $MetricTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>";
        //}
        $tableHeader .= "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Platform</b></td>
                                " . $DataTypetd . $SectionTypetd . $YOPtd . $AccessTypetd . $AccessMethodtd . $MetricTypetd . "
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        if ($Exclude_Monthly == 1) {
        } else {
            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $year      = $monthsyear[$j]['year'];
                $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
                $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
            }
            $tableHeader .= $tableHeader_years;
        }
        $tableHeader .= " </tr>   ";
        $table_rows      = "";
        $tr_data         = "";
        $tr_unique_data  = "";
        $jan             = 0;
        $feb             = 0;
        $mar             = 0;
        $apr             = 0;
        $may             = 0;
        $jun             = 0;
        $july            = 0;
        $aug             = 0;
        $sep             = 0;
        $oct             = 0;
        $nov             = 0;
        $dec             = 0;
        $unique_jan      = 0;
        $unique_feb      = 0;
        $unique_mar      = 0;
        $unique_apr      = 0;
        $unique_may      = 0;
        $unique_jun      = 0;
        $unique_july     = 0;
        $unique_aug      = 0;
        $unique_sep      = 0;
        $oct             = 0;
        $unique_nov      = 0;
        $unique_dec      = 0;
        $prev_journal_id = 0;
        $TR              = array();
        $cnt = 0;

        if (in_array('Journal', $Data_Type)) {
            if ($result) {
                foreach ($result as $row) {
                    if (in_array('Total_Item_Requests', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Journal';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    if (in_array('Unique_Item_Requests', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Journal';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);
                        $cnt++;
                    }
                } //while

            } //if
            if ($result_investigation) {
                foreach ($result_investigation as $row) {
                    if (in_array('Total_Item_Investigations', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Journal';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Journal';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);
                        $cnt++;
                    }
                } //while
            } //if
        }
        if (in_array('Book', $Data_Type)) {
            if ($book_result) {
                foreach ($book_result as $row) {
                    if (in_array('Total_Item_Requests', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    if (in_array('Unique_Item_Requests', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);                      
                    
                        $cnt++;
                    }
                    if (in_array('Unique_Title_Requests', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Requests';
                        $TR[$cnt]['total_sale'] = round($row->unique_title_January, 2) + round($row->unique_title_February, 2) + round($row->unique_title_March, 2) + round($row->unique_title_April, 2) + round($row->unique_title_May, 2) + round($row->unique_title_June, 2) + round($row->unique_title_July, 2) + round($row->unique_title_August, 2) + round($row->unique_title_September, 2) + round($row->unique_title_October, 2) + round($row->unique_title_November, 2) + round($row->unique_title_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_title_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_title_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_title_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_title_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_title_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_title_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_title_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_title_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_title_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_title_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_title_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_title_December, 2);
                        $cnt++;
                    }
                } //while

            } //if
            if ($book_result_investigation) {
                foreach ($book_result_investigation as $row) {
                    if (in_array('Total_Item_Investigations', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);
                        
                    
                        $cnt++;
                    }
                    if (in_array('Unique_Title_Investigations', $Metric_Type)) {
                        $TR[$cnt]['Data_Type'] = 'Book';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Investigations';
                        $TR[$cnt]['total_sale'] = round($row->unique_title_inves_January, 2) + round($row->unique_title_inves_February, 2) + round($row->unique_title_inves_March, 2) + round($row->unique_title_inves_April, 2) + round($row->unique_title_inves_May, 2) + round($row->unique_title_inves_June, 2) + round($row->unique_title_inves_July, 2) + round($row->unique_title_inves_August, 2) + round($row->unique_title_inves_September, 2) + round($row->unique_title_inves_October, 2) + round($row->unique_title_inves_November, 2) + round($row->unique_title_inves_December, 2);

                        $TR[$cnt]['jan']  = round($row->unique_title_inves_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_title_inves_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_title_inves_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_title_inves_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_title_inves_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_title_inves_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_title_inves_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_title_inves_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_title_inves_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_title_inves_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_title_inves_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_title_inves_December, 2);                 
                    
                        $cnt++;
                    }
                } //while
            } //if
        }
        if ($result_searches_platform) {
            foreach ($result_searches_platform as $row) {
                $TR[$cnt]['Metric_Type'] = 'Searches_Platform';
                $TR[$cnt]['Data_Type'] = 'Platform';
                $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                $TR[$cnt]['jan']  = round($row->January, 2);
                $TR[$cnt]['feb']  = round($row->February, 2);
                $TR[$cnt]['mar']  = round($row->March, 2);
                $TR[$cnt]['apr']  = round($row->April, 2);
                $TR[$cnt]['may']  = round($row->May, 2);
                $TR[$cnt]['jun']  = round($row->June, 2);
                $TR[$cnt]['july'] = round($row->July, 2);
                $TR[$cnt]['aug']  = round($row->August, 2);
                $TR[$cnt]['sep']  = round($row->September, 2);
                $TR[$cnt]['oct']  = round($row->October, 2);
                $TR[$cnt]['nov']  = round($row->November, 2);
                $TR[$cnt]['dec']  = round($row->December, 2);
                $cnt++;
            } //while
        } //if
        if (!empty($TR)) {
            //print_r($TR); die;
            //ksort($TR);
            for ($i = 0; $i < count($TR); $i++) {
                $TR[$i]['volume_year'] = '';
                $tr_data .= "<tr align='center'> ";
                $tr_data .= "<td>Eurekaselect</td>
                        ";
                if ($dtcolumn == 1) {
                    $tr_data .= "<td>" . $TR[$i]['Data_Type'] . "</td>";
                }
                if ($yopcolumn == 1) {
                    $tr_data .= "<td>" . $TR[$i]['volume_year'] . "</td>";
                }
                /*            if ($atcolumn == 1) {
                $tr_data .= "<td>" . $Access_Type . "</td>";
            }
*/
                if ($atcolumn == 1) {
                    if (isset($TR[$i]['content_download_type']) && $TR[$i]['content_download_type'] == 'FO') {
                        $tr_data .= "<td>OA_Gold</td>";
                    } else {
                        $tr_data .= "<td>Controlled</td>";
                    }
                }
                if ($amcolumn == 1) {
                    $tr_data .= "<td>" . $Access_Method . "</td>";
                }
                //if($mtcolumn==1){
                $tr_data .= "<td>" . $TR[$i]['Metric_Type'] . "</td>";
                //}
                $tr_data .= "<td>" . $TR[$i]['total_sale'] . "</td>";
                if ($Exclude_Monthly == 1) {
                } else {
                    for ($j = 0; $j < count($monthsyear); $j++) {
                        $monthNum  = $monthsyear[$j]['month'];
                        $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
                        //$tr_data .="<td>".round($row->$monthName,2)."</td>";
                        if ($monthName == "January") {
                            $tr_data .= "<td>" . round($TR[$i]['jan'], 2) . "</td>";
                        }
                        if ($monthName == "February") {
                            $tr_data .= "<td>" . round($TR[$i]['feb'], 2) . "</td>";
                        }
                        if ($monthName == "March") {
                            $tr_data .= "<td>" . round($TR[$i]['mar'], 2) . "</td>";
                        }
                        if ($monthName == "April") {
                            $tr_data .= "<td>" . round($TR[$i]['apr'], 2) . "</td>";
                        }
                        if ($monthName == "May") {
                            $tr_data .= "<td>" . round($TR[$i]['may'], 2) . "</td>";
                        }
                        if ($monthName == "June") {
                            $tr_data .= "<td>" . round($TR[$i]['jun'], 2) . "</td>";
                        }
                        if ($monthName == "July") {
                            $tr_data .= "<td>" . round($TR[$i]['july'], 2) . "</td>";
                        }
                        if ($monthName == "August") {
                            $tr_data .= "<td>" . round($TR[$i]['aug'], 2) . "</td>";
                        }
                        if ($monthName == "September") {
                            $tr_data .= "<td>" . round($TR[$i]['sep'], 2) . "</td>";
                        }
                        if ($monthName == "October") {
                            $tr_data .= "<td>" . round($TR[$i]['oct'], 2) . "</td>";
                        }
                        if ($monthName == "November") {
                            $tr_data .= "<td>" . round($TR[$i]['nov'], 2) . "</td>";
                        }
                        if ($monthName == "December") {
                            $tr_data .= "<td>" . round($TR[$i]['dec'], 2) . "</td>";
                        }
                    }
                }
                $tr_data .= "</tr>";
            } //while
        }
        $table .= $tableTopHeader . $tableHeader . $tr_data . "</table>";
        return $table;
    }
    public function get_PR_P1($request)
    {
        $error_flag  = "";
        $exception_data = "";
        $result_searches_platform = "";
        $tableHeader = "";
        $Exclude_Monthly = "";
        $tableHeader_years = "";
        $Data_Type = "";
        $dtcolumn  = "";
        $Access_Type = "";
        $atcolumn    = "";
        $YOP       = "";
        $yopcolumn = "";
        $Exclude_Monthly = "";
        $Data_Type = $request->get("Data_Type");
        $dtcolumn  = $request->get("dtcolumn");
        $Access_Type = $request->get("Access_Type");
        $atcolumn    = $request->get("atcolumn");
        $Access_Method = $request->get("Access_Method");
        $amcolumn      = $request->get("amcolumn");
        $YOP       = $request->get("YOP");
        $yopcolumn = $request->get("yopcolumn");
        $Exclude_Monthly = $request->get("Exclude_Monthly");
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $gentype       = $request->get("exportrange");
        $user_id       = $request->get("user_id");
        $reptype       = $request->get("reporttype");
        $frommonthwise = $request->get('frommonthwise');
        $fromyearwise  = $request->get('toyearwise');
        $tomonthwise   = $request->get('tomonthwise');
        $toyearwise    = $request->get('toyearwise');
        $exporttype    = $request->get('exporttype');
        $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
        $todate        = $toyearwise . '-' . $tomonthwise . '-28';
        $UDRBegin = date('Y-m-01', strtotime($fromdate));
        $UDREnd   = $orginal_UDREnd = date('Y-m-t', strtotime($todate));
        
        $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[0]['year'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
        }
        //if partial data return
        if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
            $error_flag = 3031;
            $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
            $UDREnd     = date('Y-m-t');
        }

        $resultuser = User::getUserById($user_id);
        $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
        $user = User::where('id', $user_id)->first();
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        $query = " select 
                    user_id,
                    sum(January) as 'January',
                    sum(February) as 'February',
                    sum(March) as 'March',
                    sum(April) as 'April',
                    sum(May) as 'May',
                    sum(June) as 'June',
                    sum(July) as 'July',
                    sum(August) as 'August',
                    sum(September) as 'September',
                    sum(October) as 'October',
                    sum(November) as 'November',
                    sum(December) as 'December',
                    sum(unique_January) as 'unique_January',
                    sum(unique_February) as 'unique_February',
                    sum(unique_March) as 'unique_March',
                    sum(unique_April) as 'unique_April',
                    sum(unique_May) as 'unique_May',
                    sum(unique_June) as 'unique_June',
                    sum(unique_July) as 'unique_July',
                    sum(unique_August) as 'unique_August',
                    sum(unique_September) as 'unique_September',
                    sum(unique_October) as 'unique_October',
                    sum(unique_November) as 'unique_November',
                    sum(unique_December) as 'unique_December'
        
            from (
                select user_id,
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN article_id END) AS 'January',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN article_id END) AS 'February',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN article_id END) AS 'March',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN article_id END) AS 'April',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN article_id END) AS 'May',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN article_id END) AS 'June',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN article_id END) AS 'July',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN article_id END) AS 'August',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN article_id END) AS 'September',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN article_id END) AS 'October',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN article_id END) AS 'November',
                COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN article_id END) AS 'December' ,
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',article_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',article_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',article_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',article_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',article_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',article_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',article_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',article_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',article_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',article_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',article_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',article_id) END) AS 'unique_December'
                from download_count_articles a
                where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and a.download_file_type!=0
                and a.user_id='" . $user_id . "'
                 group by a.user_id

            UNION 

                SELECT user_id,
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'January' THEN ebook_volume_id END) AS 'January',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'February' THEN ebook_volume_id END) AS 'February',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'March' THEN ebook_volume_id END) AS 'March',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'April' THEN ebook_volume_id END) AS 'April',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'May' THEN ebook_volume_id END) AS 'May',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'June' THEN ebook_volume_id END) AS 'June',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'July' THEN ebook_volume_id END) AS 'July',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'August' THEN ebook_volume_id END) AS 'August',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'September' THEN ebook_volume_id END) AS 'September',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'October' THEN ebook_volume_id END) AS 'October',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'November' THEN ebook_volume_id END) AS 'November',
                        COUNT( CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'December' THEN ebook_volume_id END) AS 'December',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'January' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_January',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'February' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_February',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'March' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_March',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'April' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_April',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'May' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_May',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'June' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_June',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'July' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_July',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'August' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_August',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'September' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_September',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'October' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_October',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'November' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_November',
                        COUNT( DISTINCT CASE WHEN DATE_FORMAT(FROM_UNIXTIME(timestamp), '%M') = 'December' THEN CONCAT(session_id, ',', ebook_volume_id,chapter_id) END) AS 'unique_December'
                    FROM
                        (
                        SELECT
                        CAST('0'  AS SIGNED) as chapter_id,dcv.user_id,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year
                        FROM download_count_volume dcv
                        INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                        INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                        INNER JOIN year Y ON Y.id = ev.year_id
                        and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') AND to_days('" . $UDREnd . "')
                        and dcv.download_file_type!=0     
                                and dcv.user_id='" . $user_id . "'
                        UNION
                    SELECT
                    dcv.chapter_id,dcv.user_id, dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year
                    FROM download_count_chapter dcv
                    INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                    INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON  Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.download_file_type!=0     
                    and dcv.user_id='" . $user_id . "'
                    ) AS tmp
                    GROUP BY user_id
        ) as tmp  GROUP BY user_id";
        //echo $query; die;
        $result = DB::select($query);
        $query_searches_platform = "select
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN user_id END) AS 'January',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN user_id END) AS 'February',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN user_id END) AS 'March',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN user_id END) AS 'April',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN user_id END) AS 'May',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN user_id END) AS 'June',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN user_id END) AS 'July',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN user_id END) AS 'August',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN user_id END) AS 'September',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN user_id END) AS 'October',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN user_id END) AS 'November',
            COUNT(CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN user_id END) AS 'December'
            from access_searches_report a
            where to_days(from_unixtime(a.timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
            " . $user_clause . " group by a.user_id";
        $result_searches_platform = DB::select($query_searches_platform);
        $query_unique_title = "SELECT *,
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='January' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_January',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='February' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_February',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='March' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_March',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='April' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_April',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='May' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_May',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='June' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_June',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='July' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_July',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='August' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_August',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='September' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_September',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='October' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_October',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='November' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_November',
                COUNT(distinct CASE WHEN date_format(from_unixtime(timestamp),'%M')='December' THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_December'
                FROM
                    (
                    SELECT
                    timestamp,session_id,volume_id ebook_volume_id,user_id
                    FROM download_count_volume dcv                    
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.download_file_type!=0  
                    and dcv.user_id='" . $user_id . "'

                    UNION

                    SELECT
                    timestamp,session_id,ev.ebook_volume_id,user_id
                    FROM download_count_chapter dcv          
                    INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                    INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id      
                    where to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')                
                    and dcv.download_file_type!=0  
                    and dcv.user_id='" . $user_id . "'
                ) AS tmp
                GROUP BY user_id";               
                
        //echo $query_unique_title; die;      
        $result_unique_title = DB::select($query_unique_title);

        $table = "<table width='800' align='center' >";
        $blankTDs = "";
        $colspanccnt = 27;
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));

        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $exceptions_data = '';
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];
            if($error_flag==3031){                   
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        if ($gentype == "3" && $reptype == "8") {
            $Report_Name       = "Platform Usage";
            $Report_ID         = "PR_P1";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Type      = array("Total_Item_Requests", "Unique_Item_Requests", "Searches_Platform", "Unique_Title_Requests");
            $Metric_Types      = implode("; ", $Metric_Type);
            $Report_Filters    = "Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td >Reporting_Period</td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";
        $DataTypetd = "";

        $MetricTypetd = "";
        //if($mtcolumn==1){
        $MetricTypetd = "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>";
        //}
        $tableHeader .= "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Platform</b></td>
                                " . $MetricTypetd . "
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        if ($Exclude_Monthly == 1) {
        } else {
            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $year      = $monthsyear[$j]['year'];
                $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
                $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
            }
            $tableHeader .= $tableHeader_years;
        }
        $tableHeader .= " </tr>   ";
        $table_rows      = "";
        $tr_data         = "";
        $tr_unique_data  = "";
        $jan             = 0;
        $feb             = 0;
        $mar             = 0;
        $apr             = 0;
        $may             = 0;
        $jun             = 0;
        $july            = 0;
        $aug             = 0;
        $sep             = 0;
        $oct             = 0;
        $nov             = 0;
        $dec             = 0;
        $unique_jan      = 0;
        $unique_feb      = 0;
        $unique_mar      = 0;
        $unique_apr      = 0;
        $unique_may      = 0;
        $unique_jun      = 0;
        $unique_july     = 0;
        $unique_aug      = 0;
        $unique_sep      = 0;
        $oct             = 0;
        $unique_nov      = 0;
        $unique_dec      = 0;
        $prev_journal_id = 0;
        $TR              = array();
        $cnt = 0;
        if ($result) {
            foreach ($result as $row) {
                if (in_array('Total_Item_Requests', $Metric_Type)) {
                    $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                    $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    $TR[$cnt]['jan']  = round($row->January, 2);
                    $TR[$cnt]['feb']  = round($row->February, 2);
                    $TR[$cnt]['mar']  = round($row->March, 2);
                    $TR[$cnt]['apr']  = round($row->April, 2);
                    $TR[$cnt]['may']  = round($row->May, 2);
                    $TR[$cnt]['jun']  = round($row->June, 2);
                    $TR[$cnt]['july'] = round($row->July, 2);
                    $TR[$cnt]['aug']  = round($row->August, 2);
                    $TR[$cnt]['sep']  = round($row->September, 2);
                    $TR[$cnt]['oct']  = round($row->October, 2);
                    $TR[$cnt]['nov']  = round($row->November, 2);
                    $TR[$cnt]['dec']  = round($row->December, 2);
                    $cnt++;
                }
                if (in_array('Unique_Item_Requests', $Metric_Type)) {

                    $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                    $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                    $TR[$cnt]['jan']  = round($row->unique_January, 2);
                    $TR[$cnt]['feb']  = round($row->unique_February, 2);
                    $TR[$cnt]['mar']  = round($row->unique_March, 2);
                    $TR[$cnt]['apr']  = round($row->unique_April, 2);
                    $TR[$cnt]['may']  = round($row->unique_May, 2);
                    $TR[$cnt]['jun']  = round($row->unique_June, 2);
                    $TR[$cnt]['july'] = round($row->unique_July, 2);
                    $TR[$cnt]['aug']  = round($row->unique_August, 2);
                    $TR[$cnt]['sep']  = round($row->unique_September, 2);
                    $TR[$cnt]['oct']  = round($row->unique_October, 2);
                    $TR[$cnt]['nov']  = round($row->unique_November, 2);
                    $TR[$cnt]['dec']  = round($row->unique_December, 2);
                    $cnt++;
                }
            } //while
        } //if
        if ($result_searches_platform) {
            foreach ($result_searches_platform as $row) {
                $TR[$cnt]['Metric_Type'] = 'Searches_Platform';
                $TR[$cnt]['total_sale']  = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                $TR[$cnt]['jan']  = round($row->January, 2);
                $TR[$cnt]['feb']  = round($row->February, 2);
                $TR[$cnt]['mar']  = round($row->March, 2);
                $TR[$cnt]['apr']  = round($row->April, 2);
                $TR[$cnt]['may']  = round($row->May, 2);
                $TR[$cnt]['jun']  = round($row->June, 2);
                $TR[$cnt]['july'] = round($row->July, 2);
                $TR[$cnt]['aug']  = round($row->August, 2);
                $TR[$cnt]['sep']  = round($row->September, 2);
                $TR[$cnt]['oct']  = round($row->October, 2);
                $TR[$cnt]['nov']  = round($row->November, 2);
                $TR[$cnt]['dec']  = round($row->December, 2);
                $cnt++;
            } //while
        } //if
        if ($result_unique_title) {
            foreach ($result_unique_title as $row) {
                $TR[$cnt]['Metric_Type'] = 'Unique_Title_Requests';
                $TR[$cnt]['total_sale'] = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);

                $TR[$cnt]['jan']  = round($row->unique_January, 2);
                $TR[$cnt]['feb']  = round($row->unique_February, 2);
                $TR[$cnt]['mar']  = round($row->unique_March, 2);
                $TR[$cnt]['apr']  = round($row->unique_April, 2);
                $TR[$cnt]['may']  = round($row->unique_May, 2);
                $TR[$cnt]['jun']  = round($row->unique_June, 2);
                $TR[$cnt]['july'] = round($row->unique_July, 2);
                $TR[$cnt]['aug']  = round($row->unique_August, 2);
                $TR[$cnt]['sep']  = round($row->unique_September, 2);
                $TR[$cnt]['oct']  = round($row->unique_October, 2);
                $TR[$cnt]['nov']  = round($row->unique_November, 2);
                $TR[$cnt]['dec']  = round($row->unique_December, 2);
                $cnt++;
            } //while
        }
        /*        usort($TR, function($a,$b){
            //$c = $a['title'] - $b['title'];
            //$c .= $b['volume_year'] - $a['volume_year'];
            //return $c;
            if ($a['title'] == $b['title']) {
                return $b['volume_year'] - $a['volume_year'];
            }
            return strcmp($a['title'], $b['title']);
        });
*/
        //print_r($TR); die;
        //ksort($TR);
        //  echo $yopcolumn; die;
        for ($i = 0; $i < count($TR); $i++) {
            $TR[$i]['volume_year'] = '';
            $tr_data .= "<tr align='center'> ";
            $tr_data .= "<td>Eurekaselect</td>
                        ";
            /* if ($dtcolumn == 1) {
                $tr_data .= "<td>" . $Data_Type . "</td>";
            }*/
            /*if ($yopcolumn == 1) {
                $tr_data .= "<td>" . $TR[$i]['volume_year'] . "</td>";
            }*/
            /*if ($atcolumn == 1) {
                $tr_data .= "<td>" . $Access_Type . "</td>";
            }*/
            /*if ($amcolumn == 1) {
                $tr_data .= "<td>" . $Access_Method . "</td>";
            }*/
            //if($mtcolumn==1){
            $tr_data .= "<td>" . $TR[$i]['Metric_Type'] . "</td>";
            //}
            $tr_data .= "<td>" . $TR[$i]['total_sale'] . "</td>";
            if ($Exclude_Monthly == 1) {
            } else {
                for ($j = 0; $j < count($monthsyear); $j++) {
                    $monthNum  = $monthsyear[$j]['month'];
                    $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
                    //$tr_data .="<td>".round($row->$monthName,2)."</td>";
                    if ($monthName == "January") {
                        $tr_data .= "<td>" . round($TR[$i]['jan'], 2) . "</td>";
                    }
                    if ($monthName == "February") {
                        $tr_data .= "<td>" . round($TR[$i]['feb'], 2) . "</td>";
                    }
                    if ($monthName == "March") {
                        $tr_data .= "<td>" . round($TR[$i]['mar'], 2) . "</td>";
                    }
                    if ($monthName == "April") {
                        $tr_data .= "<td>" . round($TR[$i]['apr'], 2) . "</td>";
                    }
                    if ($monthName == "May") {
                        $tr_data .= "<td>" . round($TR[$i]['may'], 2) . "</td>";
                    }
                    if ($monthName == "June") {
                        $tr_data .= "<td>" . round($TR[$i]['jun'], 2) . "</td>";
                    }
                    if ($monthName == "July") {
                        $tr_data .= "<td>" . round($TR[$i]['july'], 2) . "</td>";
                    }
                    if ($monthName == "August") {
                        $tr_data .= "<td>" . round($TR[$i]['aug'], 2) . "</td>";
                    }
                    if ($monthName == "September") {
                        $tr_data .= "<td>" . round($TR[$i]['sep'], 2) . "</td>";
                    }
                    if ($monthName == "October") {
                        $tr_data .= "<td>" . round($TR[$i]['oct'], 2) . "</td>";
                    }
                    if ($monthName == "November") {
                        $tr_data .= "<td>" . round($TR[$i]['nov'], 2) . "</td>";
                    }
                    if ($monthName == "December") {
                        $tr_data .= "<td>" . round($TR[$i]['dec'], 2) . "</td>";
                    }
                }
            }
            $tr_data .= "</tr>";
        } //while
        $table .= $tableTopHeader . $tableHeader . $tr_data . "</table>";
        return $table;
    }
    /*
     * Date: 16-07-2019
     * Author: Muhammad Arshad
     * Parameter: Table Html
     * Description:  This function return the csv format of a html table.
     */
    public function getTableCSV($table)
    {
        $csv = array();
        preg_match('/<table(>| [^>]*>)(.*?)<\/table( |>)/is', $table, $b);
        $table = $b[2];
        preg_match_all('/<tr(>| [^>]*>)(.*?)<\/tr( |>)/is', $table, $b);
        $rows = $b[2];
        foreach ($rows as $row) {
            //cycle through each row
            $row = str_replace(",", "", $row);
            $row = str_replace("'", "", $row);
            $row = str_replace("&nbsp;", "", $row);
            if (preg_match('/<th(>| [^>]*>)(.*?)<\/th( |>)/is', $row)) {
                //match for table headers
                preg_match_all('/<th(>| [^>]*>)(.*?)<\/th( |>)/is', $row, $b);
                $csv[] = strip_tags(implode(',', $b[2]));
            } elseif (preg_match('/<td(>| [^>]*>)(.*?)<\/td( |>)/is', $row)) {
                //match for table cells
                preg_match_all('/<td(>| [^>]*>)(.*?)<\/td( |>)/is', $row, $b);
                $csv[] = strip_tags(implode(',', $b[2]));
            }
        }
        $csv = implode("\n", $csv);
        return $csv;
    }
    public function getTableTSV($table)
    {
        $tsv = array();
		//print($table); die;
        preg_match('/<table(>| [^>]*>)(.*?)<\/table( |>)/is', $table, $b);
        $table = $b[2];

        preg_match_all('/<tr(>| [^>]*>)(.*?)<\/tr( |>)/is', $table, $b);
        $rows = $b[2];
        foreach ($rows as $row) {
            //cycle through each row
            $row = str_replace(",", "", $row);
            $row = str_replace("'", "", $row);
            $row = str_replace("&nbsp;", "", $row);
            if (preg_match('/<th(>| [^>]*>)(.*?)<\/th( |>)/is', $row)) {
                //match for table headers
                preg_match_all('/<th(>| [^>]*>)(.*?)<\/th( |>)/is', $row, $b);
                $tsv[] = strip_tags(implode(',', $b[2]));
            } elseif (preg_match('/<td(>| [^>]*>)(.*?)<\/td( |>)/is', $row)) {
                //match for table cells
                preg_match_all('/<td(>| [^>]*>)(.*?)<\/td( |>)/is', $row, $b);
                $tsv[] = strip_tags(implode("\t", $b[2]));
            }
        }
        $tsv = implode("\n", $tsv);
        return $tsv;
    }
    public function get_TR_B3($request)
    {
        $error_flag  = "";
        $exception_data = "";
        $error_codes = array(
            '3031' => 'Usage Not Ready for Requested Dates',
            '3030' => 'No Usage Available for Requested Dates',
        );
        $gentype       = $request->get("exportrange");
        $user_id       = $request->get("user_id");
        $reptype       = $request->get("reporttype");
        $frommonthwise = $request->get('frommonthwise');
        $fromyearwise  = $request->get('toyearwise');
        $tomonthwise   = $request->get('tomonthwise');
        $toyearwise    = $request->get('toyearwise');
        $exporttype    = $request->get('exporttype');
        $eureka_ingenta = $request->get('eureka_ingenta');
        $fromdate      = $toyearwise . '-' . $frommonthwise . '-01';
        $todate        = $toyearwise . '-' . $tomonthwise . '-28';
        $UDRBegin = date('Y-m-01', strtotime($fromdate));
        $UDREnd   = $orginal_UDREnd = date('Y-m-t', strtotime($todate));
        
        $monthsyear = $this->getMonthsInRange5($UDRBegin, $UDREnd);
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[0]['year'];
            $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
        }
        //if partial data return
        if (strtotime($UDREnd) > strtotime(date('Y-m-t'))) {
            $error_flag = 3031;
            $exception_data = "Request could not be fulfilled in its entirety. Data that was available was returned.";
            $UDREnd     = date('Y-m-t');
        }
        
        $resultuser = User::getUserById($user_id);
        $org_name   = (isset($resultuser->display_name) ? $resultuser->display_name : $resultuser->first_name . ' ' . $resultuser->last_name);
        $user = User::where('id', $user_id)->first();
        $user_clause = " and a.user_id ='" . $user->id . "' ";
        $ingenta_user_clause = " and u.id ='" . $user->id . "' ";
        $user_clause_denail = " and a.userid ='" . $user->id . "' ";
        $user_clause_volume = " and dcv.user_id ='" . $user->id . "' ";
        $user_clause_chapter = " and dcc.user_id ='" . $user->id . "' ";
        if (!empty($user->user_associations)) {
            $a = explode(",", $user->user_associations);
            $user_associations = implode("','", $a);
            $rec = User::GetUsergroupConcat($user_associations);
            $user_clause = " and a.user_id in (" . $rec->user_associations . ") ";
            $ingenta_user_clause = " and u.id in (" . $rec->user_associations . ") ";
            $user_clause_denail = " and a.userid in (" . $rec->user_associations . ") ";
            $user_clause_volume = " and dcv.user_id in (" . $rec->user_associations . ") ";
            $user_clause_chapter = " and dcc.user_id in (" . $rec->user_associations . ") ";
        }
        //-------------------------Book Report Start---------------------------------
        if ($eureka_ingenta == 0) {
            if ($reptype == "8") {
                $query = "SELECT *,
                   
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_December',

                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and content_download_type='FO') THEN ebook_volume_id END) AS 'oa_January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO'  ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_December'

                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id, dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year,CAST('Book'  AS CHAR CHARACTER SET utf8) as Section_Type
                    FROM download_count_volume dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                            
                            " . $user_clause_volume . "
                    UNION
                SELECT
                dcv.chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year,CAST('Chapter'  AS CHAR CHARACTER SET utf8) as Section_Type
                FROM download_count_chapter dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                
                " . $user_clause_volume . "
                ) AS tmp
                GROUP BY ebook_volume_id";
                //dd($query);
                $result = DB::select($query);

                $query_investigation = "SELECT *,
                   
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN ebook_volume_id END) AS 'January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN  ebook_volume_id END) AS 'December' ,
                    
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type!='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'unique_title_inves_December',

                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January'  and content_download_type='FO') THEN ebook_volume_id END) AS 'oa_January',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_February',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_March',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_April',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_May',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_June',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_July',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_August',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_September',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_October',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_November',
                    COUNT(CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN  ebook_volume_id END) AS 'oa_December' ,

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO') THEN concat(session_id,',',ebook_volume_id,chapter_id) END) AS 'oa_unique_December',

                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='January' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_January',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='February' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_February',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='March' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_March',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='April' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_April',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='May' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_May',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='June' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_June',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='July' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_July',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='August' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_August',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='September' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_September',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='October' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_October',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='November' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_November',
                    COUNT(distinct CASE WHEN (date_format(from_unixtime(timestamp),'%M')='December' and content_download_type='FO' ) THEN concat(session_id,',',ebook_volume_id) END) AS 'oa_unique_title_inves_December'

                FROM
                    (
                    SELECT
                    CAST('0'  AS SIGNED) as chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn,Y.year AS volume_year,CAST('Book'  AS CHAR CHARACTER SET utf8) as Section_Type
                    FROM ebook_access_investigation_report dcv
                    INNER JOIN ebook_volume ev ON dcv.volume_id = ev.ebook_volume_id
                    INNER JOIN ebook e ON ev.ebook_id = e.ebook_id
                    INNER JOIN year Y ON Y.id = ev.year_id
                    and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                    and dcv.investigation_type!=0        
                            " . $user_clause_volume . "
                    UNION
                SELECT
                dcv.chapter_id,dcv.content_download_type,dcv.session_id,dcv.timestamp,ev.ebook_volume_id,e.title,ev.nid,ev.doi,ev.eissn,ev.issn,ev.isbn,ev.eisbn, Y.year AS volume_year,CAST('Chapter'  AS CHAR CHARACTER SET utf8) as Section_Type                    
                FROM chapter_access_investigation_report dcv
                INNER JOIN chapter c ON  dcv.chapter_id = c.chapter_id
                INNER JOIN ebook_volume ev ON  c.ebook_volume_id = ev.ebook_volume_id
                INNER JOIN ebook e ON  ev.ebook_id = e.ebook_id
                INNER JOIN year Y ON  Y.id = ev.year_id
                and to_days(from_unixtime(timestamp)) between to_days('" . $UDRBegin . "') and to_days('" . $UDREnd . "')
                and dcv.investigation_type!=0 
                " . $user_clause_volume . "
                ) AS tmp
                GROUP BY ebook_volume_id";
                //print($query_investigation); die;          
                $result_investigation = DB::select($query_investigation);
            }
        }
        $table = "<table width='800' align='center' >";
        $tableHeader_years = '';
        $blankTDs = "";
        $colspanccnt = 24;
        $colspanccnt = $colspanccnt - (12 - count($monthsyear));
        for ($k = 1; $colspanccnt >= $k; $k++) {
            $blankTDs .= "<td>&nbsp;</td>";
        }
        $exceptions_data = '';
        if (!empty($error_flag)) {
            $exceptions_data = $error_flag . ': ' . $error_codes[$error_flag];
            if($error_flag==3031){                   
                $exceptions_data =  $exceptions_data." (". $exception_data.")";
            }
        }
        if ($gentype == "0" && $reptype == "8") {
            $Report_Name       = "Book Usage by Access Type";
            $Report_ID         = "TR_B3";
            $Release           = "5";
            $Institution_Name  = $org_name;
            $Institution_ID    = "";
            $Metric_Type      = array("Total_Item_Investigations", "Total_Item_Requests", "Unique_Item_Investigations", "Unique_Item_Requests", "Unique_Title_Investigations", "Unique_Title_Requests");
            $Metric_Types      = implode("; ", $Metric_Type);
            $Report_Filters    = "Data_Type=Book; Access_Method=Regular";
            $Report_Attributes = "";
            $Exceptions        = $exceptions_data;
            $Reporting_Period  = "Begin_Date=" . $UDRBegin . "; End_Date=" . $orginal_UDREnd;
            $Created           = "'" . date('Y-m-d') . 'T' . date('h:m:s') . 'Z';
            $Created_By        = $Platform        = 'Eurekaselect';
        }
        $tableTopHeader = "<tr>
                            <td ><b>Report_Name</b></td>
                            <td ><b>" . $Report_Name . "</b></td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_ID</b></td>
                            <td align='left'>" . $Report_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Release</b></td>
                            <td align='left'>" . $Release . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_Name</b></td>
                            <td  align='left'>" . $Institution_Name . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Institution_ID</b></td>
                            <td  align='left'>" . $Institution_ID . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Metric_Types</b></td>
                            <td  align='left'>" . $Metric_Types . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Filters</b></td>
                            <td  align='left'>" . $Report_Filters . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Report_Attributes</b></td>
                            <td  align='left'>" . $Report_Attributes . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Exceptions</b></td>
                            <td  align='left'>" . $Exceptions . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td >Reporting_Period</td>
                            <td  align='left'>" . $Reporting_Period . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created</b></td>
                            <td  align='left'>" . $Created . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td><b>Created_By</b></td>
                            <td  align='left'>" . $Created_By . "</td>
                            " . $blankTDs . "
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>&nbsp;</td>
                            " . $blankTDs . "
                        </tr>
                        ";
        $tableHeader = "<tr align='center'>
                                <td align='left' bgcolor='#0b5394' style='color:#ffffff;'><b>Title</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Publisher_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Platform</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>DOI</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Proprietary_ID</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>ISBN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Print_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Online_ISSN</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>URI</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>YOP</b></td>
                                 <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Access_Type</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Metric_Type</b></td>
                                <td bgcolor='#0b5394'  style='color:#ffffff;'><b>Reporting_Period_Total</b></td>
                                ";
        for ($j = 0; $j < count($monthsyear); $j++) {
            $monthNum  = $monthsyear[$j]['month'];
            $year      = $monthsyear[$j]['year'];
            $monthName = date("M", mktime(0, 0, 0, $monthNum, 10));
            $tableHeader_years .= "<td bgcolor='#0b5394'  style='color:#ffffff;'><b>'$monthName-" . $year . "</b></td>";
        }
        $tableHeader .= $tableHeader_years;
        $tableHeader .= " </tr>   ";
        $table_rows      = "";
        $tr_data         = "";
        $tr_unique_data  = "";
        $jan             = 0;
        $feb             = 0;
        $mar             = 0;
        $apr             = 0;
        $may             = 0;
        $jun             = 0;
        $july            = 0;
        $aug             = 0;
        $sep             = 0;
        $oct             = 0;
        $nov             = 0;
        $dec             = 0;
        $unique_jan      = 0;
        $unique_feb      = 0;
        $unique_mar      = 0;
        $unique_apr      = 0;
        $unique_may      = 0;
        $unique_jun      = 0;
        $unique_july     = 0;
        $unique_aug      = 0;
        $unique_sep      = 0;
        $unique_oct      = 0;
        $unique_nov      = 0;
        $unique_dec      = 0;
        $prev_journal_id = 0;
        $TR              = array();
        $cnt = 0;
        if ($result) {
            foreach ($result as $row) {
                if (in_array('Total_Item_Requests', $Metric_Type)) {
                    $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    if ($total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['isbn']        = trim($row->isbn);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type'] = 'Controlled';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale']  = $total_sale;
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                    if ($oa_total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['isbn']        = trim($row->isbn);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type'] = 'OA_Gold';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Requests';
                        $TR[$cnt]['total_sale'] = $oa_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_December, 2);
                        $cnt++;
                    }
                }
                if (in_array('Unique_Item_Requests', $Metric_Type)) {
                    $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                    if ($unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = $unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);    
                        $cnt++;
                    }
                    $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                    if ($oa_unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Requests';
                        $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);                      
                        
                        $cnt++;
                    }
                    $unique_title_total_sale = round($row->unique_title_January, 2) + round($row->unique_title_February, 2) + round($row->unique_title_March, 2) + round($row->unique_title_April, 2) + round($row->unique_title_May, 2) + round($row->unique_title_June, 2) + round($row->unique_title_July, 2) + round($row->unique_title_August, 2) + round($row->unique_title_September, 2) + round($row->unique_title_October, 2) + round($row->unique_title_November, 2) + round($row->unique_title_December, 2);
                    if ($unique_title_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Requests';
                        $TR[$cnt]['total_sale'] = $unique_title_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_title_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_title_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_title_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_title_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_title_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_title_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_title_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_title_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_title_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_title_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_title_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_title_December, 2);                        
                        $cnt++;
                            }
                    $oa_unique_title_total_sale = round($row->oa_unique_title_January, 2) + round($row->oa_unique_title_February, 2) + round($row->oa_unique_title_March, 2) + round($row->oa_unique_title_April, 2) + round($row->oa_unique_title_May, 2) + round($row->oa_unique_title_June, 2) + round($row->oa_unique_title_July, 2) + round($row->oa_unique_title_August, 2) + round($row->oa_unique_title_September, 2) + round($row->oa_unique_title_October, 2) + round($row->oa_unique_title_November, 2) + round($row->oa_unique_title_December, 2);
                    if ($oa_unique_title_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Requests';
                        $TR[$cnt]['total_sale'] = $oa_unique_title_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_title_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_title_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_title_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_title_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_title_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_title_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_title_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_title_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_title_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_title_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_title_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_title_December, 2);                        
                        $cnt++;
                    }
                }
            } //while
        } //if
        if ($result_investigation) {
            foreach ($result_investigation as $row) {
                if (in_array('Total_Item_Investigations', $Metric_Type)) {
                    $total_sale = round($row->January, 2) + round($row->February, 2) + round($row->March, 2) + round($row->April, 2) + round($row->May, 2) + round($row->June, 2) + round($row->July, 2) + round($row->August, 2) + round($row->September, 2) + round($row->October, 2) + round($row->November, 2) + round($row->December, 2);
                    if ($total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['isbn']        = trim($row->isbn);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type'] = 'Controlled';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale']  = $total_sale;
                        $TR[$cnt]['jan']  = round($row->January, 2);
                        $TR[$cnt]['feb']  = round($row->February, 2);
                        $TR[$cnt]['mar']  = round($row->March, 2);
                        $TR[$cnt]['apr']  = round($row->April, 2);
                        $TR[$cnt]['may']  = round($row->May, 2);
                        $TR[$cnt]['jun']  = round($row->June, 2);
                        $TR[$cnt]['july'] = round($row->July, 2);
                        $TR[$cnt]['aug']  = round($row->August, 2);
                        $TR[$cnt]['sep']  = round($row->September, 2);
                        $TR[$cnt]['oct']  = round($row->October, 2);
                        $TR[$cnt]['nov']  = round($row->November, 2);
                        $TR[$cnt]['dec']  = round($row->December, 2);
                        $cnt++;
                    }
                    $oa_total_sale  = round($row->oa_January, 2) + round($row->oa_February, 2) + round($row->oa_March, 2) + round($row->oa_April, 2) + round($row->oa_May, 2) + round($row->oa_June, 2) + round($row->oa_July, 2) + round($row->oa_August, 2) + round($row->oa_September, 2) + round($row->oa_October, 2) + round($row->oa_November, 2) + round($row->oa_December, 2);
                    if ($oa_total_sale > 0) {
                        $TR[$cnt]['title']       = trim($row->title);
                        $TR[$cnt]['doi']         = trim($row->doi);
                        $TR[$cnt]['isbn']        = trim($row->isbn);
                        $TR[$cnt]['issn']        = trim($row->issn);
                        $TR[$cnt]['eissn']       = trim($row->eissn);
                        $TR[$cnt]['nid']         = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type'] = 'OA_Gold';
                        $TR[$cnt]['Metric_Type'] = 'Total_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $oa_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_December, 2);
                        $cnt++;
                    }
                }
                if (in_array('Unique_Item_Investigations', $Metric_Type)) {
                    $unique_total_sale = round($row->unique_January, 2) + round($row->unique_February, 2) + round($row->unique_March, 2) + round($row->unique_April, 2) + round($row->unique_May, 2) + round($row->unique_June, 2) + round($row->unique_July, 2) + round($row->unique_August, 2) + round($row->unique_September, 2) + round($row->unique_October, 2) + round($row->unique_November, 2) + round($row->unique_December, 2);
                    if ($unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_December, 2);                        
                        $cnt++;
                    }
                    $oa_unique_total_sale = round($row->oa_unique_January, 2) + round($row->oa_unique_February, 2) + round($row->oa_unique_March, 2) + round($row->oa_unique_April, 2) + round($row->oa_unique_May, 2) + round($row->oa_unique_June, 2) + round($row->oa_unique_July, 2) + round($row->oa_unique_August, 2) + round($row->oa_unique_September, 2) + round($row->oa_unique_October, 2) + round($row->oa_unique_November, 2) + round($row->oa_unique_December, 2);
                    if ($oa_unique_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Item_Investigations';
                        $TR[$cnt]['total_sale'] = $oa_unique_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_December, 2);                      
                       
                        $cnt++;
                    }
                    $unique_title_inves_total_sale = round($row->unique_title_inves_January, 2) + round($row->unique_title_inves_February, 2) + round($row->unique_title_inves_March, 2) + round($row->unique_title_inves_April, 2) + round($row->unique_title_inves_May, 2) + round($row->unique_title_inves_June, 2) + round($row->unique_title_inves_July, 2) + round($row->unique_title_inves_August, 2) + round($row->unique_title_inves_September, 2) + round($row->unique_title_inves_October, 2) + round($row->unique_title_inves_November, 2) + round($row->unique_title_inves_December, 2);
                    if ($unique_title_inves_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'Controlled';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Investigations';
                        $TR[$cnt]['total_sale'] = $unique_title_inves_total_sale;
                        $TR[$cnt]['jan']  = round($row->unique_title_inves_January, 2);
                        $TR[$cnt]['feb']  = round($row->unique_title_inves_February, 2);
                        $TR[$cnt]['mar']  = round($row->unique_title_inves_March, 2);
                        $TR[$cnt]['apr']  = round($row->unique_title_inves_April, 2);
                        $TR[$cnt]['may']  = round($row->unique_title_inves_May, 2);
                        $TR[$cnt]['jun']  = round($row->unique_title_inves_June, 2);
                        $TR[$cnt]['july'] = round($row->unique_title_inves_July, 2);
                        $TR[$cnt]['aug']  = round($row->unique_title_inves_August, 2);
                        $TR[$cnt]['sep']  = round($row->unique_title_inves_September, 2);
                        $TR[$cnt]['oct']  = round($row->unique_title_inves_October, 2);
                        $TR[$cnt]['nov']  = round($row->unique_title_inves_November, 2);
                        $TR[$cnt]['dec']  = round($row->unique_title_inves_December, 2);                        
                        $cnt++;
                            }
                    $oa_unique_title_inves_total_sale = round($row->oa_unique_title_inves_January, 2) + round($row->oa_unique_title_inves_February, 2) + round($row->oa_unique_title_inves_March, 2) + round($row->oa_unique_title_inves_April, 2) + round($row->oa_unique_title_inves_May, 2) + round($row->oa_unique_title_inves_June, 2) + round($row->oa_unique_title_inves_July, 2) + round($row->oa_unique_title_inves_August, 2) + round($row->oa_unique_title_inves_September, 2) + round($row->oa_unique_title_inves_October, 2) + round($row->oa_unique_title_inves_November, 2) + round($row->oa_unique_title_inves_December, 2);
                    if ($oa_unique_title_inves_total_sale > 0) {
                        $TR[$cnt]['title']             = trim($row->title);
                        $TR[$cnt]['doi']               = trim($row->doi);
                        $TR[$cnt]['isbn']              = trim($row->isbn);
                        $TR[$cnt]['issn']              = trim($row->issn);
                        $TR[$cnt]['eissn']             = trim($row->eissn);
                        $TR[$cnt]['nid']               = $row->nid;
                        $TR[$cnt]['volume_year']       = trim($row->volume_year);
                        $TR[$cnt]['Access_Type']       = 'OA_Gold';
                        $TR[$cnt]['Metric_Type']       = 'Unique_Title_Investigations';
                        $TR[$cnt]['total_sale'] = $oa_unique_title_inves_total_sale;
                        $TR[$cnt]['jan']  = round($row->oa_unique_title_inves_January, 2);
                        $TR[$cnt]['feb']  = round($row->oa_unique_title_inves_February, 2);
                        $TR[$cnt]['mar']  = round($row->oa_unique_title_inves_March, 2);
                        $TR[$cnt]['apr']  = round($row->oa_unique_title_inves_April, 2);
                        $TR[$cnt]['may']  = round($row->oa_unique_title_inves_May, 2);
                        $TR[$cnt]['jun']  = round($row->oa_unique_title_inves_June, 2);
                        $TR[$cnt]['july'] = round($row->oa_unique_title_inves_July, 2);
                        $TR[$cnt]['aug']  = round($row->oa_unique_title_inves_August, 2);
                        $TR[$cnt]['sep']  = round($row->oa_unique_title_inves_September, 2);
                        $TR[$cnt]['oct']  = round($row->oa_unique_title_inves_October, 2);
                        $TR[$cnt]['nov']  = round($row->oa_unique_title_inves_November, 2);
                        $TR[$cnt]['dec']  = round($row->oa_unique_title_inves_December, 2);                        
                        $cnt++;
                    }
                }
            } //while
        } //if
        //print_r($TR); die;
        usort($TR, function ($a, $b) {
            //$c = $a['title'] - $b['title'];
            //$c .= $b['volume_year'] - $a['volume_year'];
            //return $c;
            if ($a['title'] == $b['title']) {
                return $b['Access_Type'];
            }
            return strcmp($a['title'], $b['title']);
        });
        //print_r($TR); die;
        //ksort($TR);
        for ($i = 0; $i < count($TR); $i++) {
            $tr_data .= "<tr align='center'> ";
            $tr_data .= "<td align='left'>" . trim($TR[$i]['title']) . "</td>
                        <td>Bentham Science Publishers</td>
                        <td></td>
                        <td>Eurekaselect</td>
                        <td>" . $TR[$i]['doi'] . "</td>
                        <td>&nbsp;</td>
                        <td>" . $TR[$i]['isbn'] . "</td>
                        <td>" . $TR[$i]['issn'] . "</td>
                        <td>" . $TR[$i]['eissn'] . "</td>
                        <td>https://www.eurekaselect.com/node/" . $TR[$i]['nid'] . "</td>                 
                        <td>" . $TR[$i]['volume_year'] . "</td>";
            $tr_data .= "<td>" . $TR[$i]['Access_Type'] . "</td>";
            $tr_data .= "<td>" . $TR[$i]['Metric_Type'] . "</td>";
            $tr_data .= "<td>" . $TR[$i]['total_sale'] . "</td>";
            for ($j = 0; $j < count($monthsyear); $j++) {
                $monthNum  = $monthsyear[$j]['month'];
                $monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
                //$tr_data .="<td>".round($row->$monthName,2)."</td>";
                if ($monthName == "January") {
                    $tr_data .= "<td>" . round($TR[$i]['jan'], 2) . "</td>";
                }
                if ($monthName == "February") {
                    $tr_data .= "<td>" . round($TR[$i]['feb'], 2) . "</td>";
                }
                if ($monthName == "March") {
                    $tr_data .= "<td>" . round($TR[$i]['mar'], 2) . "</td>";
                }
                if ($monthName == "April") {
                    $tr_data .= "<td>" . round($TR[$i]['apr'], 2) . "</td>";
                }
                if ($monthName == "May") {
                    $tr_data .= "<td>" . round($TR[$i]['may'], 2) . "</td>";
                }
                if ($monthName == "June") {
                    $tr_data .= "<td>" . round($TR[$i]['jun'], 2) . "</td>";
                }
                if ($monthName == "July") {
                    $tr_data .= "<td>" . round($TR[$i]['july'], 2) . "</td>";
                }
                if ($monthName == "August") {
                    $tr_data .= "<td>" . round($TR[$i]['aug'], 2) . "</td>";
                }
                if ($monthName == "September") {
                    $tr_data .= "<td>" . round($TR[$i]['sep'], 2) . "</td>";
                }
                if ($monthName == "October") {
                    $tr_data .= "<td>" . round($TR[$i]['oct'], 2) . "</td>";
                }
                if ($monthName == "November") {
                    $tr_data .= "<td>" . round($TR[$i]['nov'], 2) . "</td>";
                }
                if ($monthName == "December") {
                    $tr_data .= "<td>" . round($TR[$i]['dec'], 2) . "</td>";
                }
            }
            $tr_data .= "</tr>";
        } //while
        $table .= $tableTopHeader . $tableHeader . $tr_data . "</table>";
        return $table;
    }
}
