2022-01-20 02:15:33 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
use App\Models\Payment;
|
2022-01-20 10:09:08 +01:00
|
|
|
use App\Services\Chart\ChartQueries;
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
$final_currencies[$c_currency['id']] = $c_currency['code'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $final_currencies;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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-20 10:09:08 +01:00
|
|
|
$data['revenue'] = $this->getRevenue($start_date, $end_date);
|
|
|
|
$data['outstanding'] = $this->getOutstanding($start_date, $end_date);
|
|
|
|
$data['expenses'] = $this->getExpenses($start_date, $end_date);
|
|
|
|
|
|
|
|
return $data;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function oustanding($start_date, $end_date)
|
|
|
|
{
|
|
|
|
|
|
|
|
$company_currency = (int) $this->company->settings->currency_id;
|
|
|
|
|
|
|
|
$results = \DB::select( \DB::raw("
|
|
|
|
SELECT
|
|
|
|
sum(invoices.balance) as balance,
|
|
|
|
JSON_EXTRACT( settings, '$.currency_id' ) AS currency_id
|
|
|
|
FROM clients
|
|
|
|
JOIN invoices
|
|
|
|
on invoices.client_id = clients.id
|
|
|
|
WHERE invoices.status_id IN (2,3)
|
|
|
|
AND invoices.company_id = :company_id
|
|
|
|
AND invoices.balance > 0
|
|
|
|
AND clients.is_deleted = 0
|
|
|
|
AND invoices.is_deleted = 0
|
|
|
|
AND (invoices.due_date BETWEEN :start_date AND :end_date)
|
|
|
|
GROUP BY currency_id
|
|
|
|
"), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] );
|
|
|
|
|
|
|
|
//return $results;
|
|
|
|
|
|
|
|
//the output here will most likely contain a currency_id = null value - we need to merge this value with the company currency
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:00:32 +01:00
|
|
|
private function getRevenue($start_date, $end_date) :array
|
2022-01-20 10:09:08 +01:00
|
|
|
{
|
|
|
|
$revenue = $this->getRevenueQuery($start_date, $end_date);
|
|
|
|
$revenue = $this->parseTotals($revenue);
|
|
|
|
$revenue = $this->addCountryCodes($revenue);
|
|
|
|
|
|
|
|
return $revenue;
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:00:32 +01:00
|
|
|
private function getOutstanding($start_date, $end_date) :array
|
2022-01-20 10:09:08 +01:00
|
|
|
{
|
|
|
|
$outstanding = $this->getOutstandingQuery($start_date, $end_date);
|
|
|
|
$outstanding = $this->parseTotals($outstanding);
|
|
|
|
$outstanding = $this->addCountryCodes($outstanding);
|
|
|
|
|
|
|
|
return $outstanding;
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:00:32 +01:00
|
|
|
private function getExpenses($start_date, $end_date) :array
|
2022-01-20 10:09:08 +01:00
|
|
|
{
|
|
|
|
$expenses = $this->getExpenseQuery($start_date, $end_date);
|
|
|
|
$expenses = $this->parseTotals($expenses);
|
|
|
|
$expenses = $this->addCountryCodes($expenses);
|
|
|
|
return $expenses;
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:00:32 +01:00
|
|
|
private function parseTotals($data_set) :array
|
2022-01-20 10:09:08 +01:00
|
|
|
{
|
|
|
|
/* Find the key where the company currency amount lives*/
|
|
|
|
$c_key = array_search($this->company->id , array_column($data_set, 'currency_id'));
|
|
|
|
|
|
|
|
if(!$c_key)
|
|
|
|
return $data_set;
|
|
|
|
|
|
|
|
/* Find the key where null currency_id lives */
|
|
|
|
$key = array_search(null , array_column($data_set, 'currency_id'));
|
|
|
|
|
|
|
|
if(!$key)
|
|
|
|
return $data_set;
|
|
|
|
|
|
|
|
$null_currency_amount = $data_set[$key]['amount'];
|
|
|
|
unset($data_set[$key]);
|
|
|
|
|
|
|
|
$data_set[$c_key]['amount'] += $null_currency_amount;
|
|
|
|
|
|
|
|
return $data_set;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-01-21 02:00:32 +01:00
|
|
|
private function addCountryCodes($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:00:32 +01:00
|
|
|
nlog($currency_id);
|
|
|
|
|
2022-01-20 10:09:08 +01:00
|
|
|
$currency = $currencies->filter(function ($item) use($currency_id) {
|
|
|
|
return $item->id == $currency_id;
|
|
|
|
})->first();
|
|
|
|
|
|
|
|
if($currency)
|
|
|
|
return $currency->code;
|
|
|
|
|
|
|
|
return '';
|
2022-01-20 02:15:33 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|