Merge pull request #1052 from denisdulici/master

Refactored widgets
This commit is contained in:
Denis Duliçi 2019-12-29 03:02:22 +03:00 committed by GitHub
commit c8ba4f12c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 445 additions and 935 deletions

View File

@ -4,14 +4,15 @@ namespace App\Abstracts;
use App\Exports\Common\Reports as Export; use App\Exports\Common\Reports as Export;
use App\Models\Common\Report as Model; use App\Models\Common\Report as Model;
use App\Utilities\Chartjs; use App\Traits\Charts;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Utilities\Chartjs;
use Date; use Date;
use Illuminate\Support\Str; use Illuminate\Support\Str;
abstract class Report abstract class Report
{ {
use DateTime; use Charts, DateTime;
public $report; public $report;
@ -125,56 +126,7 @@ abstract class Report
$config = $this->chart[$this->report->chart]; $config = $this->chart[$this->report->chart];
$default_options = [ $default_options = $this->getLineChartOptions();
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'YrPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest'
],
'responsive' => true,
'scales' => [
'yAxes' => [
[
'barPercentage' => '1.6',
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.1)',
'zeroLineColor' => 'transparent',
'borderDash' => [2],
'borderDashOffset' => [2],
],
'ticks' => [
'padding' => 10,
'fontColor' => '#9e9e9e'
]
]
],
'xAxes' => [
[
'barPercentage' => '1.6',
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.0)',
'zeroLineColor' => 'transparent'
],
'ticks' => [
'suggestedMin' => 60,
'suggestedMax' => 125,
'padding' => 20,
'fontColor' => '#9e9e9e'
]
]
]
]
];
$options = array_merge($default_options, (array) $config['options']); $options = array_merge($default_options, (array) $config['options']);

63
app/Abstracts/Widget.php Normal file
View File

@ -0,0 +1,63 @@
<?php
namespace App\Abstracts;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Income\Invoice;
use App\Traits\Charts;
use Date;
abstract class Widget extends AbstractWidget
{
use Charts;
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4',
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
return $this->show();
}
public function calculateDocumentTotals($model)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$type = ($model instanceof Invoice) ? 'invoice' : 'bill';
$status_field = $type . '_status_code';
if ($model->$status_field == 'paid') {
return [$open, $overdue];
}
$payments = 0;
if ($model->$status_field == 'partial') {
foreach ($model->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if the invoice/bill is open or overdue
if ($model->due_at > $today) {
$open += $model->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $model->getAmountConvertedToDefault() - $payments;
}
return [$open, $overdue];
}
}

140
app/Traits/Charts.php Normal file
View File

@ -0,0 +1,140 @@
<?php
namespace App\Traits;
use App\Utilities\Chartjs;
trait Charts
{
public $donut = [
'colors' => [],
'labels' => [],
'values' => [],
];
public function addToDonut($color, $label, $value)
{
$this->donut['colors'][] = $color;
$this->donut['labels'][] = $label;
$this->donut['values'][] = (int) $value;
}
public function addMoneyToDonut($color, $amount, $description = '')
{
$label = money($amount, setting('default.currency'), true)->format();
if (!empty($description)) {
$label .= ' - ' . $description;
}
$this->addToDonut($color, $label, $amount);
}
public function getDonutChart($name, $width = 0, $height = 160, $limit = 10)
{
// Show donut prorated if there is no value
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->donut['values'])->sort()->reverse()->take($limit)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
}
$chart = new Chartjs();
$chart->type('doughnut')
->width($width)
->height($height)
->options($this->getDonutChartOptions($colors))
->labels(array_values($labels));
$chart->dataset($name, 'doughnut', array_values($values))
->backgroundColor(array_values($colors));
return $chart;
}
public function getDonutChartOptions($colors)
{
return [
'color' => array_values($colors),
'cutoutPercentage' => 80,
'legend' => [
'position' => 'right',
],
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'xPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest',
],
'scales' => [
'yAxes' => [
'display' => 0,
],
'xAxes' => [
'display' => 0,
],
],
];
}
public function getLineChartOptions()
{
return [
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'YrPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest',
],
'responsive' => true,
'scales' => [
'yAxes' => [[
'barPercentage' => 1.6,
'ticks' => [
'padding' => 10,
'fontColor' => '#9e9e9e',
],
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.1)',
'zeroLineColor' => 'transparent',
'borderDash' => [2],
'borderDashOffset' => [2],
],
]],
'xAxes' => [[
'barPercentage' => 1.6,
'ticks' => [
'suggestedMin' => 60,
'suggestedMax' => 125,
'padding' => 20,
'fontColor' => '#9e9e9e',
],
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.0)',
'zeroLineColor' => 'transparent',
],
]],
],
];
}
}

