added discount controller

pull/44/head
A1Gard 6 months ago
parent 03b4d64893
commit a57e934a52

@ -0,0 +1,129 @@
<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Http\Controllers\XController;
use App\Http\Requests\DiscountSaveRequest;
use App\Models\Access;
use App\Models\Discount;
use Illuminate\Http\Request;
use App\Helper;
use function App\Helpers\hasCreateRoute;
class DiscountController extends XController
{
// protected $_MODEL_ = Discount::class;
// protected $SAVE_REQUEST = DiscountSaveRequest::class;
protected $cols = ['title','code','expire'];
protected $extra_cols = ['id'];
protected $searchable = ['title','code','body'];
protected $listView = 'admin.discounts.discount-list';
protected $formView = 'admin.discounts.discount-form';
protected $buttons = [
'edit' =>
['title' => "Edit", 'class' => 'btn-outline-primary', 'icon' => 'ri-edit-2-line'],
'show' =>
['title' => "Detail", 'class' => 'btn-outline-light', 'icon' => 'ri-eye-line'],
'destroy' =>
['title' => "Remove", 'class' => 'btn-outline-danger delete-confirm', 'icon' => 'ri-close-line'],
];
public function __construct()
{
parent::__construct(Discount::class, DiscountSaveRequest::class);
}
/**
* @param $discount Discount
* @param $request DiscountSaveRequest
* @return Discount
*/
public function save($discount, $request)
{
if ($request->product_id != ''){
$discount->product_id = $request->product_id;
}
$discount->title = $request->title;
$discount->body = $request->body;
$discount->amount = $request->amount;
$discount->expire = date('Y-m-d H:i:s',floor($request->expire));
$discount->code = $request->code;
$discount->type = $request->type;
$discount->save();
return $discount;
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
return view($this->formView);
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Discount $item)
{
//
return view($this->formView, compact('item'));
}
public function bulk(Request $request)
{
// dd($request->all());
$data = explode('.', $request->input('action'));
$action = $data[0];
$ids = $request->input('id');
switch ($action) {
case 'delete':
$msg = __(':COUNT items deleted successfully', ['COUNT' => count($ids)]);
$this->_MODEL_::destroy($ids);
break;
/**restore*/
case 'restore':
$msg = __(':COUNT items restored successfully', ['COUNT' => count($ids)]);
foreach ($ids as $id) {
$this->_MODEL_::withTrashed()->find($id)->restore();
}
break;
/*restore**/
default:
$msg = __('Unknown bulk action : :ACTION', ["ACTION" => $action]);
}
return $this->do_bulk($msg, $action, $ids);
}
public function destroy(Discount $item)
{
return parent::delete($item);
}
public function update(Request $request, Discount $item)
{
return $this->bringUp($request, $item);
}
/**restore*/
public function restore($item)
{
return parent::restoreing(Discount::withTrashed()->where('id', $item)->first());
}
/*restore**/
}

@ -0,0 +1,32 @@
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class DiscountSaveRequest extends FormRequest
{
/**
* Determine if the user is authorized to make this request.
*/
public function authorize(): bool
{
return auth()->check();
}
/**
* Get the validation rules that apply to the request.
*
* @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
*/
public function rules(): array
{
return [
//
'title' => ['nullable', 'string'],
'body' => ['nullable', 'string'],
'type' => ['required'],
'amount' => ['required', 'string', 'min:1'],
];
}
}

