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

167 lines
4.9 KiB
PHP
Raw Normal View History

2023-05-21 12:31:55 +02: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)
2023-05-21 12:31:55 +02:00
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\Services\Chart;
use App\Models\Client;
use App\Models\Company;
use App\Models\Expense;
use Illuminate\Support\Facades\Cache;
class ChartServiceLegacy
{
use ChartQueriesLegacy;
public Company $company;
public function __construct(Company $company)
{
$this->company = $company;
}
/**
* Returns an array of currencies that have
* transacted with a company
*/
2024-01-14 05:05:00 +01:00
public function getCurrencyCodes(): array
2023-05-21 12:31:55 +02:00
{
/* Get all the distinct client currencies */
$currencies = Client::withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0)
->distinct()
->pluck('settings->currency_id as id');
/* Push the company currency on also */
$currencies->push((int) $this->company->settings->currency_id);
/* Add our expense currencies*/
$expense_currencies = Expense::withTrashed()
->where('company_id', $this->company->id)
->where('is_deleted', 0)
->distinct()
->pluck('currency_id as id');
/* Merge and filter by unique */
$currencies = $currencies->merge($expense_currencies)->unique();
/** @var \Illuminate\Support\Collection<\App\Models\Currency> */
$cache_currencies = app('currencies');
2023-05-21 12:31:55 +02:00
$filtered_currencies = $cache_currencies->whereIn('id', $currencies)->all();
$final_currencies = [];
foreach ($filtered_currencies as $c_currency) {
$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
2023-05-21 12:31:55 +02:00
{
$currencies = $this->getCurrencyCodes();
$data = [];
foreach ($currencies as $key => $value) {
$data[$key]['invoices'] = $this->getInvoiceChartQuery($start_date, $end_date, $key);
$data[$key]['payments'] = $this->getPaymentChartQuery($start_date, $end_date, $key);
$data[$key]['expenses'] = $this->getExpenseChartQuery($start_date, $end_date, $key);
}
return $data;
}
/* Chart Data */
/* Totals */
2024-01-14 05:05:00 +01:00
public function totals($start_date, $end_date): array
2023-05-21 12:31:55 +02:00
{
$data = [];
$data['currencies'] = $this->getCurrencyCodes();
foreach ($data['currencies'] as $key => $value) {
$revenue = $this->getRevenue($start_date, $end_date);
$outstanding = $this->getOutstanding($start_date, $end_date);
$expenses = $this->getExpenses($start_date, $end_date);
2024-01-14 05:05:00 +01:00
$data[$key]['revenue'] = count($revenue) > 0 ? $revenue[array_search($key, array_column($revenue, 'currency_id'))] : new \stdClass();
$data[$key]['outstanding'] = count($outstanding) > 0 ? $outstanding[array_search($key, array_column($outstanding, 'currency_id'))] : new \stdClass();
$data[$key]['expenses'] = count($expenses) > 0 ? $expenses[array_search($key, array_column($expenses, 'currency_id'))] : new \stdClass();
2023-05-21 12:31:55 +02:00
}
return $data;
}
2024-01-14 05:05:00 +01:00
public function getRevenue($start_date, $end_date): array
2023-05-21 12:31:55 +02:00
{
$revenue = $this->getRevenueQuery($start_date, $end_date);
$revenue = $this->addCurrencyCodes($revenue);
return $revenue;
}
2024-01-14 05:05:00 +01:00
public function getOutstanding($start_date, $end_date): array
2023-05-21 12:31:55 +02:00
{
$outstanding = $this->getOutstandingQuery($start_date, $end_date);
$outstanding = $this->addCurrencyCodes($outstanding);
return $outstanding;
}
2024-01-14 05:05:00 +01:00
public function getExpenses($start_date, $end_date): array
2023-05-21 12:31:55 +02:00
{
$expenses = $this->getExpenseQuery($start_date, $end_date);
$expenses = $this->addCurrencyCodes($expenses);
return $expenses;
}
/* Totals */
/* Helpers */
2024-01-14 05:05:00 +01:00
private function addCurrencyCodes($data_set): array
2023-05-21 12:31:55 +02:00
{
/** @var \Illuminate\Support\Collection<\App\Models\Currency> */
$currencies = app('currencies');
2023-05-21 12:31:55 +02:00
foreach ($data_set as $key => $value) {
$data_set[$key]->currency_id = str_replace('"', '', $value->currency_id);
$data_set[$key]->code = $this->getCode($currencies, $data_set[$key]->currency_id);
}
return $data_set;
}
2024-01-14 05:05:00 +01:00
private function getCode($currencies, $currency_id): string
2023-05-21 12:31:55 +02:00
{
$currency_id = str_replace('"', '', $currency_id);
$currency = $currencies->filter(function ($item) use ($currency_id) {
return $item->id == $currency_id;
})->first();
if ($currency) {
return $currency->code;
}
return '';
}
}