feat(debt): crud,route service provider dan custom js

This commit is contained in:
Yoga Pangestu 2024-11-30 22:56:32 +07:00
parent 917310d8a0
commit 8d7d455a7b
10 changed files with 431 additions and 0 deletions

View File

@ -0,0 +1,127 @@
<?php
namespace App\Http\Controllers\Admin\Finance;
use App\Http\Controllers\Controller;
use App\Http\Requests\Admin\Finance\DebtRequest;
use App\Models\Barbershop;
use App\Models\Debt;
use App\Models\Transaction;
use App\Models\User;
use App\Traits\UploadAttachment;
use Illuminate\Contracts\View\View;
use Illuminate\Http\RedirectResponse;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
class DebtController extends Controller
{
use UploadAttachment;
public function index(): View
{
return view('pages.admin.finance.debts', [
'pageTitle' => 'Kasbon',
'debts' => Debt::with(['user', 'user.biography'])
->when(auth()->user()->role_id === 4, function ($query) {
return $query->where('user_id', auth()->id());
})
->latest()
->get(),
'employees' => User::with('biography')->barbershop()->latest()->get(),
]);
}
public function store(DebtRequest $request): RedirectResponse
{
$validatedData = $request->validated();
$barbershopId = Auth::user()->barbershop_id;
$barbershop = Barbershop::findOrFail($barbershopId);
$user = User::findOrFail($validatedData['employee']);
$transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->first();
$transactionAmount = $transaction ? $transaction->total : 0;
$maxDebt = $user->base_salary + (0.5 * $transactionAmount);
if ($barbershop->cash < $validatedData['amount']) {
notify()->error('Kas tidak cukup untuk melakukan transaksi ini', 'Gagal');
return back()->withInput();
}
if ($validatedData['amount'] > $maxDebt) {
notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi karyawan. Maksimal kasbon: '.formatToRupiah($maxDebt), 'Gagal');
return back()->withInput();
}
DB::transaction(function () use ($validatedData, $barbershop) {
Debt::create([
'user_id' => $validatedData['employee'],
'amount' => $validatedData['amount'],
]);
$barbershop->decrement('cash', $validatedData['amount']);
});
notify()->success('Data berhasil ditambahkan', 'Berhasil');
return back();
}
public function update(DebtRequest $request, Debt $debt): RedirectResponse
{
$validatedData = $request->validated();
$barbershopId = Auth::user()->barbershop_id;
$barbershop = Barbershop::findOrFail($barbershopId);
$user = User::findOrFail($debt->user_id);
$transaction = Transaction::where('user_id', $user->id)->where('is_paid', '0')->first();
$transactionAmount = $transaction ? $transaction->total : 0;
$maxDebt = $user->base_salary + (0.5 * $transactionAmount);
$amountDifference = $validatedData['amount'] - $debt->amount;
if ($amountDifference > 0 && $barbershop->cash < $amountDifference) {
notify()->error('Kas tidak cukup untuk menambah jumlah utang', 'Gagal');
return back()->withInput();
}
if ($validatedData['amount'] > $maxDebt) {
notify()->error('Jumlah utang tidak boleh lebih dari gaji pokok + 50% dari transaksi karyawan. Maksimal utang: Rp '.number_format($maxDebt, 2), 'Gagal');
return back()->withInput();
}
DB::transaction(function () use ($validatedData, $debt, $barbershop, $amountDifference) {
$debt->update([
'user_id' => $validatedData['employee'],
'amount' => $validatedData['amount'],
]);
if ($amountDifference !== 0) {
$barbershop->decrement('cash', $amountDifference);
}
});
notify()->success('Data berhasil diperbarui', 'Berhasil');
return back();
}
public function delete(Debt $debt): RedirectResponse
{
$barbershopId = Auth::user()->barbershop_id;
$barbershop = Barbershop::findOrFail($barbershopId);
DB::transaction(function () use ($debt, $barbershop) {
$barbershop->increment('cash', $debt->amount);
$debt->delete();
});
notify()->success('Data berhasil dihapus', 'Berhasil');
return back();
}
}

View File

@ -0,0 +1,39 @@
<?php
namespace App\Http\Requests\Admin\Finance;
use App\Traits\NotificationTrait;
use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
class DebtRequest extends FormRequest
{
use NotificationTrait;
public function authorize(): bool
{
return Auth::check();
}
protected function prepareForValidation(): void
{
$this->merge([
'amount' => removeRupiahFormatting($this->amount),
]);
}
public function rules(): array
{
return [
'employee' => ['required', Rule::exists('users', 'id')],
'amount' => ['required', 'numeric', 'min:0', 'max:99999999.99'],
];
}
protected function failedValidation(Validator $validator): void
{
$this->handleValidationFailure($validator);
}
}

