Skip to content

Commit

Permalink
Merge pull request #363 from code4romania/362-ngo-admin-reports-imple…
Browse files Browse the repository at this point in the history
…ment-add-cases-in-monitoring-checkbox

Reports - fix interval filter and add cases in monitoring status
  • Loading branch information
gheorghelupu17 authored Nov 30, 2024
2 parents f583bb8 + 0e25ae5 commit 3a5097f
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 30 deletions.
10 changes: 10 additions & 0 deletions app/Actions/ExportReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ class ExportReport extends Action

protected bool | null $showMissingValues = false;

protected bool | null $addCasesInMonitoring = false;

protected function setUp(): void
{
parent::setUp();
Expand Down Expand Up @@ -58,13 +60,21 @@ public function setShowMissingValues(?bool $showMissingValues): self
return $this;
}

public function setAddCasesInMonitoring(?bool $addCasesInMonitoring): self
{
$this->addCasesInMonitoring = $addCasesInMonitoring;

return $this;
}

public function generateExport(): BinaryFileResponse
{
$service = new BeneficiariesV2();
$service->setReportType($this->reportType)
->setStartDate($this->startDate)
->setEndDate($this->endDate)
->setShowMissingValue($this->showMissingValues)
->setAddCasesInMonitoring($this->addCasesInMonitoring)
->composeReport();

$fileName = \sprintf('%s_%s_%s.xlsx', $this->startDate, $this->endDate, $this->reportType->value);
Expand Down
11 changes: 10 additions & 1 deletion app/Concerns/HasPermissions.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function hasAccessToNomenclature(): bool
return (bool) $this->permissions?->admin_permissions->contains(AdminPermission::CAN_CHANGE_NOMENCLATURE);
}

public function hasAccessToCommunity(): bool
public function hasAccessToCommunity()
{
if ($this->isAdmin()) {
return true;
Expand All @@ -87,4 +87,13 @@ public function canSearchBeneficiary(): bool

return $this->permissions?->case_permissions->contains(CasePermission::CAN_SEARCH_AND_COPY_CASES_IN_ALL_CENTERS);
}

public function hasAccessToReports(): bool
{
if ($this->isNgoAdmin()) {
return true;
}

return (bool) $this->permissions?->case_permissions->contains(CasePermission::HAS_ACCESS_TO_STATISTICS);
}
}
7 changes: 7 additions & 0 deletions app/Concerns/LogsActivityOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Concerns;

use App\Models\Activity;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

Expand All @@ -31,4 +32,10 @@ public function tapActivity(Activity $activity, string $eventName)
$activity->event = $activity->subject_type;
$activity->subject()->associate($this->beneficiary);
}

public function activity(): HasMany
{
return $this->hasMany(Activity::class, 'subject_id')
->where('subject_type', 'beneficiary');
}
}
47 changes: 39 additions & 8 deletions app/Filament/Organizations/Pages/ReportsPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,20 @@
use App\Enums\ReportType;
use App\Forms\Components\ReportTable;
use Filament\Forms;
use Filament\Forms\Components\Checkbox;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Select;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Infolists\Components\Section;
use Filament\Infolists\Contracts\HasInfolists;
use Filament\Infolists\Infolist;
use Filament\Pages\Concerns\InteractsWithFormActions;
use Filament\Pages\Page;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\View\View;
use Illuminate\Support\HtmlString;

