File: /home/armgroup/libs/app/Http/Livewire/Account/Point/Exchange.php
<?php
namespace App\Http\Livewire\Account\Point;
use App\Http\Livewire\ConvertEmptyStringsToNull;
use App\Http\Livewire\General;
use App\Models\Point;
use App\Models\Wallet;
use Illuminate\Support\Facades\DB;
use Livewire\Component;
class Exchange extends Component
{
use ConvertEmptyStringsToNull, General;
public $point;
public $credit;
public function mount()
{
$this->point = auth()->user()->point;
$this->credit = round($this->point * settings('point-exchange-rate'));
}
protected function rules()
{
return [
'point' => ['required', 'integer', 'min:'.settings('minimum-points-allowed-to-convert'), 'max:'.auth()->user()->point],
];
}
public function updatedPoint()
{
$this->credit = 0;
$this->validate();
$this->credit = round($this->point * settings('point-exchange-rate'));
}
public function render()
{
return view('livewire.account.point.exchange');
}
public function exchange()
{
$this->validate();
$this->credit = round($this->point * settings('point-exchange-rate'));
DB::transaction(function () {
$wallet = Wallet::create([
'user_id' => auth()->id(),
'amount' => $this->credit,
'walletable_id' => 0,
'walletable_type' => Point::class,
'description' => 'تبدیل شده توسط '.to_persian_numbers($this->point).' امتیاز',
]);
$point = Point::create([
'user_id' => auth()->id(),
'amount' => $this->point * -1,
'pointable_id' => $wallet->id,
'pointable_type' => Wallet::class,
'description' => 'تبدیل به '.to_persian_numbers(number_format($this->credit)).' تومان اعتبار',
]);
$wallet->update(['walletable_id' => $point->id]);
});
$this->doneMessage('امتیاز شما با موفقیت به اعتبار تبدیل شد');
return redirect()->route('account.points.show-exchange');
}
}