feat(transaction): menambahkan kolom is_paid untuk menandakan transaksi tersebut sudah di bayar atau belum dan penyesuaikan lagi penggajiannya
This commit is contained in:
parent
5db53dd797
commit
769c813d7b
@ -11,6 +11,7 @@
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\RedirectResponse;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class PayrollController extends Controller
|
||||
{
|
||||
@ -40,11 +41,18 @@ public function create(): View
|
||||
public function store(PayrollRequest $request): RedirectResponse
|
||||
{
|
||||
$validatedData = $request->validated();
|
||||
Payroll::create(array_merge($validatedData, [
|
||||
'user_id' => Auth::id(),
|
||||
'employee_id' => $validatedData['employee'],
|
||||
'barbershop_id' => Auth::user()->barbershop_id,
|
||||
]));
|
||||
|
||||
DB::transaction(function () use ($validatedData) {
|
||||
Payroll::create(array_merge($validatedData, [
|
||||
'user_id' => Auth::id(),
|
||||
'employee_id' => $validatedData['employee'],
|
||||
'barbershop_id' => Auth::user()->barbershop_id,
|
||||
]));
|
||||
|
||||
Transaction::where('user_id', $validatedData['employee'])
|
||||
->where('is_paid', '0')
|
||||
->update(['is_paid' => '1']);
|
||||
});
|
||||
|
||||
notify()->success('Data berhasil ditambahkan', 'Berhasil');
|
||||
|
||||
@ -82,11 +90,22 @@ public function delete(Payroll $payroll): RedirectResponse
|
||||
|
||||
public function getSalaryEmployee(string $employeeId): JsonResponse
|
||||
{
|
||||
$baseSalary = User::find($employeeId)->base_salary;
|
||||
$latestPayroll = Payroll::where('user_id', $employeeId)->latest()->first();
|
||||
$shavingResults = Transaction::where('user_id', $employeeId)
|
||||
->when($latestPayroll, function ($query) use ($latestPayroll) {
|
||||
$employee = User::find($employeeId);
|
||||
$latestPayroll = Payroll::where('employee_id', $employeeId)->latest()->first();
|
||||
|
||||
$shavingResults = Transaction::query()
|
||||
->when($employee->role_id === 4, function ($query) use ($employee) {
|
||||
return $query->where('barbershop_id', $employee->barbershop_id);
|
||||
})
|
||||
->when($employee->role_id === 5, function ($query) use ($employeeId) {
|
||||
return $query->where('user_id', $employeeId)
|
||||
->where('is_paid', '0');
|
||||
})
|
||||
->when($latestPayroll, function ($query) use ($latestPayroll, $employee) {
|
||||
$query->whereBetween('created_at', [$latestPayroll->created_at, now()]);
|
||||
if ($employee->role_id === 4 && $latestPayroll->created_at->isToday()) {
|
||||
$query->where('is_paid', '0');
|
||||
}
|
||||
})
|
||||
->sum('total');
|
||||
|
||||
@ -94,8 +113,8 @@ public function getSalaryEmployee(string $employeeId): JsonResponse
|
||||
'status' => true,
|
||||
'message' => 'Data retrieved successfully',
|
||||
'data' => [
|
||||
'baseSalary' => formatToRupiah($baseSalary),
|
||||
'shavingResults' => formatToRupiah($shavingResults),
|
||||
'baseSalary' => formatToRupiah($employee->base_salary),
|
||||
'shavingResults' => in_array($employee->role_id, [4, 5]) ? formatToRupiah($shavingResults) : 'Rp0',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
@ -24,6 +24,7 @@ class Transaction extends Model
|
||||
'discount',
|
||||
'total',
|
||||
'payment_method',
|
||||
'is_paid',
|
||||
];
|
||||
|
||||
public function scopeBarbershop(Builder $query): Builder
|
||||
|
||||
@ -0,0 +1,28 @@
|
||||
<?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::table('transactions', function (Blueprint $table) {
|
||||
$table->enum('is_paid', ['0', '1'])->comment('0:belum dibayarkan gaji 1:Sudah dibayarkan gaji')->default('0')->after('payment_method');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('transactions', function (Blueprint $table) {
|
||||
$table->dropColumn('is_paid');
|
||||
});
|
||||
}
|
||||
};
|
||||
Loading…
Reference in New Issue
Block a user