Feature #7 : Module Debitur
This commit is contained in:
81
app/Exports/DebitureExport.php
Normal file
81
app/Exports/DebitureExport.php
Normal file
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Exports;
|
||||
|
||||
use Maatwebsite\Excel\Concerns\FromCollection;
|
||||
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
|
||||
use Maatwebsite\Excel\Concerns\WithHeadings;
|
||||
use Maatwebsite\Excel\Concerns\WithMapping;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;
|
||||
|
||||
class DebitureExport implements WithColumnFormatting, WithHeadings, FromCollection, withMapping
|
||||
{
|
||||
public function collection()
|
||||
{
|
||||
return Debiture::with('branch', 'province', 'city', 'district', 'village')->get();
|
||||
}
|
||||
|
||||
public function map($row)
|
||||
: array
|
||||
{
|
||||
return [
|
||||
$row->id,
|
||||
$row->branch->name .
|
||||
$row->cif,
|
||||
$row->nomor_rekening,
|
||||
$row->name,
|
||||
$row->npwp,
|
||||
$row->nomor_id,
|
||||
$row->email,
|
||||
$row->phone,
|
||||
$row->province->name,
|
||||
$row->city->name,
|
||||
$row->district->name,
|
||||
$row->village->name,
|
||||
$row->postal_code,
|
||||
$row->address,
|
||||
$row->registered_at,
|
||||
$row->created_at
|
||||
];
|
||||
}
|
||||
|
||||
public function headings()
|
||||
: array
|
||||
{
|
||||
return [
|
||||
'ID',
|
||||
'Branch',
|
||||
'CIF',
|
||||
'Nomor Rekening',
|
||||
'Name',
|
||||
'NPWP',
|
||||
'Nomor ID',
|
||||
'Email',
|
||||
'Phone',
|
||||
'Province',
|
||||
'City',
|
||||
'District',
|
||||
'Village',
|
||||
'Postal Code',
|
||||
'Address',
|
||||
'Registered At',
|
||||
'Created At'
|
||||
];
|
||||
}
|
||||
|
||||
public function columnFormats()
|
||||
: array
|
||||
{
|
||||
return [
|
||||
'A' => NumberFormat::FORMAT_NUMBER,
|
||||
'C' => NumberFormat::FORMAT_NUMBER,
|
||||
'D' => NumberFormat::FORMAT_NUMBER,
|
||||
'F' => NumberFormat::FORMAT_NUMBER,
|
||||
'G' => NumberFormat::FORMAT_NUMBER,
|
||||
'P' => NumberFormat::FORMAT_DATE_DATETIME,
|
||||
'Q' => NumberFormat::FORMAT_DATE_DATETIME
|
||||
|
||||
];
|
||||
}
|
||||
}
|
||||
169
app/Http/Controllers/DebitureController.php
Normal file
169
app/Http/Controllers/DebitureController.php
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Controllers;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use Exception;
|
||||
use Illuminate\Http\Request;
|
||||
use Maatwebsite\Excel\Facades\Excel;
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Province;
|
||||
use Modules\Location\Models\Village;
|
||||
use Modules\Lpj\Exports\DebitureExport;
|
||||
use Modules\Lpj\Http\Requests\DebitureRequest;
|
||||
use Modules\Lpj\Models\Branch;
|
||||
use Modules\Lpj\Models\Debiture;
|
||||
|
||||
class DebitureController extends Controller
|
||||
{
|
||||
public $user;
|
||||
|
||||
public function index()
|
||||
{
|
||||
return view('lpj::debitur.index');
|
||||
}
|
||||
|
||||
public function store(DebitureRequest $request)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Save to database
|
||||
Debiture::create($validate);
|
||||
return redirect()
|
||||
->route('debitur.index')
|
||||
->with('success', 'Debitur created successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('debitur.create')
|
||||
->with('error', 'Failed to create debitur');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
$branches = Branch::all();
|
||||
$provinces = Province::all();
|
||||
return view('lpj::debitur.create', compact('branches', 'provinces'));
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$debitur = Debiture::find($id);
|
||||
$branches = Branch::all();
|
||||
$provinces = Province::all();
|
||||
$cities = City::where('province_code', $debitur->province_code)->get();
|
||||
$districts = District::where('city_code', $debitur->city_code)->get();
|
||||
$villages = Village::where('district_code', $debitur->district_code)->get();
|
||||
return view('lpj::debitur.create', compact('debitur', 'branches', 'provinces', 'cities', 'districts', 'villages'));
|
||||
}
|
||||
|
||||
public function update(DebitureRequest $request, $id)
|
||||
{
|
||||
$validate = $request->validated();
|
||||
|
||||
if ($validate) {
|
||||
try {
|
||||
// Update in database
|
||||
$debitur = Debiture::find($id);
|
||||
$debitur->update($validate);
|
||||
return redirect()
|
||||
->route('debitur.index')
|
||||
->with('success', 'Debitur updated successfully');
|
||||
} catch (Exception $e) {
|
||||
return redirect()
|
||||
->route('debitur.edit', $id)
|
||||
->with('error', 'Failed to update debitur');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
try {
|
||||
// Delete from database
|
||||
$debitur = Debiture::find($id);
|
||||
$debitur->delete();
|
||||
|
||||
echo json_encode(['success' => true, 'message' => 'Debitur deleted successfully']);
|
||||
} catch (Exception $e) {
|
||||
echo json_encode(['success' => false, 'message' => 'Failed to delete debitur']);
|
||||
}
|
||||
}
|
||||
|
||||
public function dataForDatatables(Request $request)
|
||||
{
|
||||
if (is_null($this->user) || !$this->user->can('debitur.view')) {
|
||||
//abort(403, 'Sorry! You are not allowed to view users.');
|
||||
}
|
||||
|
||||
// Retrieve data from the database
|
||||
$query = Debiture::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('cif', 'LIKE', "%$search%");
|
||||
$q->orWhere('name', 'LIKE', "%$search%");
|
||||
$q->orWhereRelation('branch', 'name', 'LIKE', "%$search%");
|
||||
$q->orWhere('address', 'LIKE', "%$search%");
|
||||
$q->orWhere('npwp', 'LIKE', "%$search%");
|
||||
$q->orWhere('nomor_id', 'LIKE', "%$search%");
|
||||
$q->orWhere('email', 'LIKE', "%$search%");
|
||||
$q->orWhere('phone', 'LIKE', "%$search%");
|
||||
$q->orWhere('nomor_rekening', '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->with('branch')->get();
|
||||
|
||||
// Calculate the page count
|
||||
$pageCount = ceil($totalRecords / $request->get('size'));
|
||||
|
||||
// Calculate the current page number
|
||||
$currentPage = 0 + 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,
|
||||
]);
|
||||
}
|
||||
|
||||
public function export()
|
||||
{
|
||||
return Excel::download(new DebitureExport, 'debitur.xlsx');
|
||||
}
|
||||
}
|
||||
50
app/Http/Requests/DebitureRequest.php
Normal file
50
app/Http/Requests/DebitureRequest.php
Normal file
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Http\Requests;
|
||||
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
|
||||
class DebitureRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* Get the validation rules that apply to the request.
|
||||
*/
|
||||
public function rules()
|
||||
: array
|
||||
{
|
||||
$rules = [
|
||||
'branch_id' => 'required|exists:branches,id',
|
||||
'province_code' => 'nullable|exists:provinces,code',
|
||||
'city_code' => 'nullable|exists:cities,code',
|
||||
'district_code' => 'nullable|exists:districts,code',
|
||||
'village_code' => 'nullable|exists:villages,code',
|
||||
'nomor_rekening' => 'nullable|string|max:50',
|
||||
'name' => 'required',
|
||||
'registered_at' => 'nullable|date',
|
||||
'npwp' => 'nullable|string|max:16',
|
||||
'nomor_id' => 'nullable|string|max:16',
|
||||
'email' => 'nullable|email',
|
||||
'phone' => 'nullable|string|max:15',
|
||||
'address' => 'nullable|string',
|
||||
'postal_code' => 'nullable|string|max:10',
|
||||
'status' => 'nullable|boolean'
|
||||
];
|
||||
|
||||
if ($this->method() == 'PUT') {
|
||||
$rules['cif'] = 'required|unique:debitures,cif,' . $this->id;
|
||||
} else {
|
||||
$rules['cif'] = 'required|unique:debitures,cif';
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
|
||||
/**
|
||||
* Determine if the user is authorized to make this request.
|
||||
*/
|
||||
public function authorize()
|
||||
: bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
}
|
||||
60
app/Models/Debiture.php
Normal file
60
app/Models/Debiture.php
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Modules\Lpj\Models;
|
||||
|
||||
use Modules\Location\Models\City;
|
||||
use Modules\Location\Models\District;
|
||||
use Modules\Location\Models\Province;
|
||||
use Modules\Location\Models\Village;
|
||||
|
||||
class Debiture extends Base
|
||||
{
|
||||
protected $table = 'debitures';
|
||||
protected $fillable = [
|
||||
'branch_id',
|
||||
'cif',
|
||||
'name',
|
||||
'registered_at',
|
||||
'npwp',
|
||||
'nomor_id',
|
||||
'email',
|
||||
'phone',
|
||||
'nomor_rekening',
|
||||
'province_code',
|
||||
'city_code',
|
||||
'district_code',
|
||||
'village_code',
|
||||
'postal_code',
|
||||
'address',
|
||||
'status',
|
||||
'authorized_at',
|
||||
'authorized_status',
|
||||
'authorized_by'
|
||||
];
|
||||
|
||||
public function branch()
|
||||
{
|
||||
return $this->belongsTo(Branch::class, 'branch_id', 'id');
|
||||
}
|
||||
|
||||
public function province()
|
||||
{
|
||||
return $this->belongsTo(Province::class, 'province_code', 'code');
|
||||
}
|
||||
|
||||
public function city()
|
||||
{
|
||||
return $this->belongsTo(City::class, 'city_code', 'code');
|
||||
}
|
||||
|
||||
public function district()
|
||||
{
|
||||
return $this->belongsTo(District::class, 'district_code', 'code');
|
||||
}
|
||||
|
||||
public function village()
|
||||
{
|
||||
return $this->belongsTo(Village::class, 'village_code', 'code');
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user