@ -9,6 +9,9 @@ class Adv extends Model
{
use SoftDeletes;
protected $casts = [
'expire' => 'date'
];
public function imgUrl()
{
if ($this->image == null) {

@ -4,8 +4,21 @@ namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Translatable\HasTranslations;
class Discount extends Model
{
use HasFactory;
use HasFactory,SoftDeletes,HasTranslations;
public $translatable = ['title', 'body'];
public static $doscount_type =['PRICE','PERCENT'];
protected $casts = [
'expire' => 'datetime'
];
public function product(){
return $this->belongsTo(Product::class);
}
}

@ -13,8 +13,10 @@ return new class extends Migration
{
Schema::create('discounts', function (Blueprint $table) {
$table->id();
$table->text('title')->nullable();
$table->longText('body')->nullable();
$table->unsignedBigInteger('product_id')->nullable()->default(null);
$table->enum('type',['price','percent']);
$table->enum('type',\App\Models\Discount::$doscount_type);
$table->string('code',100)->nullable()->default(null);
$table->unsignedBigInteger('amount');
$table->dateTime('expire')->default(null)->nullable();

@ -18,7 +18,9 @@ return new class extends Migration
$table->text('body');
$table->string('file',2048);
$table->string('type');
$table->boolean('is_fillable')->default(true);
$table->unsignedBigInteger('size')->default(0);
$table->morphs('attachable');
$table->timestamps();
});
}

@ -30,6 +30,7 @@ class DatabaseSeeder extends Seeder
CustomerSeeder::class,
CategorySeeder::class,
PropSeeder::class,
ProductSeeder::class,
]
);
}

@ -0,0 +1,143 @@
@extends('admin.templates.panel-form-template')
@section('title')
@if(isset($item))
{{__("Edit discount")}} [{{$item->title}}]
@else
{{__("Add new discount")}}
@endif -
@endsection
@section('form')
<div class="row">
<div class="col-lg-3">
@include('components.err')
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-message-3-line"></i>
{{__("Tips")}}
</h3>
<ul>
<li>
{{__("Recommends")}}
</li>
</ul>
</div>
<div class="item-list mb-3">
<h3 class="p-3">
<i class="ri-message-3-line"></i>
{{__("Discount data")}}
</h3>
<div class="px-3 pb-4">
<div class=" mt-3">
<div class="form-group">
<label for="code">
{{__('Code')}}
</label>
<input name="code" type="text" class="form-control @error('code') is-invalid @enderror" placeholder="{{__('Code')}}" value="{{old('code',$item->code??null)}}" />
</div>
</div>
<div class=" mt-3">
<div class="form-group">
<label for="expire">
{{__('Expire date')}}
</label>
<vue-datetime-picker-input
:xmin="{{strtotime('yesterday')}}"
xid="dp" xname="expire" xshow="datetime" xtitle="Expire date" def-tab="1"
@if(isset($item)) :xvalue="{{strtotime($item->expire)}}" @endif
:timepicker="true"
></vue-datetime-picker-input>
</div>
</div>
<div class=" mt-3">
<div class="form-group">
<label for="product_id">
{{__('Product')}}
</label>
<searchable-select
@error('product_id') :err="true" @enderror
:items='@json(\App\Models\Product::all(['id','name']))'
title-field="name"
value-field="id"
xlang="{{config('app.locale')}}"
xid="product_id"
xname="product_id"
@error('category_id') :err="true" @enderror
xvalue='{{old('product_id',$item->product_id??null)}}'
:close-on-Select="true"></searchable-select>
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-9 ps-xl-1 ps-xxl-1">
<div class="general-form ">
<h1>
@if(isset($item))
{{__("Edit discount")}} [{{$item->title}}]
@else
{{__("Add new discount")}}
@endif
</h1>
<div class="row">
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="title">
{{__('Title')}}
</label>
<input name="title" type="text"
class="form-control @error('title') is-invalid @enderror"
placeholder="{{__('Title')}}" value="{{old('title',$item->title??null)}}"/>
</div>
</div>
<div class="col-md-12 mt-3">
<div class="form-group">
<label for="body">
{{__('Post Text')}}
</label>
<textarea name="body" class="ckeditorx form-control @error('body') is-invalid @enderror"
placeholder="{{__('Post Text')}}"
rows="8">{{old('body',$item->body??null)}}</textarea>
{{-- @trix(\App\Post::class, 'body')--}}
</div>
</div>
<div class="col-md-6 mt-3">
<div class="form-group">
<label for="type">
{{__('Type')}}
</label>
<select name="type" id="type" class="form-control @error('type') is-invalid @enderror" >
@foreach(\App\Models\Discount::$doscount_type as $k => $v)
<option
value="{{ $v }}" {{ old("type", $item->type??null) == $v ? "selected" : "" }}>{{ __($v) }}</option>
@endforeach
</select>
</div>
</div>
<div class="col-md-6 mt-3">
<div class="form-group">
<label for="amount">
{{__('Amount')}}
</label>
<currency-input xname="amount" xid="amount" @error('amount')
:err="true" @enderror :xvalue="{{old('amount',$item->amount??null)}}"></currency-input>
</div>
</div>
<div class="col-md-12">
<label> &nbsp;</label>
<input name="" type="submit" class="btn btn-primary mt-2" value="{{__('Save')}}"/>
</div>
</div>
</div>
</div>
</div>
@endsection

@ -0,0 +1,15 @@
@extends('admin.templates.panel-list-template')
@section('list-title')
<i class="ri-user-3-line"></i>
{{__("Discounts list")}}
@endsection
@section('title')
{{__("Discounts list")}} -
@endsection
@section('filter')
{{-- Other filters --}}
@endsection
@section('bulk')
{{-- <option value="-"> - </option> --}}
@endsection

@ -16,7 +16,7 @@
<select class="form-control" name="stock_status" id="status">
@foreach(\App\Models\Product::$stock_status as $k => $v)
<option
value="{{ $k }}" {{ old("stock_status", $item->stock_status??null) == $k ? "selected" : "" }}>{{ __($v) }}</option>
value="{{ $v }}" {{ old("stock_status", $item->stock_status??null) == $v ? "selected" : "" }}>{{ __($v) }}</option>
@endforeach
</select>
</div>

@ -26,7 +26,7 @@
</a>
</li>
<li>
<a>
<a href="{{route('admin.discount.index')}}">
<i class="ri-percent-fill"></i>
{{__('Discounts')}}
</a>
@ -48,7 +48,7 @@
<li>
<a href="{{route('admin.category.index')}}">
<i class="ri-box-3-fill"></i>
{{__('Product categories')}}
{{__('Categories')}}
</a>
</li>
<li>

@ -52,6 +52,18 @@ Route::prefix(config('app.panel.prefix'))->name('admin.')->group(
Route::post('bulk', [\App\Http\Controllers\Admin\GroupController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\GroupController::class, "trashed"])->name('trashed');
});
Route::prefix('discounts')->name('discount.')->group(
function () {
Route::get('', [\App\Http\Controllers\Admin\DiscountController::class, 'index'])->name('index');
Route::get('create', [\App\Http\Controllers\Admin\DiscountController::class, 'create'])->name('create');
Route::post('store', [\App\Http\Controllers\Admin\DiscountController::class, 'store'])->name('store');
Route::get('edit/{item}', [\App\Http\Controllers\Admin\DiscountController::class, 'edit'])->name('edit');
Route::post('update/{item}', [\App\Http\Controllers\Admin\DiscountController::class, 'update'])->name('update');
Route::get('delete/{item}', [\App\Http\Controllers\Admin\DiscountController::class, 'destroy'])->name('destroy');
Route::get('restore/{item}', [\App\Http\Controllers\Admin\DiscountController::class, 'restore'])->name('restore');
Route::post('bulk', [\App\Http\Controllers\Admin\DiscountController::class, "bulk"])->name('bulk');
Route::get('trashed', [\App\Http\Controllers\Admin\DiscountController::class, "trashed"])->name('trashed');
});
Route::prefix('categories')->name('category.')->group(
function () {
Route::get('', [\App\Http\Controllers\Admin\CategoryController::class, 'index'])->name('index');

Loading…
Cancel
Save