View File

@ -2,20 +2,11 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Banking\Account; use App\Models\Banking\Account;
class AccountBalance extends AbstractWidget class AccountBalance extends Widget
{ {
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/** /**
* Treat this method as a controller action. * Treat this method as a controller action.
* Return view() or other content to display. * Return view() or other content to display.

View File

@ -2,31 +2,22 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Banking\Transaction; use App\Models\Banking\Transaction;
use App\Traits\Currencies; use App\Traits\Currencies;
use App\Traits\DateTime; use App\Traits\DateTime;
use App\Utilities\Chartjs; use App\Utilities\Chartjs;
use Date; use Date;
class CashFlow extends AbstractWidget class CashFlow extends Widget
{ {
use Currencies, DateTime; use Currencies, DateTime;
/**
* The configuration array.
*
* @var array
*/
protected $config = [ protected $config = [
'width' => 'col-md-12' 'width' => 'col-md-12',
]; ];
/** public function show()
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{ {
$financial_start = $this->getFinancialStart()->format('Y-m-d'); $financial_start = $this->getFinancialStart()->format('Y-m-d');
@ -44,7 +35,7 @@ class CashFlow extends AbstractWidget
$end_month = $end->month; $end_month = $end->month;
// Monthly // Monthly
$labels = array(); $labels = [];
$s = clone $start; $s = clone $start;
@ -75,18 +66,9 @@ class CashFlow extends AbstractWidget
$chart->type('line') $chart->type('line')
->width(0) ->width(0)
->height(300) ->height(300)
->options($this->getChartOptions()) ->options($this->getLineChartOptions())
->labels(array_values($labels)); ->labels(array_values($labels));
$chart->dataset(trans_choice('general.profits', 1), 'line', array_values($profit))
->backgroundColor('#6da252')
->color('#6da252')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
$chart->dataset(trans_choice('general.incomes', 1), 'line', array_values($income)) $chart->dataset(trans_choice('general.incomes', 1), 'line', array_values($income))
->backgroundColor('#328aef') ->backgroundColor('#328aef')
->color('#328aef') ->color('#328aef')
@ -96,7 +78,7 @@ class CashFlow extends AbstractWidget
]) ])
->fill(false); ->fill(false);
$chart->dataset(trans_choice('general.expenses', 1), 'line', array_values($expense)) $chart->dataset(trans_choice('general.expenses', 2), 'line', array_values($expense))
->backgroundColor('#ef3232') ->backgroundColor('#ef3232')
->color('#ef3232') ->color('#ef3232')
->options([ ->options([
@ -105,6 +87,15 @@ class CashFlow extends AbstractWidget
]) ])
->fill(false); ->fill(false);
$chart->dataset(trans_choice('general.profits', 1), 'line', array_values($profit))
->backgroundColor('#6da252')
->color('#6da252')
->options([
'borderWidth' => 4,
'pointStyle' => 'line',
])
->fill(false);
return view('widgets.cash_flow', [ return view('widgets.cash_flow', [
'config' => (object) $this->config, 'config' => (object) $this->config,
'chart' => $chart, 'chart' => $chart,
@ -113,7 +104,7 @@ class CashFlow extends AbstractWidget
private function calculateTotals($type, $start, $end, $period) private function calculateTotals($type, $start, $end, $period)
{ {
$totals = array(); $totals = [];
$date_format = 'Y-m'; $date_format = 'Y-m';
@ -184,51 +175,4 @@ class CashFlow extends AbstractWidget
return $profit; return $profit;
} }
private function getChartOptions()
{
return [
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'YrPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest',
],
'responsive' => true,
'scales' => [
'yAxes' => [[
'barPercentage' => 1.6,
'ticks' => [
'padding' => 10,
'fontColor' => '#9e9e9e',
],
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.1)',
'zeroLineColor' => 'transparent',
'borderDash' => [2],
'borderDashOffset' => [2],
],
]],
'xAxes' => [[
'barPercentage' => 1.6,
'ticks' => [
'suggestedMin' => 60,
'suggestedMax' => 125,
'padding' => 20,
'fontColor' => '#9e9e9e',
],
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(29,140,248,0.0)',
'zeroLineColor' => 'transparent',
],
]],
],
];
}
} }

View File

@ -2,204 +2,32 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Setting\Category; use App\Models\Setting\Category;
use App\Utilities\Chartjs;
use Date;
class ExpensesByCategory extends AbstractWidget class ExpensesByCategory extends Widget
{ {
public $donut = ['colors' => [], 'labels' => [], 'values' => []];
/**
* The configuration array.
*
* @var array
*/
protected $config = [ protected $config = [
'width' => 'col-md-6', 'width' => 'col-md-6',
]; ];
/** public function show()
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{ {
$expenses_amount = $open_bill = $overdue_bill = 0; Category::with(['expense_transactions'])->type(['expense'])->enabled()->each(function ($category) {
// Get categories
$categories = Category::with(['bills', 'expense_transactions'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0; $amount = 0;
// Transactions foreach ($category->expense_transactions as $transacion) {
foreach ($category->expense_transactions as $transaction) { $amount += $transacion->getAmountConvertedToDefault();
$amount += $transaction->getAmountConvertedToDefault();
} }
$expenses_amount += $amount; $this->addMoneyToDonut($category->color, $amount, $category->name);
});
// Bills $chart = $this->getDonutChart(trans_choice('general.expenses', 2), 0, 160, 6);
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
$this->addToDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no expense
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
}
$chart = new Chartjs();
$chart->type('doughnut')
->width(0)
->height(160)
->options($this->getChartOptions($colors))
->labels(array_values($labels));
$chart->dataset(trans_choice('general.expenses', 2), 'doughnut', array_values($values))
->backgroundColor(array_values($colors));
return view('widgets.expenses_by_category', [ return view('widgets.expenses_by_category', [
'config' => (object) $this->config, 'config' => (object) $this->config,
'chart' => $chart, 'chart' => $chart,
]); ]);
} }
public function getData()
{
//
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'expense_transactions'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->expense_transactions as $transaction) {
$amount += $transaction->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
$this->addToDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no expense
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
}
return [
'labels' => $labels,
'colors' => $colors,
'values' => $values,
];
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
private function addToDonut($color, $amount, $text)
{
$this->donut['colors'][] = $color;
$this->donut['labels'][] = money($amount, setting('default.currency'), true)->format() . ' - ' . $text;
$this->donut['values'][] = (int) $amount;
}
private function getChartOptions($colors)
{
return [
'color' => array_values($colors),
'cutoutPercentage' => 80,
'legend' => [
'position' => 'right',
],
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'xPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest',
],
'scales' => [
'yAxes' => [
'display' => 0,
],
'xAxes' => [
'display' => 0,
],
],
];
}
} }

View File

@ -0,0 +1,33 @@
<?php
namespace App\Widgets;
use App\Abstracts\Widget;
use App\Models\Setting\Category;
class IncomeByCategory extends Widget
{
protected $config = [
'width' => 'col-md-6',
];
public function show()
{
Category::with(['income_transacions'])->type(['income'])->enabled()->each(function ($category) {
$amount = 0;
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
}
$this->addMoneyToDonut($category->color, $amount, $category->name);
});
$chart = $this->getDonutChart(trans_choice('general.incomes', 1), 0, 160, 6);
return view('widgets.income_by_category', [
'config' => (object) $this->config,
'chart' => $chart,
]);
}
}

View File

@ -1,205 +0,0 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use App\Utilities\Chartjs;
use Date;
class IncomesByCategory extends AbstractWidget
{
public $donut = ['colors' => [], 'labels' => [], 'values' => []];
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-6',
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'income_transacions'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
$this->addToDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no income
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
}
$chart = new Chartjs();
$chart->type('doughnut')
->width(0)
->height(160)
->options($this->getChartOptions($colors))
->labels(array_values($labels));
$chart->dataset(trans_choice('general.incomes', 2), 'doughnut', array_values($values))
->backgroundColor(array_values($colors));
return view('widgets.incomes_by_category', [
'config' => (object) $this->config,
'chart' => $chart,
]);
}
public function getData()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'income_transacions'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
$this->addToDonut($category->color, $amount, $category->name);
}
// Show donut prorated if there is no income
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
}
}
// Get 6 categories by amount
$colors = $labels = [];
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
foreach ($values as $id => $val) {
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
}
return [
'labels' => $labels,
'colors' => $colors,
'values' => $values,
];
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
private function addToDonut($color, $amount, $text)
{
$this->donut['colors'][] = $color;
$this->donut['labels'][] = money($amount, setting('default.currency'), true)->format() . ' - ' . $text;
$this->donut['values'][] = (int) $amount;
}
private function getChartOptions($colors)
{
return [
'backgroudColor' => array_values($colors),
'cutoutPercentage' => 80,
'legend' => [
'position' => 'right',
],
'tooltips' => [
'backgroundColor' => '#f5f5f5',
'titleFontColor' => '#333',
'bodyFontColor' => '#666',
'bodySpacing' => 4,
'xPadding' => 12,
'mode' => 'nearest',
'intersect' => 0,
'position' => 'nearest',
],
'scales' => [
'yAxes' => [
'display' => 0,
],
'xAxes' => [
'display' => 0,
],
],
];
}
}

View File

@ -2,32 +2,18 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Banking\Transaction; use App\Models\Banking\Transaction;
class LatestExpenses extends AbstractWidget class LatestExpenses extends Widget
{ {
/** public function show()
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{ {
// $transactions = Transaction::with('category')->type('expense')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
$latest_expenses = Transaction::type('expense')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
return view('widgets.latest_expenses', [ return view('widgets.latest_expenses', [
'config' => (object) $this->config, 'config' => (object) $this->config,
'latest_expenses' => $latest_expenses, 'transactions' => $transactions,
]); ]);
} }
} }

View File

@ -0,0 +1,19 @@
<?php
namespace App\Widgets;
use App\Abstracts\Widget;
use App\Models\Banking\Transaction;
class LatestIncome extends Widget
{
public function show()
{
$transactions = Transaction::with('category')->type('income')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
return view('widgets.latest_income', [
'config' => (object) $this->config,
'transactions' => $transactions,
]);
}
}

View File

@ -1,33 +0,0 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Banking\Transaction;
class LatestIncomes extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$latest_incomes = Transaction::type('income')->orderBy('paid_at', 'desc')->isNotTransfer()->take(5)->get();
return view('widgets.latest_incomes', [
'config' => (object) $this->config,
'latest_incomes' => $latest_incomes,
]);
}
}

View File

@ -2,97 +2,43 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Setting\Category; use App\Models\Banking\Transaction;
use Date; use App\Models\Expense\Bill;
class TotalExpenses extends AbstractWidget class TotalExpenses extends Widget
{ {
/** public function show()
* The configuration array. {
* $current = $open = $overdue = 0;
* @var array
*/ Transaction::type('expense')->isNotTransfer()->each(function ($transaction) use (&$current) {
protected $config = [ $current += $transaction->getAmountConvertedToDefault();
'width' => 'col-md-4' });
Bill::accrued()->notPaid()->each(function ($bill) use (&$open, &$overdue) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
$open += $open_tmp;
$overdue += $overdue_tmp;
});
$progress = 100;
if (!empty($open) && !empty($overdue)) {
$progress = (int) ($open * 100) / ($open + $overdue);
}
$totals = [
'current' => $current,
'open' => money($open, setting('default.currency'), true),
'overdue' => money($overdue, setting('default.currency'), true),
'progress' => $progress,
]; ];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'expense_transactions'])->type(['expense'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->expense_transactions as $transaction) {
$amount += $transaction->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
}
$expenses_progress = 100;
if (!empty($open_bill) && !empty($overdue_bill)) {
$expenses_progress = (int) ($open_bill * 100) / ($open_bill + $overdue_bill);
}
$total_expenses = array(
'total' => $expenses_amount,
'open_bill' => money($open_bill, setting('default.currency'), true),
'overdue_bill' => money($overdue_bill, setting('default.currency'), true),
'progress' => $expenses_progress
);
return view('widgets.total_expenses', [ return view('widgets.total_expenses', [
'config' => (object) $this->config, 'config' => (object) $this->config,
'total_expenses' => $total_expenses, 'totals' => $totals,
]); ]);
} }
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
} }

