Skip to content

Commit 98aa0c8

Browse files
done
1 parent 6241acd commit 98aa0c8

8 files changed

Lines changed: 181 additions & 19 deletions

File tree

app/Helpers/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function user () {
1414
if ( forced_user_exists() ) return forced_user();
1515

1616
if ( config('app.env') === 'local' )
17-
return $user ??= User::role(in_array('admin', request()->segments()) ? 'admin' : 'client')->where('id', 3)->first();
17+
return $user ??= User::role(in_array('admin', request()->segments()) ? 'admin' : 'client')->first();
1818

1919
return $user ??= auth()->user() ?? auth('sanctum')->user();
2020

app/Repositories/ReferralRepository.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,14 @@ public function __construct( Referral $model ) { parent::__construct($model); }
1010

1111
public function newReferral ( User $referred, User $referrer = null ) {
1212

13-
if ( !$referrer?->has('allow_referrals') ) return;
14-
return parent::create(['referrer_id' => $referrer->id, 'referred_id' => $referred->id]);
13+
if ( !$referrer?->has('allow_referrals') || $referrer->id === $referred->id ) return;
14+
return parent::firstOrCreate(['referrer_id' => $referrer->id, 'referred_id' => $referred->id]);
15+
16+
}
17+
public function findReferrer ( User $user ) {
18+
19+
$referral = $this->findBy('referred_id', $user->id);
20+
return $referral?->isAvailable() ? $referral->referrer : null;
1521

1622
}
1723

app/Repositories/WalletRepository.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ public function transferProcess ( Wallet $wallet, Wallet $toWallet, float $amoun
7878

7979
});
8080

