Skip to content

Commit

Permalink
fix statistics (#374)
Browse files Browse the repository at this point in the history
* fix statistics

* fix: remove only total_amount global scope

* refactoring

---------

Co-authored-by: Andrei Ioniță <[email protected]>
  • Loading branch information
alexPopaCode4 and andreiio authored Jun 19, 2024
1 parent 0577cb9 commit d7562f1
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 42 deletions.
8 changes: 4 additions & 4 deletions app/Filament/Widgets/StatisticsCards.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@

use App\Models\Donation;
use App\Models\Project;
use DB;
use Filament\Widgets\StatsOverviewWidget;
use Filament\Widgets\StatsOverviewWidget\Card;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\DB;

class StatisticsCards extends StatsOverviewWidget
{
Expand All @@ -20,7 +20,7 @@ class StatisticsCards extends StatsOverviewWidget
*/
public function getLastEndDate(string $cardLabel): Carbon
{
if ($cardLabel == 'year') {
if ($cardLabel === 'year') {
return now()->subYear(2);
}

Expand All @@ -32,7 +32,7 @@ public function getLastEndDate(string $cardLabel): Carbon
*/
private function getCurrentEndDate(string $cardLabel): Carbon
{
if ($cardLabel == 'year') {
if ($cardLabel === 'year') {
return now()->subYear(1);
}

Expand All @@ -59,7 +59,7 @@ private function getProjectCard(string $cardLabel): Card
$color = $currentGrandThanLast ? 'success' : 'danger';
$icon = $currentGrandThanLast ? 'heroicon-s-trending-up' : 'heroicon-s-trending-down';

$description = $cardLabel == 'year' ?
$description = $cardLabel === 'year' ?
(
$currentGrandThanLast ?
__('statistics.labels.current_year_grand_than_last_year', ['number' => $currentAvg - $lastAvg]) :
Expand Down
31 changes: 13 additions & 18 deletions app/Filament/Widgets/StatisticsDonationsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@

namespace App\Filament\Widgets;

use App\Enums\EuPlatescStatus;
use App\Models\Donation;
use DB;
use Filament\Widgets\LineChartWidget;
use Illuminate\Support\Facades\DB;
use Tpetry\QueryExpressions\Function\Aggregate\Count;
use Tpetry\QueryExpressions\Language\Alias;

class StatisticsDonationsChart extends LineChartWidget
{
Expand All @@ -22,23 +23,17 @@ protected function getHeading(): ?string

protected function getData(): array
{
$selectedFields = 'count(*) as total';
$chartInterval = 'month';
$formatDate = "DATE_FORMAT(created_at,'%Y %m') as month";

$whereCreatedCondition = now()->subYear(1);
$interval = 'month';

$data = Donation::query()
->select(
[
DB::raw($selectedFields),
DB::raw($formatDate),
]
)
->where('created_at', '>', $whereCreatedCondition)
->whereIn('status', [EuPlatescStatus::CHARGED])
->groupBy($chartInterval)
->orderBy($chartInterval)
->select([
new Alias(new Count('*'), 'total'),
new Alias(DB::raw("DATE_FORMAT(created_at, '%Y %m')"), $interval),
])
->where('created_at', '>', now()->subYear(1))
->whereCharged()
->groupBy($interval)
->orderBy($interval)
->get();

return [
Expand All @@ -48,7 +43,7 @@ protected function getData(): array
'data' => $data->map(fn ($value) => $value->total),
],
],
'labels' => $data->map(fn ($value) => $value->$chartInterval),
'labels' => $data->map(fn ($value) => $value->$interval),
];
}
}
35 changes: 16 additions & 19 deletions app/Filament/Widgets/StatisticsProjectsChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace App\Filament\Widgets;

use App\Models\Project;
use DB;
use Filament\Widgets\LineChartWidget;
use Illuminate\Support\Facades\DB;
use Tpetry\QueryExpressions\Function\Aggregate\Count;
use Tpetry\QueryExpressions\Language\Alias;

class StatisticsProjectsChart extends LineChartWidget
{
Expand All @@ -21,28 +23,23 @@ protected function getHeading(): ?string

protected function getData(): array
{
$selectedFields = 'count(*) as total';
$chartInterval = 'month';
$formatDate = "DATE_FORMAT(created_at,'%Y %m') as month";

$whereCreatedCondition = now()->subYear(1);
$interval = 'month';

$data = Project::query()
->select(
[
DB::raw($selectedFields),
DB::raw($formatDate),
]
)
->where('created_at', '>', $whereCreatedCondition)
->without(['media',
->withoutGlobalScope('total_amount')
->select([
new Alias(new Count('*'), 'total'),
new Alias(DB::raw("DATE_FORMAT(created_at, '%Y %m')"), $interval),
])
->where('created_at', '>', now()->subYear(1))
->without([
'media',
'organization',
'donations',
'counties',
'categories',
'approvedDonations'])
->groupBy($chartInterval)
->orderBy($chartInterval)
])
->groupBy($interval)
->orderBy($interval)
->get();

return [
Expand All @@ -52,7 +49,7 @@ protected function getData(): array
'data' => $data->map(fn ($value) => $value->total),
],
],
'labels' => $data->map(fn ($value) => $value->$chartInterval),
'labels' => $data->map(fn ($value) => $value->$interval),
];
}
}
1 change: 0 additions & 1 deletion app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ class Project extends Model implements HasMedia
'organization',
'counties',
'categories',

];

protected static function booted()
Expand Down

0 comments on commit d7562f1

Please sign in to comment.