[Done] Same changements as with the mosaic, use a range now.

This commit is contained in:
guillaume91 2024-06-19 15:51:46 +02:00
parent 0c1b247154
commit 64fcacb013
6 changed files with 80 additions and 81 deletions

View file

@ -36,7 +36,7 @@ public function __construct(ProjectReport $projectReport, $sendMail = false)
public function handle() public function handle()
{ {
// TODO check the changements due to migration // TODO check the changements due to migration
$this->projectReport->weeksAgo(); //$this->projectReport->weeksAgo();
$projectFolder = base_path('../'); $projectFolder = base_path('../');

View file

@ -6,6 +6,7 @@
use App\Models\Project; use App\Models\Project;
use App\Models\ProjectReport; use App\Models\ProjectReport;
use App\Rules\AllMosaicsPresentRule; use App\Rules\AllMosaicsPresentRule;
use Carbon\Carbon;
use Livewire\Attributes\Reactive; use Livewire\Attributes\Reactive;
use Livewire\Component; use Livewire\Component;
use Livewire\WithPagination; use Livewire\WithPagination;
@ -14,7 +15,10 @@ class Report extends Component
{ {
use WithPagination; use WithPagination;
public $formData = []; public $formData = [
'end_date' => '',
'offset' => 7
];
public Project $project; public Project $project;
public $search = ""; public $search = "";
@ -22,6 +26,11 @@ class Report extends Component
public $showReportModal = false; public $showReportModal = false;
public function mount()
{
$this->formData['end_date'] = Carbon::yesterday()->toDateString();
}
public function openCreateReportModal() public function openCreateReportModal()
{ {
$this->resetFormData(); $this->resetFormData();
@ -29,24 +38,24 @@ public function openCreateReportModal()
} }
private function resetFormData() private function resetFormData()
{ {
$this->formData['week'] = now()->weekOfYear; $this->formData['end_date'] = Carbon::yesterday()->toDateString();
$this->formData['year'] = now()->year; $this->formData['offset'] = 7;
} }
public function saveProjectReport() public function saveProjectReport()
{ {
$this->validate([ $this->validate([
'formData.week' => ['required', 'integer', 'min:1', 'max:53'], 'formData.end_date' => ['required','date','before:today'],
'formData.year' => 'required|integer|min:2020|max:'.now()->addYear()->year, 'formData.offset' => 'required|integer|min:1|max:1000',
'formData' => [new AllMosaicsPresentRule($this->project->id)], 'formData' => [new AllMosaicsPresentRule($this->project->id)],
]); ]);
$newReport = Project::find($this->project->id) $newReport = Project::find($this->project->id)
->reports()->create([ ->reports()->create([
'name' => 'Report for week '.$this->formData['week'].' of '.$this->formData['year'], 'name' => ProjectReport::projectReportNameFormat(new Carbon($this->formData['end_date']),(int) $this->formData['offset']),
'week' => $this->formData['week'], 'end_date' => $this->formData['end_date'],
'year' => $this->formData['year'], 'offset' => $this->formData['offset'],
'path' => 'reports/week_'.$this->formData['week'].'_'.$this->formData['year'].'.docx', 'path' => 'reports/'.ProjectReport::getFileName($this->formData['end_date'],$this->formData['offset']).'.docx',
]); ]);
ProjectReportGeneratorJob::dispatch($newReport); ProjectReportGeneratorJob::dispatch($newReport);
@ -58,17 +67,12 @@ public function saveProjectReport()
public function getDateRangeProperty() public function getDateRangeProperty()
{ {
if (empty($this->formData['week']) || strlen($this->formData['year']) !== 4) { if (!$this->formData['end_date'] || !$this->formData['offset']) {
return '<span class="text-red-500">Invalid week or year</span>'; return '<span class="text-red-500">Please give a correct date or offset</span>';
} }
// @TODO dit moet gecorrigeerd voor de project settings; $start = (new Carbon($this->formData['end_date']))->subDays($this->formData['offset']-1);
$begin = now() $end = new Carbon($this->formData['end_date']);
->setISODate($this->formData['year'], $this->formData['week']) return 'from '.$start->format('Y-m-d').' to '.$end->format('Y-m-d');
->startOfWeek();
$end = now()
->setISODate($this->formData['year'], $this->formData['week'])
->endOfWeek();
return $begin->format('Y-m-d').' - '.$end->format('Y-m-d');
} }
public function deleteReport(ProjectReport $report) public function deleteReport(ProjectReport $report)
@ -81,8 +85,8 @@ private function applySearch($query)
{ {
return $query->when($this->search !== '', function ($q){ return $query->when($this->search !== '', function ($q){
$q->where('name', 'like', '%'.$this->search.'%') $q->where('name', 'like', '%'.$this->search.'%')
->orWhere('year', 'like', '%'.$this->search.'%') ->orWhere('end_date', 'like', '%'.$this->search.'%')
->orWhere('week', 'like', '%'.$this->search.'%'); ->orWhere('offset', 'like', '%'.$this->search.'%');
}); });
} }
@ -92,8 +96,8 @@ public function render()
$query = Project::find($this->project->id) $query = Project::find($this->project->id)
->reports() ->reports()
->with('project') ->with('project')
->orderBy('year', 'desc') ->orderBy('end_date', 'desc')
->orderBy('week', 'desc'); ->orderBy('offset', 'desc');
$query = $this->applySearch($query); $query = $this->applySearch($query);
$reports = $query->paginate(10, pageName: 'reportPage'); $reports = $query->paginate(10, pageName: 'reportPage');

View file

@ -3,12 +3,8 @@
namespace App\Models; namespace App\Models;
use App\Events\ProjectReportStatus; use App\Events\ProjectReportStatus;
use App\Jobs\ProjectDownloadTiffJob;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Traits\HasStatus; use App\Traits\HasStatus;
use Carbon\Carbon; use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\File;
@ -16,38 +12,36 @@ class ProjectReport extends Model
{ {
use HasStatus; use HasStatus;
protected $casts = [ 'end_date' => 'datetime']; protected $casts = [ 'end_date' => 'datetime'];
protected $fillable = ['name', 'path', 'week', 'year','status', 'end_date']; protected $fillable = ['name', 'path', 'week', 'year','status', 'end_date','offset'];
public function project() public function project()
{ {
return $this->belongsTo(Project::class); return $this->belongsTo(Project::class);
} }
public function getFileNameAttribute() public function getFileNameAttribute(): string
{ {
return static::getFileName($this->end_date,$this->offset); return static::getFileName($this->end_date,$this->offset);
} }
public static function getFileName(Carbon $endDate, int $offset): string public static function getFileName(Carbon $endDate, int $offset): string
{ {
return 'period_'.$endDate->copy()->subDays($offset).'_'.$endDate; return 'period_'.$endDate->copy()->subDays($offset).'_'.$endDate;
} }
public static function projectReportNameFormat(Carbon $endDate,int $offset):string
{
return 'Report from '.$endDate->copy()->subDays($offset)->toDateString().' to '.$endDate->toDateString();
}
public function getFullPathName() public function getFullPathName()
{ {
return storage_path('app/'.$this->project->download_path.'/'.$this->path); return storage_path('app/'.$this->project->download_path.'/'.$this->path);
} }
public function getReportDate() public function getReportDate():string
{ {
// TODO remove year and week // TODO remove year and week
return self::getReportDateForYearAndWeek( return $this->end_date->format('Y-m-d');
$this->project,
$this->year,
$this->week
)->toDateString();
} }
public static function getReportDateForYearAndWeek(Project $project, $year, $week) public static function getReportDateForYearAndWeek(Project $project, $year, $week)

View file

@ -16,13 +16,13 @@
<x-slot name="form"> <x-slot name="form">
<div class="col-span-6 sm:col-span-4"> <div class="col-span-6 sm:col-span-4">
<x-label for="year" value="{{ __('Year') }}"/> <x-label for="end_date" value="{{ __('Date') }}"/>
<x-input id="year" type="text" class="mt-1 block w-full" wire:model.live="formData.year" autofocus/> <x-input id="end_date" type="date" max="{{\Carbon\Carbon::yesterday()->toDateString()}}" class="mt-1 block w-full" wire:model.live="formData.end_date" autofocus/>
<x-input-error for="formData.year" class="mt-2"/> <x-input-error for="formData.end_date" class="mt-2"/>
</div><div class="col-span-6 sm:col-span-4"> </div><div class="col-span-6 sm:col-span-4">
<x-label for="week" value="{{ __('Week') }}"/> <x-label for="offset" value="{{ __('Offset') }}"/>
<x-input id="week" type="text" class="mt-1 block w-full" wire:model.live="formData.week" autofocus/> <x-input id="offset" type="number" min="1" max="1000" placeholder="{{__('Number of days used to generate the mosaic (7 by default)')}}" class="mt-1 block w-full" wire:model.live="formData.offset" autofocus/>
<x-input-error for="formData.week" class="mt-2"/> <x-input-error for="formData.offset" class="mt-2"/>
<x-input-error for="formData" class="mt-2"/> <x-input-error for="formData" class="mt-2"/>
</div> </div>
<div> <div>

View file

@ -52,6 +52,7 @@ class="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6 lg
</div> </div>
</div> </div>
</div> </div>
<x-report-manager-properties-modal :reportManager="$this"/> <x-report-manager-properties-modal :reportManager="$this"/>
</div> </div>

View file

@ -21,28 +21,28 @@ protected function setUp(): void
parent::setUp(); // TODO: Change the autogenerated stub parent::setUp(); // TODO: Change the autogenerated stub
} }
/** // /**
* @test // * @test
* @dataProvider weeksAgoProvider // * @dataProvider weeksAgoProvider
*/ // */
public function it_can_calculatore_weeksAgo($year, $week, $expected) // public function it_can_calculatore_weeksAgo($endDate, $offset, $expected)
{ // {
Carbon::setTestNow(Carbon::now()->setISODate('2021', '41')); // Carbon::setTestNow(Carbon::now()->setISODate('2021', '41'));
$projectReport = ProjectReport::make([ // $projectReport = ProjectReport::make([
'year' => $year, // 'end_date' => $endDate,
'week' => $week, // 'offset' => $offset,
]); // ]);
$this->assertEquals($expected, $projectReport->weeksAgo()); // $this->assertEquals($expected, $projectReport->weeksAgo());
} // }
//
public static function weeksAgoProvider() // public static function weeksAgoProvider()
{ // {
return [ // return [
'1 week ago' => [2021, 40, 1], // '1 week ago' => [2021, 40, 1],
'2 weeks ago' => [2021, 39, 2], // '2 weeks ago' => [2021, 39, 2],
'3 weeks ago' => [2021, 38, 3], // '3 weeks ago' => [2021, 38, 3],
]; // ];
} // }
/** /**
* @test * @test
@ -55,8 +55,8 @@ public function it_can_get_the_full_path_name()
]); ]);
$projectReport = $project->reports()->create([ $projectReport = $project->reports()->create([
'name' => 'name', 'name' => 'name',
'year' => 2021, 'end_date' => new Carbon('2021-01-01'),
'week' => 41, 'offset' => 10,
'path' => 'path/doc.pdf', 'path' => 'path/doc.pdf',
]); ]);
$projectReport->setStatusSuccess(); $projectReport->setStatusSuccess();
@ -67,7 +67,7 @@ public function it_can_get_the_full_path_name()
* @test * @test
* @dataProvider reportDateProvider * @dataProvider reportDateProvider
*/ */
public function it_can_return_the_reportDate($expected, $mail_day, $week, $year) public function it_can_return_the_reportDate($expected, $mail_day, $endDate, $offset)
{ {
$project = Project::create([ $project = Project::create([
'name' => 'project_name', 'name' => 'project_name',
@ -76,8 +76,8 @@ public function it_can_return_the_reportDate($expected, $mail_day, $week, $year)
]); ]);
$projectReport = $project->reports()->create([ $projectReport = $project->reports()->create([
'name' => 'name', 'name' => 'name',
'year' => $year, 'end_date' => $endDate,
'week' => $week, 'offset' => $offset,
'path' => 'path/doc.pdf', 'path' => 'path/doc.pdf',
]); ]);
$projectReport->setStatusSuccess(); $projectReport->setStatusSuccess();
@ -88,13 +88,13 @@ public function it_can_return_the_reportDate($expected, $mail_day, $week, $year)
public static function reportDateProvider() public static function reportDateProvider()
{ {
return [ return [
'monday' => ['2023-12-10', 'monday', 50, 2023], 'monday' => ['2023-12-10', 'monday',new Carbon('2023-12-10'),10 ],
'tuesday' => ['2023-12-11', 'tuesday', 50, 2023], 'tuesday' => ['2023-12-11', 'tuesday',new Carbon('2023-12-11'),10 ],
'wednesday' => ['2023-12-12', 'wednesday', 50, 2023], 'wednesday' => ['2023-12-12', 'wednesday',new Carbon('2023-12-12'),10 ],
'thursday' => ['2023-12-13', 'thursday', 50, 2023], 'thursday' => ['2023-12-13', 'thursday',new Carbon('2023-12-13'),10 ],
'friday' => ['2023-12-14', 'friday', 50, 2023], 'friday' => ['2023-12-14', 'friday',new Carbon('2023-12-14'),10 ],
'saturday' => ['2023-12-15', 'saturday', 50, 2023], 'saturday' => ['2023-12-15', 'saturday',new Carbon('2023-12-15'),10 ],
'sunday' => ['2023-12-16', 'sunday', 50, 2023], 'sunday' => ['2023-12-16', 'sunday',new Carbon('2023-12-16'),10 ],
]; ];
} }