akaunting/app/Widgets/IncomesByCategory.php

223 lines
6.7 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Widgets;
use Arrilot\Widgets\AbstractWidget;
use App\Models\Setting\Category;
use App\Utilities\Chartjs;
use Date;
class IncomesByCategory extends AbstractWidget
{
2019-12-28 22:30:10 +03:00
public $donut = ['colors' => [], 'labels' => [], 'values' => []];
2019-11-16 10:21:14 +03:00
/**
* The configuration array.
*
* @var array
*/
protected $config = [
2019-12-28 22:30:10 +03:00
'width' => 'col-md-6',
2019-11-16 10:21:14 +03:00
];
/**
* 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();
2019-11-16 10:21:14 +03:00
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
2019-11-16 10:21:14 +03:00
}
$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;
}
2019-12-28 22:30:10 +03:00
$this->addToDonut($category->color, $amount, $category->name);
2019-11-16 10:21:14 +03:00
}
// Show donut prorated if there is no income
2019-12-28 22:30:10 +03:00
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
2019-11-16 10:21:14 +03:00
}
}
// Get 6 categories by amount
$colors = $labels = [];
2019-12-28 22:30:10 +03:00
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
2019-11-16 10:21:14 +03:00
foreach ($values as $id => $val) {
2019-12-28 22:30:10 +03:00
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
2019-11-16 10:21:14 +03:00
}
2019-12-28 22:30:10 +03:00
$chart = new Chartjs();
2019-11-16 10:21:14 +03:00
2019-12-28 22:30:10 +03:00
$chart->type('doughnut')
2019-11-16 10:21:14 +03:00
->width(0)
->height(160)
2019-12-28 22:30:10 +03:00
->options($this->getChartOptions($colors))
2019-11-16 10:21:14 +03:00
->labels(array_values($labels));
2019-12-28 22:30:10 +03:00
$chart->dataset(trans_choice('general.incomes', 2), 'doughnut', array_values($values))
2019-11-16 10:21:14 +03:00
->backgroundColor(array_values($colors));
return view('widgets.incomes_by_category', [
'config' => (object) $this->config,
2019-12-28 22:30:10 +03:00
'chart' => $chart,
2019-11-16 10:21:14 +03:00
]);
}
public function getData()
{
//
$incomes_amount = $open_invoice = $overdue_invoice = 0;
// Get categories
$categories = Category::with(['invoices', 'income_transacions'])->type(['income'])->enabled()->get();
2019-11-16 10:21:14 +03:00
foreach ($categories as $category) {
$amount = 0;
// Transactions
foreach ($category->income_transacions as $transacion) {
$amount += $transacion->getAmountConvertedToDefault();
2019-11-16 10:21:14 +03:00
}
$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;
}
2019-12-28 22:30:10 +03:00
$this->addToDonut($category->color, $amount, $category->name);
2019-11-16 10:21:14 +03:00
}
// Show donut prorated if there is no income
2019-12-28 22:30:10 +03:00
if (array_sum($this->donut['values']) == 0) {
foreach ($this->donut['values'] as $key => $value) {
$this->donut['values'][$key] = 1;
2019-11-16 10:21:14 +03:00
}
}
// Get 6 categories by amount
$colors = $labels = [];
2019-12-28 22:30:10 +03:00
$values = collect($this->donut['values'])->sort()->reverse()->take(6)->all();
2019-11-16 10:21:14 +03:00
foreach ($values as $id => $val) {
2019-12-28 22:30:10 +03:00
$colors[$id] = $this->donut['colors'][$id];
$labels[$id] = $this->donut['labels'][$id];
2019-11-16 10:21:14 +03:00
}
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];
}
2019-12-28 22:30:10 +03:00
private function addToDonut($color, $amount, $text)
2019-11-16 10:21:14 +03:00
{
2019-12-28 22:30:10 +03:00
$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,
'ticks' => [
'display' => false,
],
'gridLines' => [
'drawBorder' => false,
'zeroLineColor' => 'transparent',
'color' => 'rgba(255,255,255,0.05)',
],
],
'xAxes' => [
'display' => 0,
'barPercentage' => 1.6,
'ticks' => [
'display' => false,
],
'gridLines' => [
'drawBorder' => false,
'color' => 'rgba(255,255,255,0.1)',
'zeroLineColor' => 'transparent',
],
],
],
];
2019-11-16 10:21:14 +03:00
}
}