Add support to add time for voucher expires_at

This commit is contained in:
Jovan Jovanovic 2021-07-28 18:22:12 +02:00
parent 07e4537e01
commit f0a31fd4e2
5 changed files with 310 additions and 314 deletions

View file

@ -48,7 +48,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
]);
Voucher::create($request->except('_token'));
@ -75,7 +75,7 @@ class VoucherController extends Controller
*/
public function edit(Voucher $voucher)
{
return view('admin.vouchers.edit' , [
return view('admin.vouchers.edit', [
'voucher' => $voucher
]);
}
@ -94,7 +94,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => ['nullable','date_format:d-m-Y','after:today',"before:10 years"],
'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years',
]);
$voucher->update($request->except('_token'));
@ -127,7 +127,7 @@ class VoucherController extends Controller
]);
#get voucher by code
$voucher = Voucher::where('code' , '=' , $request->input('code'))->firstOrFail();
$voucher = Voucher::where('code', '=', $request->input('code'))->firstOrFail();
#extra validations
if ($voucher->getStatus() == 'USES_LIMIT_REACHED') throw ValidationException::withMessages([
@ -138,7 +138,7 @@ class VoucherController extends Controller
'code' => 'This voucher has expired'
]);
if (!$request->user()->vouchers()->where('id' , '=' , $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
if (!$request->user()->vouchers()->where('id', '=', $voucher->id)->get()->isEmpty()) throw ValidationException::withMessages([
'code' => 'You already redeemed this voucher code'
]);
@ -191,5 +191,4 @@ class VoucherController extends Controller
->rawColumns(['actions', 'code', 'status'])
->make();
}
}

View file

@ -44,7 +44,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years'
]);
return Voucher::create($request->all());
@ -88,7 +88,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999',
'expires_at' => 'nullable|date_format:d-m-Y|after:today|before:10 years'
'expires_at' => 'nullable|multiple_date_format:d-m-Y H:i:s,d-m-Y|after:now|before:10 years'
]);
$voucher->update($request->all());
@ -116,7 +116,8 @@ class VoucherController extends Controller
* @param Voucher $voucher
* @return LengthAwarePaginator
*/
public function users(Request $request, Voucher $voucher){
public function users(Request $request, Voucher $voucher)
{
$request->validate([
'include' => [
'nullable',
@ -125,7 +126,7 @@ class VoucherController extends Controller
]
]);
if($request->input('include') == 'discorduser'){
if ($request->input('include') == 'discorduser') {
return $voucher->users()->with('discordUser')->paginate($request->query('per_page') ?? 50);
}

View file

@ -4,6 +4,7 @@ namespace App\Providers;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
@ -27,5 +28,27 @@ class AppServiceProvider extends ServiceProvider
{
Paginator::useBootstrap();
Schema::defaultStringLength(191);
Validator::extend('multiple_date_format', function ($attribute, $value, $parameters, $validator) {
$ok = true;
$result = [];
// iterate through all formats
foreach ($parameters as $parameter) {
//validate with laravels standard date format validation
$result[] = $validator->validateDateFormat($attribute, $value, [$parameter]);
}
//if none of result array is true. it sets ok to false
if (!in_array(true, $result)) {
$ok = false;
$validator->setCustomMessages(['multiple_date_format' => 'The format must be one of ' . join(",", $parameters)]);
}
return $ok;
});
}
}

View file

@ -1,181 +1,170 @@
@extends('layouts.main')
@section('content')
<!-- CONTENT HEADER -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Vouchers</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{route('home')}}">Dashboard</a></li>
<li class="breadcrumb-item"><a href="{{route('admin.vouchers.index')}}">Vouchers</a></li>
<li class="breadcrumb-item"><a class="text-muted" href="{{route('admin.products.create')}}">Create</a>
</li>
</ol>
</div>
<!-- CONTENT HEADER -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Vouchers</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{route('home')}}">Dashboard</a></li>
<li class="breadcrumb-item"><a href="{{route('admin.vouchers.index')}}">Vouchers</a></li>
<li class="breadcrumb-item"><a class="text-muted" href="{{route('admin.products.create')}}">Create</a>
</li>
</ol>
</div>
</div>
</section>
<!-- END CONTENT HEADER -->
</div>
</section>
<!-- END CONTENT HEADER -->
<!-- MAIN CONTENT -->
<section class="content">
<div class="container-fluid">
<!-- MAIN CONTENT -->
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title">
<i class="fas fa-money-check-alt mr-2"></i>Voucher details
</h5>
</div>
<div class="card-body">
<form action="{{route('admin.vouchers.store')}}" method="POST">
@csrf
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title">
<i class="fas fa-money-check-alt mr-2"></i>Voucher details
</h5>
</div>
<div class="card-body">
<form action="{{route('admin.vouchers.store')}}" method="POST">
@csrf
<div class="form-group">
<label for="memo">Memo <i data-toggle="popover" data-trigger="hover" data-content="Only admins can see this" class="fas fa-info-circle"></i></label>
<input value="{{old('memo')}}" placeholder="Summer break voucher" id="memo"
name="memo" type="text"
class="form-control @error('memo') is-invalid @enderror">
@error('memo')
<div class="text-danger">
{{$message}}
</div>
@enderror
<div class="form-group">
<label for="memo">Memo <i data-toggle="popover" data-trigger="hover" data-content="Only admins can see this" class="fas fa-info-circle"></i></label>
<input value="{{old('memo')}}" placeholder="Summer break voucher" id="memo" name="memo" type="text" class="form-control @error('memo') is-invalid @enderror">
@error('memo')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="credits">* Credits</label>
<input value="{{old('credits')}}" placeholder="500" id="credits"
name="credits" type="number" step="any" min="0"
max="99999999"
class="form-control @error('credits') is-invalid @enderror">
@error('credits')
<div class="text-danger">
{{$message}}
</div>
@enderror
<div class="form-group">
<label for="credits">* Credits</label>
<input value="{{old('credits')}}" placeholder="500" id="credits" name="credits" type="number" step="any" min="0" max="99999999" class="form-control @error('credits') is-invalid @enderror">
@error('credits')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="code">* Code</label>
<div class="input-group">
<input value="{{old('code')}}" placeholder="SUMMER" id="code" name="code"
type="text"
class="form-control @error('code') is-invalid @enderror"
required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button">
Random
</button>
</div>
<div class="form-group">
<label for="code">* Code</label>
<div class="input-group">
<input value="{{old('code')}}" placeholder="SUMMER" id="code" name="code" type="text" class="form-control @error('code') is-invalid @enderror" required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button">
Random
</button>
</div>
@error('code')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="uses">* Uses<i data-toggle="popover" data-trigger="hover" data-content="A voucher can only be used one time per user. Uses specifies the number of different users that can use this voucher." class="fas fa-info-circle"></i></label>
<div class="input-group">
<input value="{{old('uses') ?? 1}}" id="uses" min="1" max="2147483647"
name="uses" type="number"
class="form-control @error('uses') is-invalid @enderror"
required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button>
</div>
</div>
@error('uses')
<div class="text-danger">
{{$message}}
</div>
@enderror
@error('code')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group mb-3">
<label for="expires_at">Expires at</label>
<div class="input-group date" id="expires_at" data-target-input="nearest">
<input value="{{old('expires_at')}}" name="expires_at" placeholder="dd-mm-yyyy" type="text" class="form-control @error('expires_at') is-invalid @enderror datetimepicker-input" data-target="#expires_at"/>
<div class="input-group-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
<div class="form-group">
<label for="uses">* Uses <i data-toggle="popover" data-trigger="hover" data-content="A voucher can only be used one time per user. Uses specifies the number of different users that can use this voucher." class="fas fa-info-circle"></i></label>
<div class="input-group">
<input value="{{old('uses') ?? 1}}" id="uses" min="1" max="2147483647" name="uses" type="number" class="form-control @error('uses') is-invalid @enderror" required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button>
</div>
@error('expires_at')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
@error('uses')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary">
Submit
</button>
<div class="form-group mb-3">
<label for="expires_at">Expires at <i data-toggle="popover" data-trigger="hover" data-content="Timezone: {{ Config::get('app.timezone') }}" class="fas fa-info-circle"></i></label>
<div class="input-group date" id="expires_at" data-target-input="nearest">
<input value="{{old('expires_at')}}" name="expires_at" placeholder="dd-mm-yyyy hh:mm:ss" type="text" class="form-control @error('expires_at') is-invalid @enderror datetimepicker-input" data-target="#expires_at" />
<div class="input-group-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
</div>
</form>
</div>
@error('expires_at')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</form>
</div>
</div>
</div>
<i class="fas"></i>
</div>
</section>
<!-- END CONTENT -->
<i class="fas"></i>
</div>
</section>
<!-- END CONTENT -->
<script>
document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({
format : 'DD-MM-yyyy',
icons: {
time: 'far fa-clock',
date: 'far fa-calendar',
up: 'fas fa-arrow-up',
down: 'fas fa-arrow-down',
previous: 'fas fa-chevron-left',
next: 'fas fa-chevron-right',
today: 'fas fa-calendar-check',
clear: 'far fa-trash-alt',
close: 'far fa-times-circle'
}
});
})
function setMaxUses() {
let element = document.getElementById('uses')
element.value = element.max;
console.log(element.max)
}
function setRandomCode() {
let element = document.getElementById('code')
element.value = getRandomCode(36)
}
function getRandomCode(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
<script>
document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({
format: 'DD-MM-yyyy HH:mm:ss',
icons: {
time: 'far fa-clock',
date: 'far fa-calendar',
up: 'fas fa-arrow-up',
down: 'fas fa-arrow-down',
previous: 'fas fa-chevron-left',
next: 'fas fa-chevron-right',
today: 'fas fa-calendar-check',
clear: 'far fa-trash-alt',
close: 'far fa-times-circle'
}
return result;
});
})
function setMaxUses() {
let element = document.getElementById('uses')
element.value = element.max;
console.log(element.max)
}
function setRandomCode() {
let element = document.getElementById('code')
element.value = getRandomCode(36)
}
function getRandomCode(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
</script>
return result;
}
</script>
@endsection
@endsection

