Skip to content

Commit 6622e72

Browse files
committed
some suggestion added
1 parent b7da428 commit 6622e72

File tree

1 file changed

+24
-25
lines changed

1 file changed

+24
-25
lines changed

src/Services/CouponValidityService.php

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ protected function validation($array)
4646
throw new CouponValidationException("user_limit accepted integer value", 422);
4747
}
4848

49-
}
49+
}//end method validation
5050

5151
/**
5252
* Coupon update validation
@@ -81,7 +81,7 @@ protected function updateValidation($array)
8181
throw new CouponValidationException("user_limit accepted integer value", 422);
8282
}
8383

84-
}
84+
}//end method updateValidation
8585

8686
/**
8787
* Coupon history validation
@@ -98,7 +98,7 @@ protected function historyValidation($array)
9898

9999
$requiredFields = ['user_id', 'coupon_id', 'order_id', 'discount_amount'];
100100
$this->fieldValidation($array, $requiredFields);
101-
}
101+
}//end method historyValidation
102102

103103
/**
104104
* Apply coupon validation
@@ -118,7 +118,7 @@ protected function applyValidation($array)
118118
}
119119

120120
$this->fieldValidation($array, ["code", "amount", "user_id", "order_id"]);
121-
}
121+
}//end method applyValidation
122122

123123
/**
124124
* Array data validation
@@ -149,20 +149,19 @@ private function fieldValidation(array $array, array $requiredFields)
149149
throw new CouponValidationException("$filed is required", 422);
150150
}
151151
}
152-
}
152+
}//end method fieldValidation
153153

154154
/**
155155
* Check coupon validity
156156
*
157-
* @param string $couponCode
158-
* @param float $amount
157+
* @param string $couponCode {coupon code}
158+
* @param float $amount (cart sub total amount)
159159
*
160-
* @param string $userId
161-
* @param string|null $deviceName
162-
*
163-
* @param string|null $ipaddress
164-
* @param string $vendorId
165-
* @param array $skip
160+
* @param string $userId {user id}
161+
* @param string|null $deviceName {device ref, example: web, android, ios etc, default empty}
162+
* @param string|null $ipaddress {ip address }
163+
* @param string $vendorId {vendor id/shop id}
164+
* @param array $skip {some functionality need to skip from different scope while applying the coupon}
166165
*
167166
* @return array|\Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object
168167
* @throws CouponException
@@ -178,46 +177,47 @@ public function validity($couponCode, float $amount, string $userId, string $dev
178177
])
179178
->first();
180179

180+
//01.Check if coupon exists
181181
if (!$coupon) {
182182
throw new CouponException("Invalid coupon code!", 500);
183183
}
184184

185-
// check coupon status
185+
//02. Check coupon status
186186
if ($coupon->status != 1) {
187187
throw new CouponException("Coupon apply failed. This coupon is inactive.", 500);
188188
}
189189

190-
// check coupon start date validity
190+
//03. Check coupon start date validity
191191
if ($coupon->start_date > Carbon::today()->toDateTimeString()) {
192192
throw new CouponException("Coupon apply failed! Invalid coupon code.", 500);
193193
}
194194

195-
// check coupon end date validity
195+
//04. Check coupon end date validity
196196
if ($coupon->end_date && $coupon->end_date < Carbon::today()->toDateTimeString()) {
197197
throw new CouponException("Coupon apply failed! This coupon has expired.", 500);
198198
}
199199

200-
// check coupon per user use limitation
200+
//05. check coupon per user use limitation
201201
if ($coupon->use_limit_per_user && $coupon->use_limit_per_user > 0) {
202202
$couponHistories = $coupon->couponHistories->where("user_id", $userId);
203203
if ($couponHistories && $couponHistories->count() >= $coupon->use_limit_per_user) {
204204
throw new CouponException("Coupon apply failed! You have overcome the usage limit.", 500);
205205
}
206206
}
207207

208-
// check total coupon applied limitation
208+
//06. Check total coupon applied limitation
209209
if ($coupon->use_limit && $coupon->use_limit > 0) {
210210
if ($coupon->couponHistories->count() && $coupon->couponHistories->count() >= $coupon->use_limit) {
211211
throw new CouponException("The coupon apply failed! Because of overcoming the total usage limit.", 500);
212212
}
213213
}
214214

215-
// check minimum order amount to applied this coupon
215+
//07. Check minimum order amount to applied this coupon
216216
if ($coupon->minimum_spend > 0 && $coupon->minimum_spend > $amount) {
217217
throw new CouponException("Invalid Amount! To apply this coupon minimum {$coupon->minimum_spend} amount is required", 500);
218218
}
219219

220-
// check maximum order amount to applied this coupon
220+
//08. Check maximum order amount to applied this coupon
221221
if ($coupon->maximum_spend > 0 && $coupon->maximum_spend < $amount) {
222222
throw new CouponException("Invalid Amount! To apply this coupon maximum {$coupon->maximum_spend} amount is required", 500);
223223
}
@@ -263,16 +263,15 @@ public function validity($couponCode, float $amount, string $userId, string $dev
263263
// calculate discount amount
264264
$discount_amount = 0;
265265
if ($coupon->type == 'fixed') {
266-
$discount_amount = floatval($coupon->amount);
266+
$discount_amount += floatval($coupon->amount);
267267
} else {
268268
$discount_percentage = floatval($coupon->amount);
269-
$discount_amount = ($discount_percentage / 100) * floatval($amount);
269+
$discount_amount += ($discount_percentage / 100) * floatval($amount);
270270
}
271271

272272
$coupon->discount_amount = $discount_amount;
273273

274274
return $coupon;
275+
}//end method validity
275276

276-
}
277-
278-
}
277+
}//end class CouponValidityService

0 commit comments

Comments
 (0)