[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()
{
// TODO check the changements due to migration
$this->projectReport->weeksAgo();
//$this->projectReport->weeksAgo();
$projectFolder = base_path('../');

View file

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

View file

@ -3,12 +3,8 @@
namespace App\Models;
use App\Events\ProjectReportStatus;
use App\Jobs\ProjectDownloadTiffJob;
use App\Jobs\ProjectMosiacGeneratorJob;
use App\Traits\HasStatus;
use Carbon\Carbon;
use Carbon\CarbonPeriod;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\File;
@ -16,17 +12,14 @@ class ProjectReport extends Model
{
use HasStatus;
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()
{
return $this->belongsTo(Project::class);
}
public function getFileNameAttribute()
public function getFileNameAttribute(): string
{
return static::getFileName($this->end_date,$this->offset);
}
@ -35,19 +28,20 @@ public static function getFileName(Carbon $endDate, int $offset): string
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()
{
return storage_path('app/'.$this->project->download_path.'/'.$this->path);
}
public function getReportDate()
public function getReportDate():string
{
// TODO remove year and week
return self::getReportDateForYearAndWeek(
$this->project,
$this->year,
$this->week
)->toDateString();
return $this->end_date->format('Y-m-d');
}
public static function getReportDateForYearAndWeek(Project $project, $year, $week)

View file

@ -16,13 +16,13 @@
<x-slot name="form">
<div class="col-span-6 sm:col-span-4">
<x-label for="year" value="{{ __('Year') }}"/>
<x-input id="year" type="text" class="mt-1 block w-full" wire:model.live="formData.year" autofocus/>
<x-input-error for="formData.year" class="mt-2"/>
<x-label for="end_date" value="{{ __('Date') }}"/>
<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.end_date" class="mt-2"/>
</div><div class="col-span-6 sm:col-span-4">
<x-label for="week" value="{{ __('Week') }}"/>
<x-input id="week" type="text" class="mt-1 block w-full" wire:model.live="formData.week" autofocus/>
<x-input-error for="formData.week" class="mt-2"/>
<x-label for="offset" value="{{ __('Offset') }}"/>
<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.offset" class="mt-2"/>
<x-input-error for="formData" class="mt-2"/>
</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>
<x-report-manager-properties-modal :reportManager="$this"/>
</div>

View file

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