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

163 lines
4.7 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
*
2023-01-28 23:21:40 +01:00
* @copyright Copyright (c) 2023. 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;
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;
2022-01-20 02:15:33 +01:00
public Company $company;
public function __construct(Company $company)
{
$this->company = $company;
}
2022-01-20 10:09:08 +01:00
/**
* Returns an array of currencies that have
* transacted with a company
*/
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)
->distinct()
2022-01-20 10:09:08 +01:00
->pluck('settings->currency_id as id');
/* 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)
->distinct()
2022-01-21 02:00:32 +01:00
->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 */
2022-01-21 07:04:01 +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 = [];
foreach ($currencies as $key => $value) {
2022-01-21 07:04:01 +01:00
$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;
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
2022-01-21 02:00:32 +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();
2022-01-21 07:04:01 +01:00
foreach ($data['currencies'] as $key => $value) {
2022-01-21 07:04:01 +01:00
$revenue = $this->getRevenue($start_date, $end_date);
$outstanding = $this->getOutstanding($start_date, $end_date);
$expenses = $this->getExpenses($start_date, $end_date);
$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;
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
2022-01-21 07:04:01 +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;
}
2022-01-21 07:04:01 +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;
}
2022-01-21 07:04:01 +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
2022-01-21 07:04:01 +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;
}
2022-01-21 02:00:32 +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
}
}