<?php
class ReportsController extends Controller {
    //Index Action
    public function indexAction() {
        $this->ordersAction();
    }
    // To Report Orders
    public function ordersAction() {
        $ObjAffiliate       = new AffiliateModel();
        $settingsDateFormat = $this->getSettingValue('site_settings', 'date_format');
        $affiliateUserID    = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $links              = "/reports/orders/";
        $sortby             = $this->escape($this->getRequestValue("sortby"));
        $orderby            = $this->escape($this->getRequestValue("orderby"));
        $widget             = $this->escape($this->getRequestValue('widgetname'));
        $export             = $this->escape($this->getRequestValue("export"));
        // to fetch widget names
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 10);
        //page settings
        if (isset($_POST['formSubmitSearch'])) {
            if (trim($_POST['orderID'])) {
                $links .= "id/" . $_POST['orderID'] . "/";
            }
            if ($_POST['orderStatus']) {
                $links .= "status/" . $this->escape($_POST['orderStatus']) . "/";
            }
            if ($_POST['date']) {
                $date     = explode(" - ", $this->escape($_POST['date']));
                $fromdate = trim($date[0]);
                $toDate   = trim((isset($date[1])) ? $date[1] : '');
                $links .= "fromdate/" . $this->urlDate($fromdate, 'set', $settingsDateFormat) . "/";
                if ($toDate) {
                    $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
                }
            }
            if ($_POST['affiliateWidgetName']) {
                $links .= "widgetname/" . $this->escape($_POST['affiliateWidgetName']) . "/";
            }
            $this->setRedirect($links);
        }
        $orderID     = $this->getRequestValue('id');
        $orderStatus = $this->escape($this->getRequestValue('status'));
        $fromDate    = $this->urlDate($this->escape($this->getRequestValue('fromdate')), 'get', $settingsDateFormat);
        $toDate      = $this->urlDate($this->escape($this->getRequestValue('todate')), 'get', $settingsDateFormat);
        if ($fromDate) {
            $date = $fromDate;
            if ($toDate) {
                $date .= " - " . $toDate;
            }
        } else {
            $date     = $this->getFormattedDate("01" . date("-m-Y")) . " - " . $this->getFormattedDate(date('d-m-Y'));
            $fromDate = $this->getFormattedDate("01" . date("-m-Y"));
            $toDate   = $this->getFormattedDate(date('d-m-Y'));
        }
        if ($orderID) {
            $links .= "id/" . $orderID . "/";
        }
        if ($orderStatus) {
            $links .= "status/" . $orderStatus . "/";
        }
        if ($fromDate) {
            $links .= "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
        }
        if ($toDate) {
            $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
        }
        if ($widget) {
            $links .= "widgetname/" . $widget . "/";
        }
        if (($sortby == "oid") && ($orderby == "asc")) {
            $sortkey = "desc";
        }
        if (($sortby == "oid") && ($orderby == "desc")) {
            $sortkey = "asc";
        }
        if (($sortby == "odate") && ($orderby == "asc")) {
            $sortkey1 = "desc";
        }
        if (($sortby == "odate") && ($orderby == "desc")) {
            $sortkey1 = "asc";
        }
        if (($sortby == "ocom") && ($orderby == "asc")) {
            $sortkey2 = "desc";
        }
        if (($sortby == "ocom") && ($orderby == "desc")) {
            $sortkey2 = "asc";
        }
        if (($sortby == "ostatus") && ($orderby == "asc")) {
            $sortkey3 = "desc";
        }
        if (($sortby == "ostatus") && ($orderby == "desc")) {
            $sortkey3 = "asc";
        }
        $POST['limitFrom']       = $limitFrom;
        $POST['limitTo']         = $limitTo;
        $POST['affiliateUserID'] = $affiliateUserID;
        $POST['orderID']         = $orderID;
        $POST['orderStatus']     = $orderStatus;
        $POST['fromdate']        = $this->convertDateForSQL($fromDate, $settingsDateFormat);
        $POST['todate']          = $this->convertDateForSQL($toDate, $settingsDateFormat);
        $POST['widget']          = $widget;
        $POST['sortby']          = $sortby;
        $POST['orderby']         = $orderby;
        $languageLabel[]         = $this->getLanguageValue('lblAdjusted');
        $languageLabel[]         = $this->getLanguageValue('lblNotAdjusted');
        $languageLabel[]         = $this->getLanguageValue('lblCompleted');
        $languageLabel[]         = $this->getLanguageValue('lblCancelledd');
        $languageLabel[]         = $this->getLanguageValue('lblReceived');
        $languageLabel[]         = $this->getLanguageValue('lblPending');
        if ($export == 1) {
            $POST['limitFrom']  = 0;
            $POST['limitTo']    = '';
            $resOrderListExport = $ObjAffiliate->listOrderDetails($POST, $purpose = 'order', $languageLabel);
            $MAX                = 6; // maximum number of elements
            $header             = array(
                $this->getLanguageValue('lblOrderID'),
                $this->getLanguageValue('lblOrderDate'),
                $this->getLanguageValue('lblQuantity'),
                $this->getLanguageValue('lblSubTotalAmount'),
                $this->getLanguageValue('lblCommission'),
                $this->getLanguageValue('lblOrderStatus')
            );
            foreach ($resOrderListExport as &$element) {
                $element = array_slice($element, 0, $MAX);
            }
            $ObjCsv = new Csv();
            if (!empty($resOrderListExport)) {
                $dateTime = "Order_Details_" . date('d_m_Y_H_i_s');
                $data     = $ObjCsv->output($dateTime . '.csv', $resOrderListExport, $header);
            } else {
                $this->setMessage("language.msgNoBusiness", 'error');
            }
        }
        $resOrderList  = $ObjAffiliate->listOrderDetails($POST, $purpose = 'order', $languageLabel);
        //To fetch the details of Orders
        $resPagesCount = $this->setPagesCount($key = 'page');
        $history       = $this->getCurrentURL();
        $reswidget     = $ObjAffiliate->getOrderWidgetDetails($affiliateUserID, $widget);
        $this->ObjView->assign('resAffiliate', $resOrderList);
        $this->ObjView->assign('resOrderWidget', $reswidget);
        $this->ObjView->assign('orderID', $orderID);
        $this->ObjView->assign('orderStatus', $orderStatus);
        $this->ObjView->assign('sortkey', (isset($sortkey)) ? $sortkey : '');
        $this->ObjView->assign('sortkey1', (isset($sortkey1)) ? $sortkey1 : '');
        $this->ObjView->assign('sortkey2', (isset($sortkey2)) ? $sortkey2 : '');
        $this->ObjView->assign('sortkey3', (isset($sortkey3)) ? $sortkey3 : '');
        $this->ObjView->assign('date', $date);
        $this->ObjView->assign('previousUrl', $history);
        $this->ObjView->assign('links', $links);
        $this->ObjView->display('Orders.tpl');
    }
    //To Report Charge Back Orders
    public function chargebackordersAction() {
        $ObjAffiliate       = new AffiliateModel();
        $settingsDateFormat = $this->getSettingValue('site_settings', 'date_format');
        $affiliateUserID    = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $links              = "/reports/chargebackorders/";
        $sortby             = $this->escape($this->getRequestValue("sortby"));
        $orderby            = $this->escape($this->getRequestValue("orderby"));
        $export             = $this->escape($this->getRequestValue("export"));
        $history            = $this->getCurrentURL();
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 10);
        //page Settings
        if (isset($_POST['formSubmitSearch'])) {
            if ($_POST['orderID']) {
                $links .= "id/" . $_POST['orderID'] . "/";
            }
            if ($_POST['date']) {
                $date     = explode(" - ", $_POST['date']);
                $fromdate = trim($date[0]);
                $toDate   = trim((isset($date[1])) ? $date[1] : '');
                $links .= "fromdate/" . $this->urlDate($fromdate, 'set', $settingsDateFormat) . "/";
                if ($toDate) {
                    $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
                }
            }
            $this->setRedirect($links);
        }
        $orderID  = $this->escape((trim($this->getRequestValue('id'))));
        $fromDate = $this->urlDate($this->escape($this->getRequestValue('fromdate')), 'get', $settingsDateFormat);
        $toDate   = $this->urlDate($this->escape($this->getRequestValue('todate')), 'get', $settingsDateFormat);
        if ($fromDate) {
            $date = $fromDate;
            if ($toDate) {
                $date .= " - " . $toDate;
            }
        } else {
            $date     = $this->getFormattedDate("01" . date("-m-Y")) . " - " . $this->getFormattedDate(date('d-m-Y'));
            $fromDate = $this->getFormattedDate("01" . date("-m-Y"));
            $toDate   = $this->getFormattedDate(date('d-m-Y'));
        }
        if ($orderID) {
            $links .= "id/" . $orderID . "/";
        }
        if ($fromDate) {
            $links .= "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
        }
        if ($toDate) {
            $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
        }
        if (($sortby == "oid") && ($orderby == "asc")) {
            $sortkey = "desc";
        }
        if (($sortby == "oid") && ($orderby == "desc")) {
            $sortkey = "asc";
        }
        if (($sortby == "odate") && ($orderby == "asc")) {
            $sortkey1 = "desc";
        }
        if (($sortby == "odate") && ($orderby == "desc")) {
            $sortkey1 = "asc";
        }
        if (($sortby == "ldate") && ($orderby == "asc")) {
            $sortkey2 = "desc";
        }
        if (($sortby == "ldate") && ($orderby == "desc")) {
            $sortkey2 = "asc";
        }
        if (($sortby == "ocom") && ($orderby == "asc")) {
            $sortkey3 = "desc";
        }
        if (($sortby == "ocom") && ($orderby == "desc")) {
            $sortkey3 = "asc";
        }
        if (($sortby == "ostatus") && ($orderby == "asc")) {
            $sortkey4 = "desc";
        }
        if (($sortby == "ostatus") && ($orderby == "desc")) {
            $sortkey4 = "asc";
        }
        $POST['limitFrom']       = $limitFrom;
        $POST['limitTo']         = $limitTo;
        $POST['affiliateUserID'] = $affiliateUserID;
        $POST['orderID']         = $orderID;
        $POST['fromdate']        = $this->convertDateForSQL($fromDate, $settingsDateFormat);
        $POST['todate']          = $this->convertDateForSQL($toDate, $settingsDateFormat);
        $POST['sortby']          = $sortby;
        $POST['orderby']         = $orderby;
        $languageLabel[]         = $this->getLanguageValue('lblAdjusted');
        $languageLabel[]         = $this->getLanguageValue('lblNotAdjusted');
        if ($export == 1) {
            $POST['limitFrom']  = 0;
            $POST['limitTo']    = '';
            $resOrderListExport = $ObjAffiliate->listChargeBackOrders($POST, $purpose = 'chargebackorders', $languageLabel);
            $MAX                = 7; // maximum number of elements
            $header             = array(
                $this->getLanguageValue('lblOrderID'),
                $this->getLanguageValue('lblOrderDate'),
                $this->getLanguageValue('lblLastModifiedDate'),
                $this->getLanguageValue('lblQuantity'),
                $this->getLanguageValue('lblSubTotalAmount'),
                $this->getLanguageValue('lblCommissionAmount'),
                $this->getLanguageValue('lblStatus')
            );
            foreach ($resOrderListExport as &$element) {
                $element = array_slice($element, 0, $MAX);
            }
            $ObjCsv = new Csv();
            if (!empty($resOrderListExport)) {
                $dateTime = "Charge_Back_Orders_Details" . date('d_m_Y_H_i_s');
                $data     = $ObjCsv->output($dateTime . '.csv', $resOrderListExport, $header);
            } else {
                $this->setMessage("language.msgNoBusiness", 'error');
            }
        }
        $resOrderList  = $ObjAffiliate->listChargeBackOrders($POST, $purpose = 'chargebackorders', $languageLabel);
        //To fetch charge back orders details
        $resPagesCount = $this->setPagesCount($key = 'page');
        $this->ObjView->assign('links', $links);
        $this->ObjView->assign('orderID', $orderID);
        $this->ObjView->assign('date', $date);
        $this->ObjView->assign('sortkey', (isset($sortkey)) ? $sortkey : '');
        $this->ObjView->assign('sortkey1', (isset($sortkey1)) ? $sortkey1 : '');
        $this->ObjView->assign('sortkey2', (isset($sortkey2)) ? $sortkey2 : '');
        $this->ObjView->assign('sortkey3', (isset($sortkey3)) ? $sortkey3 : '');
        $this->ObjView->assign('sortkey4', (isset($sortkey4)) ? $sortkey4 : '');
        $this->ObjView->assign('resOrderlist', $resOrderList);
        $this->ObjView->assign('previousUrl', $history);
        $this->ObjView->display('ChargeBackOrders.tpl');
    }
    public function demographicsAction() {
        $ObjAffiliate       = new AffiliateModel();
        $export             = (int) $this->getRequestValue("export");
        $languageIDs        = $this->ObjView->languageIDs;
        $affiliateUserID    = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $settingsDateFormat = $this->getSettingValue('site_settings', 'date_format');
        $fromDate           = $toDate = $location = $widget = '';
        if (isset($_POST['formSubmitSearch'])) {
            $links         = "/reports/demographics/";
            $location      = $this->escape($_POST['locationName']);
            $widget        = $this->escape($_POST['widgetSelect']);
            $_POST['date'] = $this->escape($_POST['date']);
            if ($_POST['date'] != 'Select') {
                if (strpos($_POST['date'], " - ")) {
                    list($fromDate, $toDate) = explode(" - ", $_POST['date']);
                } else {
                    $fromDate = $toDate = $_POST['date'];
                }
            }
            if ($location != '') {
                $links .= "loc/" . $location . "/";
            }
            if ($widget != '') {
                $links .= "widget/" . $widget . "/";
            }
            if ($fromDate != '') {
                $links .= "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
            }
            if ($toDate != '') {
                $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
            }
            $this->setRedirect($links);
        } else {
            $normalLink = 'reports/demographics';
            $location   = $this->escape($this->getRequestValue("loc"));
            $widget     = $this->escape($this->getRequestValue("widget"));
            $field      = $this->escape($this->getRequestValue("sortby"));
            $order      = $this->escape($this->getRequestValue("orderby"));
            $fromDate   = $this->escape($this->getRequestValue("fromdate"));
            $toDate     = $this->escape($this->getRequestValue("todate"));
            if ($fromDate) {
                $date = $fromDate;
                if ($toDate) {
                    $date .= " - " . $toDate;
                }
            } else {
                $date     = $this->getFormattedDate(date("Y-m-")) . "01 - " . $this->getFormattedDate(date('Y-m-d'));
                $fromDate = $this->getFormattedDate(date("Y-m-") . "01");
                $toDate   = $this->getFormattedDate(date('Y-m-d'));
            }
            if ($location != '')
                $normalLink .= "/loc/" . $location;
            if ($widget != '') {
                $normalLink .= "/widget/" . $widget;
            }
            if ($fromDate != '') {
                $normalLink .= "/fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat);
            }
            if ($toDate != '') {
                $normalLink .= "/todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat);
            }
            if ($fromDate) {
                $fromDate = $this->urlDate($fromDate, 'get', $settingsDateFormat);
            }
            if ($toDate) {
                $toDate = $this->urlDate($toDate, 'get', $settingsDateFormat);
            }
            $fromDateDB = $this->convertDateForSQL($fromDate, $settingsDateFormat);
            $toDateDB   = $this->convertDateForSQL($toDate, $settingsDateFormat);
        }
        $demoGraphic = $ObjAffiliate->getDemographics($affiliateUserID, $widget, $fromDateDB, $toDateDB, $location, $field, $order, $export);
        if ($export == 1) {
            foreach ($demoGraphic as $key => $element) {
                $resOrderListExport[$key]['location']     = $element['location'];
                $resOrderListExport[$key]['visitors']     = $element['visitors'];
                $resOrderListExport[$key]['subscription'] = $element['subscription'];
                $resOrderListExport[$key]['registration'] = $element['registration'];
                $resOrderListExport[$key]['sales']        = $element['sales'];
                $resOrderListExport[$key]['revenue']      = $element['revenue'];
            }
            $MAX      = 6; // maximum number of elements
            $header   = array(
                $this->getLanguageValue("lblLocation"),
                $this->getLanguageValue("lblViists"),
                $this->getLanguageValue("lblSubscriptions"),
                $this->getLanguageValue("lblRegistrations"),
                $this->getLanguageValue("lblSales"),
                $this->getLanguageValue("lblRevenue")
            );
            $visitors = $register = $subscribe = $sales = $revenue = 0;
            foreach ($resOrderListExport as $key => $element) {
                $visitors += $element['visitors'];
                $register += $element['subscription'];
                $subscribe += $element['registration'];
                $sales += $element['sales'];
                $revenue += $element['revenue'];
            }
            array_push($resOrderListExport, array(
                $this->getLanguageValue("lblTotalAvg"),
                $visitors,
                $register,
                $subscribe,
                number_format($sales, 2, '.', ''),
                number_format($revenue, 2, '.', '')
            ));
            $ObjCsv = new Csv();
            if (!empty($resOrderListExport)) {
                $dateTime = "Demographics_Details_" . date('d_m_Y_H_i_s');
                $data     = $ObjCsv->output($dateTime . '.csv', $resOrderListExport, $header);
            } else {
                $this->setMessage("language.msgNoBusiness", 'error');
            }
        }
        $widgets = $ObjAffiliate->listWidgets($affiliateUserID);
        $this->ObjView->assign("widgets", $widgets);
        $this->ObjView->assign("field", $field);
        $this->ObjView->assign("orderClass", ($order == 'asc' || $order == 'desc') ? $order : 'asc');
        $this->ObjView->assign("order", $order == 'asc' ? 'desc' : 'asc');
        $this->ObjView->assign("previousUrl", $normalLink);
        $this->ObjView->assign("demoGraphic", $demoGraphic);
        $this->ObjView->assign("location", $location);
        $this->ObjView->assign("widgetSelected", $widget);
        ($toDate != '') ? $dateRange = $fromDate . " - " . $toDate : $dateRange = $fromDate;
        $this->ObjView->assign("dateRange", $dateRange);
        $this->ObjView->display('DemographicsReport.tpl');
    }
    //Revenue Summary Details
    public function revenuesummaryAction() {
        $ObjAffiliate             = new AffiliateModel();
        $settingsDateFormat       = $this->getSettingValue('site_settings', 'date_format');
        $affiliateUserID          = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $links                    = "/reports/revenuesummary/";
        $sortby                   = $this->escape($this->getRequestValue("sortby"));
        $orderby                  = $this->escape($this->getRequestValue("orderby"));
        $export                   = (int) $this->getRequestValue("export");
        $history                  = $this->getCurrentURL();
        $resAffiliateSettings     = $ObjAffiliate->getAffCommissionSettings($affiliateUserID);
        $indSubSettings           = isset($resAffiliateSettings['subscriptionCommission']) ? $resAffiliateSettings['subscriptionCommission'] : '';
        $indRegSettings           = isset($resAffiliateSettings['registrationCommission']) ? $resAffiliateSettings['registrationCommission'] : '';
        $impUserSettings          = isset($resAffiliateSettings['bulkUserImport']) ? $resAffiliateSettings['bulkUserImport'] : '';
        $impSubscriberSettings    = isset($resAffiliateSettings['importedSubscriber']) ? $resAffiliateSettings['importedSubscriber'] : '';
        $subscribeEnable          = $this->getSettingValue('affiliate_settings', 'affiliate_subscription_commission');
        $registrationEnable       = $this->getSettingValue('affiliate_settings', 'affiliate_registeration_commission');
        $importedUserEnable       = $this->getSettingValue('affiliate_settings', 'affiliate_bulk_user_import');
        $importedSubscriberEnable = $this->getSettingValue('affiliate_settings', 'affiliate_bulk_subscriber_import');
        list($limitFrom, $limitTo) = $this->getPageSettings($key = 'page', $rows = 10);
        //page Settings
        if (isset($_POST['formSubmitSearch'])) {
            if ($_POST['daterange']) {
                $date     = explode(" - ", $this->escape($_POST['daterange']));
                $fromdate = trim($date[0]);
                $toDate   = trim((isset($date[1])) ? $date[1] : '');
                $links .= "fromdate/" . $this->urlDate($fromdate, 'set', $settingsDateFormat) . "/";
                if ($toDate) {
                    $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
                }
            }
            if ($_POST['source']) {
                $links .= "source/" . $this->escape($_POST['source']) . "/";
            }
            $this->setRedirect($links);
        }
        $source   = $this->escape($this->getRequestValue('source'));
        $fromDate = $this->urlDate($this->escape($this->getRequestValue('fromdate')), 'get', $settingsDateFormat);
        $toDate   = $this->urlDate($this->escape($this->getRequestValue('todate')), 'get', $settingsDateFormat);
        if ($fromDate) {
            $date = $fromDate;
            if ($toDate) {
                $date .= " - " . $toDate;
            }
        } else {
            $date     = $this->getFormattedDate("01" . date("-m-Y")) . " - " . $this->getFormattedDate(date('d-m-Y'));
            $fromDate = $this->getFormattedDate("01" . date("-m-Y"));
            $toDate   = $this->getFormattedDate(date('d-m-Y'));
        }
        if ($source) {
            $links .= "source/" . $source . "/";
        }
        if ($fromDate) {
            $links .= "fromdate/" . $this->urlDate($fromDate, 'set', $settingsDateFormat) . "/";
        }
        if ($toDate) {
            $links .= "todate/" . $this->urlDate($toDate, 'set', $settingsDateFormat) . "/";
        }
        if (($sortby == "date") && ($orderby == "asc")) {
            $sortkey = "desc";
        }
        if (($sortby == "date") && ($orderby == "desc")) {
            $sortkey = "asc";
        }
        if (($sortby == "ocommission") && ($orderby == "asc")) {
            $sortkey1 = "desc";
        }
        if (($sortby == "ocommission") && ($orderby == "desc")) {
            $sortkey1 = "asc";
        }
        if (($sortby == "scommission") && ($orderby == "asc")) {
            $sortkey2 = "desc";
        }
        if (($sortby == "scommission") && ($orderby == "desc")) {
            $sortkey2 = "asc";
        }
        if (($sortby == "rcommission") && ($orderby == "asc")) {
            $sortkey3 = "desc";
        }
        if (($sortby == "rcommission") && ($orderby == "desc")) {
            $sortkey3 = "asc";
        }
        if (($sortby == "tcommission") && ($orderby == "asc")) {
            $sortkey4 = "desc";
        }
        if (($sortby == "tcommission") && ($orderby == "desc")) {
            $sortkey4 = "asc";
        }
        if (($sortby == "iucommission") && ($orderby == "asc")) {
            $sortkey5 = "desc";
        }
        if (($sortby == "iucommission") && ($orderby == "desc")) {
            $sortkey5 = "asc";
        }
        if (($sortby == "iscommission") && ($orderby == "asc")) {
            $sortkey6 = "desc";
        }
        if (($sortby == "iscommission") && ($orderby == "desc")) {
            $sortkey6 = "asc";
        }
        $resRevenueList     = $ObjAffiliate->listRevenueSummaryDetails($limitFrom, $limitTo, $affiliateUserID, $source, $sortby, $orderby, $this->convertDateForSQL($fromDate, $settingsDateFormat), $this->convertDateForSQL($toDate, $settingsDateFormat));
        $subCh              = 1;
        $regCh              = 1;
        $iuserCh            = 1;
        $isubscriberCh      = 1;
        $exportElement      = 7;
        $subNo              = 1;
        $regNo              = 1;
        $iuserNo            = 1;
        $isubNo             = 1;
        $totalSubscription  = 0;
        $totalRegistration  = 0;
        $totalImpUser       = 0;
        $totalImpSubscriber = 0;
        $totalOrder         = 0;
        $totalRevenue       = 0;
        foreach ($resRevenueList as $key => $val) {
            $totalOrder         = $totalOrder + $val['OrderAmount'];
            $totalSubscription  = $totalSubscription + $val['SubscriptionAmount'];
            $totalRegistration  = $totalRegistration + $val['RegistrationAmount'];
            $totalImpUser       = $totalImpUser + $val['impUserCommission'];
            $totalImpSubscriber = $totalImpSubscriber + $val['impSubscriberCommission'];
            $totalRevenue       = $totalRevenue + $val['TotalAmount'];
        }
        foreach ($resRevenueList as $key => $val) {
            if ($val['SubscriptionAmount'] > 0) {
                $subNo = 0;
                break;
            }
        }
        foreach ($resRevenueList as $key => $val) {
            if ($val['RegistrationAmount'] > 0) {
                $regNo = 0;
                break;
            }
        }
        foreach ($resRevenueList as $key => $val) {
            if ($val['impUserCommission'] > 0) {
                $iuserNo = 0;
                break;
            }
        }
        foreach ($resRevenueList as $key => $val) {
            if ($val['impSubscriberCommission'] > 0) {
                $isubNo = 0;
                break;
            }
        }
        if ($subNo == 1) {
            if (($indSubSettings == 'Yes' && $subscribeEnable == 'No') || ($indSubSettings == 'No')) {
                foreach ($resRevenueList as $key => $val) {
                    unset($resRevenueList[$key]['SubscriptionAmount']);
                }
                $subCh         = 0;
                $exportElement = $exportElement - 1;
            }
        }
        if ($regNo == 1) {
            if (($indRegSettings == 'Yes' && $registrationEnable == 'No') || $indRegSettings == 'No') {
                foreach ($resRevenueList as $key => $val) {
                    unset($resRevenueList[$key]['RegistrationAmount']);
                }
                $regCh         = 0;
                $exportElement = $exportElement - 1;
            }
        }
        if ($iuserNo == 1) {
            if (($impUserSettings == 'Yes' && $importedUserEnable == 'No') || $impUserSettings == 'No') {
                foreach ($resRevenueList as $key => $val) {
                    unset($resRevenueList[$key]['impUserCommission']);
                }
                $iuserCh       = 0;
                $exportElement = $exportElement - 1;
            }
        }
        if ($isubNo == 1) {
            if (($impSubscriberSettings == 'Yes' && $importedSubscriberEnable == 'No') || $impSubscriberSettings == 'No') {
                foreach ($resRevenueList as $key => $val) {
                    unset($resRevenueList[$key]['impSubscriberCommission']);
                }
                $isubscriberCh = 0;
                $exportElement = $exportElement - 1;
            }
        }
        //To fetch Revenue Summary details
        $resPagesCount = $this->setPagesCount($key = 'page');
        if ($export == 1) {
            $resRevenueListExport = $ObjAffiliate->listRevenueSummaryDetails($limitFrom, $limitTo, $affiliateUserID, $source, $sortby, $orderby, $this->convertDateForSQL($fromDate, $settingsDateFormat), $this->convertDateForSQL($toDate, $settingsDateFormat));
            $MAX                  = $exportElement; // maximum number of elements
            $header               = array(
                $this->getLanguageValue('lbldate'),
                $this->getLanguageValue('lblOrderCommissions'),
                $this->getLanguageValue('lblSubscriptionCommission'),
                $this->getLanguageValue('lblRegistrationCommission'),
                $this->getLanguageValue('lblImportedUserCommission'),
                $this->getLanguageValue('lblImportedSubscriberCommission'),
                $this->getLanguageValue('lblTotalCommission')
            );
            if ($subNo == 1) {
                if (($indSubSettings == 'Yes' && $subscribeEnable == 'No') || ($indSubSettings == 'No')) {
                    foreach ($resRevenueListExport as $key => $val) {
                        unset($resRevenueListExport[$key]['SubscriptionAmount']);
                    }
                }
            } else {
                foreach ($resRevenueListExport as $key => $val) {
                    $resRevenueListExport[$key]['date'] = date(str_replace("%", "", $settingsDateFormat), strtotime($val['date']));
                }
            }
            if ($regNo == 1) {
                if (($indRegSettings == 'Yes' && $registrationEnable == 'No') || $indRegSettings == 'No') {
                    foreach ($resRevenueListExport as $key => $val) {
                        unset($resRevenueListExport[$key]['RegistrationAmount']);
                    }
                }
            }
            if ($iuserNo == 1) {
                if (($impUserSettings == 'Yes' && $importedUserEnable == 'No') || $impUserSettings == 'No') {
                    foreach ($resRevenueListExport as $key => $val) {
                        unset($resRevenueListExport[$key]['impUserCommission']);
                    }
                }
            }
            if ($isubNo == 1) {
                if (($impSubscriberSettings == 'Yes' && $importedSubscriberEnable == 'No') || $impSubscriberSettings == 'No') {
                    foreach ($resRevenueListExport as $key => $val) {
                        unset($resRevenueListExport[$key]['impSubscriberCommission']);
                    }
                }
            }
            $lastKey                           = count($resRevenueListExport) + 1;
            $resRevenueListExport[$lastKey][0] = $this->getLanguageValue('lblTotal');
            $resRevenueListExport[$lastKey][1] = $totalOrder;
            $resRevenueListExport[$lastKey][2] = $totalSubscription;
            $resRevenueListExport[$lastKey][3] = $totalRegistration;
            $resRevenueListExport[$lastKey][4] = $totalImpUser;
            $resRevenueListExport[$lastKey][5] = $totalImpSubscriber;
            $resRevenueListExport[$lastKey][6] = $totalRevenue;
            if ($subCh == 0) {
                unset($header[2]);
                unset($resRevenueListExport[$lastKey][2]);
            }
            if ($regCh == 0) {
                unset($header[3]);
                unset($resRevenueListExport[$lastKey][3]);
            }
            if ($iuserCh == 0) {
                unset($header[4]);
                unset($resRevenueListExport[$lastKey][4]);
            }
            if ($isubscriberCh == 0) {
                unset($header[5]);
                unset($resRevenueListExport[$lastKey][5]);
            }
            foreach ($resRevenueListExport as &$element) {
                $element = array_slice($element, 0, $MAX);
            }
            $ObjCsv = new Csv();
            if (!empty($resRevenueListExport)) {
                $dateTime = "Revenue_Summary_Details" . date('d_m_Y_H_i_s');
                $data     = $ObjCsv->output($dateTime . '.csv', $resRevenueListExport, $header);
            } else {
                $this->setMessage("language.msgNoBusiness", 'error');
            }
        }
        $this->ObjView->assign('source', $source);
        $this->ObjView->assign('daterange', $date);
        $this->ObjView->assign('sortkey', (isset($sortkey)) ? $sortkey : '');
        $this->ObjView->assign('sortkey1', (isset($sortkey1)) ? $sortkey1 : '');
        $this->ObjView->assign('sortkey2', (isset($sortkey2)) ? $sortkey2 : '');
        $this->ObjView->assign('sortkey3', (isset($sortkey3)) ? $sortkey3 : '');
        $this->ObjView->assign('sortkey4', (isset($sortkey4)) ? $sortkey4 : '');
        $this->ObjView->assign('sortkey5', (isset($sortkey5)) ? $sortkey5 : '');
        $this->ObjView->assign('sortkey6', (isset($sortkey6)) ? $sortkey6 : '');
        $this->ObjView->assign('resRevenueList', $resRevenueList);
        $this->ObjView->assign('previousUrl', $history);
        $this->ObjView->assign('links', $links);
        $this->ObjView->assign('subCh', $subCh);
        $this->ObjView->assign('regCh', $regCh);
        $this->ObjView->assign('iuserCh', $iuserCh);
        $this->ObjView->assign('isubCh', $isubscriberCh);
        $this->ObjView->display('RevenueSummary.tpl');
    }
    //Ajax Revenue details
    public function ajaxRevenueAction() {
        $ObjAffiliate    = new AffiliateModel();
        $affiliateUserID = $this->ObjSession->getSession("AFFILIATE_USER_ID");
        $revenueDate     = $_POST['revenueDate'];
        $source          = $_POST['source'];
        if ($revenueDate != '') {
            $settingsDateFormat   = $this->getSettingValue('site_settings', 'date_format');
            $revenueDate          = $this->convertDateForSQL($revenueDate, $settingsDateFormat);
            $getCommissionDetails = $ObjAffiliate->getOrderRevenueDetails($affiliateUserID, $revenueDate, $source);
            $this->ObjView->assign('getCommissionDetails', $getCommissionDetails);
            $this->ObjView->display('AjaxRevenueDetails.tpl');
        }
    }
}