1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-10 05:02:36 +01:00
invoiceninja/app/Http/Controllers/DashboardController.php

217 lines
11 KiB
PHP
Raw Normal View History

2015-03-17 02:30:56 +01:00
<?php namespace App\Http\Controllers;
2015-03-26 07:24:02 +01:00
use Auth;
2015-03-17 02:30:56 +01:00
use DB;
2015-03-26 07:24:02 +01:00
use View;
use App\Models\Activity;
use App\Models\Invoice;
2015-07-29 21:55:12 +02:00
use App\Models\Payment;
2015-03-16 22:45:25 +01:00
/**
* Class DashboardController
*/
2015-03-26 07:24:02 +01:00
class DashboardController extends BaseController
2015-03-16 22:45:25 +01:00
{
/**
* @return \Illuminate\Contracts\View\View
*/
2015-03-16 22:45:25 +01:00
public function index()
{
2016-04-10 14:49:45 +02:00
$view_all = Auth::user()->hasPermission('view_all');
2016-03-17 15:11:14 +01:00
$user_id = Auth::user()->id;
2015-03-16 22:45:25 +01:00
// total_income, billed_clients, invoice_sent and active_clients
2016-06-30 15:14:54 +02:00
$select = DB::raw(
'COUNT(DISTINCT CASE WHEN '.DB::getQueryGrammar()->wrap('invoices.id', true).' IS NOT NULL THEN '.DB::getQueryGrammar()->wrap('clients.id', true).' ELSE null END) billed_clients,
SUM(CASE WHEN '.DB::getQueryGrammar()->wrap('invoices.invoice_status_id', true).' >= '.INVOICE_STATUS_SENT.' THEN 1 ELSE 0 END) invoices_sent,
COUNT(DISTINCT '.DB::getQueryGrammar()->wrap('clients.id', true).') active_clients'
);
2015-03-16 22:45:25 +01:00
$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', '=', INVOICE_TYPE_STANDARD);
2016-03-17 15:11:14 +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){
$query->where('invoices.user_id', '=', null);
2016-03-17 15:11:14 +01:00
$query->where('clients.user_id', '=', $user_id);
});
});
}
2016-03-17 15:11:14 +01:00
$metrics = $metrics->groupBy('accounts.id')
2015-03-16 22:45:25 +01:00
->first();
2016-06-30 15:14:54 +02:00
$select = DB::raw(
'SUM('.DB::getQueryGrammar()->wrap('clients.paid_to_date', true).') as value,'
.DB::getQueryGrammar()->wrap('clients.currency_id', true).' as currency_id'
);
2015-03-16 22:45:25 +01:00
$paidToDate = DB::table('accounts')
->select($select)
->leftJoin('clients', 'accounts.id', '=', 'clients.account_id')
->where('accounts.id', '=', Auth::user()->account_id)
2016-03-17 15:11:14 +01:00
->where('clients.is_deleted', '=', false);
2016-03-17 15:11:14 +01:00
if(!$view_all){
$paidToDate = $paidToDate->where('clients.user_id', '=', $user_id);
}
2016-03-17 15:11:14 +01:00
$paidToDate = $paidToDate->groupBy('accounts.id')
2016-06-30 15:14:54 +02:00
->groupBy(DB::raw('CASE WHEN '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' IS NULL THEN CASE WHEN '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' IS NULL THEN 1 ELSE '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' END ELSE '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' END'))
2015-03-16 22:45:25 +01:00
->get();
2016-06-30 15:14:54 +02:00
$select = DB::raw(
'AVG('.DB::getQueryGrammar()->wrap('invoices.amount', true).') as invoice_avg, '
.DB::getQueryGrammar()->wrap('clients.currency_id', true).' as currency_id'
);
2015-03-16 22:45:25 +01:00
$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-17 15:11:14 +01:00
->where('invoices.is_recurring', '=', false);
2016-03-17 15:11:14 +01:00
if(!$view_all){
$averageInvoice = $averageInvoice->where('invoices.user_id', '=', $user_id);
}
2016-03-17 15:11:14 +01:00
$averageInvoice = $averageInvoice->groupBy('accounts.id')
2016-06-30 15:14:54 +02:00
->groupBy(DB::raw('CASE WHEN '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' IS NULL THEN CASE WHEN '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' IS NULL THEN 1 ELSE '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' END ELSE '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' END'))
2015-03-16 22:45:25 +01:00
->get();
2016-06-30 15:14:54 +02:00
$select = DB::raw(
'SUM('.DB::getQueryGrammar()->wrap('clients.balance', true).') as value, '
.DB::getQueryGrammar()->wrap('clients.currency_id', true).' as currency_id'
);
2015-07-29 21:55:12 +02:00
$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-06-30 15:14:54 +02:00
->groupBy(DB::raw('CASE WHEN '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' IS NULL THEN CASE WHEN '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' IS NULL THEN 1 ELSE '.DB::getQueryGrammar()->wrap('accounts.currency_id', true).' END ELSE '.DB::getQueryGrammar()->wrap('clients.currency_id', true).' END'));
if (!$view_all) {
$balances->where('clients.user_id', '=', $user_id);
}
$balances = $balances->get();
2015-03-16 22:45:25 +01:00
$activities = Activity::where('activities.account_id', '=', Auth::user()->account_id)
2016-03-16 03:29:30 +01:00
->where('activities.activity_type_id', '>', 0);
2016-03-17 15:11:14 +01:00
if(!$view_all){
2016-03-16 03:29:30 +01:00
$activities = $activities->where('activities.user_id', '=', $user_id);
}
2016-03-16 03:29:30 +01:00
$activities = $activities->orderBy('activities.created_at', 'desc')
->with('client.contacts', 'user', 'invoice', 'payment', 'credit', 'account')
2015-07-29 21:55:12 +02:00
->take(50)
->get();
$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)
2015-09-07 11:07:55 +02:00
//->where('invoices.is_quote', '=', false)
->where('invoices.quote_invoice_id', '=', null)
2015-07-29 21:55:12 +02:00
->where('invoices.balance', '>', 0)
->where('invoices.is_deleted', '=', false)
->where('invoices.deleted_at', '=', null)
2015-07-29 21:55:12 +02:00
->where('contacts.is_primary', '=', true)
2016-03-16 03:29:30 +01:00
->where('invoices.due_date', '<', date('Y-m-d'));
2016-03-17 15:11:14 +01:00
if(!$view_all){
2016-03-16 03:29:30 +01:00
$pastDue = $pastDue->where('invoices.user_id', '=', $user_id);
}
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'])
2015-07-29 21:55:12 +02: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)
2015-07-29 21:55:12 +02:00
->where('invoices.is_recurring', '=', false)
2015-09-07 11:07:55 +02:00
//->where('invoices.is_quote', '=', false)
->where('invoices.quote_invoice_id', '=', null)
2015-07-29 21:55:12 +02:00
->where('invoices.balance', '>', 0)
->where('invoices.is_deleted', '=', false)
->where('contacts.is_primary', '=', true)
->where('invoices.due_date', '>=', date('Y-m-d'))
2016-03-16 03:29:30 +01:00
->orderBy('invoices.due_date', 'asc');
2016-03-17 15:11:14 +01:00
if(!$view_all){
2016-03-16 03:29:30 +01:00
$upcoming = $upcoming->where('invoices.user_id', '=', $user_id);
}
2016-03-16 03:29:30 +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'])
2015-07-29 21:55:12 +02:00
->get();
2015-03-16 22:45:25 +01:00
2015-07-29 21:55:12 +02:00
$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)
2015-11-18 17:08:37 +01:00
->where('payments.is_deleted', '=', false)
->where('invoices.is_deleted', '=', false)
->where('clients.is_deleted', '=', false)
2015-07-29 21:55:12 +02:00
->where('contacts.deleted_at', '=', null)
2016-03-16 03:29:30 +01:00
->where('contacts.is_primary', '=', true);
2016-03-17 15:11:14 +01:00
if(!$view_all){
2016-03-16 03:29:30 +01:00
$payments = $payments->where('payments.user_id', '=', $user_id);
}
2016-03-16 03:29:30 +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'])
2015-12-22 13:06:53 +01:00
->orderBy('payments.payment_date', 'desc')
2015-07-29 21:55:12 +02:00
->take(50)
->get();
2015-03-16 22:45:25 +01:00
$hasQuotes = false;
foreach ([$upcoming, $pastDue] as $data) {
foreach ($data as $invoice) {
2016-05-26 16:56:54 +02:00
if ($invoice->invoice_type_id == INVOICE_TYPE_QUOTE) {
$hasQuotes = true;
}
}
}
2015-03-16 22:45:25 +01:00
$data = [
2015-08-20 17:09:04 +02:00
'account' => Auth::user()->account,
'paidToDate' => $paidToDate,
'balances' => $balances,
'averageInvoice' => $averageInvoice,
'invoicesSent' => $metrics ? $metrics->invoices_sent : 0,
'activeClients' => $metrics ? $metrics->active_clients : 0,
'activities' => $activities,
'pastDue' => $pastDue,
'upcoming' => $upcoming,
'payments' => $payments,
'title' => trans('texts.dashboard'),
'hasQuotes' => $hasQuotes,
2015-08-20 17:09:04 +02:00
];
2015-03-16 22:45:25 +01:00
return View::make('dashboard', $data);
}
}