2016-03-19 23:38:05 +01:00
< ? php namespace App\Http\Controllers ;
use Auth ;
use DB ;
2016-05-31 11:54:57 +02:00
use Illuminate\Support\Facades\Log ;
2016-03-19 23:38:05 +01:00
use View ;
use App\Models\Activity ;
class DashboardApiController extends BaseAPIController
{
public function index ()
{
2016-05-31 12:00:23 +02:00
$view_all = Auth :: user () -> hasPermission ( 'view_all' );
2016-03-19 23:38:05 +01:00
$user_id = Auth :: user () -> id ;
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
// total_income, billed_clients, invoice_sent and active_clients
$select = DB :: raw ( ' COUNT ( DISTINCT CASE WHEN invoices . id IS NOT NULL THEN clients . id ELSE null END ) billed_clients ,
SUM ( CASE WHEN invoices . invoice_status_id >= '.INVOICE_STATUS_SENT.' THEN 1 ELSE 0 END ) invoices_sent ,
COUNT ( DISTINCT clients . id ) active_clients ' );
$metrics = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> leftJoin ( 'invoices' , 'clients.id' , '=' , 'invoices.client_id' )
-> where ( 'accounts.id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'invoices.is_recurring' , '=' , false )
2016-05-26 16:56:54 +02:00
-> where ( 'invoices.invoice_type_id' , '=' , false );
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$metrics = $metrics -> where ( function ( $query ) use ( $user_id ){
$query -> where ( 'invoices.user_id' , '=' , $user_id );
$query -> orwhere ( function ( $query ) use ( $user_id ){
2016-03-22 11:21:05 +01:00
$query -> where ( 'invoices.user_id' , '=' , null );
2016-03-19 23:38:05 +01:00
$query -> where ( 'clients.user_id' , '=' , $user_id );
});
});
}
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
$metrics = $metrics -> groupBy ( 'accounts.id' )
-> first ();
$select = DB :: raw ( 'SUM(clients.paid_to_date) as value, clients.currency_id as currency_id' );
$paidToDate = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> where ( 'accounts.id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.is_deleted' , '=' , false );
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$paidToDate = $paidToDate -> where ( 'clients.user_id' , '=' , $user_id );
}
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
$paidToDate = $paidToDate -> groupBy ( 'accounts.id' )
-> 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' ))
-> get ();
$select = DB :: raw ( 'AVG(invoices.amount) as invoice_avg, clients.currency_id as currency_id' );
$averageInvoice = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> leftJoin ( 'invoices' , 'clients.id' , '=' , 'invoices.client_id' )
-> where ( 'accounts.id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
2016-05-26 16:56:54 +02:00
-> where ( 'invoices.invoice_type_id' , '=' , INVOICE_TYPE_STANDARD )
2016-03-19 23:38:05 +01:00
-> where ( 'invoices.is_recurring' , '=' , false );
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$averageInvoice = $averageInvoice -> where ( 'invoices.user_id' , '=' , $user_id );
}
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
$averageInvoice = $averageInvoice -> groupBy ( 'accounts.id' )
-> 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' ))
-> get ();
$select = DB :: raw ( 'SUM(clients.balance) as value, clients.currency_id as currency_id' );
$balances = DB :: table ( 'accounts' )
-> select ( $select )
-> leftJoin ( 'clients' , 'accounts.id' , '=' , 'clients.account_id' )
-> where ( 'accounts.id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.is_deleted' , '=' , false )
-> groupBy ( 'accounts.id' )
2016-05-22 14:31:41 +02: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' ));
if ( ! $view_all ) {
$balances -> where ( 'clients.user_id' , '=' , $user_id );
}
$balances = $balances -> get ();
2016-03-19 23:38:05 +01:00
$pastDue = DB :: table ( 'invoices' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'invoices.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'invoices.account_id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'contacts.deleted_at' , '=' , null )
-> where ( 'invoices.is_recurring' , '=' , false )
//->where('invoices.is_quote', '=', false)
-> where ( 'invoices.balance' , '>' , 0 )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'invoices.deleted_at' , '=' , null )
-> where ( 'contacts.is_primary' , '=' , true )
-> where ( 'invoices.due_date' , '<' , date ( 'Y-m-d' ));
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$pastDue = $pastDue -> where ( 'invoices.user_id' , '=' , $user_id );
}
2016-03-22 11:21:05 +01:00
2016-05-26 16:56:54 +02:00
$pastDue = $pastDue -> select ([ 'invoices.due_date' , 'invoices.balance' , 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' , 'invoice_type_id' ])
2016-03-19 23:38:05 +01:00
-> orderBy ( 'invoices.due_date' , 'asc' )
-> take ( 50 )
-> get ();
$upcoming = DB :: table ( 'invoices' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'invoices.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> where ( 'invoices.account_id' , '=' , Auth :: user () -> account_id )
-> where ( 'clients.deleted_at' , '=' , null )
-> where ( 'contacts.deleted_at' , '=' , null )
-> where ( 'invoices.deleted_at' , '=' , null )
-> where ( 'invoices.is_recurring' , '=' , false )
//->where('invoices.is_quote', '=', false)
-> where ( 'invoices.balance' , '>' , 0 )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'contacts.is_primary' , '=' , true )
-> where ( 'invoices.due_date' , '>=' , date ( 'Y-m-d' ))
-> orderBy ( 'invoices.due_date' , 'asc' );
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$upcoming = $upcoming -> where ( 'invoices.user_id' , '=' , $user_id );
}
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
$upcoming = $upcoming -> take ( 50 )
2016-05-26 16:56:54 +02:00
-> select ([ 'invoices.due_date' , 'invoices.balance' , 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' , 'invoice_type_id' ])
2016-03-19 23:38:05 +01:00
-> get ();
$payments = DB :: table ( 'payments' )
-> leftJoin ( 'clients' , 'clients.id' , '=' , 'payments.client_id' )
-> leftJoin ( 'contacts' , 'contacts.client_id' , '=' , 'clients.id' )
-> leftJoin ( 'invoices' , 'invoices.id' , '=' , 'payments.invoice_id' )
-> where ( 'payments.account_id' , '=' , Auth :: user () -> account_id )
-> where ( 'payments.is_deleted' , '=' , false )
-> where ( 'invoices.is_deleted' , '=' , false )
-> where ( 'clients.is_deleted' , '=' , false )
-> where ( 'contacts.deleted_at' , '=' , null )
-> where ( 'contacts.is_primary' , '=' , true );
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
if ( ! $view_all ){
$payments = $payments -> where ( 'payments.user_id' , '=' , $user_id );
}
2016-03-22 11:21:05 +01:00
2016-03-19 23:38:05 +01:00
$payments = $payments -> select ([ 'payments.payment_date' , 'payments.amount' , 'invoices.public_id' , 'invoices.invoice_number' , 'clients.name as client_name' , 'contacts.email' , 'contacts.first_name' , 'contacts.last_name' , 'clients.currency_id' , 'clients.public_id as client_public_id' , 'clients.user_id as client_user_id' ])
-> orderBy ( 'payments.payment_date' , 'desc' )
-> take ( 50 )
-> get ();
$hasQuotes = false ;
foreach ([ $upcoming , $pastDue ] as $data ) {
foreach ( $data as $invoice ) {
2016-05-31 12:03:52 +02:00
if ( $invoice -> invoice_type_id == INVOICE_TYPE_QUOTE ) {
2016-03-19 23:38:05 +01:00
$hasQuotes = true ;
}
}
}
2016-05-31 11:54:57 +02:00
Log :: info ( $paidToDate );
2016-05-31 11:59:04 +02:00
Log :: info ( $balances );
Log :: info ( $averageInvoice );
Log :: info ( $metrics );
2016-05-31 12:02:41 +02:00
2016-03-22 11:21:05 +01:00
$data = [
'id' => 1 ,
2016-05-21 01:17:53 +02:00
'paidToDate' => $paidToDate [ 0 ] -> value ? $paidToDate [ 0 ] -> value : 0 ,
'paidToDateCurrency' => $paidToDate [ 0 ] -> currency_id ? $paidToDate [ 0 ] -> currency_id : 0 ,
'balances' => $balances [ 0 ] -> value ? $balances [ 0 ] -> value : 0 ,
'balancesCurrency' => $balances [ 0 ] -> currency_id ? $balances [ 0 ] -> currency_id : 0 ,
'averageInvoice' => $averageInvoice [ 0 ] -> invoice_avg ? $averageInvoice [ 0 ] -> invoice_avg : 0 ,
'averageInvoiceCurrency' => $averageInvoice [ 0 ] -> currency_id ? $averageInvoice [ 0 ] -> currency_id : 0 ,
2016-03-22 11:21:05 +01:00
'invoicesSent' => $metrics ? $metrics -> invoices_sent : 0 ,
'activeClients' => $metrics ? $metrics -> active_clients : 0 ,
];
2016-05-31 12:03:52 +02:00
2016-03-22 11:21:05 +01:00
return $this -> response ( $data );
2016-03-19 23:38:05 +01:00
}
}