117 lines
4.0 KiB
PHP
117 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Livewire\TopUp;
|
|
|
|
use Livewire\Component;
|
|
use App\Services\ApiService;
|
|
use Illuminate\Support\Facades\Session;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class Create extends Component
|
|
{
|
|
public $fee_code = '';
|
|
public $name = '';
|
|
public $amount = '';
|
|
public $type = '1'; // Default to PH Peso
|
|
public $loading = false;
|
|
public $mounted = false;
|
|
public $errors = [];
|
|
|
|
protected $apiService;
|
|
|
|
public function boot(ApiService $apiService)
|
|
{
|
|
$this->apiService = $apiService;
|
|
}
|
|
|
|
public function mount()
|
|
{
|
|
// Check role-based access
|
|
$userInfo = Session::get('userInfo');
|
|
if (!$userInfo || $userInfo['role'] != 1) {
|
|
return redirect()->route('404');
|
|
}
|
|
|
|
$this->loading = true;
|
|
|
|
try {
|
|
$response = $this->apiService->get('generateFeeCode');
|
|
if ($response && isset($response['data']['fee_code'])) {
|
|
$this->fee_code = $response['data']['fee_code'];
|
|
$this->mounted = true;
|
|
}
|
|
} catch (\Exception $e) {
|
|
Session::flash('error', 'Something went wrong generating fee code: ' . $e->getMessage());
|
|
Log::error('Failed to generate fee code', ['error' => $e->getMessage(), 'source' => 'TopUpCreate']);
|
|
$this->mounted = false;
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function create()
|
|
{
|
|
$this->loading = true;
|
|
$this->errors = [];
|
|
|
|
try {
|
|
$this->validate([
|
|
'fee_code' => ['required'],
|
|
'name' => ['required', 'max:128', 'regex:/^[A-Za-z0-9.ñÑ ]+$/'],
|
|
'amount' => ['required', 'numeric'],
|
|
'type' => ['required'],
|
|
], [
|
|
'fee_code.required' => 'Fee Code is required!',
|
|
'name.required' => 'Name is required!',
|
|
'name.max' => 'Maximum character is 128.',
|
|
'name.regex' => 'Invalid Name.',
|
|
'amount.required' => 'Amount is required!',
|
|
'amount.numeric' => 'Amount must be a number.',
|
|
'type.required' => 'Type is required!',
|
|
]);
|
|
|
|
$params = [
|
|
'fee_code' => $this->fee_code,
|
|
'name' => trim($this->name),
|
|
'amount' => $this->amount,
|
|
'type' => $this->type,
|
|
];
|
|
|
|
$response = $this->apiService->post('topUp', $params);
|
|
|
|
if ($response && isset($response['status']) && $response['status'] === 200) {
|
|
Session::flash('success', 'New record added.');
|
|
Log::info('Top-up created successfully', ['source' => 'TopUpCreate']);
|
|
return redirect()->route('top-up');
|
|
}
|
|
} catch (\Illuminate\Validation\ValidationException $e) {
|
|
$this->errors = $e->errors();
|
|
Session::flash('error', 'Something went wrong creating new record: ' . implode(', ', array_merge(...array_values($e->errors()))));
|
|
Log::error('Validation failed', ['errors' => $e->errors(), 'source' => 'TopUpCreate']);
|
|
} catch (\Exception $e) {
|
|
$errorMessage = 'Something went wrong creating new record.';
|
|
if ($e->getCode() === 422) {
|
|
$errors = json_decode($e->getMessage(), true)['data'] ?? [];
|
|
if (!empty($errors['fee_code'])) {
|
|
$errorMessage .= ' ' . $errors['fee_code'][0];
|
|
}
|
|
} else {
|
|
$errorMessage .= ' ' . $e->getMessage();
|
|
}
|
|
Session::flash('error', $errorMessage);
|
|
Log::error('Failed to create top-up', ['error' => $e->getMessage(), 'source' => 'TopUpCreate']);
|
|
} finally {
|
|
$this->loading = false;
|
|
}
|
|
}
|
|
|
|
public function resetAmount()
|
|
{
|
|
$this->amount = '';
|
|
}
|
|
|
|
public function render()
|
|
{
|
|
return view('livewire.top-up.create')->layout('layouts.app', ['title' => 'Add Top-Up']);
|
|
}
|
|
} |