1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-17 00:22:51 +01:00
invoiceninja/app/Http/Controllers/DashboardApiController.php

60 lines
2.5 KiB
PHP
Raw Normal View History

2017-01-30 20:40:43 +01:00
<?php
namespace App\Http\Controllers;
2016-03-19 23:38:05 +01:00
2016-08-15 05:46:47 +02:00
use App\Ninja\Repositories\DashboardRepository;
2016-08-15 15:43:26 +02:00
use App\Ninja\Transformers\ActivityTransformer;
2017-01-30 20:40:43 +01:00
use Auth;
2016-03-19 23:38:05 +01:00
class DashboardApiController extends BaseAPIController
{
2016-08-15 05:46:47 +02:00
public function __construct(DashboardRepository $dashboardRepo)
2016-03-19 23:38:05 +01:00
{
2016-08-15 15:43:26 +02:00
parent::__construct();
2016-08-15 05:46:47 +02:00
$this->dashboardRepo = $dashboardRepo;
}
2016-03-22 11:21:05 +01:00
2016-08-15 05:46:47 +02:00
public function index()
{
$user = Auth::user();
$viewAll = $user->hasPermission('view_all');
$userId = $user->id;
$accountId = $user->account->id;
$dashboardRepo = $this->dashboardRepo;
$metrics = $dashboardRepo->totals($accountId, $userId, $viewAll);
2016-11-03 12:39:29 +01:00
$paidToDate = $dashboardRepo->paidToDate($user->account, $userId, $viewAll);
$averageInvoice = $dashboardRepo->averages($user->account, $userId, $viewAll);
2016-08-15 05:46:47 +02:00
$balances = $dashboardRepo->balances($accountId, $userId, $viewAll);
$activities = $dashboardRepo->activities($accountId, $userId, $viewAll);
$pastDue = $dashboardRepo->pastDue($accountId, $userId, $viewAll);
$upcoming = $dashboardRepo->upcoming($accountId, $userId, $viewAll);
$payments = $dashboardRepo->payments($accountId, $userId, $viewAll);
2016-03-19 23:38:05 +01:00
$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-08-15 05:46:47 +02:00
$data = [
'id' => 1,
2016-09-19 09:04:30 +02:00
'paidToDate' => count($paidToDate) && $paidToDate[0]->value ? $paidToDate[0]->value : 0,
'paidToDateCurrency' => count($paidToDate) && $paidToDate[0]->currency_id ? $paidToDate[0]->currency_id : 0,
'balances' => count($balances) && $balances[0]->value ? $balances[0]->value : 0,
'balancesCurrency' => count($balances) && $balances[0]->currency_id ? $balances[0]->currency_id : 0,
'averageInvoice' => count($averageInvoice) && $averageInvoice[0]->invoice_avg ? $averageInvoice[0]->invoice_avg : 0,
'averageInvoiceCurrency' => count($averageInvoice) && $averageInvoice[0]->currency_id ? $averageInvoice[0]->currency_id : 0,
2016-08-15 05:46:47 +02:00
'invoicesSent' => $metrics ? $metrics->invoices_sent : 0,
'activeClients' => $metrics ? $metrics->active_clients : 0,
2016-08-15 15:43:26 +02:00
'activities' => $this->createCollection($activities, new ActivityTransformer(), ENTITY_ACTIVITY),
2016-08-15 05:46:47 +02:00
];
return $this->response($data);
2016-03-19 23:38:05 +01:00
}
}