24
app/Models/Debt.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;
class Debt extends Model
{
use HasFactory, SoftDeletes;
protected $fillable = [
'user_id',
'amount',
'is_paid',
];
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
}

View File

@ -5,6 +5,7 @@
use App\Models\Barbershop;
use App\Models\Cash;
use App\Models\Customer;
use App\Models\Debt;
use App\Models\Expense;
use App\Models\Inventory;
use App\Models\Payroll;
@ -37,6 +38,7 @@ public function boot(): void
Route::bind('expense', fn (string $expense) => Expense::findOrFail(decryptId($expense)));
Route::bind('payroll', fn (string $payroll) => Payroll::findOrFail(decryptId($payroll)));
Route::bind('cash', fn (string $cash) => Cash::findOrFail(decryptId($cash)));
Route::bind('debt', fn (string $debt) => Debt::findOrFail(decryptId($debt)));
Route::bind('inventory', fn (string $inventory) => Inventory::findOrFail(decryptId($inventory)));
Route::bind('transaction', fn (string $transaction) => Transaction::findOrFail(decryptId($transaction)));
Route::bind('application', fn (string $application) => Setting::findOrFail(decryptId($application)));

View File

@ -0,0 +1,31 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('debts', function (Blueprint $table) {
$table->id();
$table->foreignId('user_id')->constrained()->cascadeOnDelete();
$table->decimal('amount', 10, 2);
$table->enum('is_paid', ['0', '1'])->comment('0:belum bayar 1:Sudah bayar')->default('0');
$table->timestamps();
$table->softDeletes();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('debts');
}
};

View File

@ -0,0 +1,30 @@
$(document).ready(function () {
$("#createModal").on("show.bs.modal", function (event) {
const button = $(event.relatedTarget);
const data = {
modalTitle: button.data("modal-title"),
url: button.data("url"),
method: button.data("method"),
employee: button.data("employee"),
amount: button.data("amount"),
};
$(".modal-title").text(data.modalTitle);
$("#form").attr('action', data.url);
$("#form-method").val(data.method);
if (data.method === 'put') {
$("#employee").val(data.employee).trigger('change');
$("#amount").val(data.amount);
}
});
$('#createModal').on('hidden.bs.modal', function () {
const method = $("#form-method").val();
if (method === 'put') {
$("#employee").val('').trigger('change');
$("#amount").val('');
}
});
});

View File

@ -4,4 +4,11 @@ $(document).ready(function () {
placeholder: '-- Pilih --',
tags: true
});
$('.select2-modal').select2({
allowClear: true,
placeholder: '-- Pilih --',
tags: true,
dropdownParent: $('#form')
});
})

View File

