mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2025-07-09 03:14:30 -04:00
Calculations for cards on dashbaord
This commit is contained in:
parent
2a4dd7e593
commit
7acb369408
@ -11,10 +11,12 @@
|
|||||||
|
|
||||||
namespace App\Services\Chart;
|
namespace App\Services\Chart;
|
||||||
|
|
||||||
|
use App\Models\Expense;
|
||||||
use App\Models\Invoice;
|
use App\Models\Invoice;
|
||||||
use App\Models\Payment;
|
use App\Models\Payment;
|
||||||
use App\Models\Quote;
|
use App\Models\Quote;
|
||||||
use App\Models\Task;
|
use App\Models\Task;
|
||||||
|
use Illuminate\Contracts\Database\Eloquent\Builder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class ChartCalculations.
|
* Class ChartCalculations.
|
||||||
@ -174,53 +176,144 @@ trait ChartCalculations
|
|||||||
|
|
||||||
public function getLoggedTasks($data): int|float
|
public function getLoggedTasks($data): int|float
|
||||||
{
|
{
|
||||||
//tasks with at least 1 timelog entry.
|
|
||||||
|
$q = $this->taskQuery($data);
|
||||||
|
|
||||||
|
return $this->taskCalculations($q, $data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getPaidTasks($data): int|float
|
||||||
|
{
|
||||||
|
$q = $this->taskQuery($data);
|
||||||
|
$q->whereHas('invoice', function ($query){
|
||||||
|
$query->where('status_id', 4)->where('is_deleted', 0);
|
||||||
|
});
|
||||||
|
|
||||||
|
return $this->taskCalculations($q, $data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getInvoicedTasks($data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$q = $this->taskQuery($data);
|
||||||
|
$q->whereHas('invoice');
|
||||||
|
|
||||||
|
return $this->taskCalculations($q, $data);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* All Expenses
|
||||||
|
*/
|
||||||
|
public function getLoggedExpenses($data): int|float
|
||||||
|
{
|
||||||
|
$q = $this->expenseQuery($data);
|
||||||
|
|
||||||
|
return $this->expenseCalculations($q, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Expenses that should be invoiced - but are not yet invoiced.
|
||||||
|
*/
|
||||||
|
public function getPendingExpenses($data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$q = $this->expenseQuery($data);
|
||||||
|
$q->where('should_be_invoiced', true)->whereNull('invoice_id');
|
||||||
|
return $this->expenseCalculations($q, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoiced.
|
||||||
|
*/
|
||||||
|
public function getInvoicedExpenses($data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$q = $this->expenseQuery($data);
|
||||||
|
$q->whereNotNull('invoice_id');
|
||||||
|
return $this->expenseCalculations($q, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paid.
|
||||||
|
*/
|
||||||
|
public function getPaidExpenses($data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$q = $this->expenseQuery($data);
|
||||||
|
$q->whereNotNull('payment_date');
|
||||||
|
return $this->expenseCalculations($q, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Paid.
|
||||||
|
*/
|
||||||
|
public function getInvoicedPaidExpenses($data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$q = $this->expenseQuery($data);
|
||||||
|
$q->whereNotNull('invoice_id')->whereNotNull('payment_date');
|
||||||
|
return $this->expenseCalculations($q, $data);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function expenseCalculations(Builder $query, array $data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
$result = 0;
|
$result = 0;
|
||||||
$calculated = collect();
|
$calculated = $this->expenseCalculator($query, $data);
|
||||||
|
|
||||||
$q = Task::query()
|
|
||||||
->withTrashed()
|
|
||||||
->where('company_id', $this->company->id)
|
|
||||||
->where('is_deleted',0);
|
|
||||||
|
|
||||||
if(in_array($data['period'], ['current,previous'])) {
|
|
||||||
$q->whereBetween('calculated_start_date', [$data['start_date'], $data['end_date']]);
|
|
||||||
}
|
|
||||||
|
|
||||||
if($data['calculation'] != 'count' && $data['format'] == 'money')
|
|
||||||
{
|
|
||||||
if($data['currency_id'] != '999')
|
|
||||||
{
|
|
||||||
|
|
||||||
$q->whereHas('client', function ($query) use ($data){
|
|
||||||
$query->where('settings->currency_id', $data['currency_id']);
|
|
||||||
});
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
$calculated = $this->taskMoneyCalculator($q, $data);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if($data['calculation'] != 'count' && $data['format'] == 'time')
|
|
||||||
{
|
|
||||||
$calculated = $q->get()->map(function ($t){
|
|
||||||
return $t->calcDuration();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
match ($data['calculation']) {
|
match ($data['calculation']) {
|
||||||
'sum' => $result = $calculated->sum(),
|
'sum' => $result = $calculated->sum(),
|
||||||
'avg' => $result = $calculated->avg(),
|
'avg' => $result = $calculated->avg(),
|
||||||
'count' => $result = $q->count(),
|
'count' => $result = $query->count(),
|
||||||
default => $result = 0,
|
default => $result = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function expenseCalculator(Builder $query, array $data)
|
||||||
|
{
|
||||||
|
|
||||||
|
return $query->get()
|
||||||
|
->when($data['currency_id'] == '999', function ($collection) {
|
||||||
|
$collection->map(function ($e) {
|
||||||
|
/** @var \App\Models\Expense $e */
|
||||||
|
return $e->amount * $e->exchange_rate;
|
||||||
|
});
|
||||||
|
})
|
||||||
|
->when($data['currency_id'] != '999', function ($collection) {
|
||||||
|
|
||||||
|
$collection->map(function ($e) {
|
||||||
|
|
||||||
|
/** @var \App\Models\Expense $e */
|
||||||
|
return $e->amount;
|
||||||
|
});
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function expenseQuery($data): Builder
|
||||||
|
{
|
||||||
|
$query = Expense::query()
|
||||||
|
->withTrashed()
|
||||||
|
->where('company_id', $this->company->id)
|
||||||
|
->where('is_deleted', 0);
|
||||||
|
|
||||||
|
if(in_array($data['period'], ['current,previous'])) {
|
||||||
|
$query->whereBetween('date', [$data['start_date'], $data['end_date']]);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $query;
|
||||||
|
}
|
||||||
|
|
||||||
|
////////////////////////////////////////////////////////////////
|
||||||
private function taskMoneyCalculator($query, $data)
|
private function taskMoneyCalculator($query, $data)
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -240,24 +333,28 @@ trait ChartCalculations
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getInvoicedTasks($data): int|float
|
private function taskQuery($data): Builder
|
||||||
{
|
{
|
||||||
|
|
||||||
$result = 0;
|
|
||||||
$calculated = collect();
|
|
||||||
|
|
||||||
$q = Task::query()
|
$q = Task::query()
|
||||||
->withTrashed()
|
->withTrashed()
|
||||||
->where('company_id', $this->company->id)
|
->where('company_id', $this->company->id)
|
||||||
->where('is_deleted', 0)
|
->where('is_deleted', 0);
|
||||||
->whereHas('invoice');
|
|
||||||
|
|
||||||
if(in_array($data['period'], ['current,previous'])) {
|
if(in_array($data['period'], ['current,previous'])) {
|
||||||
$q->whereBetween('calculated_start_date', [$data['start_date'], $data['end_date']]);
|
$q->whereBetween('calculated_start_date', [$data['start_date'], $data['end_date']]);
|
||||||
}
|
}
|
||||||
|
|
||||||
if($data['calculation'] != 'count' && $data['format'] == 'money') {
|
return $q;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function taskCalculations(Builder $q, array $data): int|float
|
||||||
|
{
|
||||||
|
|
||||||
|
$result = 0;
|
||||||
|
$calculated = collect();
|
||||||
|
|
||||||
|
if($data['calculation'] != 'count' && $data['format'] == 'money') {
|
||||||
if($data['currency_id'] != '999') {
|
if($data['currency_id'] != '999') {
|
||||||
|
|
||||||
$q->whereHas('client', function ($query) use ($data) {
|
$q->whereHas('client', function ($query) use ($data) {
|
||||||
@ -286,4 +383,5 @@ trait ChartCalculations
|
|||||||
return $result;
|
return $result;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
@ -224,6 +224,8 @@ class ChartService
|
|||||||
* period - current/previous
|
* period - current/previous
|
||||||
* calculation - sum/count/avg
|
* calculation - sum/count/avg
|
||||||
*
|
*
|
||||||
|
* May require currency_id
|
||||||
|
*
|
||||||
* date_range - this_month
|
* date_range - this_month
|
||||||
* or
|
* or
|
||||||
* start_date - end_date
|
* start_date - end_date
|
||||||
@ -241,11 +243,11 @@ class ChartService
|
|||||||
'unapproved_quotes' => $results = $this->getUnapprovedQuotes($data),
|
'unapproved_quotes' => $results = $this->getUnapprovedQuotes($data),
|
||||||
'logged_tasks' => $results = $this->getLoggedTasks($data),
|
'logged_tasks' => $results = $this->getLoggedTasks($data),
|
||||||
'invoiced_tasks' => $results = $this->getInvoicedTasks($data),
|
'invoiced_tasks' => $results = $this->getInvoicedTasks($data),
|
||||||
'paid_tasks' => $results = 0,
|
'paid_tasks' => $results = $this->getPaidTasks($data),
|
||||||
'logged_expenses' => $results = 0,
|
'logged_expenses' => $results = $this->getLoggedExpenses($data),
|
||||||
'pending_expenses' => $results = 0,
|
'pending_expenses' => $results = $this->getPendingExpenses($data),
|
||||||
'invoiced_expenses' => $results = 0,
|
'invoiced_expenses' => $results = $this->getInvoicedExpenses($data),
|
||||||
'invoice_paid_expenses' => $results = 0,
|
'invoice_paid_expenses' => $results = $this->getInvoicedPaidExpenses($data),
|
||||||
default => $results = 0,
|
default => $results = 0,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ class TemplateService
|
|||||||
$this->twig->addFilter($filter);
|
$this->twig->addFilter($filter);
|
||||||
|
|
||||||
$allowedTags = ['if', 'for', 'set', 'filter'];
|
$allowedTags = ['if', 'for', 'set', 'filter'];
|
||||||
$allowedFilters = ['escape', 'e', 'upper', 'lower', 'capitalize', 'filter', 'length', 'merge','format_currency', 'format_number','format_percent_number','map', 'join', 'first', 'date', 'sum', 'number_format'];
|
$allowedFilters = ['escape', 'e', 'upper', 'lower', 'capitalize', 'filter', 'length', 'merge','format_currency', 'format_number','format_percent_number','map', 'join', 'first', 'date', 'sum', 'number_format','nl2br'];
|
||||||
$allowedFunctions = ['range', 'cycle', 'constant', 'date',];
|
$allowedFunctions = ['range', 'cycle', 'constant', 'date',];
|
||||||
$allowedProperties = ['type_id'];
|
$allowedProperties = ['type_id'];
|
||||||
$allowedMethods = ['img','t'];
|
$allowedMethods = ['img','t'];
|
||||||
|
Loading…
x
Reference in New Issue
Block a user