View File

@ -0,0 +1,44 @@
<?php
namespace App\Widgets;
use App\Abstracts\Widget;
use App\Models\Banking\Transaction;
use App\Models\Income\Invoice;
class TotalIncome extends Widget
{
public function show()
{
$current = $open = $overdue = 0;
Transaction::type('income')->isNotTransfer()->each(function ($transaction) use (&$current) {
$current += $transaction->getAmountConvertedToDefault();
});
Invoice::accrued()->notPaid()->each(function ($invoice) use (&$open, &$overdue) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($invoice);
$open += $open_tmp;
$overdue += $overdue_tmp;
});
$progress = 100;
if (!empty($open) && !empty($overdue)) {
$progress = (int) ($open * 100) / ($open + $overdue);
}
$totals = [
'current' => $current,
'open' => money($open, setting('default.currency'), true),
'overdue' => money($overdue, setting('default.currency'), true),
'progress' => $progress,
];
return view('widgets.total_income', [
'config' => (object) $this->config,
'totals' => $totals,
]);
}
}

View File

@ -1,99 +0,0 @@
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use Date;
class TotalIncomes extends AbstractWidget
{
/**
* The configuration array.
*
* @var array
*/
protected $config = [
'width' => 'col-md-4'
];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'income_transacions'])->type(['income'])->enabled()->get();
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
}
$incomes_progress = 100;
if (!empty($open_invoice) && !empty($overdue_invoice)) {
$incomes_progress = (int) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
}
// Totals
$total_incomes = array(
'total' => $incomes_amount,
'open_invoice' => money($open_invoice, setting('default.currency'), true),
'overdue_invoice' => money($overdue_invoice, setting('default.currency'), true),
'progress' => $incomes_progress
);
return view('widgets.total_incomes', [
'config' => (object) $this->config,
'total_incomes' => $total_incomes,
]);
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
}

