2014-02-16 21:32:25 +01:00
< ? php
2015-01-11 13:30:08 +01:00
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 ,
2015-01-22 10:52:26 +01:00
COUNT ( DISTINCT clients . id ) active_clients ' );
2014-02-16 21:32:25 +01:00
2015-01-11 13:30:08 +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-09-14 08:05:16 +02:00
-> where ( 'clients.is_deleted' , '=' , false )
2015-01-22 10:23:52 +01:00
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'invoices.is_recurring' , '=' , false )
-> where ( 'invoices.is_quote' , '=' , false )
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
2015-01-22 10:52:26 +01:00
$select = DB :: raw ( ' SUM ( clients . paid_to_date ) as value , AVG ( invoices . amount ) as invoice_avg ,
clients . currency_id as currency_id ' );
2015-01-11 13:30:08 +01:00
2015-01-22 10:31:43 +01:00
$totalIncomes = DB :: table ( 'accounts' )
2014-02-17 17:25:38 +01:00
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
2015-01-22 10:52:26 +01:00
-> leftJoin ( 'invoices' , 'clients.id' , '=' , 'invoices.client_id' )
2014-02-17 17:25:38 +01:00
-> where ( 'accounts.id' , '=' , Auth :: user () -> account_id )
2014-09-14 08:05:16 +02:00
-> where ( 'clients.is_deleted' , '=' , false )
2014-02-17 17:25:38 +01:00
-> groupBy ( 'accounts.id' )
2015-01-26 23:38:54 +01:00
-> groupBy ( DB :: raw ( 'CASE WHEN clients.currency_id IS NULL THEN CASE WHEN accounts.currency_id IS NULL THEN 1 ELSE accounts.currency_id END ELSE clients.currency_id END' ))
2015-01-22 10:31:43 +01:00
-> get ();
2014-11-11 18:47:15 +01:00
2015-01-11 13:30:08 +01:00
$activities = Activity :: where ( 'activities.account_id' , '=' , Auth :: user () -> account_id )
2014-02-16 21:32:25 +01:00
-> orderBy ( 'created_at' , 'desc' ) -> take ( 6 ) -> get ();
2015-01-11 13:30:08 +01:00
$pastDue = Invoice :: scope ()
2014-02-17 17:25:38 +01:00
-> where ( 'due_date' , '<' , date ( 'Y-m-d' ))
-> where ( 'balance' , '>' , 0 )
2014-05-20 23:40:09 +02:00
-> where ( 'is_recurring' , '=' , false )
-> where ( 'is_quote' , '=' , false )
2015-01-11 13:30:08 +01:00
-> where ( 'is_deleted' , '=' , false )
2014-02-16 21:32:25 +01:00
-> orderBy ( 'due_date' , 'asc' ) -> take ( 6 ) -> get ();
2015-01-11 13:30:08 +01:00
$upcoming = Invoice :: scope ()
2014-12-03 23:05:38 +01:00
-> where ( 'due_date' , '>=' , date ( 'Y-m-d' ))
2014-02-17 17:25:38 +01:00
-> where ( 'balance' , '>' , 0 )
2014-05-20 23:40:09 +02:00
-> where ( 'is_recurring' , '=' , false )
-> where ( 'is_quote' , '=' , false )
2014-09-14 08:05:16 +02:00
-> where ( 'is_deleted' , '=' , false )
2014-02-16 21:32:25 +01:00
-> orderBy ( 'due_date' , 'asc' ) -> take ( 6 ) -> get ();
2015-01-11 13:30:08 +01:00
$data = [
2015-01-22 10:31:43 +01:00
'totalIncomes' => $totalIncomes ,
2014-03-23 12:11:04 +01:00
'billedClients' => $metrics ? $metrics -> billed_clients : 0 ,
'invoicesSent' => $metrics ? $metrics -> invoices_sent : 0 ,
'activeClients' => $metrics ? $metrics -> active_clients : 0 ,
2014-02-16 21:32:25 +01:00
'activities' => $activities ,
'pastDue' => $pastDue ,
2015-01-11 13:30:08 +01:00
'upcoming' => $upcoming ,
2014-02-16 21:32:25 +01:00
];
2015-01-11 13:30:08 +01:00
return View :: make ( 'dashboard' , $data );
}
2014-11-04 01:31:28 +01:00
}