class ReportsPage extends Page implements Forms\Contracts\HasForms, HasInfolists
{
Expand All @@ -37,6 +42,13 @@ class ReportsPage extends Page implements Forms\Contracts\HasForms, HasInfolists

public $show_missing_values;

public $add_cases_in_monitoring;

public static function canAccess(): bool
{
return auth()->user()->hasAccessToReports();
}

public static function getNavigationGroup(): ?string
{
return __('navigation.statistics._group');
Expand Down Expand Up @@ -69,22 +81,39 @@ protected function getFormSchema(): array
Forms\Components\Section::make()
->columns(4)
->schema([
Forms\Components\Select::make('report_type')
Select::make('report_type')
->key('report_type')
->label(__('report.labels.report_type'))
->columnSpan(2)
->options(ReportType::options())
->searchable(),

Forms\Components\DatePicker::make('start_date')
DatePicker::make('start_date')
->label(__('report.labels.start_date'))
->default(now()->startOfMonth()),
->default(now()->startOfMonth())
->maxDate(fn (Get $get) => $get('end_date') ? debug($get('end_date')) : now())
->live(),

Forms\Components\DatePicker::make('end_date')
DatePicker::make('end_date')
->label(__('report.labels.end_date'))
->default(now()),
->default(now())
->minDate(fn (Get $get) => $get('start_date') ?? null)
->maxDate(now())
->live(),

Checkbox::make('add_cases_in_monitoring')
->label(
new HtmlString(
\sprintf(
'<span class="heroicon heroicon-o-information-circle" title="%s">%s</span>',
__('report.helpers.add_cases_in_monitoring'),
__('report.labels.add_cases_in_monitoring'),
)
)
)
->columnSpan(2),

Forms\Components\Checkbox::make('show_missing_values')
Checkbox::make('show_missing_values')
->label(__('report.labels.show_missing_values'))
->default(true)
->columnSpan(2),
Expand All @@ -107,7 +136,8 @@ public function infolist(Infolist $infolist): Infolist
->setReportType($this->report_type)
->setStartDate($this->start_date)
->setEndDate($this->end_date)
->setShowMissingValues($this->show_missing_values),
->setShowMissingValues($this->show_missing_values)
->setAddCasesInMonitoring($this->add_cases_in_monitoring),
])
->schema([
$this->reportTable(),
Expand All @@ -121,7 +151,8 @@ public function reportTable(): ReportTable
->setReportType($this->report_type ? ReportType::tryFrom($this->report_type) : null)
->setStartDate($this->start_date)
->setEndDate($this->end_date)
->setShowMissingValue($this->show_missing_values);
->setShowMissingValue($this->show_missing_values)
->setAddCasesInMonitoring($this->add_cases_in_monitoring);
}

public function render(): View
Expand Down
17 changes: 10 additions & 7 deletions app/Forms/Components/ReportTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace App\Forms\Components;

use App\Enums\ReportType;
use App\Services\Reports\Beneficiaries;
use App\Services\Reports\BeneficiariesV2;
use Filament\Infolists\Components\Component;
use Illuminate\Support\Collection;
Expand All @@ -14,7 +13,7 @@ class ReportTable extends Component
{
protected string $view = 'forms.components.report-table';

protected Beneficiaries | BeneficiariesV2 $reportService;
protected BeneficiariesV2 $reportService;

protected ReportType | null $reportType = null;

Expand All @@ -24,6 +23,8 @@ class ReportTable extends Component

protected bool | null $showMissingValues = false;

protected bool | null $addCasesInMonitoring = false;

public static function make(string | null $id = null): static
{
$static = app(static::class, ['id' => $id]);
Expand All @@ -36,7 +37,6 @@ protected function setUp(): void
{
parent::setUp();
$this->reportService = new BeneficiariesV2();
// $this->reportService = new Beneficiaries();
}

public function setReportType(ReportType | string | null $reportType): self
Expand Down Expand Up @@ -71,14 +71,17 @@ public function setShowMissingValue(?bool $showMissingValue): self
return $this;
}

public function composeReport(): void
public function setAddCasesInMonitoring(?bool $addCasesInMonitoring): self
{
$this->reportService->composeReport();
$this->addCasesInMonitoring = $addCasesInMonitoring;
$this->reportService->setAddCasesInMonitoring($addCasesInMonitoring);

return $this;
}

public function getReportType(): ?ReportType
public function composeReport(): void
{
return $this->reportService->getReportType();
$this->reportService->composeReport();
}

public function getReportData(): Collection
Expand Down
95 changes: 82 additions & 13 deletions app/Services/Reports/BeneficiariesReports/BaseGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,45 @@

namespace App\Services\Reports\BeneficiariesReports;

use App\Enums\ActivityDescription;
use App\Enums\CaseStatus;
use App\Models\Beneficiary;
use Carbon\Carbon;
use DB;
use Illuminate\Database\Eloquent\Builder as EloquentBuilder;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;

abstract class BaseGenerator
{
protected string | null $startDate;
protected string | null $startDate = null;

protected string | null $endDate = null;

protected bool | null $showMissingValues = false;

protected bool | null $addCasesInMonitoring = false;

protected $query;

public function __construct()
{
$this->query = Beneficiary::query()
->leftJoin('close_files', 'beneficiaries.id', '=', 'close_files.beneficiary_id')
->toBase()
->selectRaw('COUNT(*) as total_cases');
}

public function setStartDate(?string $startDate): self
{
$this->startDate = $startDate;
$this->startDate = $startDate ? Carbon::parse($startDate)->startOfDay()->format('Y-m-d H:i:s') : null;

return $this;
}

public function setEndDate(?string $endDate): self
{
$this->endDate = $endDate;
$this->endDate = $endDate ?
Carbon::parse($endDate)->endOfDay()->format('Y-m-d H:i:s') :
Carbon::now()->endOfDay()->format('Y-m-d H:i:s');

return $this;
}
Expand All @@ -47,6 +54,13 @@ public function setShowMissingValues(?bool $showMissingValues): self
return $this;
}

public function setAddCasesInMonitoring(?bool $addCasesInMonitoring): self
{
$this->addCasesInMonitoring = $addCasesInMonitoring;

return $this;
}

public function getReportData(): Collection
{
$this->setSelectedFields();
Expand All @@ -56,6 +70,7 @@ public function getReportData(): Collection
->groupBy($this->getGroupBy());

return $this->query
->toBase()
->get();
}

Expand Down Expand Up @@ -89,16 +104,70 @@ public function addConditions(): void
}
}

if ($this->endDate) {
$this->query->where('beneficiaries.created_at', '<=', $this->endDate . ' 23:59:59');
}
$this->addDateConditions();
}

if ($this->startDate) {
$this->query->where(
fn (Builder $query) => $query->where('close_files.date', '>=', $this->startDate)
->orWhereNull('close_files.date')
public function addDateConditions(): void
{
$this->query
->when(
$this->startDate,
fn (EloquentBuilder $query) => $query
->whereHas(
'activity',
fn (EloquentBuilder $query) => $query
->where(
fn (EloquentBuilder $query) => $query->whereJsonContains('properties->attributes->status', CaseStatus::ACTIVE->value)
->when(
$this->addCasesInMonitoring,
fn (EloquentBuilder $query) => $query->orWhereJsonContains('properties->attributes->status', CaseStatus::MONITORED->value)
)
)
->where('created_at', '<=', $this->endDate)
->whereIn('activity_log.description', [ActivityDescription::CREATED->value, ActivityDescription::UPDATED->value])
->whereNotExists(
fn (Builder $subQuery) => $subQuery->select(DB::raw(1))
->from('activity_log as sublog')
->whereColumn('sublog.subject_id', 'activity_log.subject_id')
->whereIn('sublog.description', [
ActivityDescription::CREATED->value,
ActivityDescription::UPDATED->value,
])
->where('sublog.subject_type', 'beneficiary')
->whereColumn('sublog.created_at', '>', 'activity_log.created_at')
->whereJsonContainsKey('properties->attributes->status')
->where('sublog.created_at', '<=', $this->endDate)
)
)
->orWhereHas(
'activity',
fn (EloquentBuilder $query) => $query->whereJsonContains('properties->old->status', CaseStatus::ACTIVE->value)
->when(
$this->addCasesInMonitoring,
fn (EloquentBuilder $query) => $query->orWhereJsonContains('properties->old->status', CaseStatus::MONITORED->value)
)
->whereBetween('created_at', [$this->startDate, $this->endDate])
)
)
->when(
! $this->startDate,
fn (EloquentBuilder $query) => $query
->whereHas(
'activity',
fn (EloquentBuilder $query) => $query
->where(
fn (EloquentBuilder $query) => $query
->whereJsonContains('properties->attributes->status', CaseStatus::ACTIVE->value)
->when(
$this->addCasesInMonitoring,
fn (EloquentBuilder $query) => $query->orWhereJsonContains('properties->attributes->status', CaseStatus::MONITORED->value)
)
)
->whereIn('description', [ActivityDescription::CREATED->value, ActivityDescription::UPDATED->value])
->where('created_at', '<=', $this->endDate)
->where('subject_type', 'beneficiary')
)
);
}
}

public function addRelatedTables(): void
Expand Down
Loading

0 comments on commit 3a5097f

Please sign in to comment.