Update Fix
This commit is contained in:
200
app/Http/Controllers/Admin/VoucherController.php
Normal file
200
app/Http/Controllers/Admin/VoucherController.php
Normal file
@@ -0,0 +1,200 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Admin;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Voucher;
|
||||
use App\Models\VoucherHistory;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class VoucherController extends Controller
|
||||
{
|
||||
public function index(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = Voucher::query()->with('pegawai:id,nama,nip')->orderByDesc('id');
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('kode', 'like', "%{$s}%")
|
||||
->orWhere('catatan', 'like', "%{$s}%");
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get()->map(function (Voucher $v) {
|
||||
return [
|
||||
'id' => $v->id,
|
||||
'pegawai_id' => $v->pegawai_id,
|
||||
'pegawai_nama' => $v->pegawai?->nama,
|
||||
'pegawai_nip' => $v->pegawai?->nip,
|
||||
'kode' => $v->kode,
|
||||
'saldo' => (int) $v->saldo,
|
||||
'aktif' => (bool) $v->aktif,
|
||||
'catatan' => $v->catatan,
|
||||
'created_at' => $v->created_at,
|
||||
];
|
||||
});
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'pegawai_id' => ['required', 'integer', 'exists:pegawai,id'],
|
||||
'kode' => ['nullable', 'string', 'max:255', 'unique:vouchers,kode'],
|
||||
'saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'aktif' => ['nullable', 'boolean'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$kode = trim((string) ($data['kode'] ?? ''));
|
||||
if ($kode === '') {
|
||||
$kode = $this->generateKode();
|
||||
}
|
||||
|
||||
$voucher = Voucher::create([
|
||||
'pegawai_id' => (int) $data['pegawai_id'],
|
||||
'kode' => $kode,
|
||||
'saldo' => (int) ($data['saldo'] ?? 0),
|
||||
'aktif' => isset($data['aktif']) ? (bool) $data['aktif'] : true,
|
||||
'catatan' => $data['catatan'] ?? null,
|
||||
'created_by' => $request->user()?->id,
|
||||
]);
|
||||
|
||||
return response()->json(['data' => $voucher->load('pegawai:id,nama,nip')], 201);
|
||||
}
|
||||
|
||||
public function update(Request $request, Voucher $voucher): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'add_saldo' => ['nullable', 'integer', 'min:0'],
|
||||
'aktif' => ['nullable', 'boolean'],
|
||||
'catatan' => ['nullable', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$updated = DB::transaction(function () use ($voucher, $data, $request) {
|
||||
$voucher = Voucher::query()->where('id', $voucher->id)->lockForUpdate()->firstOrFail();
|
||||
$saldoBefore = (int) $voucher->saldo;
|
||||
if (array_key_exists('add_saldo', $data) && $data['add_saldo'] !== null) {
|
||||
$voucher->saldo = (int) $voucher->saldo + (int) $data['add_saldo'];
|
||||
} elseif (array_key_exists('saldo', $data) && $data['saldo'] !== null) {
|
||||
$voucher->saldo = (int) $data['saldo'];
|
||||
}
|
||||
if (array_key_exists('aktif', $data)) {
|
||||
$voucher->aktif = (bool) $data['aktif'];
|
||||
}
|
||||
if (array_key_exists('catatan', $data)) {
|
||||
$voucher->catatan = $data['catatan'];
|
||||
}
|
||||
$voucher->save();
|
||||
if (array_key_exists('add_saldo', $data) && $data['add_saldo'] !== null) {
|
||||
VoucherHistory::create([
|
||||
'voucher_id' => $voucher->id,
|
||||
'kode' => $voucher->kode,
|
||||
'tipe' => 'TOPUP',
|
||||
'jumlah' => (int) $data['add_saldo'],
|
||||
'saldo_before' => $saldoBefore,
|
||||
'saldo_after' => (int) $voucher->saldo,
|
||||
'penjualan_id' => null,
|
||||
'user_id' => $request->user()?->id,
|
||||
'catatan' => 'Top up',
|
||||
]);
|
||||
}
|
||||
return $voucher->fresh()->load('pegawai:id,nama,nip');
|
||||
});
|
||||
|
||||
return response()->json(['data' => $updated]);
|
||||
}
|
||||
|
||||
public function history(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'from' => ['nullable', 'date'],
|
||||
'to' => ['nullable', 'date'],
|
||||
'q' => ['nullable', 'string', 'max:100'],
|
||||
'tipe' => ['nullable', 'string', 'max:20'],
|
||||
'limit' => ['nullable', 'integer', 'min:1', 'max:200'],
|
||||
]);
|
||||
|
||||
$limit = (int) ($data['limit'] ?? 100);
|
||||
|
||||
$query = DB::table('voucher_histories as h')
|
||||
->leftJoin('penjualan as p', 'p.id', '=', 'h.penjualan_id')
|
||||
->select([
|
||||
'h.id',
|
||||
'h.voucher_id',
|
||||
'h.kode',
|
||||
'h.tipe',
|
||||
'h.jumlah',
|
||||
'h.saldo_before',
|
||||
'h.saldo_after',
|
||||
'h.penjualan_id',
|
||||
'p.nomor_penjualan',
|
||||
'h.user_id',
|
||||
'h.catatan',
|
||||
'h.created_at',
|
||||
])
|
||||
->orderByDesc('h.id');
|
||||
|
||||
if (!empty($data['from'])) {
|
||||
$query->where('h.created_at', '>=', $data['from'] . ' 00:00:00');
|
||||
}
|
||||
if (!empty($data['to'])) {
|
||||
$query->where('h.created_at', '<=', $data['to'] . ' 23:59:59');
|
||||
}
|
||||
if (!empty($data['q'])) {
|
||||
$s = trim((string) $data['q']);
|
||||
$query->where('h.kode', 'like', "%{$s}%");
|
||||
}
|
||||
if (!empty($data['tipe'])) {
|
||||
$query->where('h.tipe', $data['tipe']);
|
||||
}
|
||||
|
||||
$rows = $query->limit($limit)->get();
|
||||
|
||||
return response()->json(['data' => $rows]);
|
||||
}
|
||||
|
||||
public function lookup(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'kode' => ['required', 'string', 'max:255'],
|
||||
]);
|
||||
|
||||
$kode = trim((string) $data['kode']);
|
||||
if ($kode === '') {
|
||||
return response()->json(['message' => 'Kode voucher wajib diisi.'], 422);
|
||||
}
|
||||
|
||||
$voucher = Voucher::query()->where('kode', $kode)->first();
|
||||
if (!$voucher) {
|
||||
return response()->json(['message' => 'Voucher tidak ditemukan.'], 404);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'data' => [
|
||||
'id' => $voucher->id,
|
||||
'kode' => $voucher->kode,
|
||||
'saldo' => (int) $voucher->saldo,
|
||||
'aktif' => (bool) $voucher->aktif,
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
private function generateKode(): string
|
||||
{
|
||||
do {
|
||||
$kode = 'VCH' . now()->format('ymd') . random_int(1000, 9999);
|
||||
} while (Voucher::query()->where('kode', $kode)->exists());
|
||||
|
||||
return $kode;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user