View File

@ -2,161 +2,62 @@
namespace App\Widgets; namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget; use App\Abstracts\Widget;
use App\Models\Setting\Category; use App\Models\Banking\Transaction;
use Date; use App\Models\Expense\Bill;
use App\Models\Income\Invoice;
class TotalProfit extends AbstractWidget class TotalProfit extends Widget
{ {
/** public function show()
* The configuration array. {
* $current_income = $open_invoice = $overdue_invoice = 0;
* @var array $current_expenses = $open_bill = $overdue_bill = 0;
*/
protected $config = [ Transaction::isNotTransfer()->each(function ($transaction) use (&$current_income, &$current_expenses) {
'width' => 'col-md-4' $amount = $transaction->getAmountConvertedToDefault();
if ($transaction->type == 'income') {
$current_income += $amount;
} else {
$current_expenses += $amount;
}
});
Invoice::accrued()->notPaid()->each(function ($invoice) use (&$open_invoice, &$overdue_invoice) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($invoice);
$open_invoice += $open_tmp;
$overdue_invoice += $overdue_tmp;
});
Bill::accrued()->notPaid()->each(function ($bill) use (&$open_bill, &$overdue_bill) {
list($open_tmp, $overdue_tmp) = $this->calculateDocumentTotals($bill);
$open_bill += $open_tmp;
$overdue_bill += $overdue_tmp;
});
$current = $current_income - $current_expenses;
$open = $open_invoice - $open_bill;
$overdue = $overdue_invoice - $overdue_bill;
$progress = 100;
if (!empty($open) && !empty($overdue)) {
$progress = (int) ($open * 100) / ($open + $overdue);
}
$totals = [
'current' => $current,
'open' => money($open, setting('default.currency'), true),
'overdue' => money($overdue, setting('default.currency'), true),
'progress' => $progress,
]; ];
/**
* Treat this method as a controller action.
* Return view() or other content to display.
*/
public function run()
{
//
list($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill) = $this->calculateAmounts();
$incomes_progress = 100;
if (!empty($open_invoice) && !empty($overdue_invoice)) {
$incomes_progress = (int) ($open_invoice * 100) / ($open_invoice + $overdue_invoice);
}
// Totals
$total_incomes = array(
'total' => $incomes_amount,
'open_invoice' => money($open_invoice, setting('default.currency'), true),
'overdue_invoice' => money($overdue_invoice, setting('default.currency'), true),
'progress' => $incomes_progress
);
$expenses_progress = 100;
if (!empty($open_bill) && !empty($overdue_bill)) {
$expenses_progress = (int) ($open_bill * 100) / ($open_bill + $overdue_bill);
}
$total_expenses = array(
'total' => $expenses_amount,
'open_bill' => money($open_bill, setting('default.currency'), true),
'overdue_bill' => money($overdue_bill, setting('default.currency'), true),
'progress' => $expenses_progress
);
$amount_profit = $incomes_amount - $expenses_amount;
$open_profit = $open_invoice - $open_bill;
$overdue_profit = $overdue_invoice - $overdue_bill;
$total_progress = 100;
if (!empty($open_profit) && !empty($overdue_profit)) {
$total_progress = (int) ($open_profit * 100) / ($open_profit + $overdue_profit);
}
$total_profit = array(
'total' => $amount_profit,
'open' => money($open_profit, setting('default.currency'), true),
'overdue' => money($overdue_profit, setting('default.currency'), true),
'progress' => $total_progress
);
return view('widgets.total_profit', [ return view('widgets.total_profit', [
'config' => (object) $this->config, 'config' => (object) $this->config,
'total_profit' => $total_profit, 'totals' => $totals,
]); ]);
} }
private function calculateAmounts()
{
$incomes_amount = $open_invoice = $overdue_invoice = 0;
$expenses_amount = $open_bill = $overdue_bill = 0;
// Get categories
$categories = Category::with(['bills', 'expense_transactions', 'invoices', 'income_transacions'])->type(['income', 'expense'])->enabled()->get();
foreach ($categories as $category) {
switch ($category->type) {
case 'income':
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
}
$incomes_amount += $amount;
// Invoices
$invoices = $category->invoices()->accrued()->get();
foreach ($invoices as $invoice) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($invoice, 'invoice');
$open_invoice += $open;
$overdue_invoice += $overdue;
}
break;
case 'expense':
$amount = 0;
// Transactions
foreach ($category->expense_transactions as $transaction) {
$amount += $transaction->getAmountConvertedToDefault();
}
$expenses_amount += $amount;
// Bills
$bills = $category->bills()->accrued()->get();
foreach ($bills as $bill) {
list($open, $overdue) = $this->calculateInvoiceBillTotals($bill, 'bill');
$open_bill += $open;
$overdue_bill += $overdue;
}
break;
}
}
return array($incomes_amount, $open_invoice, $overdue_invoice, $expenses_amount, $open_bill, $overdue_bill);
}
private function calculateInvoiceBillTotals($item, $type)
{
$open = $overdue = 0;
$today = Date::today()->toDateString();
$code_field = $type . '_status_code';
if ($item->$code_field != 'paid') {
$payments = 0;
if ($item->$code_field == 'partial') {
foreach ($item->transactions as $transaction) {
$payments += $transaction->getAmountConvertedToDefault();
}
}
// Check if it's open or overdue invoice
if ($item->due_at > $today) {
$open += $item->getAmountConvertedToDefault() - $payments;
} else {
$overdue += $item->getAmountConvertedToDefault() - $payments;
}
}
return [$open, $overdue];
}
} }

