Changeset View
Changeset View
Standalone View
Standalone View
src/app/Http/Controllers/API/V4/Admin/WalletsController.php
Show First 20 Lines • Show All 74 Lines • ▼ Show 20 Lines | public function oneOff(Request $request, $id) | ||||
] | ] | ||||
); | ); | ||||
if ($v->fails()) { | if ($v->fails()) { | ||||
return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | return response()->json(['status' => 'error', 'errors' => $v->errors()], 422); | ||||
} | } | ||||
$amount = (int) ($request->amount * 100); | $amount = (int) ($request->amount * 100); | ||||
$type = $amount > 0 ? Transaction::WALLET_AWARD : Transaction::WALLET_PENALTY; | $method = $amount > 0 ? 'award' : 'penalty'; | ||||
DB::beginTransaction(); | DB::beginTransaction(); | ||||
$wallet->balance += $amount; | $wallet->{$method}(abs($amount), $request->description); | ||||
mollekopf: I would just call the methods instead of assembling a string. | |||||
$wallet->save(); | |||||
Transaction::create( | |||||
[ | |||||
'user_email' => \App\Utils::userEmailOrNull(), | |||||
'object_id' => $wallet->id, | |||||
'object_type' => Wallet::class, | |||||
'type' => $type, | |||||
'amount' => $amount, | |||||
'description' => $request->description | |||||
] | |||||
); | |||||
if ($user->role == 'reseller') { | if ($user->role == 'reseller') { | ||||
if ($user->tenant && ($tenant_wallet = $user->tenant->wallet())) { | if ($user->tenant && ($tenant_wallet = $user->tenant->wallet())) { | ||||
$desc = ($amount > 0 ? 'Awarded' : 'Penalized') . " user {$wallet->owner->email}"; | $desc = ($amount > 0 ? 'Awarded' : 'Penalized') . " user {$wallet->owner->email}"; | ||||
$method = $amount > 0 ? 'debit' : 'credit'; | $tenant_method = $amount > 0 ? 'debit' : 'credit'; | ||||
$tenant_wallet->{$method}(abs($amount), $desc); | $tenant_wallet->{$tenant_method}(abs($amount), $desc); | ||||
} | } | ||||
} | } | ||||
DB::commit(); | DB::commit(); | ||||
$response = [ | $response = [ | ||||
'status' => 'success', | 'status' => 'success', | ||||
'message' => \trans("app.wallet-{$type}-success"), | 'message' => \trans("app.wallet-{$method}-success"), | ||||
'balance' => $wallet->balance | 'balance' => $wallet->balance | ||||
]; | ]; | ||||
return response()->json($response); | return response()->json($response); | ||||
} | } | ||||
/** | /** | ||||
* Update wallet data. | * Update wallet data. | ||||
Show All 37 Lines |
I would just call the methods instead of assembling a string.