@ -0,0 +1,152 @@
@extends('layouts.admin')
@section('styles')
<link rel="stylesheet" type="text/css" href="{{ asset('assets/admin/css/vendors/datatables.css') }}">
<link rel="stylesheet" href="{{ asset('assets/admin/css/vendors/select2.css') }}">
@endsection
@section('app')
<div class="container-fluid">
<div class="row">
<div class="col-sm-12">
@if (in_array(auth()->user()->role_id, [1, 3, 4]))
<div class="list-product-header">
<div>
<a href="javascript:void(0)" data-bs-toggle="modal" data-bs-target="#createModal"
data-modal-title="Tambah Kasbon" data-url="{{ route('finance.debt.store') }}"
data-metho="post" class="btn btn-primary">
Tambah
</a>
</div>
</div>
@endif
<div class="card">
<div class="card-body">
<div class="table-responsive">
<table class="display" id="datatable">
<thead>
<tr>
<th>No</th>
<th>Pegawai</th>
<th>Jumlah</th>
<th>Tanggal</th>
<th>Status</th>
@if (in_array(auth()->user()->role_id, [1, 3, 4]))
<th>Aksi</th>
@endif
</tr>
</thead>
<tbody>
@foreach ($debts as $debt)
<tr>
<td>{{ $loop->iteration }}</td>
<td>{{ $debt->user->biography->full_name ?? '-deleted-' }}</td>
<td>{{ formatToRupiah($debt->amount) ?? '-' }}</td>
<td>{{ formatDateIndo($debt->created_at) }}</td>
<td>
{!! $debt->is_paid === '1'
? '<span class="badge bg-success">Lunas</span>'
: '<span class="badge bg-danger">Belum</span>' !!}
</td>
@if (in_array(auth()->user()->role_id, [1, 3, 4]))
<td>
<ul class="action">
<li class="edit">
<a href="javascript:void(0)" data-bs-toggle="modal"
data-bs-target="#createModal" data-modal-title="Edit Kasbon"
data-url="{{ route('finance.debt.update', encryptId($debt->id)) }}"
data-method="put" data-employee="{{ $debt->user_id }}"
data-amount="{{ formatToRupiah($debt->amount) }}">
<i data-feather="edit"></i>
</a>
</li>
@if (auth()->user()->role_id === 1)
<li class="delete">
<form
action="{{ route('finance.debt.delete', encryptId($debt->id)) }}"
method="post">
@csrf
@method('delete')
<a href="javascript:void(0)" class="delete-data"
data-bs-toggle="tooltip" data-bs-placement="left"
data-bs-title="Hapus">
<i data-feather="trash-2"></i>
</a>
</form>
</li>
@endif
</ul>
</td>
@endif
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
<div class="modal fade" id="createModal" tabindex="-1" role="dialog" aria-labelledby="createModal"
aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title"></h5>
<button class="btn-close py-0" type="button" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<form class="row g-3 custom-input" method="post" id="form" enctype="multipart/form-data">
<input type="hidden" name="_method" id="form-method">
@csrf
<div class="col-lg-12 position-relative">
<label class="form-label" for="employee">Pegawai
</label>
<select name="employee"
class="form-control @error('employee') is-invalid @enderror select2-modal"
id="employee">
<option value=""></option>
@foreach ($employees as $employee)
<option value="{{ $employee->id }}" @selected(old('employee') == $employee->id)>
{{ $employee->biography->full_name }}
</option>
@endforeach
</select>
@error('employee')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<label class="form-label" for="amount">Jumlah</label>
<input name="amount"
class="form-control rupiah-format @error('amount') is-invalid @enderror" id="amount"
type="text" placeholder="Masukan Jumlah" value="{{ old('amount') }}"
inputmode="numeric">
@error('amount')
<div class="invalid-feedback">
{{ $message }}
</div>
@enderror
</div>
<div class="col-lg-12 position-relative">
<button class="btn btn-secondary" type="button" data-bs-dismiss="modal">Tutup</button>
<button class="btn btn-primary">Simpan</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('afterScripts')
<script src="{{ asset('assets/admin/js/select2/select2.js') }}"></script>
<script src="{{ asset('assets/admin/js/select2/select2-config.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/admin/js/datatable/jquery.dataTables.min.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/admin/js/datatable/client-side.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/js/interactions/confirmation.js?ver=1.0.0') }}"></script>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script src="{{ asset('assets/admin/js/custom/debt.js?ver=1.0.0') }}"></script>
<script src="{{ asset('assets/js/forms/rupiah-format.js?ver=1.0.0') }}"></script>
@endsection

View File

@ -121,6 +121,13 @@
</a>
</li>
@endif
<li class="sidebar-list">
<i class="fa fa-thumb-tack"></i>
<a class="sidebar-link sidebar-title link-nav" href="{{ route('finance.debts') }}">
<i data-feather="percent"></i>
<span>Kasbon</span>
</a>
</li>
<li class="sidebar-main-title">
<div>
<h6>Kelola</h6>

View File

@ -4,6 +4,7 @@
use App\Http\Controllers\Admin\Dashboard\AnalyticController;
use App\Http\Controllers\Admin\Dashboard\OverviewController;
use App\Http\Controllers\Admin\Finance\CashController;
use App\Http\Controllers\Admin\Finance\DebtController;
use App\Http\Controllers\Admin\Finance\ExpenseController;
use App\Http\Controllers\Admin\Finance\PayrollController;
use App\Http\Controllers\Admin\Manage\AttendanceController;
@ -116,6 +117,17 @@
Route::delete('delete/{cash}', [CashController::class, 'delete'])->name('finance.cash.delete')->middleware('check_role:1');
});
});
Route::middleware(['check_role:1,2,3,4,5'])->group(function () {
Route::get('/debts', [DebtController::class, 'index'])->name('finance.debts');
Route::prefix('debt')->group(function () {
Route::middleware(['check_role:1,3,4'])->group(function () {
Route::post('store', [DebtController::class, 'store'])->name('finance.debt.store');
Route::put('update/{debt}', [DebtController::class, 'update'])->name('finance.debt.update');
});
Route::delete('delete/{debt}', [DebtController::class, 'delete'])->name('finance.debt.delete')->middleware('check_role:1');
});
});
});
Route::prefix('manage')->group(function () {