Skip to content

Instantly share code, notes, and snippets.

@jobcerto
Created May 11, 2018 19:35
Show Gist options
  • Save jobcerto/6ac6a05633b2fe8d98aeb097c0adb087 to your computer and use it in GitHub Desktop.
Save jobcerto/6ac6a05633b2fe8d98aeb097c0adb087 to your computer and use it in GitHub Desktop.
<?php
namespace App\Charts;
use App\Charts\ColorPallete;
use App\Solicitation;
use ConsoleTVs\Charts\Classes\Highcharts\Chart;
use Jenssegers\Date\Date;
class VolumePerSolicitationsType extends Chart
{
use ColorPallete;
/**
* Initializes the chart.
*
* @return void
*/
public function __construct()
{
parent::__construct();
$this->render();
}
public function render()
{
$solicitations = $this->solicitations()
->mapWithKeys(function ($collection, $type) {
return [$type => $this->groupedByMonth($collection)];
})->map(function ($collection) {
return $this->collectionOfMonth($collection);
});
$solicitations->each(function ($group, $type) {
$this->dataset($type, 'column', $group->values());
});
$this->labels($this->months());
$this->options([
'plotOptions' => [
'column' => [
'pointPadding' => 0.2,
'borderWidth' => 0,
],
],
]);
}
public function collectionOfMonth($collection)
{
$months = collect($this->months())->mapWithKeys(function ($month) {
return [$month => 0.00];
});
return $months->merge($collection);
}
public function groupedByMonth($collection)
{
return $collection->groupBy(function ($solicitation) {
return $solicitation->created_at->format('m');
})->mapWithKeys(function ($group, $month) {
return $this->solicitationsByMonth($group, $month);
});
}
public function solicitationsByMonth($group, $month)
{
return [$this->months($month) => $group->pluck('contracts')->flatten()->sum('price')];
}
public function solicitations()
{
return Solicitation::with('contracts')->whereHas('contracts')
->whereYear('created_at', date('Y'))
->orderBy('type')
->otherCurrentStatus('Convertida', 'Substituído', 'Cancelado')
->get()
->groupBy('label');
}
public function months($month = null)
{
$months = [
'JAN', 'FEV', 'MAR', 'ABR', 'MAI', 'JUN', 'JUL', 'AGO', 'SET', 'OUT', 'NOV', 'DEZ',
];
return $month ? array_get($months, (int) $month - 1) : $months;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment