Skip to content

Commit e4490ca

Browse files
done
1 parent 693ccfa commit e4490ca

11 files changed

Lines changed: 153 additions & 140 deletions

File tree

app/Helpers/Request.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ function store_has ( ...$permissions ) {
229229
function middleware_has ( string $permission ) {
230230

231231
$permission = Str::plural(Str::snake(str_replace("{related}", request()->route('related') ?? '', $permission)));
232-
$permission = str_contains($permission, 'admin') ? 'supervisor' : $permission;
232+
$permission = str_contains($permission, 'admins') ? 'supervisor' : $permission;
233233

234234
if ( is_admin() ) return (is_super() || user_has($permission)) && store_has($permission);
235235
return user_has($permission) && setting_has($permission) && store_has($permission);

app/Http/Resources/ProductResource.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,6 @@ public function data () {
3939
'views' => $this->views,
4040
'likes' => $this->likes,
4141
'dislikes' => $this->dislikes,
42-
'allow_cancel' => $this->allow_cancel,
43-
'allow_refund' => $this->allow_refund,
44-
'allow_pay_later' => $this->allow_pay_later,
4542
'cancel_before' => $this->formatDate('cancel_before'),
4643
'refund_before' => $this->formatDate('refund_before'),
4744
];

app/Repositories/OrderRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,9 @@ public function newOrder ( Product $product, User $user, array $data = [] ) {
6262
'refund_before' => $product->refund_before,
6363
'region_id' => $product->region_id,
6464
'platform_id' => $product->platform_id,
65-
'allow_cancel' => $product->has('allow_cancel'),
65+
'allow_cancel' => $product->has('allow_cancellations'),
6666
'allow_refund' => $product->has('allow_refunds'),
67-
'allow_pay_later' => $product->has('allow_pay_later'),
67+
'allow_pay_later' => $product->has('allow_late_payments'),
6868
'delivery_method' => string(data_get($data, 'delivery_method', $product->delivery_method)),
6969
'ip' => ip(),
7070
'agent' => agent(),

app/Repositories/SettingRepository.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,95 @@
22

33
namespace App\Repositories;
44
use App\Models\Setting;
5+
use App\Models\User;
56

67
class SettingRepository extends BaseRepository {
78

89
public function __construct( Setting $model ) { parent::__construct($model); }
910

11+
public function findSetting ( string $key, string $group = null ) {
12+
13+
return $this->query()
14+
->where('key', $key)
15+
->when($group, fn($q) => $q->where('group', $group))
16+
->active()
17+
->latest()
18+
->first();
19+
20+
}
21+
public function getKey ( Setting $setting ) {
22+
23+
return (string) $setting->key;
24+
25+
}
26+
public function getValue ( Setting $setting, bool $object = false ) {
27+
28+
$value = (array) $setting->json_value;
29+
return $object ? optional((object) $value) : $value;
30+
31+
}
32+
public function setValue ( Setting $setting, string $key, string $value = null ) {
33+
34+
$value = [...$this->getValue($setting), $key => $value];
35+
$setting->update(['json_value' => $value]);
36+
return $value;
37+
38+
}
39+
public function setNewUsage ( Setting $setting ) {
40+
41+
$usages = integer(data_get($this->getValue($setting), 'max_usages'));
42+
$this->setValue($setting, 'max_usages', positive($usages - 1));
43+
44+
}
45+
public function validateCommission ( Setting $setting, User $user, float $amount = null ) {
46+
47+
$value = $this->getValue($setting, true);
48+
49+
if (
50+
!$user?->active ||
51+
!$user?->has('allow_commissions') ||
52+
$value->max_usages <= 0 ||
53+
($value->min_level && $user->level < $value->min_level) ||
54+
($value->max_level && $user->level > $value->max_level) ||
55+
($value->min_price && $amount && $amount < $value->min_price) ||
56+
($value->max_price && $amount && $amount > $value->max_price)
57+
) return false;
58+
59+
return true;
60+
61+
}
62+
public function commissionContext ( Setting $setting, float $amount = null ) {
63+
64+
$key = $this->getKey($setting);
65+
$value = $this->getValue($setting, true);
66+
67+
$isFixed = $value->amount_type === 'fixed';
68+
$perUnit = $value->scope === 'unit';
69+
$balance = string($value->balance ?? 'buy');
70+
$type = string($value->type);
71+
72+
$ValPoints = positive($value->points);
73+
$valAmount = positive($value->amount);
74+
$maxAmount = positive($value->max_amount);
75+
$maxPoints = positive($value->max_points);
76+
$mainAmount = positive($amount);
77+
78+
$amount = positive($isFixed ? $valAmount : $mainAmount * $valAmount / 100);
79+
$amount = positive($perUnit ? $amount * $mainAmount : $amount);
80+
$points = floor($perUnit ? $ValPoints * $mainAmount : $ValPoints);
81+
82+
$amount = $maxAmount ? min($maxAmount, $amount) : $amount;
83+
$points = $maxPoints ? min($maxPoints, $points) : $points;
84+
85+
return [
86+
...$this->getValue($setting),
87+
'reward_amount' => $amount,
88+
'reward_points' => $points,
89+
'balance' => $balance,
90+
'type' => $type,
91+
'key' => $key,
92+
];
93+
94+
}
95+
1096
}

app/Repositories/WalletRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ public function removeFees ( Wallet $wallet, float $amount ) {
164164
}
165165
public function addCommission ( Wallet $wallet, float $amount, string $balance = 'buy', string $type = null ) {
166166

167-
return $this->incrementProcess($wallet, $amount, $balance ?: 'buy') && $this->aggregate($wallet, $amount, $type);
167+
return $this->incrementProcess($wallet, $amount, $balance ?: 'buy') && $this->aggregate($wallet, $amount, $type) ? $amount : 0;
168168

169169
}
170170
public function addPoints ( Wallet $wallet, int $points ) {
171171

172-
return $this->incrementProcess($wallet, $points, 'points') && $wallet->increment('earned_points', $points);
172+
return $this->incrementProcess($wallet, $points, 'points') && $wallet->increment('earned_points', $points) ? $points : 0;
173173

174174
}
175175
public function removePoints ( Wallet $wallet, int $points ) {

app/Services/OrderService.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public function validateData ( Product $product, User $user, int $quantity, stri
6868
return match ( true ) {
6969
!in_array($payType, ['wallet', 'directly', 'later']) => ['pay_type' => 'invalid pay type'],
7070
$product->max_quantity < 1 || $quantity > $product->max_quantity => ['quantity' => 'maximum exceeded'],
71-
$payType === 'later' && !$product->has('allow_pay_later') => ['pay_later' => 'pay later not allowed'],
71+
$payType === 'later' && !$product->has('allow_late_payments') => ['pay_later' => 'pay later not allowed'],
7272
!$user->active || !$user->has('allow_orders') => ['user' => 'permission access denied'],
7373
!$product->active || !$product->has('allow_orders') => ['product' => 'permission access denied'],
7474
!$product->store?->active || !$product->store->has('allow_orders') => ['store' => 'permission access denied'],
@@ -177,7 +177,7 @@ public function startOrder ( Product $product, User $user, int $quantity, string
177177
});
178178

179179
}
180-
180+
181181
public function payOrder ( Order $order, array $data = [] ) {
182182

183183
$amount = $order->canPay() ? $order->payAmount(float($data['amount'] ?? 0)) : 0;

app/Services/SettingService.php

Lines changed: 20 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -17,89 +17,37 @@ public function __construct(
1717
protected ReferralRepository $referralRepository
1818
) { parent::__construct($settingRepository); }
1919

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;
20+
public function executeCommission ( User $user, array $context = [] ) {
4221

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;
22+
$context = optional((object) $context);
6023

61-
$wallet = $type !== 'coupon' ? $user->wallet : null;
62-
$coupon = $type === 'coupon' ? $this->couponRepository->find($couponId) : null;
24+
if ( $context->type === 'coupon' && !$coupon = $this->couponRepository->find(integer($context->coupon_id)) ) return;
25+
if ( $context->type !== 'coupon' && !$wallet = $user->wallet ) return;
6326

64-
return [
65-
'user' => $user,
66-
'wallet' => $wallet,
67-
'coupon' => $coupon,
68-
'balance' => $balance,
69-
'amount' => $amount,
70-
'points' => $points,
71-
'type' => $type,
72-
'key' => $key,
73-
];
27+
return match ( $context->type ) {
28+
'coupon' => $this->couponRepository->redeem($coupon, $user),
29+
'points' => $this->walletRepository->addPoints($wallet, $context->reward_points),
30+
'amount' => $this->walletRepository->addCommission($wallet, $context->reward_amount, $context->balance, $context->key),
31+
default => null,
32+
};
7433

7534
}
76-
public function applyCommission ( Setting $commission, User $user, float $amount = null ) {
35+
public function applyCommission ( Setting $setting, User $user, float $amount = null ) {
7736

78-
if ( !$context = $this->contextCommission($commission, $user, $amount) ) return;
37+
if ( $setting->key === 'referral' ) $user = $this->referralRepository->findReferrer($user);
38+
if ( !$user || !$this->settingRepository->validateCommission($setting, $user, $amount) ) return;
39+
40+
$context = $this->settingRepository->commissionContext($setting, $amount);
41+
if ( !$reward = $this->executeCommission($user, $context) ) return;
7942

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;
43+
$this->settingRepository->setNewUsage($setting);
44+
return [...$context, 'user' => $user, 'reward' => $reward];
9745

9846
}
9947
public function resolveCommission ( User $user, string $key, float $amount = null ) {
10048

101-
$record = $this->query()->where('group', 'commission')->where('key', $key)->active()->latest()->first();
102-
return $record ? $this->applyCommission($record, $user, $amount) : null;
49+
$setting = $this->settingRepository->findSetting($key, 'commission');
50+
return $setting ? $this->applyCommission($setting, $user, $amount) : null;
10351

10452
}
10553
public function handleCommission ( User $user, string $key, float $amount = null, bool $queue = true ) {

0 commit comments

Comments
 (0)