<?php
require_once 'modules/admin/models/DashboardPlugin.php';
require_once 'modules/support/models/TicketGateway.php';
require_once 'modules/clients/models/UserGateway.php';
require_once 'modules/billing/models/BillingGateway.php';

class PluginAtaglance extends DashboardPlugin
{
    var $hasTab = false;
    var $hasPanel = true;
    var $name = /*T*/"Today At A Glance"/*/T*/;
    var $description = /*T*/"This plugin quickly summarizes todays events such as invoices generated and tickets opened."/*/T*/;
    var $default = true;

    function getTab()
    {
        return '';
    }
    
    function getPanel()
    {
        $this->tpl->define(array('ataglance' => '../../plugins/dashboard/ataglance/PluginAtaglance.tpl'));

        $this->tpl->setKey($this->gatherData());
        $this->tpl->assign('today', date('d/m/Y'));

        $company = NE_Registry::getCompany();
        if ($company->supports('accounts')) {
            if ($company->supports('products')) {
                $this->tpl->parse('productsSection_out', 'productsSection');
            }
            $this->tpl->parse('customerDetails_out', 'customerDetails');
        }

        if ($this->user->hasPermission('billing_view')
                && $company->supports('billing')) {
            $this->tpl->parse('billing_out', 'billing');
        }

        if ($this->user->hasPermission('support_view')
                && $company->supports('support')) {
            $this->tpl->parse('support_out', 'support');
        }
        
        $this->tpl->parse('ataglance_out', 'ataglance');
        return $this->tpl->fetch('ataglance_out');
    }

    function gatherData()
    {
        require_once 'newedge/classes/NE_Cache.php';

        $company = NE_Registry::getCompany();
        $cache = new NE_Cache("ATAGLANCE_".$company->getId());
        $content = $cache->getContent();
        $lastupdated = $cache->GetLastUpdated();
        //Cache this data for 15 seconds.
        if ($content != '' && $lastupdated != '' && $lastupdated >= time() - 15) {
            return unserialize($content);
        }

        $data = array();

        /*********************************
         * Customer Details
         *********************************/

        // Get number of new customers today.
        $usersGateway = new UserGateway();
        $data['new_customers'] = $usersGateway->getCountOfNewCustomersToday();

        if ($company->supports('products')) {
            // Get number of products ordered today.
            $data['products_ordered'] = $usersGateway->getNumberOfNewProductsToday();

            // Get number of domains ordered today.
            $data['domains_ordered'] = $usersGateway->getNumberOfDomainsOrderedToday();

            // Get number of domains expiring today.
            $data['domains_expiring'] = $usersGateway->getNumberOfDomainsExpiringToday();
        }

        // Get number of customers seen today.
        $data['customers_seen'] = $usersGateway->getCountOfCustomersSeenToday();

        /*********************************
         * Billing Details
         *********************************/
        $billingGateway = new BillingGateway($this->user);
        $data['invoices_generated'] = $billingGateway->getCountOfInvoicesGeneratedToday();
        $data['invoices_sent'] = $billingGateway->getCountOfInvoicesSentToday();
        $data['invoices_paid'] = $billingGateway->getCountOfInvoicesPaidToday();
        $data['invoices_overdue'] = $billingGateway->getCountOfOverdueInvoices();

        $revenue = $billingGateway->getTodaysRevenue();
        $currency = new Currency($this->user);
        $data['revenue'] = '';
        foreach($revenue AS $currencyRevenue => $totalRevenue){
            if($data['revenue'] != ''){
                $data['revenue'] .= '<br>';
            }
            $data['revenue'] .= $currency->format($currencyRevenue, $totalRevenue, true);
        }

        /*********************************
         * Support Details
         *********************************/

        $ticketGateway = new TicketGateway();
        $data['tickets_opened'] = $ticketGateway->getCountOfTicketsOpenedToday();
        $data['tickets_closed'] = $ticketGateway->getCountOfTicketsClosedToday();
        $data['tickets_replies'] = $ticketGateway->getCountOfTicketRepliedToday();
        $data['tickets_rated'] = $ticketGateway->getCountOfTicketsRatedToday();

        // Get number of articles added today.
        $query = "SELECT COUNT(*) as count FROM kb_articles WHERE DATE(created)=DATE(NOW())";
        $result = $this->db->query($query);
        $row = $result->fetch();
        $data['kb_articles'] = $row['count'];

        // Get number of article comments added today.
        $query = "SELECT COUNT(*) as count FROM kb_articles_comments WHERE DATE(added)=DATE(NOW())";
        $result = $this->db->query($query);
        $row = $result->fetch();
        $data['kb_comments'] = $row['count'];

        /*********************************
         * Team Details
         *********************************/

        // Get number of staff seen today.
        $query = "SELECT COUNT(*) as count FROM users WHERE DATE(lastseen)=DATE(NOW()) AND groupid>1";
        $result = $this->db->query($query);
        $row = $result->fetch();
        $data['staff_seen'] = $row['count'];

        // Get number of staff status updates today.
        $query = "SELECT COUNT(*) as count FROM team_status WHERE DATE(status_datetime)=DATE(NOW())";
        $result = $this->db->query($query);
        $row = $result->fetch();
        $data['staff_status'] = $row['count'];

        // Get the number of calendar events today.
        $query = "SELECT COUNT(*) as count FROM calendar WHERE DATE(NOW()) BETWEEN DATE(start) AND DATE(end)";
        $result = $this->db->query($query);
        $row = $result->fetch();
        $data['staff_events'] = $row['count'];

        $cache->SetContent(serialize($data));
        $cache->Update();
        
        return $data;
    }
}

?>
