From a662dcf154e94fd67125d92957c75eb72d059aba Mon Sep 17 00:00:00 2001 From: Aji Kamaludin Date: Sun, 4 Jun 2023 15:55:40 +0700 Subject: [PATCH] coin referral and history --- TODO.md | 4 +- .../Controllers/Customer/AuthController.php | 22 +++++ .../Controllers/Customer/CoinController.php | 27 ++++++ app/Models/CoinHistory.php | 41 +++++++++ app/Models/Customer.php | 12 ++- app/Models/CustomerRefferal.php | 6 +- resources/js/Pages/Customer/Index.jsx | 12 +++ resources/js/Pages/Home/Auth/Register.jsx | 11 +++ resources/js/Pages/Home/Coin/Detail.jsx | 36 ++++++++ resources/js/Pages/Home/Coin/Index.jsx | 83 +++++++++++++++++++ resources/js/Pages/Home/Profile/Index.jsx | 36 ++++++-- routes/web.php | 5 ++ 12 files changed, 283 insertions(+), 12 deletions(-) create mode 100644 app/Http/Controllers/Customer/CoinController.php create mode 100644 resources/js/Pages/Home/Coin/Detail.jsx create mode 100644 resources/js/Pages/Home/Coin/Index.jsx diff --git a/TODO.md b/TODO.md index 21bf2b6..bc267c7 100644 --- a/TODO.md +++ b/TODO.md @@ -33,7 +33,7 @@ - [x] Customer Deposit Payment Gateway - [x] Customer Purchase Voucher - [x] Customer Share Buyed Voucher, via WA dll -- [ ] Register Refferal -- [ ] Customer View Coin History +- [x] Register Refferal +- [x] Customer View Coin History - [ ] Verified Akun - [ ] Notification (purchase success, deposit success) diff --git a/app/Http/Controllers/Customer/AuthController.php b/app/Http/Controllers/Customer/AuthController.php index abe110e..5218fc6 100644 --- a/app/Http/Controllers/Customer/AuthController.php +++ b/app/Http/Controllers/Customer/AuthController.php @@ -4,6 +4,7 @@ namespace App\Http\Controllers\Customer; use App\Http\Controllers\Controller; use App\Models\Customer; +use App\Models\Setting; use Illuminate\Http\Request; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\DB; @@ -96,6 +97,7 @@ class AuthController extends Controller 'phone' => 'required|numeric', 'username' => 'required|string|min:5|alpha_dash|unique:customers,username', 'password' => 'required|string|min:8|confirmed', + 'referral_code' => 'nullable|exists:customers,referral_code' ]); DB::beginTransaction(); @@ -107,6 +109,26 @@ class AuthController extends Controller 'username' => $request->username, 'password' => bcrypt($request->password), ]); + + if ($request->referral_code != '') { + $refferal = Customer::where('referral_code', $request->referral_code)->first(); + $refferal->customerRefferals()->create([ + 'refferal_id' => $customer->id, + 'customer_code' => $refferal->referral_code + ]); + + $affilateEnabled = Setting::getByKey('AFFILATE_ENABLED'); + if ($affilateEnabled == 1) { + $bonusCoin = Setting::getByKey('AFFILATE_COIN_AMOUNT'); + $coin = $refferal->coins()->create([ + 'debit' => $bonusCoin, + 'description' => 'Bonus Refferal #' . Str::random(5), + ]); + + $coin->update_customer_balance(); + } + } + DB::commit(); Auth::guard('customer')->loginUsingId($customer->id); diff --git a/app/Http/Controllers/Customer/CoinController.php b/app/Http/Controllers/Customer/CoinController.php new file mode 100644 index 0000000..e2adcbc --- /dev/null +++ b/app/Http/Controllers/Customer/CoinController.php @@ -0,0 +1,27 @@ +id()) + ->orderBy('updated_at', 'desc'); + + return inertia('Home/Coin/Index', [ + 'coins' => $coins->paginate(20), + ]); + } + + public function show(CoinHistory $coin) + { + return inertia('Home/Coin/Detail', [ + 'coin' => $coin, + ]); + } +} diff --git a/app/Models/CoinHistory.php b/app/Models/CoinHistory.php index 2d3c739..c9efbda 100644 --- a/app/Models/CoinHistory.php +++ b/app/Models/CoinHistory.php @@ -2,6 +2,9 @@ namespace App\Models; +use Illuminate\Database\Eloquent\Casts\Attribute; +use Illuminate\Support\Carbon; + class CoinHistory extends Model { protected $fillable = [ @@ -13,8 +16,46 @@ class CoinHistory extends Model 'related_id', ]; + protected $appends = [ + 'amount', + 'format_human_created_at', + 'format_created_at', + ]; + + public function formatHumanCreatedAt(): Attribute + { + return Attribute::make(get: function () { + return Carbon::parse($this->created_at)->locale('id')->translatedFormat('d F Y'); + }); + } + + public function formatCreatedAt(): Attribute + { + return Attribute::make(get: function () { + return Carbon::parse($this->created_at)->locale('id')->translatedFormat('d F Y H:i:s'); + }); + } + + + public function amount(): Attribute + { + return Attribute::make(get: function () { + if ($this->credit == 0) { + return 'Rp' . number_format($this->debit, 0, ',', '.'); + } + + return '-Rp' . number_format($this->credit, 0, ',', '.'); + }); + } + public function customer() { return $this->belongsTo(Customer::class); } + + public function update_customer_balance() + { + $customer = Customer::find($this->customer_id); + $customer->update(['coin_balance' => $customer->coin_balance + $this->debit - $this->credit]); + } } diff --git a/app/Models/Customer.php b/app/Models/Customer.php index a24d8bc..3254ea8 100644 --- a/app/Models/Customer.php +++ b/app/Models/Customer.php @@ -59,7 +59,7 @@ class Customer extends Authenticatable $basic = CustomerLevel::where('key', CustomerLevel::BASIC)->first(); $customer->customer_level_id = $basic->id; - $customer->referral_code = Str::random(6); + $customer->referral_code = Str::upper(Str::random(6)); CustomerLevelHistory::create([ 'customer_id' => $customer->id, @@ -142,4 +142,14 @@ class Customer extends Authenticatable { return $this->hasMany(DepositHistory::class); } + + public function coins() + { + return $this->hasMany(CoinHistory::class); + } + + public function customerRefferals() + { + return $this->hasMany(CustomerRefferal::class); + } } diff --git a/app/Models/CustomerRefferal.php b/app/Models/CustomerRefferal.php index 5570a54..9da6a2b 100644 --- a/app/Models/CustomerRefferal.php +++ b/app/Models/CustomerRefferal.php @@ -5,8 +5,8 @@ namespace App\Models; class CustomerRefferal extends Model { protected $fillable = [ - 'customer_id', - 'refferal_id', - 'customer_code', + 'customer_id', //customer has code + 'refferal_id', //customer use the code + 'customer_code', //code of referal ]; } diff --git a/resources/js/Pages/Customer/Index.jsx b/resources/js/Pages/Customer/Index.jsx index 7754314..8348c15 100644 --- a/resources/js/Pages/Customer/Index.jsx +++ b/resources/js/Pages/Customer/Index.jsx @@ -107,6 +107,12 @@ export default function Customer(props) { > Coin + + Referal Code + {customer.display_coin} + + {customer.referral_code} + { @@ -129,6 +130,16 @@ export default function Index({ app_name, flash }) { onKeyDownCapture={(e) => handleKeyDown(e)} /> +
+ handleKeyDown(e)} + /> +
+
+
+ handleCopyToClipboard(user.referral_code) + } + > +
Referral Code:
+
{user.referral_code}
+
+ +
+
+
Upgrade Member
@@ -76,7 +97,10 @@ export default function Index({ auth: { user } }) {
Deposit Saldo
-
+
router.get(route('customer.coin.index'))} + >
Coin
diff --git a/routes/web.php b/routes/web.php index d15908e..d8f3799 100644 --- a/routes/web.php +++ b/routes/web.php @@ -2,6 +2,7 @@ use App\Http\Controllers\Customer\AuthController; use App\Http\Controllers\Customer\CartController; +use App\Http\Controllers\Customer\CoinController; use App\Http\Controllers\Customer\DepositController; use App\Http\Controllers\Customer\HomeController; use App\Http\Controllers\Customer\ProfileController; @@ -39,6 +40,10 @@ Route::middleware(['http_secure_aware', 'guard_should_customer', 'inertia.custom Route::get('deposit/trx/{deposit}', [DepositController::class, 'show'])->name('customer.deposit.show'); Route::post('deposit/trx/{deposit}', [DepositController::class, 'update'])->name('customer.deposit.update'); + // coin + Route::get('coin', [CoinController::class, 'index'])->name('customer.coin.index'); + Route::get('coin/{coin}', [CoinController::class, 'show'])->name('customer.coin.show'); + // cart Route::get('cart', [CartController::class, 'index'])->name('cart.index'); Route::post('cart/process', [CartController::class, 'purchase'])->name('cart.purchase');