feat(webstatement): tambah fitur manajemen Jenis Kartu
- Menambahkan model, migrasi, seed, controller, request, dan tampilan untuk fitur Jenis Kartu. - Menambahkan routing dan breadcrumbs untuk Jenis Kartu. - Mengimplementasikan fungsi CRUD, ekspor data ke Excel, dan penghapusan multiple records pada Jenis Kartu. - Memperbarui `module.json` untuk menampilkan menu Jenis Kartu di bagian Master. - Menambah seeder untuk data awal Jenis Kartu. Signed-off-by: Daeng Deni Mardaeni <ddeni05@gmail.com>
This commit is contained in:
67
app/Exports/JenisKartuExport.php
Normal file
67
app/Exports/JenisKartuExport.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
|
||||
use Maatwebsite\Excel\Concerns\WithStyles;
|
||||
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
|
||||
use Modules\Webstatement\Models\JenisKartu;
|
||||
|
||||
class JenisKartuExport implements FromCollection, WithHeadings, WithMapping, ShouldAutoSize, WithStyles
|
||||
{
|
||||
/**
|
||||
* @return \Illuminate\Support\Collection
|
||||
*/
|
||||
public function collection()
|
||||
{
|
||||
return JenisKartu::all();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function headings(): array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Code',
|
||||
'Name',
|
||||
'Biaya',
|
||||
'Authorized Status',
|
||||
'Created At',
|
||||
'Updated At'
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param mixed $row
|
||||
* @return array
|
||||
*/
|
||||
public function map($row): array
|
||||
{
|
||||
return [
|
||||
$row->id,
|
||||
$row->code,
|
||||
$row->name,
|
||||
$row->biaya,
|
||||
$row->authorized_status ? 'Yes' : 'No',
|
||||
$row->created_at->format('Y-m-d H:i:s'),
|
||||
$row->updated_at ? $row->updated_at->format('Y-m-d H:i:s') : '',
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Worksheet $sheet
|
||||
* @return array
|
||||
*/
|
||||
public function styles(Worksheet $sheet)
|
||||
{
|
||||
return [
|
||||
// Style the first row as bold text
|
||||
1 => ['font' => ['bold' => true]],
|
||||
];
|
||||
}
|
||||
}
|
||||
207
app/Http/Controllers/JenisKartuController.php
Normal file
207
app/Http/Controllers/JenisKartuController.php
Normal file
@@ -0,0 +1,207 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Webstatement\Exports\JenisKartuExport;
|
||||
use Modules\Webstatement\Http\Requests\JenisKartuRequest;
|
||||
use Modules\Webstatement\Models\JenisKartu;
|
||||
|
||||
// Added for Request class
|
||||
|
||||
class JenisKartuController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
/**
|
||||
* Display a listing of the resource.
|
||||
*/
|
||||
public function index()
|
||||
{
|
||||
return view('webstatement::jenis-kartu.index');
|
||||
}
|
||||
|
||||
/**
|
||||
* Store a newly created resource in storage.
|
||||
*/
|
||||
public function store(JenisKartuRequest $request)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Add created_by field
|
||||
$validate['created_by'] = auth()->id();
|
||||
|
||||
// Save to database
|
||||
JenisKartu::create($validate);
|
||||
|
||||
return redirect()
|
||||
->route('jenis-kartu.index')
|
||||
->with('success', 'Jenis Kartu created successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('jenis-kartu.create')
|
||||
->with('error', 'Failed to create Jenis Kartu');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for creating a new resource.
|
||||
*/
|
||||
public function create()
|
||||
{
|
||||
return view('webstatement::jenis-kartu.create');
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the specified resource.
|
||||
*/
|
||||
public function show($id)
|
||||
{
|
||||
$jenisKartu = JenisKartu::find($id);
|
||||
return view('webstatement::jenis-kartu.show', compact('jenisKartu'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
*/
|
||||
public function edit($id)
|
||||
{
|
||||
$jenisKartu = JenisKartu::find($id);
|
||||
return view('webstatement::jenis-kartu.create', compact('jenisKartu'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the specified resource in storage.
|
||||
*/
|
||||
public function update(JenisKartuRequest $request, $id)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Add updated_by field
|
||||
$validate['updated_by'] = auth()->id();
|
||||
|
||||
// Update in database
|
||||
$jenisKartu = JenisKartu::find($id);
|
||||
$jenisKartu->update($validate);
|
||||
|
||||
return redirect()
|
||||
->route('jenis-kartu.index')
|
||||
->with('success', 'Jenis Kartu updated successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('jenis-kartu.edit', $id)
|
||||
->with('error', 'Failed to update Jenis Kartu');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove the specified resource from storage.
|
||||
*/
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
// Add deleted_by field
|
||||
$jenisKartu = JenisKartu::find($id);
|
||||
$jenisKartu->deleted_by = auth()->id();
|
||||
$jenisKartu->save();
|
||||
|
||||
// Delete from database
|
||||
$jenisKartu->delete();
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Jenis Kartu deleted successfully']);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to delete Jenis Kartu']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provide data for datatables.
|
||||
*/
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('jenis-kartu.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view jenis kartu.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = JenisKartu::query();
|
||||
// Apply search filter if provided
|
||||
if ($request->has('search') && !empty($request->get('search'))) {
|
||||
$search = $request->get('search');
|
||||
$query->where(function ($q) use ($search) {
|
||||
$q->where('code', 'LIKE', "%$search%");
|
||||
$q->orWhere('name', 'LIKE', "%$search%");
|
||||
$q->orWhere('biaya', 'LIKE', "%$search%");
|
||||
});
|
||||
}
|
||||
|
||||
// Apply sorting if provided
|
||||
if ($request->has('sortOrder') && !empty($request->get('sortOrder'))) {
|
||||
$order = $request->get('sortOrder');
|
||||
$column = $request->get('sortField');
|
||||
$query->orderBy($column, $order);
|
||||
}
|
||||
|
||||
// Get the total count of records
|
||||
$totalRecords = $query->count();
|
||||
|
||||
// Apply pagination if provided
|
||||
if ($request->has('page') && $request->has('size')) {
|
||||
$page = $request->get('page');
|
||||
$size = $request->get('size');
|
||||
$offset = ($page - 1) * $size; // Calculate the offset
|
||||
|
||||
$query->skip($offset)->take($size);
|
||||
}
|
||||
|
||||
// Get the filtered count of records
|
||||
$filteredRecords = $query->count();
|
||||
|
||||
// Get the data for the current page
|
||||
$data = $query->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($filteredRecords / ($request->get('size') ?: 1));
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = $request->get('page') ?: 1;
|
||||
|
||||
// Return the response data as a JSON object
|
||||
return response()->json([
|
||||
'draw' => $request->get('draw'),
|
||||
'recordsTotal' => $totalRecords,
|
||||
'recordsFiltered' => $filteredRecords,
|
||||
'pageCount' => $pageCount,
|
||||
'page' => $currentPage,
|
||||
'totalCount' => $totalRecords,
|
||||
'data' => $data,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete multiple records.
|
||||
*/
|
||||
public function deleteMultiple(Request $request)
|
||||
{
|
||||
$ids = $request->input('ids');
|
||||
JenisKartu::whereIn('id', $ids)->delete();
|
||||
return response()->json(['message' => 'Jenis Kartu deleted successfully']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Export data to Excel.
|
||||
*/
|
||||
public function export()
|
||||
{
|
||||
return Excel::download(new JenisKartuExport, 'jenis-kartu.xlsx');
|
||||
}
|
||||
}
|
||||
41
app/Http/Requests/JenisKartuRequest.php
Normal file
41
app/Http/Requests/JenisKartuRequest.php
Normal file
@@ -0,0 +1,41 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class JenisKartuRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
'code' => 'required|string|max:50',
|
||||
'name' => 'required|string|max:255',
|
||||
'biaya' => 'required|numeric',
|
||||
'authorized_status' => 'nullable|boolean',
|
||||
];
|
||||
|
||||
// Add unique validation for code field on create
|
||||
if ($this->isMethod('post')) {
|
||||
$rules['code'] .= '|unique:jenis_kartu,code';
|
||||
}
|
||||
|
||||
// Add unique validation for code field on update, excluding the current record
|
||||
if ($this->isMethod('put') || $this->isMethod('patch')) {
|
||||
$rules['code'] .= '|unique:jenis_kartu,code,' . $this->id;
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
51
app/Models/Base.php
Normal file
51
app/Models/Base.php
Normal file
@@ -0,0 +1,51 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\SoftDeletes;
|
||||
use Spatie\Activitylog\LogOptions;
|
||||
use Spatie\Activitylog\Traits\LogsActivity;
|
||||
use Wildside\Userstamps\Userstamps;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Base extends Model
|
||||
{
|
||||
use LogsActivity, SoftDeletes, Userstamps;
|
||||
|
||||
protected $connection;
|
||||
|
||||
/**
|
||||
* Constructs a new instance of the class.
|
||||
*
|
||||
* @param array $attributes Optional attributes to initialize the object with.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct(array $attributes = [])
|
||||
{
|
||||
parent::__construct($attributes);
|
||||
|
||||
// Retrieve the module configuration from the module.json file
|
||||
$modulePath = dirname(__FILE__, 3) . '/module.json';
|
||||
$module = file_get_contents($modulePath);
|
||||
$module = json_decode($module);
|
||||
|
||||
// Set the connection property to the database connection specified in the module configuration
|
||||
$this->connection = $module->database;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the activity log options for the User Management.
|
||||
*
|
||||
* @return LogOptions The activity log options.
|
||||
*/
|
||||
public function getActivitylogOptions()
|
||||
: LogOptions
|
||||
{
|
||||
return LogOptions::defaults()->logAll()->useLogName('User Management : ');
|
||||
}
|
||||
}
|
||||
22
app/Models/JenisKartu.php
Normal file
22
app/Models/JenisKartu.php
Normal file
@@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Webstatement\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
// use Modules\Webstatement\Database\Factories\JenisKartuFactory;
|
||||
|
||||
class JenisKartu extends Base
|
||||
{
|
||||
protected $table = "jenis_kartu";
|
||||
protected $fillable = [
|
||||
'code',
|
||||
'name',
|
||||
'biaya',
|
||||
'authorized_status',
|
||||
'created_by',
|
||||
'updated_by',
|
||||
'deleted_by',
|
||||
'authorized_by',
|
||||
];
|
||||
}
|
||||
Reference in New Issue
Block a user