mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-06-03 17:54:38 -04:00
commit
4df25b2421
76
app/Http/Controllers/ChartController.php
Normal file
76
app/Http/Controllers/ChartController.php
Normal file
@ -0,0 +1,76 @@
|
||||
<?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\Http\Controllers;
|
||||
|
||||
use App\Http\Requests\Chart\ShowChartRequest;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Response;
|
||||
|
||||
class ChartController extends BaseController
|
||||
{
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
* @OA\Post(
|
||||
* path="/api/v1/charts",
|
||||
* operationId="getCharts",
|
||||
* tags={"charts"},
|
||||
* summary="Get chart data",
|
||||
* description="Get chart data",
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Api-Secret"),
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Api-Token"),
|
||||
* @OA\Parameter(ref="#/components/parameters/X-Requested-With"),
|
||||
* @OA\Parameter(ref="#/components/parameters/include"),
|
||||
* @OA\Parameter(ref="#/components/parameters/index"),
|
||||
* @OA\Parameter(
|
||||
* name="rows",
|
||||
* in="query",
|
||||
* description="The number of activities to return",
|
||||
* example="50",
|
||||
* required=false,
|
||||
* @OA\Schema(
|
||||
* type="number",
|
||||
* format="integer",
|
||||
* ),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=200,
|
||||
* description="json dataset of chart data",
|
||||
* @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"),
|
||||
* @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"),
|
||||
* @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="default",
|
||||
* description="Unexpected Error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/Error"),
|
||||
* ),
|
||||
* )
|
||||
* @param Request $request
|
||||
* @return Response|mixed
|
||||
*/
|
||||
public function index(ShowChartRequest $request)
|
||||
{
|
||||
|
||||
return response()->json([],200);
|
||||
}
|
||||
|
||||
}
|
34
app/Http/Requests/Chart/ShowChartRequest.php
Normal file
34
app/Http/Requests/Chart/ShowChartRequest.php
Normal file
@ -0,0 +1,34 @@
|
||||
<?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\Http\Requests\Chart;
|
||||
|
||||
use App\Http\Requests\Request;
|
||||
use App\Models\Activity;
|
||||
|
||||
class ShowChartRequest extends Request
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function authorize() : bool
|
||||
{
|
||||
return auth()->user()->isAdmin();
|
||||
}
|
||||
|
||||
public function rules()
|
||||
{
|
||||
return [
|
||||
];
|
||||
}
|
||||
}
|
80
app/Services/Chart/ChartQueries.php
Normal file
80
app/Services/Chart/ChartQueries.php
Normal file
@ -0,0 +1,80 @@
|
||||
<?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;
|
||||
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
/**
|
||||
* Class ChartQueries.
|
||||
*/
|
||||
trait ChartQueries
|
||||
{
|
||||
|
||||
public function getRevenueQuery($start_date, $end_date)
|
||||
{
|
||||
|
||||
return DB::select( DB::raw("
|
||||
SELECT
|
||||
sum(invoices.paid_to_date) as paid_to_date,
|
||||
JSON_EXTRACT( settings, '$.currency_id' ) AS currency_id
|
||||
FROM clients
|
||||
JOIN invoices
|
||||
on invoices.client_id = clients.id
|
||||
WHERE invoices.status_id IN (3,4)
|
||||
AND invoices.company_id = :company_id
|
||||
AND invoices.amount > 0
|
||||
AND clients.is_deleted = 0
|
||||
AND invoices.is_deleted = 0
|
||||
AND (invoices.date BETWEEN :start_date AND :end_date)
|
||||
GROUP BY currency_id
|
||||
"), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] );
|
||||
|
||||
}
|
||||
|
||||
public function getOutstandingQuery($start_date, $end_date)
|
||||
{
|
||||
|
||||
return 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] );
|
||||
|
||||
}
|
||||
|
||||
public function getExpenseQuery($start_date, $end_date)
|
||||
{
|
||||
|
||||
return DB::select( DB::raw("
|
||||
SELECT sum(expenses.amount) as amount,
|
||||
expenses.currency_id as currency_id
|
||||
FROM expenses
|
||||
WHERE expenses.is_deleted = 0
|
||||
AND expenses.company_id = :company_id
|
||||
AND (expenses.date BETWEEN :start_date AND :end_date)
|
||||
GROUP BY currency_id
|
||||
"), ['company_id' => $this->company->id, 'start_date' => $start_date, 'end_date' => $end_date] );
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
197
app/Services/Chart/ChartService.php
Normal file
197
app/Services/Chart/ChartService.php
Normal file
@ -0,0 +1,197 @@
|
||||
<?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;
|
||||
|
||||
use App\Models\Client;
|
||||
use App\Models\Company;
|
||||
use App\Models\Expense;
|
||||
use App\Models\Payment;
|
||||
use App\Services\Chart\ChartQueries;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
|
||||
class ChartService
|
||||
{
|
||||
use ChartQueries;
|
||||
|
||||
public Company $company;
|
||||
|
||||
public function __construct(Company $company)
|
||||
{
|
||||
$this->company = $company;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array of currencies that have
|
||||
* transacted with a company
|
||||
*/
|
||||
public function getCurrencyCodes() :array
|
||||
{
|
||||
|
||||
// $currencies = Payment::withTrashed()
|
||||
// ->where('company_id', $this->company->id)
|
||||
// ->where('is_deleted', 0)
|
||||
// ->distinct()
|
||||
// ->get(['currency_id']);
|
||||
|
||||
/* 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()
|
||||
->get(['currency_id']);
|
||||
|
||||
/* Merge and filter by unique */
|
||||
$currencies = $currencies->merge($expense_currencies)->unique();
|
||||
|
||||
$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;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
public function totals($start_date, $end_date)
|
||||
{
|
||||
$data = [];
|
||||
|
||||
$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
|
||||
|
||||
}
|
||||
|
||||
private function getRevenue($start_date, $end_date)
|
||||
{
|
||||
$revenue = $this->getRevenueQuery($start_date, $end_date);
|
||||
$revenue = $this->parseTotals($revenue);
|
||||
$revenue = $this->addCountryCodes($revenue);
|
||||
|
||||
return $revenue;
|
||||
}
|
||||
|
||||
private function getOutstanding($start_date, $end_date)
|
||||
{
|
||||
$outstanding = $this->getOutstandingQuery($start_date, $end_date);
|
||||
$outstanding = $this->parseTotals($outstanding);
|
||||
$outstanding = $this->addCountryCodes($outstanding);
|
||||
|
||||
return $outstanding;
|
||||
}
|
||||
|
||||
private function getExpenses($start_date, $end_date)
|
||||
{
|
||||
$expenses = $this->getExpenseQuery($start_date, $end_date);
|
||||
$expenses = $this->parseTotals($expenses);
|
||||
$expenses = $this->addCountryCodes($expenses);
|
||||
|
||||
return $expenses;
|
||||
}
|
||||
|
||||
private function parseTotals($data_set)
|
||||
{
|
||||
/* 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;
|
||||
|
||||
}
|
||||
|
||||
private function addCountryCodes($data_set)
|
||||
{
|
||||
|
||||
$currencies = Cache::get('currencies');
|
||||
|
||||
foreach($data_set as $key => $value)
|
||||
{
|
||||
$data_set[$key]['code'] = $this->getCode($currencies, $value);
|
||||
}
|
||||
|
||||
return $data_set;
|
||||
|
||||
}
|
||||
|
||||
private function getCode($currencies, $currency_id)
|
||||
{
|
||||
$currency = $currencies->filter(function ($item) use($currency_id) {
|
||||
return $item->id == $currency_id;
|
||||
})->first();
|
||||
|
||||
if($currency)
|
||||
return $currency->code;
|
||||
|
||||
return '';
|
||||
|
||||
}
|
||||
|
||||
}
|
840
composer.lock
generated
840
composer.lock
generated
File diff suppressed because it is too large
Load Diff
125
tests/Unit/Chart/ChartCurrencyTest.php
Normal file
125
tests/Unit/Chart/ChartCurrencyTest.php
Normal file
@ -0,0 +1,125 @@
|
||||
<?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://opensource.org/licenses/AAL
|
||||
*/
|
||||
namespace Tests\Unit\Chart;
|
||||
|
||||
use App\Services\Chart\ChartService;
|
||||
use App\Utils\Ninja;
|
||||
use Tests\MockAccountData;
|
||||
use Tests\TestCase;
|
||||
|
||||
/**
|
||||
* @test
|
||||
*/
|
||||
class ChartCurrencyTest extends TestCase
|
||||
{
|
||||
use MockAccountData;
|
||||
|
||||
public function setUp() :void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->makeTestData();
|
||||
}
|
||||
|
||||
public function testClientServiceDataSetBuild()
|
||||
{
|
||||
|
||||
$haystack = [
|
||||
[
|
||||
'currency_id' => null,
|
||||
'amount' => 10
|
||||
],
|
||||
[
|
||||
'currency_id' => 1,
|
||||
'amount' => 11
|
||||
],
|
||||
[
|
||||
'currency_id' => 2,
|
||||
'amount' => 12
|
||||
],
|
||||
[
|
||||
'currency_id' => 3,
|
||||
'amount' => 13
|
||||
],
|
||||
];
|
||||
|
||||
$cs = new ChartService($this->company);
|
||||
|
||||
nlog($cs->totals(now()->subYears(10), now()));
|
||||
|
||||
$this->assertTrue(is_array($cs->totals(now()->subYears(10), now())));
|
||||
|
||||
}
|
||||
|
||||
/* coalesces the company currency with the null currencies */
|
||||
public function testFindNullValueinArray()
|
||||
{
|
||||
|
||||
$haystack = [
|
||||
[
|
||||
'currency_id' => null,
|
||||
'amount' => 10
|
||||
],
|
||||
[
|
||||
'currency_id' => 1,
|
||||
'amount' => 11
|
||||
],
|
||||
[
|
||||
'currency_id' => 2,
|
||||
'amount' => 12
|
||||
],
|
||||
[
|
||||
'currency_id' => 3,
|
||||
'amount' => 13
|
||||
],
|
||||
];
|
||||
|
||||
$company_currency_id = 1;
|
||||
|
||||
$c_key = array_search($company_currency_id , array_column($haystack, 'currency_id'));
|
||||
|
||||
$this->assertNotEquals($c_key, 2);
|
||||
$this->assertEquals($c_key, 1);
|
||||
|
||||
$key = array_search(null , array_column($haystack, 'currency_id'));
|
||||
|
||||
$this->assertNotEquals($key, 39);
|
||||
$this->assertEquals($key, 0);
|
||||
|
||||
$null_currency_amount = $haystack[$key]['amount'];
|
||||
|
||||
unset($haystack[$key]);
|
||||
|
||||
$haystack[$c_key]['amount'] += $null_currency_amount;
|
||||
|
||||
$this->assertEquals($haystack[$c_key]['amount'], 21);
|
||||
|
||||
}
|
||||
|
||||
|
||||
public function testCollectionMerging()
|
||||
{
|
||||
$currencies = collect([1,2,3,4,5,6]);
|
||||
|
||||
$expense_currencies = collect([4,5,6,7,8]);
|
||||
|
||||
$currencies = $currencies->merge($expense_currencies);
|
||||
|
||||
$this->assertEquals($currencies->count(), 11);
|
||||
|
||||
$currencies = $currencies->unique();
|
||||
|
||||
$this->assertEquals($currencies->count(), 8);
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user