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

190 lines
6.0 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;
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;
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();
2022-01-20 10:09:08 +01:00
$cache_currencies = Cache::get('currencies');
$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;
}
/* 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);
}
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
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
{
$currencies = Cache::get('currencies');
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
}
}