feat(admin): manual deposit recording, transaction export, ticket filters
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
40bf6b54f9
commit
0cdf224309
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Transaction\Concerns;
|
||||
|
||||
use App\Exports\BrandedReportWriter;
|
||||
use App\Exports\TransactionsExport;
|
||||
use App\Http\Requests\BillingExportRequest;
|
||||
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
||||
|
||||
trait ExportsTransactions
|
||||
{
|
||||
public function export(BillingExportRequest $request, BrandedReportWriter $writer): BinaryFileResponse
|
||||
{
|
||||
$query = $this->filteredQuery(
|
||||
$request->safe()->only(['filter', 'status', 'date_from', 'date_to', 'customer_id'])
|
||||
)->with('user');
|
||||
|
||||
return $writer->download(new TransactionsExport($query), 'giao-dich-' . now()->format('Y-m-d-His') . '.xlsx');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers\Api\Admin\Transaction\Concerns;
|
||||
|
||||
use App\Enums\TransactionStatus;
|
||||
use App\Models\Customer;
|
||||
use App\Models\Log;
|
||||
use App\Models\Notification;
|
||||
use App\Models\Transaction;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Str;
|
||||
|
||||
trait RecordsManualDeposits
|
||||
{
|
||||
public function store(Request $request): JsonResponse
|
||||
{
|
||||
$data = $request->validate([
|
||||
'customer_id' => 'required|integer|exists:customers,id',
|
||||
'amount' => 'required|integer|min:1000',
|
||||
'note' => 'nullable|string|max:1000',
|
||||
'proof' => 'nullable|image|mimes:jpeg,jpg,png,webp|max:5120',
|
||||
]);
|
||||
|
||||
$admin = Auth::guard('admin')->user();
|
||||
$proofPath = $request->file('proof')?->store('deposit-proofs/' . $data['customer_id'], 'local');
|
||||
|
||||
try {
|
||||
$transaction = DB::transaction(function () use ($data, $admin, $proofPath, $request) {
|
||||
$customer = Customer::whereKey($data['customer_id'])->lockForUpdate()->firstOrFail();
|
||||
$amount = (int) $data['amount'];
|
||||
$creditEarned = Customer::creditFor($amount);
|
||||
|
||||
$publicCode = Transaction::newPublicCode('MAN');
|
||||
|
||||
$transaction = Transaction::create([
|
||||
'uuid' => (string) Str::uuid(),
|
||||
'public_code' => $publicCode,
|
||||
'trans' => str_replace('-', '', $publicCode),
|
||||
'amount' => $amount,
|
||||
'received_amount' => $amount,
|
||||
'customer_id' => $customer->id,
|
||||
'user_id' => $admin?->id,
|
||||
'type' => 'manual',
|
||||
'status' => TransactionStatus::COMPLETED->value,
|
||||
'note' => trim($data['note'] ?? '') ?: null,
|
||||
'proof_image' => $proofPath,
|
||||
]);
|
||||
|
||||
$customer->total += $amount;
|
||||
$customer->credit += $creditEarned;
|
||||
$customer->save();
|
||||
|
||||
Log::create([
|
||||
'agent' => 'admin',
|
||||
'client_ip' => $request->ip(),
|
||||
'description' => sprintf(
|
||||
'%s nạp tiền thủ công cho khách hàng: %s VNĐ (+%d credit) - %s',
|
||||
$admin?->username ?? $admin?->email ?? 'Admin',
|
||||
number_format($amount),
|
||||
$creditEarned,
|
||||
$transaction->public_code,
|
||||
),
|
||||
'customer_id' => $customer->id,
|
||||
]);
|
||||
|
||||
Notification::notifyCustomer($customer, [
|
||||
'type' => 'deposit_success',
|
||||
'title' => 'Nạp tiền thành công',
|
||||
'body' => 'Giao dịch ' . $transaction->public_code . ' đã được cộng vào tài khoản.',
|
||||
'link' => '/lich-su-giao-dich/' . $transaction->uuid,
|
||||
'data' => [
|
||||
'transaction_uuid' => $transaction->uuid,
|
||||
'amount' => $amount,
|
||||
'public_code' => $transaction->public_code,
|
||||
],
|
||||
]);
|
||||
|
||||
return $transaction;
|
||||
});
|
||||
} catch (\Throwable $e) {
|
||||
if ($proofPath) {
|
||||
Storage::disk('local')->delete($proofPath);
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
'message' => 'Đã nạp tiền cho khách hàng',
|
||||
'data' => ['uuid' => $transaction->uuid, 'public_code' => $transaction->public_code],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user