View File

@ -29,8 +29,8 @@ class Widgets extends Seeder
$rows = [ $rows = [
[ [
'company_id' => $company_id, 'company_id' => $company_id,
'name' => trans('dashboard.total_incomes'), 'name' => trans('dashboard.total_income'),
'alias' => 'total-incomes', 'alias' => 'total-income',
'settings' => ['width' => 'col-md-4'], 'settings' => ['width' => 'col-md-4'],
'enabled' => 1, 'enabled' => 1,
], ],
@ -57,8 +57,8 @@ class Widgets extends Seeder
], ],
[ [
'company_id' => $company_id, 'company_id' => $company_id,
'name' => trans('dashboard.incomes_by_category'), 'name' => trans('dashboard.income_by_category'),
'alias' => 'incomes-by-category', 'alias' => 'income-by-category',
'settings' => ['width' => 'col-md-6'], 'settings' => ['width' => 'col-md-6'],
'enabled' => 1, 'enabled' => 1,
], ],
@ -78,8 +78,8 @@ class Widgets extends Seeder
], ],
[ [
'company_id' => $company_id, 'company_id' => $company_id,
'name' => trans('dashboard.latest_incomes'), 'name' => trans('dashboard.latest_income'),
'alias' => 'latest-incomes', 'alias' => 'latest-income',
'settings' => ['width' => 'col-md-4'], 'settings' => ['width' => 'col-md-4'],
'enabled' => 1, 'enabled' => 1,
], ],
@ -89,7 +89,7 @@ class Widgets extends Seeder
'alias' => 'latest-expenses', 'alias' => 'latest-expenses',
'settings' => ['width' => 'col-md-4'], 'settings' => ['width' => 'col-md-4'],
'enabled' => 1, 'enabled' => 1,
] ],
]; ];
foreach ($rows as $row) { foreach ($rows as $row) {

View File

@ -2,7 +2,7 @@
return [ return [
'total_incomes' => 'Total Incomes', 'total_income' => 'Total Income',
'receivables' => 'Receivables', 'receivables' => 'Receivables',
'open_invoices' => 'Open Invoices', 'open_invoices' => 'Open Invoices',
'overdue_invoices' => 'Overdue Invoices', 'overdue_invoices' => 'Overdue Invoices',
@ -15,9 +15,9 @@ return [
'overdue_profit' => 'Overdue Profit', 'overdue_profit' => 'Overdue Profit',
'cash_flow' => 'Cash Flow', 'cash_flow' => 'Cash Flow',
'no_profit_loss' => 'No Profit Loss', 'no_profit_loss' => 'No Profit Loss',
'incomes_by_category' => 'Incomes By Category', 'income_by_category' => 'Income By Category',
'expenses_by_category' => 'Expenses By Category', 'expenses_by_category' => 'Expenses By Category',
'account_balance' => 'Account Balance', 'account_balance' => 'Account Balance',
'latest_incomes' => 'Latest Incomes', 'latest_income' => 'Latest Income',
'latest_expenses' => 'Latest Expenses', 'latest_expenses' => 'Latest Expenses',
]; ];

View File

@ -4,7 +4,7 @@
<div class="row align-items-center"> <div class="row align-items-center">
<div class="col-6 text-nowrap"> <div class="col-6 text-nowrap">
<h4 class="mb-0">{{ trans('dashboard.incomes_by_category') }}</h4> <h4 class="mb-0">{{ trans('dashboard.income_by_category') }}</h4>
</div> </div>
<div class="col-6 hidden-sm"> <div class="col-6 hidden-sm">

View File

@ -34,16 +34,16 @@
<thead class="thead-light"> <thead class="thead-light">
<tr class="row table-head-line"> <tr class="row table-head-line">
<th class="col-xs-4 col-md-4 text-left">{{ trans('general.date') }}</th> <th class="col-xs-4 col-md-4 text-left">{{ trans('general.date') }}</th>
<th class="col-xs-4 col-md-4 text-center">{{ trans_choice('general.categories', 1) }}</th> <th class="col-xs-4 col-md-4 text-left">{{ trans_choice('general.categories', 1) }}</th>
<th class="col-xs-4 col-md-4 text-right">{{ trans('general.amount') }}</th> <th class="col-xs-4 col-md-4 text-right">{{ trans('general.amount') }}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@if ($latest_expenses->count()) @if ($transactions->count())
@foreach($latest_expenses as $item) @foreach($transactions as $item)
<tr class="row border-top-1"> <tr class="row border-top-1">
<td class="col-xs-4 col-md-4 text-left">@date($item->paid_at)</td> <td class="col-xs-4 col-md-4 text-left">@date($item->paid_at)</td>
<td class="col-xs-4 col-md-4 text-center">{{ $item->category ? $item->category->name : trans_choice('general.bills', 2) }}</td> <td class="col-xs-4 col-md-4 text-left">{{ $item->category->name }}</td>
<td class="col-xs-4 col-md-4 text-right">@money($item->amount, $item->currency_code, true)</td> <td class="col-xs-4 col-md-4 text-right">@money($item->amount, $item->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach

View File

@ -3,7 +3,7 @@
<div class="card-header border-bottom-0"> <div class="card-header border-bottom-0">
<div class="row align-items-center"> <div class="row align-items-center">
<div class="col-6 text-nowrap"> <div class="col-6 text-nowrap">
<h4 class="mb-0">{{ trans('dashboard.latest_incomes') }}</h4> <h4 class="mb-0">{{ trans('dashboard.latest_income') }}</h4>
</div> </div>
<div class="col-6 hidden-sm"> <div class="col-6 hidden-sm">
@ -34,16 +34,16 @@
<thead class="thead-light"> <thead class="thead-light">
<tr class="row table-head-line"> <tr class="row table-head-line">
<th class="col-xs-4 col-md-4 text-left">{{ trans('general.date') }}</th> <th class="col-xs-4 col-md-4 text-left">{{ trans('general.date') }}</th>
<th class="col-xs-4 col-md-4 text-center">{{ trans_choice('general.categories', 1) }}</th> <th class="col-xs-4 col-md-4 text-left">{{ trans_choice('general.categories', 1) }}</th>
<th class="col-xs-4 col-md-4 text-right">{{ trans('general.amount') }}</th> <th class="col-xs-4 col-md-4 text-right">{{ trans('general.amount') }}</th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
@if ($latest_incomes->count()) @if ($transactions->count())
@foreach($latest_incomes as $item) @foreach($transactions as $item)
<tr class="row border-top-1"> <tr class="row border-top-1">
<td class="col-xs-4 col-md-4 text-left">@date($item->paid_at)</td> <td class="col-xs-4 col-md-4 text-left">@date($item->paid_at)</td>
<td class="col-xs-4 col-md-4 text-center">{{ $item->category ? $item->category->name : trans_choice('general.invoices', 2) }}</td> <td class="col-xs-4 col-md-4 text-left">{{ $item->category->name }}</td>
<td class="col-xs-4 col-md-4 text-right">@money($item->amount, $item->currency_code, true)</td> <td class="col-xs-4 col-md-4 text-right">@money($item->amount, $item->currency_code, true)</td>
</tr> </tr>
@endforeach @endforeach

View File

@ -23,7 +23,7 @@
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_expenses') }}</h5> <h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_expenses') }}</h5>
<span class="font-weight-bold text-white mb-0">@money($total_expenses['total'], setting('default.currency'), true)</span> <span class="font-weight-bold text-white mb-0">@money($totals['current'], setting('default.currency'), true)</span>
</div> </div>
<div class="col-auto"> <div class="col-auto">
@ -33,9 +33,9 @@
</div> </div>
</div> </div>
<p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_bills') }}: {{ $total_expenses['open_bill'] }}<br>{{ trans('dashboard.overdue_bills') }}: {{ $total_expenses['overdue_bill'] }}" data-toggle="tooltip" data-html="true"> <p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_bills') }}: {{ $totals['open'] }}<br>{{ trans('dashboard.overdue_bills') }}: {{ $totals['overdue'] }}" data-toggle="tooltip" data-html="true">
<span class="text-white">{{ trans('dashboard.payables') }}</span> <span class="text-white">{{ trans('dashboard.payables') }}</span>
<span class="text-white font-weight-bold float-right">{{ $total_expenses['open_bill'] }} / {{ $total_expenses['overdue_bill'] }}</span> <span class="text-white font-weight-bold float-right">{{ $totals['open'] }} / {{ $totals['overdue'] }}</span>
</p> </p>
</div> </div>
</div> </div>

