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', 'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1', 'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999', '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')); Voucher::create($request->except('_token'));
@ -94,7 +94,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4', 'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1', 'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999', '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')); $voucher->update($request->except('_token'));
@ -191,5 +191,4 @@ class VoucherController extends Controller
->rawColumns(['actions', 'code', 'status']) ->rawColumns(['actions', 'code', 'status'])
->make(); ->make();
} }
} }

View file

@ -44,7 +44,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4', 'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1', 'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999', '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()); return Voucher::create($request->all());
@ -88,7 +88,7 @@ class VoucherController extends Controller
'code' => 'required|string|alpha_dash|max:36|min:4', 'code' => 'required|string|alpha_dash|max:36|min:4',
'uses' => 'required|numeric|max:2147483647|min:1', 'uses' => 'required|numeric|max:2147483647|min:1',
'credits' => 'required|numeric|between:0,99999999', '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()); $voucher->update($request->all());
@ -116,7 +116,8 @@ class VoucherController extends Controller
* @param Voucher $voucher * @param Voucher $voucher
* @return LengthAwarePaginator * @return LengthAwarePaginator
*/ */
public function users(Request $request, Voucher $voucher){ public function users(Request $request, Voucher $voucher)
{
$request->validate([ $request->validate([
'include' => [ 'include' => [
'nullable', 'nullable',

View file

@ -4,6 +4,7 @@ namespace App\Providers;
use Illuminate\Pagination\Paginator; use Illuminate\Pagination\Paginator;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
@ -27,5 +28,27 @@ class AppServiceProvider extends ServiceProvider
{ {
Paginator::useBootstrap(); Paginator::useBootstrap();
Schema::defaultStringLength(191); 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

@ -39,9 +39,7 @@
<div class="form-group"> <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> <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" <input value="{{old('memo')}}" placeholder="Summer break voucher" id="memo" name="memo" type="text" class="form-control @error('memo') is-invalid @enderror">
name="memo" type="text"
class="form-control @error('memo') is-invalid @enderror">
@error('memo') @error('memo')
<div class="text-danger"> <div class="text-danger">
{{$message}} {{$message}}
@ -51,10 +49,7 @@
<div class="form-group"> <div class="form-group">
<label for="credits">* Credits</label> <label for="credits">* Credits</label>
<input value="{{old('credits')}}" placeholder="500" id="credits" <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">
name="credits" type="number" step="any" min="0"
max="99999999"
class="form-control @error('credits') is-invalid @enderror">
@error('credits') @error('credits')
<div class="text-danger"> <div class="text-danger">
{{$message}} {{$message}}
@ -66,10 +61,7 @@
<div class="form-group"> <div class="form-group">
<label for="code">* Code</label> <label for="code">* Code</label>
<div class="input-group"> <div class="input-group">
<input value="{{old('code')}}" placeholder="SUMMER" id="code" name="code" <input value="{{old('code')}}" placeholder="SUMMER" id="code" name="code" type="text" class="form-control @error('code') is-invalid @enderror" required="required">
type="text"
class="form-control @error('code') is-invalid @enderror"
required="required">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button"> <button class="btn btn-info" onclick="setRandomCode()" type="button">
Random Random
@ -86,10 +78,7 @@
<div class="form-group"> <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> <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"> <div class="input-group">
<input value="{{old('uses') ?? 1}}" id="uses" min="1" max="2147483647" <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">
name="uses" type="number"
class="form-control @error('uses') is-invalid @enderror"
required="required">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max <button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button> </button>
@ -103,9 +92,9 @@
</div> </div>
<div class="form-group mb-3"> <div class="form-group mb-3">
<label for="expires_at">Expires at</label> <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"> <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"/> <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-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div> <div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div> </div>
@ -138,7 +127,7 @@
<script> <script>
document.addEventListener('DOMContentLoaded', (event) => { document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({ $('#expires_at').datetimepicker({
format : 'DD-MM-yyyy', format: 'DD-MM-yyyy HH:mm:ss',
icons: { icons: {
time: 'far fa-clock', time: 'far fa-clock',
date: 'far fa-calendar', date: 'far fa-calendar',

View file

@ -12,8 +12,7 @@
<ol class="breadcrumb float-sm-right"> <ol class="breadcrumb float-sm-right">
<li class="breadcrumb-item"><a href="{{route('home')}}">Dashboard</a></li> <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 href="{{route('admin.vouchers.index')}}">Vouchers</a></li>
<li class="breadcrumb-item"><a class="text-muted" <li class="breadcrumb-item"><a class="text-muted" href="{{route('admin.products.edit' , $voucher->id)}}">Edit</a>
href="{{route('admin.products.edit' , $voucher->id)}}">Edit</a>
</li> </li>
</ol> </ol>
</div> </div>
@ -40,12 +39,8 @@
@method('PATCH') @method('PATCH')
<div class="form-group"> <div class="form-group">
<label for="memo">Memo <i data-toggle="popover" data-trigger="hover" <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>
data-content="Only admins can see this" <input value="{{ $voucher->memo }}" placeholder="Summer break voucher" id="memo" name="memo" type="text" class="form-control @error('memo') is-invalid @enderror">
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') @error('memo')
<div class="text-danger"> <div class="text-danger">
{{$message}} {{$message}}
@ -55,10 +50,7 @@
<div class="form-group"> <div class="form-group">
<label for="credits">Credits *</label> <label for="credits">Credits *</label>
<input value="{{$voucher->credits}}" placeholder="500" id="credits" <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">
name="credits" type="number" step="any" min="0"
max="99999999"
class="form-control @error('credits') is-invalid @enderror">
@error('credits') @error('credits')
<div class="text-danger"> <div class="text-danger">
{{$message}} {{$message}}
@ -70,10 +62,7 @@
<div class="form-group"> <div class="form-group">
<label for="code">Code *</label> <label for="code">Code *</label>
<div class="input-group"> <div class="input-group">
<input value="{{$voucher->code}}" placeholder="SUMMER" id="code" name="code" <input value="{{$voucher->code}}" placeholder="SUMMER" id="code" name="code" type="text" class="form-control @error('code') is-invalid @enderror" required="required">
type="text"
class="form-control @error('code') is-invalid @enderror"
required="required">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-info" onclick="setRandomCode()" type="button"> <button class="btn btn-info" onclick="setRandomCode()" type="button">
Random Random
@ -88,14 +77,9 @@
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="uses">Uses * <i data-toggle="popover" data-trigger="hover" <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>
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"> <div class="input-group">
<input value="{{$voucher->uses}}" id="uses" min="1" max="2147483647" <input value="{{$voucher->uses}}" id="uses" min="1" max="2147483647" name="uses" type="number" class="form-control @error('uses') is-invalid @enderror" required="required">
name="uses" type="number"
class="form-control @error('uses') is-invalid @enderror"
required="required">
<div class="input-group-append"> <div class="input-group-append">
<button class="btn btn-info" onclick="setMaxUses()" type="button">Max <button class="btn btn-info" onclick="setMaxUses()" type="button">Max
</button> </button>
@ -109,9 +93,9 @@
</div> </div>
<div class="form-group mb-3"> <div class="form-group mb-3">
<label for="expires_at">Expires at</label> <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"> <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"/> <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-append" data-target="#expires_at" data-toggle="datetimepicker">
<div class="input-group-text"><i class="fa fa-calendar"></i></div> <div class="input-group-text"><i class="fa fa-calendar"></i></div>
</div> </div>
@ -143,7 +127,7 @@
<script> <script>
document.addEventListener('DOMContentLoaded', (event) => { document.addEventListener('DOMContentLoaded', (event) => {
$('#expires_at').datetimepicker({ $('#expires_at').datetimepicker({
format : 'DD-MM-yyyy', format: 'DD-MM-yyyy HH:mm:ss',
icons: { icons: {
time: 'far fa-clock', time: 'far fa-clock',
date: 'far fa-calendar', date: 'far fa-calendar',