invoiceninja/app/Http/Controllers/DashboardApiController.php

61 lines
2.7 KiB
PHP
Raw Normal View History

2017-01-30 21:40:43 +02:00
<?php
namespace App\Http\Controllers;
2016-03-20 09:38:05 +11:00
2016-08-15 06:46:47 +03:00
use App\Ninja\Repositories\DashboardRepository;
2016-08-15 16:43:26 +03:00
use App\Ninja\Transformers\ActivityTransformer;
2017-01-30 21:40:43 +02:00
use Auth;
2016-03-20 09:38:05 +11:00
class DashboardApiController extends BaseAPIController
{
2016-08-15 06:46:47 +03:00
public function __construct(DashboardRepository $dashboardRepo)
2016-03-20 09:38:05 +11:00
{
2016-08-15 16:43:26 +03:00
parent::__construct();
2016-08-15 06:46:47 +03:00
$this->dashboardRepo = $dashboardRepo;
}
2016-03-22 21:21:05 +11:00
2016-08-15 06:46:47 +03:00
public function index()
{
$user = Auth::user();
$viewAll = $user->hasPermission('view_reports');
2016-08-15 06:46:47 +03:00
$userId = $user->id;
$accountId = $user->account->id;
$defaultCurrency = $user->account->currency_id;
2016-08-15 06:46:47 +03:00
$dashboardRepo = $this->dashboardRepo;
2018-09-28 09:51:48 +03:00
$activities = $dashboardRepo->activities($accountId, $userId, $viewAll);
// optimization for new mobile app
if (request()->only_activity) {
return $this->response([
'id' => 1,
'activities' => $this->createCollection($activities, new ActivityTransformer(), ENTITY_ACTIVITY),
]);
}
2016-08-15 06:46:47 +03:00
$metrics = $dashboardRepo->totals($accountId, $userId, $viewAll);
2016-11-03 22:39:29 +11:00
$paidToDate = $dashboardRepo->paidToDate($user->account, $userId, $viewAll);
$averageInvoice = $dashboardRepo->averages($user->account, $userId, $viewAll);
2018-09-28 09:51:48 +03:00
$balances = $dashboardRepo->balances($user->account, $userId, $viewAll);
2016-08-15 06:46:47 +03:00
$pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll);
$upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll);
$payments = $dashboardRepo->payments($accountId, $userId, $viewAll);
2016-03-20 09:38:05 +11:00
2016-08-15 06:46:47 +03:00
$data = [
'id' => 1,
2018-06-08 13:50:35 +03:00
'paidToDate' => (float) ($paidToDate->count() && $paidToDate[0]->value ? $paidToDate[0]->value : 0),
'paidToDateCurrency' => (int) ($paidToDate->count() && $paidToDate[0]->currency_id ? $paidToDate[0]->currency_id : $defaultCurrency),
'balances' => (float) ($balances->count() && $balances[0]->value ? $balances[0]->value : 0),
'balancesCurrency' => (int) ($balances->count() && $balances[0]->currency_id ? $balances[0]->currency_id : $defaultCurrency),
'averageInvoice' => (float) ($averageInvoice->count() && $averageInvoice[0]->invoice_avg ? $averageInvoice[0]->invoice_avg : 0),
'averageInvoiceCurrency' => (int) ($averageInvoice->count() && $averageInvoice[0]->currency_id ? $averageInvoice[0]->currency_id : $defaultCurrency),
'invoicesSent' => (int) ($metrics ? $metrics->invoices_sent : 0),
'activeClients' => (int) ($metrics ? $metrics->active_clients : 0),
2016-08-15 16:43:26 +03:00
'activities' => $this->createCollection($activities, new ActivityTransformer(), ENTITY_ACTIVITY),
2016-08-15 06:46:47 +03:00
];
return $this->response($data);
2016-03-20 09:38:05 +11:00
}
}