Skip to content

Commit afc1adc

Browse files
committed
modul pekerjaan PMI
1 parent 1322f3f commit afc1adc

File tree

15 files changed

+772
-115
lines changed

15 files changed

+772
-115
lines changed

app/Exports/PekerjaanPmiExport.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace App\Exports;
4+
5+
use Maatwebsite\Excel\Concerns\FromCollection;
6+
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
7+
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
8+
use Maatwebsite\Excel\Concerns\WithHeadings;
9+
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
10+
11+
class PekerjaanPmiExport implements FromCollection, WithHeadings, ShouldAutoSize, WithColumnFormatting
12+
{
13+
protected $data;
14+
15+
public function __construct($data)
16+
{
17+
$this->data = $data;
18+
}
19+
20+
public function collection()
21+
{
22+
return $this->data->map(function ($item, $index) {
23+
return [
24+
'no' => $index + 1, // Menambahkan nomor urut berdasarkan index
25+
'nama_pekerjaan' => $item->nama,
26+
];
27+
});
28+
}
29+
30+
public function headings(): array
31+
{
32+
return [
33+
'No',
34+
'Nama Pekerjaan PMI',
35+
];
36+
}
37+
38+
public function columnFormats(): array
39+
{
40+
return [
41+
'A' => NumberFormat::FORMAT_NUMBER,
42+
'B' => NumberFormat::FORMAT_TEXT,
43+
];
44+
}
45+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Admin;
4+
5+
use App\Exports\PekerjaanPmiExport;
6+
use App\Http\Controllers\Controller;
7+
use App\Http\Requests\PekerjaanPmiRequest;
8+
use App\Imports\PekerjaanPmiImport;
9+
use App\Models\PekerjaanPmi;
10+
use Illuminate\Http\Request;
11+
use Illuminate\Support\Facades\Storage;
12+
use Maatwebsite\Excel\Facades\Excel;
13+
use Yajra\DataTables\DataTables;
14+
15+
class PekerjaanPmiController extends Controller
16+
{
17+
public function index(Request $request)
18+
{
19+
if ($request->excel) {
20+
$paramDatatable = json_decode($request->get('params'), 1);
21+
$request->merge($paramDatatable);
22+
}
23+
24+
if ($request->ajax() || $request->excel) {
25+
$query = DataTables::of(PekerjaanPmi::query());
26+
if ($request->excel) {
27+
$query->filtering();
28+
29+
return Excel::download(new PekerjaanPmiExport($query->results()), 'Data-Pekerjaan-PMI.xlsx');
30+
}
31+
32+
return $query->addIndexColumn()
33+
->addColumn('action', function ($data) {
34+
$edit = '<a href="' . route('pekerjaan-pmi.edit', $data->id) . '" class="btn btn-sm btn-warning btn-sm"><i class="fas fa-pencil-alt"></i></a>';
35+
$delete = '<button data-href="' . route('pekerjaan-pmi.destroy', $data->id) . '" class="btn btn-sm btn-danger" data-toggle="modal" data-target="#confirm-delete"><i class="fas fa-trash"></i></button>';
36+
37+
return '<div class="btn btn-group">' . $edit . $delete . '</div>';
38+
})
39+
->rawColumns(['action'])
40+
->make(true);
41+
}
42+
43+
return view('admin.pekerjaan-pmi.index');
44+
}
45+
46+
public function create()
47+
{
48+
return view('admin.pekerjaan-pmi.create');
49+
}
50+
51+
public function store(PekerjaanPmiRequest $request)
52+
{
53+
$input = $request->all();
54+
55+
if (PekerjaanPmi::create($input)) {
56+
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil disimpan');
57+
}
58+
59+
return back()->with('error', 'Data gagal disimpan');
60+
}
61+
62+
public function edit($id)
63+
{
64+
return view('admin.pekerjaan-pmi.edit', [
65+
'pekerjaanPmi' => PekerjaanPmi::findOrFail($id),
66+
]);
67+
}
68+
69+
public function update(PekerjaanPmiRequest $request, $id)
70+
{
71+
$input = $request->all();
72+
$pekerjaanPmi = PekerjaanPmi::findOrFail($id);
73+
74+
if ($pekerjaanPmi->update($input)) {
75+
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil diubah');
76+
}
77+
78+
return back()->with('error', 'Data gagal diubah');
79+
}
80+
81+
public function import()
82+
{
83+
return view('admin.pekerjaan-pmi.import');
84+
}
85+
86+
public function prosesImport(Request $request)
87+
{
88+
try {
89+
Excel::import(new PekerjaanPmiImport, $request->file('file')->store('temp'));
90+
} catch (\Exception $e) {
91+
report($e);
92+
93+
return back()->with('error', 'Data gagal diimport');
94+
}
95+
96+
// Hapus folder temp ketika sudah selesai
97+
Storage::deleteDirectory('temp');
98+
99+
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil diimport');
100+
}
101+
102+
public function contohImport()
103+
{
104+
$file = public_path('/assets/import/data_pekerjaan_pmi.xlsx');
105+
106+
return response()->download($file);
107+
}
108+
109+
public function destroy($id)
110+
{
111+
if (PekerjaanPmi::destroy($id)) {
112+
return redirect('pekerjaan-pmi')->with('success', 'Data berhasil dihapus');
113+
}
114+
115+
return redirect('pekerjaan-pmi')->with('error', 'Data gagal dihapus');
116+
}
117+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace App\Http\Controllers\Api;
4+
5+
use App\Http\Controllers\Controller;
6+
use App\Models\PekerjaanPmi;
7+
use Illuminate\Http\Request;
8+
9+
class PekerjaanPmiController extends Controller
10+
{
11+
public function index(Request $request)
12+
{
13+
$search = $request->get('q');
14+
$pekerjaanPmi = PekerjaanPmi::selectRaw('id, nama, nama as text')
15+
->when($search, static fn($q) => $q->where('nama', 'like', "%{$search}%"))
16+
->paginate();
17+
18+
return response()->json([
19+
'results' => $pekerjaanPmi->items(),
20+
'pagination' => [
21+
'more' => $pekerjaanPmi->currentPage() < $pekerjaanPmi->lastPage(),
22+
],
23+
]);
24+
}
25+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace App\Http\Requests;
4+
5+
use Illuminate\Foundation\Http\FormRequest;
6+
7+
class PekerjaanPmiRequest extends FormRequest
8+
{
9+
/**
10+
* Determine if the user is authorized to make this request.
11+
*/
12+
public function authorize(): bool
13+
{
14+
return true;
15+
}
16+
17+
/**
18+
* Get the validation rules that apply to the request.
19+
*
20+
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
21+
*/
22+
public function rules(): array
23+
{
24+
return [
25+
'nama' => 'required|string|max:255',
26+
];
27+
}
28+
}

app/Imports/PekerjaanPmiImport.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace App\Imports;
4+
5+
use App\Models\PekerjaanPmi;
6+
use Maatwebsite\Excel\Concerns\ToModel;
7+
use Maatwebsite\Excel\Concerns\WithBatchInserts;
8+
use Maatwebsite\Excel\Concerns\WithHeadingRow;
9+
use Maatwebsite\Excel\Concerns\WithUpserts;
10+
11+
class PekerjaanPmiImport implements ToModel, WithBatchInserts, WithUpserts, WithHeadingRow
12+
{
13+
public function __construct()
14+
{
15+
ini_set('memory_limit', '-1');
16+
}
17+
18+
/**
19+
* @param array $row
20+
*
21+
* @return \Illuminate\Database\Eloquent\Model|null
22+
*/
23+
public function model(array $row)
24+
{
25+
return new PekerjaanPmi([
26+
'nama' => $row['nama_pekerjaan'] ?? $row['nama'],
27+
]);
28+
}
29+
30+
public function batchSize(): int
31+
{
32+
return 1000;
33+
}
34+
35+
public function uniqueBy()
36+
{
37+
return 'nama';
38+
}
39+
}

app/Models/PekerjaanPmi.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace App\Models;
4+
5+
use Illuminate\Database\Eloquent\Factories\HasFactory;
6+
use Illuminate\Database\Eloquent\Model;
7+
8+
class PekerjaanPmi extends Model
9+
{
10+
use HasFactory;
11+
12+
protected $table = 'pekerjaan_pmi';
13+
14+
protected $fillable = [
15+
'nama',
16+
];
17+
18+
protected $dates = [
19+
'created_at',
20+
'updated_at',
21+
];
22+
}

0 commit comments

Comments
 (0)