81+
}
82+
public function aggregate ( Wallet $wallet, float $amount, string $type = null ) {
83+
84+
return match ( $type ) {
85+
'referral' => (bool) $wallet->increment('referral_earnings', $amount),
86+
'cashback' => (bool) $wallet->increment('total_cashback', $amount),
87+
default => true,
88+
};
89+
8190
}
8291
public function increase ( Wallet $wallet, float $amount, string $balance = 'buy' ) {
8392

@@ -110,12 +119,6 @@ public function cashback ( Wallet $wallet, float $amount ) {
110119
$amount = $this->removeFees($wallet, $amount);
111120
return $this->incrementProcess($wallet, $amount, 'buy') && $wallet->increment('total_cashback', $amount);
112121

113-
}
114-
public function commission ( Wallet $wallet, float $amount ) {
115-
116-
$amount = $this->removeFees($wallet, $amount);
117-
return $this->incrementProcess($wallet, $amount, 'buy') && $wallet->increment('referral_earnings', $amount);
118-
119122
}
120123
public function deposit ( Wallet $wallet, float $amount ) {
121124

@@ -158,6 +161,11 @@ public function removeFees ( Wallet $wallet, float $amount ) {
158161

159162
return $remaind_amount;
160163

164+
}
165+
public function addCommission ( Wallet $wallet, float $amount, string $balance = 'buy', string $type = null ) {
166+
167+
return $this->incrementProcess($wallet, $amount, $balance ?: 'buy') && $this->aggregate($wallet, $amount, $type);
168+
161169
}
162170
public function addPoints ( Wallet $wallet, int $points ) {
163171

app/Services/EntityService.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace App\Services;
44
use App\Repositories\EntityRepository;
5-
use App\Models\Entity;
65

76
class EntityService extends BaseService {
87

@@ -21,8 +20,8 @@ public function assignPermission ( int $id, string $permission = null, array $pa
2120
bool(data_get($params, 'force'))
2221
];
2322

24-
$permissions = $all ? $this->entityRepository->getModel()->setEntity($entity->name)->allEntityPermissions() : $permissions;
25-
$this->entityRepository->getModel()->setEntity($entity->name)->assignPermission($permissions, $allow, $force);
23+
$model = $this->entityRepository->getModel()->setEntity($entity->name);
24+
$model->assignPermission($all ? $model->allEntityPermissions() : $permissions, $allow, $force);
2625

2726
return success();
2827

app/Services/SettingService.php

Lines changed: 106 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,116 @@
22

33
namespace App\Services;
44
use App\Repositories\SettingRepository;
5+
use App\Repositories\CouponRepository;
6+
use App\Repositories\WalletRepository;
7+
use App\Repositories\ReferralRepository;
8+
use App\Models\Setting;
9+
use App\Models\User;
510

611
class SettingService extends BaseService {
712

813
public function __construct(
9-
protected SettingRepository $settingRepository
14+
protected SettingRepository $settingRepository,
15+
protected WalletRepository $walletRepository,
16+
protected CouponRepository $couponRepository,
17+
protected ReferralRepository $referralRepository
1018
) { parent::__construct($settingRepository); }
1119

20+
public function validateCommission ( Setting $commission, User $user, float $amount = null ) {
21+
22+
[$key, $value] = [$commission->key, optional((object) $commission->json_value)];
23+
$user = $key === 'referral' ? $this->referralRepository->findReferrer($user) : $user;
24+
25+
if (
26+
!$user?->active ||
27+
!$user?->has('allow_commissions') ||
28+
($value->min_level && $user->level < $value->min_level) ||
29+
($value->max_level && $user->level > $value->max_level) ||
30+
($value->min_price && $amount && $amount < $value->min_price) ||
31+
($value->max_price && $amount && $amount > $value->max_price) ||
32+
( !is_null($value->max_usages) && $value->max_usages <= 0 )
33+
) return;
34+
35+
return [$key, $value, $user];
36+
37+
}
38+
public function contextCommission ( Setting $commission, User $user, float $amount = null ) {
39+
40+
if ( !$context = $this->validateCommission($commission, $user, $amount) ) return;
41+
[$key, $value, $user] = $context;
42+
43+
$isFixed = $value->amount_type === 'fixed';
44+
$perUnit = $value->scope === 'unit';
45+
$balance = string($value->balance ?? 'buy');
46+
$type = string($value->type);
47+
$couponId = integer($value->coupon_id);
48+
49+
$ValPoints = positive($value->points);
50+
$valAmount = positive($value->amount);
51+
$maxAmount = positive($value->max_amount);
52+
$maxPoints = positive($value->max_points);
53+
$mainAmount = positive($amount);
54+
55+
$amount = positive($isFixed ? $valAmount : $mainAmount * $valAmount / 100);
56+
$amount = positive($perUnit ? $amount * $mainAmount : $amount);
57+
$points = floor($perUnit ? $ValPoints * $mainAmount : $ValPoints);
58+
$amount = $maxAmount ? min($maxAmount, $amount) : $amount;
59+
$points = $maxPoints ? min($maxPoints, $points) : $points;
60+
61+
$wallet = $type !== 'coupon' ? $user->wallet : null;
62+
$coupon = $type === 'coupon' ? $this->couponRepository->find($couponId) : null;
63+
64+
return [
65+
'user' => $user,
66+
'wallet' => $wallet,
67+
'coupon' => $coupon,
68+
'key' => $key,
69+
'type' => $type,
70+
'balance' => $balance,
71+
'amount' => $amount,
72+
'points' => $points,
73+
];
74+
75+
}
76+
public function applyCommission ( Setting $commission, User $user, float $amount = null ) {
77+
78+
if ( !$context = $this->contextCommission($commission, $user, $amount) ) return;
79+
80+
$key = $context['key'];
81+
$type = $context['type'];
82+
$amount = $context['amount'];
83+
$points = $context['points'];
84+
$balance = $context['balance'];
85+
$user = $context['user'];
86+
$wallet = $context['wallet'];
87+
$coupon = $context['coupon'];
88+
89+
$result = match ( $type ) {
90+
'coupon' => $coupon ? $this->couponRepository->redeem($coupon, $user) : null,
91+
'points' => $wallet && $this->walletRepository->addPoints($wallet, $points) ? $points : null,
92+
'amount' => $wallet && $this->walletRepository->addCommission($wallet, $amount, $balance, $key) ? $amount : null,
93+
default => null,
94+
};
95+
96+
return $result ? [...$context, 'value' => $result] : null;
97+
98+
}
99+
public function resolveCommission ( User $user, string $key, float $amount = null ) {
100+
101+
$record = $this->query()->where('group', 'commission')->where('key', $key)->active()->latest()->first();
102+
return $record ? $this->applyCommission($record, $user, $amount) : null;
103+
104+
}
105+
public function handleCommission ( User $user, string $key, float $amount = null, bool $queue = true ) {
106+
107+
if ( !$queue ) return $this->resolveCommission($user, $key, $amount);
108+
return $this->runJob([static::class, 'resolveCommission'], [$user, $key, $amount]);
109+
110+
}
111+
public function handleCommissions ( User $user, array $keys, float $amount = null, bool $queue = true ) {
112+
113+
return collect($keys)->filter()->map(fn($key) => $this->handleCommission($user, $key, $amount, $queue))->all();
114+
115+
}
116+
12117
}

config/settings/permissions.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@
277277
['name' => 'allow_stores', 'default' => true], ['name' => 'allow_refunds', 'default' => true],
278278
['name' => 'allow_payments', 'default' => true], ['name' => 'allow_transactions', 'default' => true],
279279
['name' => 'allow_gift_codes', 'default' => true], ['name' => 'allow_verifications', 'default' => true],
280+
['name' => 'allow_commissions', 'default' => true],
280281
],
281282
'provider' => [
282283
['name' => 'allow_imports', 'default' => true], ['name' => 'allow_gift_codes', 'default' => true],

config/settings/settings.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
'after' => 'each_order',
1111
'scope' => 'transaction',
1212
'type' => 'amount',
13+
'balance' => 'pending',
14+
'pending_days' => 30,
1315
'points' => 0,
1416
'amount_type' => 'percentage',
1517
'amount' => 10,
@@ -52,6 +54,7 @@
5254
'after' => 'each_subscription',
5355
'scope' => 'transaction',
5456
'type' => 'amount',
57+
'balance' => 'buy',
5558
'points' => 0,
5659
'amount_type' => 'fixed',
5760
'amount' => 0.5,

routes/apis/admin.php

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,49 @@
125125

126126
});
127127

128-
// admin settings ( fields, permissions, deletes )
129-
// admin chatbox
130-
// admin notifications
131-
// default marketing ( referral commissions, register coupons, order points, offers )
132-
// fold up error in frontend
133-
// withdraw-method
128+
Route::middleware(tenantMiddleware())->namespace('\App\Http\Controllers')->group(function() {
129+
Route::get('test', function () {
130+
131+
$order = \App\Models\Order::find(1);
132+
dump(app(\App\Services\SettingService::class)->handleCommission($order->user, 'referral', $order->paid_amount, false));
133+
134+
$order = \App\Models\Order::find(1);
135+
dump(app(\App\Services\SettingService::class)->handleCommission($order->user, 'order', $order->paid_amount, false));
136+
137+
$user = \App\Models\User::find(1);
138+
dump(app(\App\Services\SettingService::class)->handleCommission($user, 'register', null, false));
139+
140+
$sub = \App\Models\subscription::find(1);
141+
dump(app(\App\Services\SettingService::class)->handleCommission($user, 'subscription', $sub->price, false));
142+
143+
});
144+
});
145+
146+
// ( fixes )
147+
// maintenance chatbox ( admin, front )
148+
// maintenance plans ( admin )
149+
// maintenance subscriptions ( admin )
150+
// maintenance logs ( admin )
151+
// maintenance transactions ( admin )
152+
// maintenance notifications ( admin, front )
153+
// maintenance content - add name url for banners ( admin, front )
154+
// apply pending period in commissions ( front )
155+
// fold up pagination ( front )
156+
// delete domain from vercel/cloudflare after delete
157+
158+
// ( additions )
159+
// withdraw-method ( admin, front )
160+
// ERP-revenew statistics ( admin )
161+
// Obervability statistics : trafic, how maney times, tracing, performance, onlines ( admin )
162+
// share plans between stores ( admin )
163+
// level users ( backend, admin )
164+
// dynamic logo in sidebar and nav ( admin )
165+
// change store admin data to reset password or email ( admin, front )
166+
// custom referrer url ( backend, front )
167+
// pop-up for welcome coupon ( front )
168+
// import/export as csv & excell ( backend, admin )
169+
170+
// ( advanced additions )
171+
// marketing system ( commissions, offers, campaigns )
172+
// Dashboard for Super main-store
173+
// Dashboard for Affiliaters

0 commit comments

Comments
 (0)