refactor(cooperation): change relation to task assignment from has many to has one and adjust related logic
This commit is contained in:
parent
627cd694d7
commit
f038da6735
@ -153,7 +153,7 @@ ### Cooperation
|
|||||||
- `partnerMedia()`: BelongsToMany - Media yang terlibat dalam kerjasama
|
- `partnerMedia()`: BelongsToMany - Media yang terlibat dalam kerjasama
|
||||||
- `cooperationMedia()`: HasMany - Pivot records dengan status
|
- `cooperationMedia()`: HasMany - Pivot records dengan status
|
||||||
- `cooperationProposals()`: HasMany - Proposal yang diupload media
|
- `cooperationProposals()`: HasMany - Proposal yang diupload media
|
||||||
- `taskAssignments()`: HasMany - Task yang diberikan ke media
|
- `taskAssignment()`: HasOne - Task yang diberikan ke media
|
||||||
|
|
||||||
### CooperationMedia (Pivot)
|
### CooperationMedia (Pivot)
|
||||||
**Location**: `app/Models/CooperationMedia.php`
|
**Location**: `app/Models/CooperationMedia.php`
|
||||||
|
|||||||
@ -63,7 +63,7 @@ protected function setUp(): void
|
|||||||
];
|
];
|
||||||
})
|
})
|
||||||
->action(function (array $data, Cooperation $record): void {
|
->action(function (array $data, Cooperation $record): void {
|
||||||
$taskAssignment = $record->taskAssignments()->create([
|
$taskAssignment = $record->taskAssignment()->create([
|
||||||
'start_date' => $data['start_date'],
|
'start_date' => $data['start_date'],
|
||||||
'end_date' => $data['end_date'],
|
'end_date' => $data['end_date'],
|
||||||
'task_description' => $data['task_description'],
|
'task_description' => $data['task_description'],
|
||||||
@ -107,7 +107,7 @@ protected function setUp(): void
|
|||||||
$user = Auth::user();
|
$user = Auth::user();
|
||||||
|
|
||||||
return $user && ! $user->hasRole('Perusahaan')
|
return $user && ! $user->hasRole('Perusahaan')
|
||||||
&& ! $record->taskAssignments()->exists()
|
&& ! $record->taskAssignment()->exists()
|
||||||
&& $record->status === CooperationStatus::ASSIGNMENT;
|
&& $record->status === CooperationStatus::ASSIGNMENT;
|
||||||
})
|
})
|
||||||
->modalWidth(Width::Large);
|
->modalWidth(Width::Large);
|
||||||
|
|||||||
@ -179,7 +179,7 @@ protected function getProposalReviewDate($record)
|
|||||||
|
|
||||||
protected function getTaskStatus($record): string
|
protected function getTaskStatus($record): string
|
||||||
{
|
{
|
||||||
if ($record->taskAssignments->isNotEmpty()) {
|
if ($record->taskAssignment()->exists()) {
|
||||||
return 'completed';
|
return 'completed';
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -188,21 +188,19 @@ protected function getTaskStatus($record): string
|
|||||||
|
|
||||||
protected function getTaskDate($record)
|
protected function getTaskDate($record)
|
||||||
{
|
{
|
||||||
$latestTask = $record->taskAssignments->sortByDesc('created_at')->first();
|
return $record->taskAssignment?->created_at?->format('d/m/Y H:i');
|
||||||
|
|
||||||
return $latestTask?->created_at?->format('d/m/Y H:i');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function getReportStatus($record): string
|
protected function getReportStatus($record): string
|
||||||
{
|
{
|
||||||
$tasks = $record->taskAssignments;
|
$task = $record->taskAssignment;
|
||||||
|
|
||||||
if ($tasks->isEmpty()) {
|
if (! $task) {
|
||||||
return 'pending';
|
return 'pending';
|
||||||
}
|
}
|
||||||
|
|
||||||
$totalReports = $tasks->sum('report_amount');
|
$totalReports = $task->report_amount;
|
||||||
$completedReports = $tasks->flatMap->reports->count();
|
$completedReports = $task->reports->count();
|
||||||
|
|
||||||
if ($completedReports >= $totalReports && $totalReports > 0) {
|
if ($completedReports >= $totalReports && $totalReports > 0) {
|
||||||
return 'completed';
|
return 'completed';
|
||||||
@ -217,8 +215,7 @@ protected function getReportStatus($record): string
|
|||||||
|
|
||||||
protected function getReportDate($record)
|
protected function getReportDate($record)
|
||||||
{
|
{
|
||||||
$latestReport = $record->taskAssignments
|
$latestReport = $record->taskAssignment?->reports
|
||||||
->flatMap->reports
|
|
||||||
->sortByDesc('created_at')
|
->sortByDesc('created_at')
|
||||||
->first();
|
->first();
|
||||||
|
|
||||||
@ -251,14 +248,17 @@ protected function canMarkAsCompleted(): bool
|
|||||||
{
|
{
|
||||||
$record = $this->getRecord();
|
$record = $this->getRecord();
|
||||||
|
|
||||||
// Check if all tasks have required number of reports
|
$task = $record->taskAssignment;
|
||||||
foreach ($record->taskAssignments as $task) {
|
|
||||||
$reportCount = $task->reports()->count();
|
if (! $task) {
|
||||||
if ($reportCount < $task->report_amount) {
|
return false;
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return $record->taskAssignments->isNotEmpty();
|
$reportCount = $task->reports()->count();
|
||||||
|
if ($reportCount < $task->report_amount) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -123,7 +123,7 @@ public function table(Table $table): Table
|
|||||||
->modalWidth(Width::Large)
|
->modalWidth(Width::Large)
|
||||||
->modalHeading('Buat Laporan')
|
->modalHeading('Buat Laporan')
|
||||||
->mutateDataUsing(function (array $data): array {
|
->mutateDataUsing(function (array $data): array {
|
||||||
$data['task_assignment_id'] = $this->getOwnerRecord()->taskAssignments()->first()?->id;
|
$data['task_assignment_id'] = $this->getOwnerRecord()->taskAssignment?->id;
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
})
|
})
|
||||||
@ -158,7 +158,7 @@ public function table(Table $table): Table
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$taskAssignment = $this->getOwnerRecord()->taskAssignments()->first();
|
$taskAssignment = $this->getOwnerRecord()->taskAssignment;
|
||||||
|
|
||||||
if (! $taskAssignment) {
|
if (! $taskAssignment) {
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@ -20,7 +20,7 @@
|
|||||||
|
|
||||||
class TaskAssignmentRelationManager extends RelationManager
|
class TaskAssignmentRelationManager extends RelationManager
|
||||||
{
|
{
|
||||||
protected static string $relationship = 'taskAssignments';
|
protected static string $relationship = 'taskAssignment';
|
||||||
|
|
||||||
protected static ?string $title = 'Penugasan';
|
protected static ?string $title = 'Penugasan';
|
||||||
|
|
||||||
@ -107,7 +107,8 @@ public function table(Table $table): Table
|
|||||||
->visible(function (): bool {
|
->visible(function (): bool {
|
||||||
$cooperation = $this->getOwnerRecord();
|
$cooperation = $this->getOwnerRecord();
|
||||||
|
|
||||||
return $cooperation->proposals()->accepted()->exists();
|
return $cooperation->proposals()->accepted()->exists()
|
||||||
|
&& ! $cooperation->taskAssignment()->exists();
|
||||||
})
|
})
|
||||||
->successNotification(
|
->successNotification(
|
||||||
Notification::make()
|
Notification::make()
|
||||||
|
|||||||
@ -10,6 +10,7 @@
|
|||||||
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||||
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
|
||||||
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||||
use Spatie\MediaLibrary\HasMedia;
|
use Spatie\MediaLibrary\HasMedia;
|
||||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||||
@ -68,9 +69,9 @@ public function proposals(): HasMany
|
|||||||
return $this->hasMany(CooperationProposal::class);
|
return $this->hasMany(CooperationProposal::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function taskAssignments(): HasMany
|
public function taskAssignment(): HasOne
|
||||||
{
|
{
|
||||||
return $this->hasMany(TaskAssignment::class);
|
return $this->hasOne(TaskAssignment::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function cooperationMedia(): HasMany
|
public function cooperationMedia(): HasMany
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user