Skip to content

Commit

Permalink
refactored widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
denisdulici committed Dec 29, 2019
1 parent e41b56a commit 6052506
Show file tree
Hide file tree
Showing 23 changed files with 438 additions and 928 deletions.
56 changes: 4 additions & 52 deletions app/Abstracts/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use App\Exports\Common\Reports as Export;
use App\Models\Common\Report as Model;
use App\Utilities\Chartjs;
use App\Traits\Charts;
use App\Traits\DateTime;
use App\Utilities\Chartjs;
use Date;
use Illuminate\Support\Str;

abstract class Report
{
use DateTime;
use Charts, DateTime;

public $report;

Expand Down Expand Up @@ -125,56 +126,7 @@ public function getChart()

$config = $this->chart[$this->report->chart];

$default_options = [
'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'
]
]
]
]
];
$default_options = $this->getLineChartOptions();

$options = array_merge($default_options, (array) $config['options']);

Expand Down
63 changes: 63 additions & 0 deletions app/Abstracts/Widget.php
Original file line number Diff line number Diff line change
@@ -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 changes: 140 additions & 0 deletions app/Traits/Charts.php
Original file line number Diff line number Diff line change
@@ -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',
],
]],
],
];
}
}
13 changes: 2 additions & 11 deletions app/Widgets/AccountBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,11 @@

namespace App\Widgets;

use Arrilot\Widgets\AbstractWidget;
use App\Abstracts\Widget;
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.
* Return view() or other content to display.
Expand Down

0 comments on commit 6052506

Please sign in to comment.