Edit File: CouponService.php
<?php namespace App\Services; use App\Models\Coupon; class CouponService { public static function enquiryCoupon(array $data) { $coupon = Coupon::where('code',$data['coupon'])->where('end_at','>',date('y-m-d'))->where('num_to_use','>',0)->first(); if(!$coupon){ return false; } if($coupon->code !== $data['coupon']){ return false; } $discount = 0; if($coupon->type == 'amount'){ $discount = $coupon->value; }else{ $discount = ($coupon->value/100) * $data['delivery_price']; } if($discount > $data['delivery_price']){ $discount = $data['delivery_price']; } return $discount; } public static function useCoupon(array $data) { $coupon = Coupon::where('code',$data['coupon'])->where('end_at','>',date('y-m-d'))->where('num_to_use','>',0)->first(); if($coupon){ $coupon->num_to_use -=1; $coupon->num_used += 1; $coupon->update(); } return true; } }
Back to File Manager