View file

@ -1,186 +1,170 @@
@extends('layouts.main')
@section('content')
<!-- CONTENT HEADER -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Vouchers</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{route('home')}}">Dashboard</a></li>
<li class="breadcrumb-item"><a href="{{route('admin.vouchers.index')}}">Vouchers</a></li>
<li class="breadcrumb-item"><a class="text-muted"
href="{{route('admin.products.edit' , $voucher->id)}}">Edit</a>
</li>
</ol>
</div>
<!-- CONTENT HEADER -->
<section class="content-header">
<div class="container-fluid">
<div class="row mb-2">
<div class="col-sm-6">
<h1>Vouchers</h1>
</div>
<div class="col-sm-6">
<ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{route('home')}}">Dashboard</a></li>
<li class="breadcrumb-item"><a href="{{route('admin.vouchers.index')}}">Vouchers</a></li>
<li class="breadcrumb-item"><a class="text-muted" href="{{route('admin.products.edit' , $voucher->id)}}">Edit</a>
</li>
</ol>
</div>
</div>
</section>
<!-- END CONTENT HEADER -->
</div>
</section>
<!-- END CONTENT HEADER -->
<!-- MAIN CONTENT -->
<section class="content">
<div class="container-fluid">
<!-- MAIN CONTENT -->
<section class="content">
<div class="container-fluid">
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title">
<i class="fas fa-money-check-alt mr-2"></i>Voucher details
</h5>
</div>
<div class="card-body">
<form action="{{route('admin.vouchers.update' , $voucher->id)}}" method="POST">
@csrf
@method('PATCH')
<div class="row">
<div class="col-lg-6">
<div class="card">
<div class="card-header">
<h5 class="card-title">
<i class="fas fa-money-check-alt mr-2"></i>Voucher details
</h5>
</div>
<div class="card-body">
<form action="{{route('admin.vouchers.update' , $voucher->id)}}" method="POST">
@csrf
@method('PATCH')
<div class="form-group">
<label for="memo">Memo <i data-toggle="popover" data-trigger="hover"
data-content="Only admins can see this"
class="fas fa-info-circle"></i></label>
<input value="{{ $voucher->memo }}" placeholder="Summer break voucher" id="memo"
name="memo" type="text"
class="form-control @error('memo') is-invalid @enderror">
@error('memo')
<div class="text-danger">
{{$message}}
</div>
@enderror
<div class="form-group">
<label for="memo">Memo <i data-toggle="popover" data-trigger="hover" data-content="Only admins can see this" class="fas fa-info-circle"></i></label>
<input value="{{ $voucher->memo }}" placeholder="Summer break voucher" id="memo" name="memo" type="text" class="form-control @error('memo') is-invalid @enderror">
@error('memo')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="credits">Credits *</label>
<input value="{{$voucher->credits}}" placeholder="500" id="credits"
name="credits" type="number" step="any" min="0"
max="99999999"
class="form-control @error('credits') is-invalid @enderror">
@error('credits')
<div class="text-danger">
{{$message}}
</div>
@enderror
<div class="form-group">
<label for="credits">Credits *</label>
<input value="{{$voucher->credits}}" placeholder="500" id="credits" name="credits" type="number" step="any" min="0" max="99999999" class="form-control @error('credits') is-invalid @enderror">
@error('credits')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="code">Code *</label>
<div class="input-group">
<input value="{{$voucher->code}}" placeholder="SUMMER" id="code" name="code"
type="text"
class="form-control @error('code') is-invalid @enderror"
required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button">
Random
</button>
</div>
<div class="form-group">
<label for="code">Code *</label>
<div class="input-group">
<input value="{{$voucher->code}}" placeholder="SUMMER" id="code" name="code" type="text" class="form-control @error('code') is-invalid @enderror" required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button">
Random
</button>
</div>
@error('code')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group">
<label for="uses">Uses * <i data-toggle="popover" data-trigger="hover"
data-content="A voucher can only be used one time per user. Uses specifies the number of different users that can use this voucher."
class="fas fa-info-circle"></i></label>
<div class="input-group">
<input value="{{$voucher->uses}}" id="uses" min="1" max="2147483647"
name="uses" type="number"
class="form-control @error('uses') is-invalid @enderror"
required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button>
</div>
</div>
@error('uses')
<div class="text-danger">
{{$message}}
</div>
@enderror
@error('code')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group mb-3">
<label for="expires_at">Expires at</label>
<div class="input-group date" id="expires_at" data-target-input="nearest">
<input value="{{$voucher->expires_at ? $voucher->expires_at->format('d-m-Y') : ''}}" name="expires_at" placeholder="dd-mm-yyyy" type="text" class="form-control @error('expires_at') is-invalid @enderror datetimepicker-input" data-target="#expires_at"/>
<div class="input-group-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
<div class="form-group">
<label for="uses">Uses * <i data-toggle="popover" data-trigger="hover" data-content="A voucher can only be used one time per user. Uses specifies the number of different users that can use this voucher." class="fas fa-info-circle"></i></label>
<div class="input-group">
<input value="{{$voucher->uses}}" id="uses" min="1" max="2147483647" name="uses" type="number" class="form-control @error('uses') is-invalid @enderror" required="required">
<div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button>
</div>
@error('expires_at')
<div class="text-danger">
{{$message}}
</div>
@error('uses')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group mb-3">
<label for="expires_at">Expires at <i data-toggle="popover" data-trigger="hover" data-content="Timezone: {{ Config::get('app.timezone') }}" class="fas fa-info-circle"></i></label>
<div class="input-group date" id="expires_at" data-target-input="nearest">
<input value="{{$voucher->expires_at ? $voucher->expires_at->format('d-m-Y H:i:s') : ''}}" name="expires_at" placeholder="dd-mm-yyyy hh:mm:ss" type="text" class="form-control @error('expires_at') is-invalid @enderror datetimepicker-input" data-target="#expires_at" />
<div class="input-group-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div>
@enderror
</div>
@error('expires_at')
<div class="text-danger">
{{$message}}
</div>
@enderror
</div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</form>
</div>
<div class="form-group text-right">
<button type="submit" class="btn btn-primary">
Submit
</button>
</div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- END CONTENT -->
</div>
</section>
<!-- END CONTENT -->
<script>
document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({
format : 'DD-MM-yyyy',
icons: {
time: 'far fa-clock',
date: 'far fa-calendar',
up: 'fas fa-arrow-up',
down: 'fas fa-arrow-down',
previous: 'fas fa-chevron-left',
next: 'fas fa-chevron-right',
today: 'fas fa-calendar-check',
clear: 'far fa-trash-alt',
close: 'far fa-times-circle'
}
});
})
function setMaxUses() {
let element = document.getElementById('uses')
element.value = element.max;
console.log(element.max)
}
function setRandomCode() {
let element = document.getElementById('code')
element.value = getRandomCode(36)
}
function getRandomCode(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
<script>
document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({
format: 'DD-MM-yyyy HH:mm:ss',
icons: {
time: 'far fa-clock',
date: 'far fa-calendar',
up: 'fas fa-arrow-up',
down: 'fas fa-arrow-down',
previous: 'fas fa-chevron-left',
next: 'fas fa-chevron-right',
today: 'fas fa-calendar-check',
clear: 'far fa-trash-alt',
close: 'far fa-times-circle'
}
return result;
});
})
function setMaxUses() {
let element = document.getElementById('uses')
element.value = element.max;
console.log(element.max)
}
function setRandomCode() {
let element = document.getElementById('code')
element.value = getRandomCode(36)
}
function getRandomCode(length) {
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() *
charactersLength));
}
</script>
return result;
}
</script>
@endsection
@endsection