138 lines
6.1 KiB
PHP
138 lines
6.1 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\Manage\Cooperations\Actions\Payment;
|
|
|
|
use App\Enums\ApprovalStatus;
|
|
use App\Enums\CooperationStatus;
|
|
use App\Enums\RoleEnum;
|
|
use App\Filament\Resources\Manage\Cooperations\CooperationResource;
|
|
use App\Filament\Support\CheerfulNotification;
|
|
use App\Models\CooperationPayment;
|
|
use App\Models\User;
|
|
use App\Notifications\BroadcastNotification;
|
|
use Asmit\FilamentUpload\Forms\Components\AdvancedFileUpload;
|
|
use Filament\Actions\Action;
|
|
use Filament\Actions\CreateAction;
|
|
use Filament\Forms\Components\Textarea;
|
|
use Filament\Forms\Components\TextInput;
|
|
use Filament\Support\Enums\Width;
|
|
use Filament\Support\RawJs;
|
|
use Illuminate\Support\Facades\Storage;
|
|
|
|
class SubmitInvoiceAction extends CreateAction
|
|
{
|
|
public static function getDefaultName(): ?string
|
|
{
|
|
return 'submit_invoice';
|
|
}
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->label('Ajukan Pembayaran')
|
|
->modalWidth(Width::Large)
|
|
->modalHeading(fn () => CheerfulNotification::getByKey('cooperation.create_payment_title'))
|
|
->modalDescription(fn () => CheerfulNotification::getByKey('cooperation.create_payment_desc'))
|
|
->schema([
|
|
TextInput::make('amount')
|
|
->label('Nominal Pembayaran')
|
|
->placeholder('1,000,000')
|
|
->autocomplete(false)
|
|
->autofocus()
|
|
->required()
|
|
->mask(RawJs::make('$money($input)'))
|
|
->prefix('Rp'),
|
|
|
|
Textarea::make('description')
|
|
->label('Keterangan')
|
|
->placeholder('Silakan isi keterangan atau rujukan pembayaran...'),
|
|
|
|
AdvancedFileUpload::make('payment_attachment')
|
|
->label('Lampiran Penagihan')
|
|
->disk(config('filesystems.default'))
|
|
->acceptedFileTypes(['application/pdf'])
|
|
->maxSize(1024 * 10)
|
|
->directory(fn (): string => 'cooperations/payment-attachment/'.now()->toDateString())
|
|
->helperText('Format yang diterima: PDF. Ukuran maksimum: 10 MB.')
|
|
->required(),
|
|
])
|
|
->mutateDataUsing(function (array $data): array {
|
|
$cooperation = $this->getLivewire()->getOwnerRecord();
|
|
|
|
if (auth()->user()->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
|
$cooperationMedia = $cooperation->cooperationMedia()
|
|
->whereHas('partnerMedia.company', function ($query) {
|
|
$query->where('user_id', auth()->id());
|
|
})
|
|
->first();
|
|
|
|
$data['cooperation_media_id'] = $cooperationMedia?->id;
|
|
$data['amount'] = str_replace(['Rp ', ' ', ','], '', $data['amount']);
|
|
}
|
|
|
|
return $data;
|
|
})
|
|
->successNotification(null)
|
|
->after(function (CooperationPayment $record, array $data): void {
|
|
if (! empty($data['payment_attachment'])) {
|
|
$filePath = collect($data['payment_attachment'])->first();
|
|
$fullPath = Storage::disk(config('filesystems.default'))->path($filePath);
|
|
|
|
if (file_exists($fullPath)) {
|
|
$record->addMediaFromDisk($filePath, config('filesystems.default'))
|
|
->preservingOriginal()
|
|
->withCustomProperties([
|
|
'feature' => 'cooperations',
|
|
'date' => now()->toDateString(),
|
|
'doc_type' => 'payment-attachment',
|
|
])
|
|
->toMediaCollection('cooperations');
|
|
}
|
|
}
|
|
|
|
CheerfulNotification::success(
|
|
CheerfulNotification::getByKey('cooperation.payment_requested_success'),
|
|
CheerfulNotification::getByKey('cooperation.payment_requested_desc')
|
|
)->send();
|
|
|
|
// Notify Admins
|
|
User::query()
|
|
->superAdmin()
|
|
->get()
|
|
->each(function ($admin) use ($record): void {
|
|
$user = auth()->user();
|
|
|
|
$admin->notify(new BroadcastNotification([
|
|
'title' => CheerfulNotification::getByKey('cooperation.create_payment_title'),
|
|
'body' => CheerfulNotification::getByKey('notification.payment_requested', ['user__name' => $user->name, 'record__partnerMedia__name' => $record->cooperationMedia->partnerMedia->name, 'record__cooperation__title' => $this->getLivewire()->getOwnerRecord()->title]),
|
|
'action' => [
|
|
Action::make('view')
|
|
->label('Lihat')
|
|
->url(CooperationResource::getUrl('view', ['record' => $this->getLivewire()->getOwnerRecord()->id, 'relation' => 4])),
|
|
],
|
|
]));
|
|
});
|
|
})
|
|
->visible(function (): bool {
|
|
$user = auth()->user();
|
|
|
|
if (! $user || ! $user->hasRole(RoleEnum::PERUSAHAAN->value)) {
|
|
return false;
|
|
}
|
|
|
|
$cooperation = $this->getLivewire()->getOwnerRecord();
|
|
|
|
return $cooperation->status === CooperationStatus::PAYMENT
|
|
&& $cooperation->proposal()
|
|
->whereHas('partnerMedia.company', fn ($q) => $q->where('user_id', auth()->id()))
|
|
->accepted()
|
|
->exists()
|
|
&& ! $cooperation->payments()
|
|
->whereHas('cooperationMedia.partnerMedia.company', fn ($q) => $q->where('user_id', auth()->id()))
|
|
->where('cooperation_payments.status', '!=', ApprovalStatus::REJECTED)
|
|
->exists();
|
|
});
|
|
}
|
|
}
|