Skip to content

Commit e5f89e9

Browse files
Add ExportDaftarPulsa action for exporting pulsa data with validation
1 parent 52c964f commit e5f89e9

File tree

2 files changed

+76
-1
lines changed

2 files changed

+76
-1
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace App\Nova\Actions;
4+
5+
use App\Helpers\Helper;
6+
use Illuminate\Bus\Queueable;
7+
use Laravel\Nova\Fields\Text;
8+
use App\Models\DaftarPulsaMitra;
9+
use Laravel\Nova\Actions\Action;
10+
use Illuminate\Support\Collection;
11+
use Rap2hpoutre\FastExcel\FastExcel;
12+
use Laravel\Nova\Fields\ActionFields;
13+
use Illuminate\Support\Facades\Storage;
14+
use Illuminate\Queue\InteractsWithQueue;
15+
use Laravel\Nova\Http\Requests\NovaRequest;
16+
17+
class ExportDaftarPulsa extends Action
18+
{
19+
use InteractsWithQueue, Queueable;
20+
21+
public function name()
22+
{
23+
return 'Export Daftar Pulsa';
24+
}
25+
26+
/**
27+
* Perform the action on the given models.
28+
*
29+
* @return mixed
30+
*/
31+
public function handle(ActionFields $fields, Collection $models)
32+
{
33+
$model = $models->first();
34+
if (DaftarPulsaMitra::where('pulsa_kegiatan_id', $model->id)->where('confirmed', false)->count() > 0) {
35+
return Action::danger('Masih Ada Mitra yang belum mengkonfirmasi nomor HP');
36+
}
37+
$filename = $fields->filename.'.xlsx';
38+
(new FastExcel(DaftarPulsaMitra::where('pulsa_kegiatan_id', $model->id)->get()))->export(Storage::disk('temp')->path($filename), function ($pulsa) {
39+
return [
40+
'No HP' => optional(Helper::getMitraById($pulsa->mitra_id))->no_pulsa,
41+
'Nominal' => $pulsa->nominal,
42+
43+
];
44+
});
45+
46+
return Action::redirect(route('dump-download', [
47+
'filename' => $filename,
48+
]));
49+
}
50+
51+
/**
52+
* Get the fields available on the action.
53+
*
54+
* @return array
55+
*/
56+
public function fields(NovaRequest $request)
57+
{
58+
return [
59+
Text::make('Nama File', 'filename')
60+
->rules('required', 'regex:/^[a-zA-Z0-9_\-\s]+$/')
61+
->help('tanpa extensi file')
62+
->default(fn () => uniqid()),
63+
];
64+
}
65+
}

app/Nova/PulsaKegiatan.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use App\Helpers\Helper;
66
use App\Helpers\Policy;
7+
use App\Nova\Actions\ExportDaftarPulsa;
78
use App\Nova\Actions\SetStatus;
89
use Illuminate\Database\Eloquent\Model;
910
use Laravel\Nova\Fields\BelongsTo;
@@ -197,11 +198,20 @@ public function actions(NovaRequest $request)
197198

198199
return $this->resource instanceof Model && $this->resource->status !== 'open';
199200
});
201+
if (Policy::make()->allowedFor('ppk.pbj')->get()) {
202+
$actions[] =
203+
ExportDaftarPulsa::make()
204+
->showInline()
205+
->showOnDetail()
206+
->exceptOnIndex()
207+
->confirmButtonText('Unduh');
208+
}
200209

201210
return $actions;
202211

203212
}
204-
public function replicate()
213+
214+
public function replicate()
205215
{
206216
return tap(parent::replicate(), function ($resource) {
207217
$model = $resource->model();

0 commit comments

Comments
 (0)