View File

@ -22,8 +22,8 @@
<div class="card-body"> <div class="card-body">
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_incomes') }}</h5> <h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_income') }}</h5>
<span class="font-weight-bold text-white mb-0">@money($total_incomes['total'], setting('default.currency'), true)</span> <span class="font-weight-bold text-white mb-0">@money($totals['current'], setting('default.currency'), true)</span>
</div> </div>
<div class="col-auto"> <div class="col-auto">
@ -33,9 +33,9 @@
</div> </div>
</div> </div>
<p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_invoices') }}: {{ $total_incomes['open_invoice'] }}<br>{{ trans('dashboard.overdue_invoices') }}: {{ $total_incomes['overdue_invoice'] }}" data-toggle="tooltip" data-html="true"> <p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_invoices') }}: {{ $totals['open'] }}<br>{{ trans('dashboard.overdue_invoices') }}: {{ $totals['overdue'] }}" data-toggle="tooltip" data-html="true">
<span class="text-white">{{ trans('dashboard.receivables') }}</span> <span class="text-white">{{ trans('dashboard.receivables') }}</span>
<span class="text-white font-weight-bold float-right">{{ $total_incomes['open_invoice'] }} / {{ $total_incomes['overdue_invoice'] }}</span> <span class="text-white font-weight-bold float-right">{{ $totals['open'] }} / {{ $totals['overdue'] }}</span>
</p> </p>
</div> </div>
</div> </div>

