1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-15 07:33:04 +01:00
invoiceninja/app/Services/Chart/ChartService.php

256 lines
8.9 KiB
PHP
Raw Normal View History

2022-01-20 02:15:33 +01:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-04-12 06:15:41 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2022-01-20 02:15:33 +01:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Chart;
2022-01-20 10:09:08 +01:00
use App\Models\Client;
2022-01-20 02:15:33 +01:00
use App\Models\Company;
use App\Models\Expense;
2024-06-28 23:42:45 +02:00
use App\Models\Invoice;
use App\Models\Payment;
use App\Models\Quote;
use App\Models\Task;
2023-10-26 04:57:44 +02:00
use App\Models\User;
2022-01-20 10:09:08 +01:00
use Illuminate\Support\Facades\Cache;
2022-01-20 02:15:33 +01:00
class ChartService
{
2022-01-20 10:09:08 +01:00
use ChartQueries;
2024-06-28 23:42:45 +02:00
use ChartCalculations;
2022-01-20 10:09:08 +01:00
2023-05-16 07:35:29 +02:00
public function __construct(public Company $company, private User $user, private bool $is_admin)
2022-01-20 02:15:33 +01:00
{
}
2022-01-20 10:09:08 +01:00
/**
* Returns an array of currencies that have
* transacted with a company
*/
2024-01-14 05:05:00 +01:00
public function getCurrencyCodes(): array
2022-01-20 02:15:33 +01:00
{
2022-01-20 10:09:08 +01:00
/* Get all the distinct client currencies */
$currencies = Client::withTrashed()
2022-01-20 02:15:33 +01:00
->where('company_id', $this->company->id)
->where('is_deleted', 0)
2023-05-16 09:57:28 +02:00
->when(!$this->is_admin, function ($query) {
$query->where('user_id', $this->user->id);
})
2023-05-16 09:50:05 +02:00
->distinct()
->pluck('settings->currency_id as id');
2022-01-20 10:09:08 +01:00
/* Push the company currency on also */
$currencies->push((int) $this->company->settings->currency_id);
2022-01-20 02:15:33 +01:00
2022-01-20 10:09:08 +01:00
/* Add our expense currencies*/
2022-01-20 02:15:33 +01:00
$expense_currencies = Expense::withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0)
2023-05-16 09:57:28 +02:00
->when(!$this->is_admin, function ($query) {
$query->where('user_id', $this->user->id);
})
2023-05-16 09:50:05 +02:00
->distinct()
->pluck('currency_id as id');
2022-01-20 02:15:33 +01:00
2022-01-20 10:09:08 +01:00
/* Merge and filter by unique */
2022-01-20 02:15:33 +01:00
$currencies = $currencies->merge($expense_currencies)->unique();
/** @var \Illuminate\Support\Collection<\App\Models\Currency> */
$cache_currencies = app('currencies');
2022-01-20 10:09:08 +01:00
$filtered_currencies = $cache_currencies->whereIn('id', $currencies)->all();
$final_currencies = [];
foreach ($filtered_currencies as $c_currency) {
2022-01-20 10:09:08 +01:00
$final_currencies[$c_currency['id']] = $c_currency['code'];
}
return $final_currencies;
}
2024-06-28 23:42:45 +02:00
/* Chart Data */
2024-01-14 05:05:00 +01:00
public function chart_summary($start_date, $end_date): array
2022-01-21 04:35:16 +01:00
{
2022-01-21 07:04:01 +01:00
$currencies = $this->getCurrencyCodes();
$data = [];
2023-06-26 07:14:58 +02:00
$data['start_date'] = $start_date;
$data['end_date'] = $end_date;
2022-01-21 07:04:01 +01:00
foreach ($currencies as $key => $value) {
2022-01-21 07:04:01 +01:00
$data[$key]['invoices'] = $this->getInvoiceChartQuery($start_date, $end_date, $key);
2023-05-12 02:54:14 +02:00
$data[$key]['outstanding'] = $this->getOutstandingChartQuery($start_date, $end_date, $key);
2022-01-21 07:04:01 +01:00
$data[$key]['payments'] = $this->getPaymentChartQuery($start_date, $end_date, $key);
$data[$key]['expenses'] = $this->getExpenseChartQuery($start_date, $end_date, $key);
}
2024-06-24 13:10:52 +02:00
$data[999]['invoices'] = $this->getAggregateInvoiceChartQuery($start_date, $end_date);
$data[999]['outstanding'] = $this->getAggregateOutstandingChartQuery($start_date, $end_date);
$data[999]['payments'] = $this->getAggregatePaymentChartQuery($start_date, $end_date);
$data[999]['expenses'] = $this->getAggregateExpenseChartQuery($start_date, $end_date);
2022-01-21 07:04:01 +01:00
return $data;
2022-01-21 04:35:16 +01:00
}
/* Chart Data */
2022-01-21 04:35:16 +01:00
/* Totals */
2022-01-21 04:35:16 +01:00
2024-01-14 05:05:00 +01:00
public function totals($start_date, $end_date): array
2022-01-20 10:09:08 +01:00
{
$data = [];
2022-01-21 02:00:32 +01:00
$data['currencies'] = $this->getCurrencyCodes();
2023-06-26 07:23:48 +02:00
2023-06-26 07:14:58 +02:00
$data['start_date'] = $start_date;
$data['end_date'] = $end_date;
2022-01-21 07:04:01 +01:00
2023-05-14 01:25:08 +02:00
$revenue = $this->getRevenue($start_date, $end_date);
$outstanding = $this->getOutstanding($start_date, $end_date);
$expenses = $this->getExpenses($start_date, $end_date);
$invoices = $this->getInvoices($start_date, $end_date);
foreach ($data['currencies'] as $key => $value) {
2024-01-14 05:05:00 +01:00
2023-05-14 01:25:08 +02:00
$invoices_set = array_search($key, array_column($invoices, 'currency_id'));
$revenue_set = array_search($key, array_column($revenue, 'currency_id'));
$outstanding_set = array_search($key, array_column($outstanding, 'currency_id'));
$expenses_set = array_search($key, array_column($expenses, 'currency_id'));
2024-01-14 05:05:00 +01:00
$data[$key]['invoices'] = $invoices_set !== false ? $invoices[array_search($key, array_column($invoices, 'currency_id'))] : new \stdClass();
$data[$key]['revenue'] = $revenue_set !== false ? $revenue[array_search($key, array_column($revenue, 'currency_id'))] : new \stdClass();
$data[$key]['outstanding'] = $outstanding_set !== false ? $outstanding[array_search($key, array_column($outstanding, 'currency_id'))] : new \stdClass();
$data[$key]['expenses'] = $expenses_set !== false ? $expenses[array_search($key, array_column($expenses, 'currency_id'))] : new \stdClass();
2023-05-14 01:25:08 +02:00
2022-01-21 07:04:01 +01:00
}
2022-01-20 10:09:08 +01:00
2024-06-24 13:10:52 +02:00
$aggregate_revenue = $this->getAggregateRevenueQuery($start_date, $end_date);
$aggregate_outstanding = $this->getAggregateOutstandingQuery($start_date, $end_date);
$aggregate_expenses = $this->getAggregateExpenseQuery($start_date, $end_date);
$aggregate_invoices = $this->getAggregateInvoicesQuery($start_date, $end_date);
2024-06-25 06:58:18 +02:00
$data[999]['invoices'] = $aggregate_invoices !== false ? reset($aggregate_invoices) : new \stdClass();
$data[999]['expense'] = $aggregate_expenses !== false ? reset($aggregate_expenses) : new \stdClass();
$data[999]['outstanding'] = $aggregate_outstanding !== false ? reset($aggregate_outstanding) : new \stdClass();
$data[999]['revenue'] = $aggregate_revenue !== false ? reset($aggregate_revenue) : new \stdClass();
2024-06-24 13:10:52 +02:00
2022-01-20 10:09:08 +01:00
return $data;
}
2022-01-20 10:09:08 +01:00
2024-01-14 05:05:00 +01:00
public function getInvoices($start_date, $end_date): array
2023-05-12 03:19:10 +02:00
{
$revenue = $this->getInvoicesQuery($start_date, $end_date);
$revenue = $this->addCurrencyCodes($revenue);
return $revenue;
}
2024-01-14 05:05:00 +01:00
public function getRevenue($start_date, $end_date): array
2022-01-20 10:09:08 +01:00
{
$revenue = $this->getRevenueQuery($start_date, $end_date);
2022-01-21 07:04:01 +01:00
$revenue = $this->addCurrencyCodes($revenue);
2022-01-20 10:09:08 +01:00
return $revenue;
}
2024-01-14 05:05:00 +01:00
public function getOutstanding($start_date, $end_date): array
2022-01-20 10:09:08 +01:00
{
$outstanding = $this->getOutstandingQuery($start_date, $end_date);
2022-01-21 07:04:01 +01:00
$outstanding = $this->addCurrencyCodes($outstanding);
2022-01-20 10:09:08 +01:00
return $outstanding;
}
2024-01-14 05:05:00 +01:00
public function getExpenses($start_date, $end_date): array
2022-01-20 10:09:08 +01:00
{
$expenses = $this->getExpenseQuery($start_date, $end_date);
2022-01-21 07:04:01 +01:00
$expenses = $this->addCurrencyCodes($expenses);
2022-01-20 10:09:08 +01:00
2022-01-21 02:25:13 +01:00
return $expenses;
2022-01-20 10:09:08 +01:00
}
/* Totals */
2022-01-21 04:35:16 +01:00
/* Helpers */
2022-01-21 04:35:16 +01:00
2024-01-14 05:05:00 +01:00
private function addCurrencyCodes($data_set): array
2022-01-20 10:09:08 +01:00
{
/** @var \Illuminate\Support\Collection<\App\Models\Currency> */
$currencies = app('currencies');
2022-01-20 10:09:08 +01:00
foreach ($data_set as $key => $value) {
2022-01-21 02:00:32 +01:00
$data_set[$key]->currency_id = str_replace('"', '', $value->currency_id);
$data_set[$key]->code = $this->getCode($currencies, $data_set[$key]->currency_id);
2022-01-20 10:09:08 +01:00
}
return $data_set;
}
2024-01-14 05:05:00 +01:00
private function getCode($currencies, $currency_id): string
2022-01-20 10:09:08 +01:00
{
2022-01-21 02:25:13 +01:00
$currency_id = str_replace('"', '', $currency_id);
2022-01-21 02:00:32 +01:00
$currency = $currencies->filter(function ($item) use ($currency_id) {
2022-01-20 10:09:08 +01:00
return $item->id == $currency_id;
})->first();
if ($currency) {
2022-01-20 10:09:08 +01:00
return $currency->code;
}
2022-01-20 10:09:08 +01:00
return '';
2022-01-20 02:15:33 +01:00
}
2024-06-28 23:42:45 +02:00
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/**
* calculatedField
*
* @param array $data -
*
* field - list of fields for calculation
* period - current/previous
* calculation - sum/count/avg
*
* date_range - this_month
* or
* start_date - end_date
*/
public function getCalculatedField(array $data)
{
$results = 0;
match($data['field']){
'active_invoices' => $results = $this->getActiveInvoices($data),
2024-08-05 10:52:34 +02:00
'outstanding_invoices' => $results = $this->getOutstandingInvoices($data),
'completed_payments' => $results = $this->getCompletedPayments($data),
'refunded_payments' => $results = $this->getRefundedPayments($data),
'active_quotes' => $results = $this->getActiveQuotes($data),
'unapproved_quotes' => $results = $this->getUnapprovedQuotes($data),
'logged_tasks' => $results = $this->getLoggedTasks($data),
'invoiced_tasks' => $results = $this->getInvoicedTasks($data),
2024-06-28 23:42:45 +02:00
'paid_tasks' => $results = 0,
'logged_expenses' => $results = 0,
'pending_expenses' => $results = 0,
'invoiced_expenses' => $results = 0,
'invoice_paid_expenses' => $results = 0,
default => $results = 0,
};
return $results;
}
2022-01-20 02:15:33 +01:00
}