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 +