1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 21:52:35 +01:00
invoiceninja/app/controllers/DashboardController.php

59 lines
2.3 KiB
PHP
Raw Normal View History

2014-02-16 21:32:25 +01:00
<?php
class DashboardController extends \BaseController {
public function index()
{
// total_income, billed_clients, invoice_sent and active_clients
2014-02-17 17:25:38 +01:00
$select = DB::raw('COUNT(DISTINCT CASE WHEN invoices.id IS NOT NULL THEN clients.id ELSE null END) billed_clients,
2014-02-16 21:32:25 +01:00
SUM(CASE WHEN invoices.invoice_status_id >= '.INVOICE_STATUS_SENT.' THEN 1 ELSE 0 END) invoices_sent,
2014-02-17 17:25:38 +01:00
COUNT(DISTINCT clients.id) active_clients,
AVG(invoices.amount) as invoice_avg');
2014-02-16 21:32:25 +01:00
2014-02-16 23:03:19 +01:00
$metrics = DB::table('accounts')
2014-02-16 21:32:25 +01:00
->select($select)
2014-02-16 23:03:19 +01:00
->leftJoin('clients', 'accounts.id', '=', 'clients.account_id')
2014-02-16 21:32:25 +01:00
->leftJoin('invoices', 'clients.id', '=', 'invoices.client_id')
2014-02-16 23:03:19 +01:00
->where('accounts.id', '=', Auth::user()->account_id)
2014-02-16 21:32:25 +01:00
->where('clients.deleted_at', '=', null)
2014-02-16 23:03:19 +01:00
->groupBy('accounts.id')
2014-02-16 21:32:25 +01:00
->first();
2014-02-17 17:25:38 +01:00
$select = DB::raw('SUM(clients.paid_to_date) value');
$totalIncome = DB::table('accounts')
->select($select)
->leftJoin('clients', 'accounts.id', '=', 'clients.account_id')
->where('accounts.id', '=', Auth::user()->account_id)
->where('clients.deleted_at', '=', null)
->groupBy('accounts.id')
->first();
2014-02-16 21:32:25 +01:00
$activities = Activity::where('activities.account_id', '=', Auth::user()->account_id)
->orderBy('created_at', 'desc')->take(6)->get();
2014-02-17 17:25:38 +01:00
$pastDue = Invoice::scope()
->where('due_date', '<', date('Y-m-d'))
->where('balance', '>', 0)
2014-02-16 21:32:25 +01:00
->orderBy('due_date', 'asc')->take(6)->get();
2014-02-17 17:25:38 +01:00
$upcoming = Invoice::scope()
->where('due_date', '>', date('Y-m-d'))
->where('balance', '>', 0)
2014-02-16 21:32:25 +01:00
->orderBy('due_date', 'asc')->take(6)->get();
$data = [
2014-02-17 17:25:38 +01:00
'totalIncome' => Utils::formatMoney($totalIncome->value, Session::get(SESSION_CURRENCY)),
2014-02-16 21:32:25 +01:00
'billedClients' => $metrics->billed_clients,
'invoicesSent' => $metrics->invoices_sent,
'activeClients' => $metrics->active_clients,
2014-02-17 17:25:38 +01:00
'invoiceAvg' => Utils::formatMoney($metrics->invoice_avg, Session::get(SESSION_CURRENCY)),
2014-02-16 21:32:25 +01:00
'activities' => $activities,
'pastDue' => $pastDue,
'upcoming' => $upcoming
];
return View::make('dashboard', $data);
}
}