@@ -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