87 lines
2.4 KiB
PHP
87 lines
2.4 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\Projects;
|
|
|
|
use App\Jobs\ProjectReportGeneratorJob;
|
|
use App\Models\Project;
|
|
use App\Models\ProjectReport;
|
|
use App\Rules\AllMosaicsPresentRule;
|
|
use Livewire\Component;
|
|
|
|
class ReportManager extends Component
|
|
{
|
|
public $formData = [];
|
|
public $project_id;
|
|
|
|
public $showReportModal = false;
|
|
|
|
public $listeners = ['refresh' => '$refresh'];
|
|
|
|
public function openCreateReportModal()
|
|
{
|
|
$this->resetFormData();
|
|
$this->showReportModal = true;
|
|
}
|
|
|
|
public function mount(Project $project)
|
|
{
|
|
$this->project_id = $project->id;
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
$project = Project::find($this->project_id);
|
|
return view('livewire.projects.report-manager')->with(compact('project'));
|
|
}
|
|
|
|
private function resetFormData()
|
|
{
|
|
$this->formData['week'] = now()->weekOfYear;
|
|
$this->formData['year'] = now()->year;
|
|
}
|
|
|
|
public function saveProjectReport()
|
|
{
|
|
sleep(1);
|
|
$this->validate([
|
|
'formData.week' => ['required', 'integer', 'min:1', 'max:53' ],
|
|
'formData.year' => 'required|integer|min:2020|max:'.now()->addYear()->year,
|
|
'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',
|
|
]);
|
|
|
|
ProjectReportGeneratorJob::dispatch($newReport);
|
|
$this->dispatch('refresh');
|
|
|
|
$this->showReportModal = false;
|
|
|
|
}
|
|
|
|
public function getDateRangeProperty()
|
|
{
|
|
if (empty($this->formData['week']) || strlen($this->formData['year']) !== 4) {
|
|
return '<span class="text-red-500">Invalid week or year</span>';
|
|
}
|
|
$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');
|
|
|
|
}
|
|
|
|
public function deleteReport(ProjectReport $report) {
|
|
$report->deleteMe();
|
|
$this->dispatch('refresh');
|
|
}
|
|
}
|