[
MAINHACK
]
Mail Test
BC
Config Scan
HOME
Create...
New File
New Folder
Viewing / Editing File: CouponService.php
<?php namespace App\Services; use App\Models\OrderCoupon; use Carbon\Carbon; use Exception; use App\Models\Coupon; use App\Libraries\AppLibrary; use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Log; use App\Http\Requests\CouponRequest; use App\Http\Requests\PaginateRequest; use App\Http\Requests\CouponCheckRequest; class CouponService { public $coupon; protected $couponFilter = [ 'name', 'code', 'discount', 'discount_type', 'start_date', 'end_date', 'minimum_order', 'maximum_discount', 'limit_per_user', ]; protected $exceptFilter = [ 'excepts' ]; /** * @throws Exception */ public function list(PaginateRequest $request) { try { $requests = $request->all(); $method = $request->get('paginate', 0) == 1 ? 'paginate' : 'get'; $methodValue = $request->get('paginate', 0) == 1 ? $request->get('per_page', 10) : '*'; $orderColumn = $request->get('order_column') ?? 'id'; $orderType = $request->get('order_type') ?? 'desc'; return Coupon::where(function ($query) use ($requests) { foreach ($requests as $key => $request) { if (in_array($key, $this->couponFilter)) { if ($key == "start_date") { $start_date = Date('Y-m-d', strtotime($request)); $query->whereDate($key, '=', $start_date); } else if ($key == "end_date") { $end_date = Date('Y-m-d', strtotime($request)); $query->whereDate($key, '=', $end_date); } else { $query->where($key, 'like', '%' . $request . '%'); } } if (in_array($key, $this->exceptFilter)) { $explodes = explode('|', $request); if (is_array($explodes)) { foreach ($explodes as $explode) { $query->where('id', '!=', $explode); } } } } })->orderBy($orderColumn, $orderType)->$method( $methodValue ); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function store(CouponRequest $request) { try { $this->coupon = Coupon::create([ 'name' => $request->name, 'description' => $request->description, 'code' => $request->code, 'discount' => $request->discount, 'discount_type' => $request->discount_type, 'start_date' => !blank($request->start_date) ? date( 'Y-m-d H:i:s', strtotime($request->start_date) ) : "", 'end_date' => !blank($request->end_date) ? date( 'Y-m-d H:i:s', strtotime($request->end_date) ) : "", 'minimum_order' => $request->minimum_order, 'maximum_discount' => $request->maximum_discount, 'limit_per_user' => $request->limit_per_user, ]); if ($request->image) { $this->coupon->addMedia($request->image)->toMediaCollection('coupon'); } return $this->coupon; } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function update(CouponRequest $request, Coupon $coupon) { try { DB::transaction(function () use ($request, $coupon) { $this->coupon = $coupon; $coupon->name = $request->name; $coupon->description = $request->description; $coupon->code = $request->code; $coupon->discount = $request->discount; $coupon->discount_type = $request->discount_type; $coupon->start_date = !blank($request->start_date) ? date( 'Y-m-d H:i:s', strtotime($request->start_date) ) : null; $coupon->end_date = !blank($request->end_date) ? date( 'Y-m-d H:i:s', strtotime($request->end_date) ) : null; $coupon->minimum_order = $request->minimum_order; $coupon->maximum_discount = $request->maximum_discount; $coupon->limit_per_user = $request->limit_per_user; $coupon->save(); if ($request->image) { $coupon->media()->delete(); $coupon->addMedia($request->image)->toMediaCollection('coupon'); } }); return $this->coupon; } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function destroy(Coupon $coupon) { try { $coupon->delete(); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function show(Coupon $coupon): Coupon { try { return $coupon; } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function couponDateWise(): \Illuminate\Database\Eloquent\Collection { try { return Coupon::all()->filter(function ($item) { if (Carbon::now()->between($item->start_date, $item->end_date)) { return $item; } }); } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } /** * @throws Exception */ public function couponChecking(CouponCheckRequest $request) { try { $coupon = Coupon::where(['code' => $request->code])->first(); if ($coupon) { if ($coupon->minimum_order > $request->total) { throw new Exception(trans('all.message.minimum_order_amount') . AppLibrary::convertAmountFormat($coupon->minimum_order), 422); } else { if (strtotime($coupon->end_date) >= strtotime(Carbon::now())) { $ordered_coupon_count = OrderCoupon::where(['user_id' => auth()->user()->id, 'coupon_id' => $coupon->id])->count(); if($coupon->limit_per_user <= $ordered_coupon_count){ throw new Exception(trans('all.message.coupon_limit_exceeded'), 422); } return $coupon; } else { throw new Exception(trans('all.message.coupon_date_expired'), 422); } } } else { throw new Exception(trans('all.message.coupon_not_exist'), 422); } } catch (Exception $exception) { Log::info($exception->getMessage()); throw new Exception($exception->getMessage(), 422); } } }
Save Changes
Cancel / Back
Close ×
Server Info
Hostname: server1.winmanyltd.com
Server IP: 203.161.60.52
PHP Version: 8.3.27
Server Software: Apache
System: Linux server1.winmanyltd.com 4.18.0-553.22.1.el8_10.x86_64 #1 SMP Tue Sep 24 05:16:59 EDT 2024 x86_64
HDD Total: 117.98 GB
HDD Free: 60.08 GB
Domains on IP: N/A (Requires external lookup)
System Features
Safe Mode:
Off
disable_functions:
None
allow_url_fopen:
On
allow_url_include:
Off
magic_quotes_gpc:
Off
register_globals:
Off
open_basedir:
None
cURL:
Enabled
ZipArchive:
Enabled
MySQLi:
Enabled
PDO:
Enabled
wget:
Yes
curl (cmd):
Yes
perl:
Yes
python:
Yes (py3)
gcc:
Yes
pkexec:
Yes
git:
Yes
User Info
Username: eliosofonline
User ID (UID): 1002
Group ID (GID): 1003
Script Owner UID: 1002
Current Dir Owner: 1002