Skip to content

Commit

Permalink
deposit cycle added
Browse files Browse the repository at this point in the history
  • Loading branch information
krutyosila committed Dec 4, 2020
1 parent b524bf1 commit 7484fe0
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/Models/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Krutyosila\Wallet\Models;

use App\Models\WalletCycle;
use Illuminate\Database\Eloquent\Model;

class Wallet extends Model
Expand All @@ -20,4 +21,9 @@ public function transactions()
{
return $this->hasMany(WalletTransaction::class);
}

public function cycle()
{
return $this->hasOne(WalletCycle::class);
}
}
21 changes: 21 additions & 0 deletions src/Models/WalletCycle.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Krutyosila\Wallet\Models\Wallet;

class WalletCycle extends Model
{
use HasFactory;

protected $fillable = [
'wallet_id', 'balance'
];

public function wallet()
{
return $this->belongsTo(Wallet::class);
}
}
19 changes: 16 additions & 3 deletions src/Services/WalletService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@

namespace Krutyosila\Wallet\Services;

use App\Models\WalletCycle;
use Krutyosila\Wallet\Models\WalletTransaction;
use Krutyosila\Wallet\Types\WalletTransactionType;

class WalletService
{
const TYPE_DEPOSIT = 'deposit';
const TYPE_WITHDRAW = 'withdraw';

const TYPE_BET = 'bet';
const TYPE_WIN = 'win';
const CYCLE_TYPES = ['deposit', 'bet'];
public function create(WalletTransactionType $types)
{
$wallet = $types->getWallet();
if ($types->getType() == self::TYPE_WITHDRAW) {
if ($types->getType() == self::TYPE_WITHDRAW OR $types->getType() == self::TYPE_BET) {
if ($types->getAmount() > $wallet->balance) {
throw new \Exception(__('wallet::errors.insufficient_balance'));
}
$types->setAmount($types->getAmount() * -1);
$wallet->balance = $wallet->balance + $types->getAmount();
$wallet->save();
}
if ($types->getType() == 'deposit' && $types->getConfirmed()) {
if (($types->getType() == self::TYPE_DEPOSIT OR $types->getType() == self::TYPE_WIN) && $types->getConfirmed()) {
$wallet->balance = $wallet->balance + $types->getAmount();
$wallet->save();
}
$types->setTxn();
if(in_array($types->getType(), self::CYCLE_TYPES)) {
$this->updateCycle($wallet->cycle, $types->getAmount());
}
return $wallet->transactions()->create($types->getAll());
}

Expand All @@ -43,4 +49,11 @@ public function confirm(WalletTransaction $transaction)
$transaction->save();
return $transaction;
}

public function updateCycle(WalletCycle $cycle, $amount)
{
$next = $cycle->balance + $amount;
$cycle->balance = $next < 0 ? 0 : $next;
$cycle->save();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateWalletCyclesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('wallet_cycles', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('wallet_id')->unique();
$table->decimal('balance', 8,2)->default(0);
$table->timestamps();
$table->foreign('wallet_id')
->references('id')->on('wallets')
->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('wallet_cycles');
}
}

0 comments on commit 7484fe0

Please sign in to comment.