214 lines
9.1 KiB
PHP
214 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Manage\Cooperations\RelationManagers;
|
|
|
|
use App\Enums\ApprovalStatus;
|
|
use App\Enums\RoleEnum;
|
|
use App\Filament\Columns\RowIndexColumn;
|
|
use App\Filament\Resources\Manage\Cooperations\Actions\Proposal\AcceptAction;
|
|
use App\Filament\Resources\Manage\Cooperations\Actions\Proposal\RejectAction;
|
|
use App\Filament\Support\CheerfulNotification;
|
|
use App\Models\CooperationProposal;
|
|
use Filament\Actions\BulkActionGroup;
|
|
use Filament\Actions\DeleteBulkAction;
|
|
use Filament\Actions\ViewAction;
|
|
use Filament\Infolists\Components\TextEntry;
|
|
use Filament\Resources\RelationManagers\RelationManager;
|
|
use Filament\Schemas\Components\Grid;
|
|
use Filament\Schemas\Schema;
|
|
use Filament\Support\Icons\Heroicon;
|
|
use Filament\Tables\Columns\TextColumn;
|
|
use Filament\Tables\Table;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Joaopaulolndev\FilamentPdfViewer\Infolists\Components\PdfViewerEntry;
|
|
|
|
class CooperationProposalRelationManager extends RelationManager
|
|
{
|
|
protected static string $relationship = 'proposal';
|
|
|
|
protected static ?string $title = 'Proposal';
|
|
|
|
public static function canViewForRecord(Model $ownerRecord, string $pageClass): bool
|
|
{
|
|
if (auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
|
return $ownerRecord->cooperationMedia()
|
|
->whereHas('partnerMedia.company', function ($query) {
|
|
$query->where('user_id', auth()->id());
|
|
})
|
|
->accepted()
|
|
->exists();
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
public static function getBadge(Model $ownerRecord, string $pageClass): ?string
|
|
{
|
|
if (auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
|
return null;
|
|
}
|
|
|
|
return (string) $ownerRecord->proposal()->count();
|
|
}
|
|
|
|
public function table(Table $table): Table
|
|
{
|
|
return $table
|
|
->columns([
|
|
...RowIndexColumn::make(),
|
|
|
|
TextColumn::make('partnerMedia.name')
|
|
->label('Media')
|
|
->searchable()
|
|
->visible(! auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)),
|
|
|
|
TextColumn::make('e_catalog')
|
|
->label('E-Catalog')
|
|
->url(fn (CooperationProposal $record): ?string => $record->e_catalog)
|
|
->openUrlInNewTab()
|
|
->color('primary'),
|
|
|
|
TextColumn::make('description')
|
|
->label('Deskripsi'),
|
|
|
|
TextColumn::make('status')
|
|
->label('Status')
|
|
->badge(),
|
|
|
|
TextColumn::make('submitted_at')
|
|
->label('Diajukan')
|
|
->dateTime('l, d F Y H:i:s')
|
|
->sortable(),
|
|
|
|
TextColumn::make('responded_at')
|
|
->label('Ditanggapi')
|
|
->dateTime('l, d F Y H:i:s')
|
|
->sortable(),
|
|
|
|
TextColumn::make('rejectionReasons.reason')
|
|
->label('Alasan Penolakan')
|
|
->searchable(),
|
|
])
|
|
->recordActions([
|
|
ViewAction::make(),
|
|
|
|
AcceptAction::make(),
|
|
|
|
RejectAction::make(),
|
|
])
|
|
->toolbarActions([
|
|
BulkActionGroup::make([
|
|
DeleteBulkAction::make()
|
|
->successNotification(
|
|
fn () => CheerfulNotification::success(
|
|
CheerfulNotification::getByKey('cooperation.proposal_deleted'),
|
|
CheerfulNotification::getByKey('cooperation.proposal_deleted_desc')
|
|
)
|
|
),
|
|
]),
|
|
])
|
|
->emptyStateIcon(Heroicon::OutlinedDocumentText)
|
|
->emptyStateHeading(fn () => CheerfulNotification::getByKey('cooperation.proposal_empty_heading'))
|
|
->emptyStateDescription(fn () => CheerfulNotification::getByKey('cooperation.proposal_empty'))
|
|
->defaultSort('created_at', 'desc')
|
|
->modifyQueryUsing(function (Builder $query): void {
|
|
$query->when(auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value), function (Builder $subQuery): void {
|
|
$subQuery->whereHas('partnerMedia.company', function (Builder $q): void {
|
|
$q->where('user_id', auth()->id());
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|
|
public function infolist(Schema $infolist): Schema
|
|
{
|
|
return $infolist
|
|
->components([
|
|
Grid::make(2)
|
|
->schema([
|
|
TextEntry::make('partnerMedia.name')
|
|
->label('Media')
|
|
->visible(! auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)),
|
|
|
|
TextEntry::make('status')
|
|
->label('Status')
|
|
->badge(),
|
|
|
|
TextEntry::make('submitted_at')
|
|
->label('Diajukan')
|
|
->dateTime('l, d F Y H:i:s'),
|
|
|
|
TextEntry::make('responded_at')
|
|
->label('Ditanggapi')
|
|
->dateTime('l, d F Y H:i:s'),
|
|
]),
|
|
|
|
TextEntry::make('e_catalog')
|
|
->label('E-Catalog')
|
|
->url(fn (CooperationProposal $record): ?string => $record->e_catalog)
|
|
->openUrlInNewTab()
|
|
->color('primary')
|
|
->visible(fn (CooperationProposal $record): ?string => $record->e_catalog),
|
|
|
|
TextEntry::make('description')
|
|
->label('Deskripsi')
|
|
->columnSpanFull(),
|
|
|
|
PdfViewerEntry::make('proposal_attachment_pdf')
|
|
->label('Lampiran Proposal')
|
|
->visible(function (CooperationProposal $record): bool {
|
|
$media = $record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-attachment')->sortByDesc('created_at')->first();
|
|
|
|
return $media && str_contains($media->mime_type ?? '', 'pdf');
|
|
})
|
|
->getStateUsing(fn (CooperationProposal $record): ?string => optional($record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-attachment')->sortByDesc('created_at')->first())->getUrl())
|
|
->disk(config('filesystems.default')),
|
|
|
|
TextEntry::make('proposal_attachment_image')
|
|
->label('Lampiran Proposal')
|
|
->html()
|
|
->visible(function (CooperationProposal $record): bool {
|
|
$media = $record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-attachment')->sortByDesc('created_at')->first();
|
|
|
|
return $media && str_contains($media->mime_type ?? '', 'image');
|
|
})
|
|
->getStateUsing(function (CooperationProposal $record): string {
|
|
$media = $record->getMedia('cooperations')->where('custom_properties.doc_type', 'proposal-attachment')->sortByDesc('created_at')->first();
|
|
|
|
if (! $media) {
|
|
return '';
|
|
}
|
|
|
|
$url = $media->getUrl();
|
|
$downloadUrl = $url;
|
|
|
|
if (in_array($media->disk, ['s3', 'minio'])) {
|
|
$downloadUrl = $media->getTemporaryUrl(now()->addMinutes(30), '', [
|
|
'ResponseContentDisposition' => 'attachment; filename="'.$media->file_name.'"',
|
|
]);
|
|
}
|
|
|
|
$downloadLink = '<a href="'.$downloadUrl.'" download="dokumen" class="mt-2 flex items-center gap-1 text-sm font-medium text-primary-600 transition hover:text-primary-500">
|
|
<svg class="h-4 w-4" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor">
|
|
<path stroke-linecap="round" stroke-linejoin="round" d="M3 16.5v2.25A2.25 2.25 0 005.25 21h13.5A2.25 2.25 0 0021 18.75V16.5M16.5 12L12 16.5m0 0L7.5 12m4.5 4.5V3" />
|
|
</svg>
|
|
Unduh Dokumen
|
|
</a>';
|
|
|
|
return '<div class="flex flex-col">'.
|
|
'<img src="'.$url.'" alt="Dokumen" style="max-width: 100%; height: auto; border-radius: 0.5rem;" />'.
|
|
$downloadLink.
|
|
'</div>';
|
|
}),
|
|
|
|
TextEntry::make('rejectionReasons.reason')
|
|
->label('Alasan Penolakan')
|
|
->listWithLineBreaks()
|
|
->visible(fn (CooperationProposal $record): bool => $record->status === ApprovalStatus::REJECTED && $record->rejectionReasons->isNotEmpty())
|
|
->columnSpanFull(),
|
|
])
|
|
->columns(1);
|
|
}
|
|
}
|