View File

@ -23,7 +23,7 @@
<div class="row"> <div class="row">
<div class="col"> <div class="col">
<h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_profit') }}</h5> <h5 class="text-uppercase text-white mb-0">{{ trans('dashboard.total_profit') }}</h5>
<span class="font-weight-bold text-white mb-0">@money($total_profit['total'], setting('default.currency'), true)</span> <span class="font-weight-bold text-white mb-0">@money($totals['current'], setting('default.currency'), true)</span>
</div> </div>
<div class="col-auto"> <div class="col-auto">
@ -33,9 +33,9 @@
</div> </div>
</div> </div>
<p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_profit') }}: {{ $total_profit['open'] }}<br>{{ trans('dashboard.overdue_profit') }}: {{ $total_profit['overdue'] }}" data-toggle="tooltip" data-html="true"> <p class="mt-3 mb-0 text-sm" title="{{ trans('dashboard.open_profit') }}: {{ $totals['open'] }}<br>{{ trans('dashboard.overdue_profit') }}: {{ $totals['overdue'] }}" data-toggle="tooltip" data-html="true">
<span class="text-white">{{ trans('general.upcoming') }}</span> <span class="text-white">{{ trans('general.upcoming') }}</span>
<span class="text-white font-weight-bold float-right">{{ $total_profit['open'] }} / {{ $total_profit['overdue'] }}</span> <span class="text-white font-weight-bold float-right">{{ $totals['open'] }} / {{ $totals['overdue'] }}</span>
</p> </p>
</div> </div>
</div> </div>