birzha-legalizasia/app/Http/Controllers/Admin/ApplicationCrudController.php

184 lines
6.7 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\ApplicationRequest;
use App\Models\Account;
use App\Models\Application;
use App\Models\Client;
use App\Notifications\ApplicationApproved;
use App\Notifications\ApplicationRefined;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
use Carbon\Carbon;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Notification;
/**
* Class ApplicationCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class ApplicationCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
public function setup(){
if(!(backpack_user()->hasPermissionTo('applications'))){
$this->crud->denyAccess(['update']);
}
if(!(backpack_user()->hasRole('Super Admin'))){
$this->crud->denyAccess(['delete']);
}
CRUD::setModel(Application::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/application');
CRUD::setEntityNameStrings('application', trans('app.application.list_title'));
Application::updating(function($entry) {
$entry->modified_by = backpack_user()->name;
});
$this->crud->addClause('where', 'state', '!=', 'Draft');
$this->crud->addFilter([
'name' => 'state',
'type' => 'dropdown',
'label' => trans('app.application.state')
], [
'new' => trans('app.application.new'),
'accepted' => trans('app.application.accepted'),
'refine' => trans('app.application.refine'),
'approved' => trans('app.application.approved'),
'archive' => trans('app.application.archived')
], function ($value) {
$this->crud->addClause('where', 'state', $value);
});
$this->crud->addFilter([
'type' => 'date_range',
'name' => 'from_to',
'label' => trans('app.application.date_filter'),
],
false,
function ($value) {
$dates = json_decode($value);
$this->crud->addClause('where', 'created_at', '>=', $dates->from);
$this->crud->addClause('where', 'created_at', '<=', $dates->to . ' 23:59:59');
});
$this->crud->addFilter([
'type' => 'text',
'name' => 'accepted_by',
'label' => trans('app.last_updates.accepted_by'),
],
false,
function ($value) {
$this->crud->addClause('where', 'accepted_by', 'LIKE', '%' . $value . '%');
});
}
/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
//$this->crud->addClause('where', 'state', '!=', 'new');
$this->crud->addColumns([
[
'label' => trans('app.application.name'),
'type' => 'select',
'name' => 'account_type',
'entity' => 'account',
'model' => "App\Models\Account",
'attribute' => 'type_and_name',
],
[
'label' => trans('app.application.leg_number'),
'type' => 'select',
'name' => 'account_legnumber',
'entity' => 'account',
'model' => "App\Models\Account",
'attribute' => 'legalization_number',
],
[
'label' => trans('app.application.expires_at'),
'type' => 'select',
'name' => 'account_exp_date',
'entity' => 'account',
'model' => "App\Models\Account",
'attribute' => 'expires_at',
],
[
'name' => 'state',
'label' => trans('app.application.state'),
'type' => 'badge',
],
[
'name' => 'accepted_by',
'label' => trans('app.last_updates.accepted_by'),
'type' => 'text',
],
[
'name' => 'created_at',
'label' => trans('app.application.created_at'),
]
]);
$this->crud->addButtonFromModelFunction('line', 'preview_button', 'preview', 'beginning');
}
public function accept($id){
$entry = Application::findOrfail($id);
$entry->accepted_by = backpack_user()->name;
$entry->state = 'accepted';
$entry->accepted_date = Carbon::now();
$entry->save();
\Alert::add('success', '<strong>Success!</strong><br>Application is accepted')->flash();
return redirect()->back();
}
public function refine($id){
$entry = Application::findOrfail($id);
$entry->state = 'refine';
$entry->refine_note = request()->get('note');
$entry->save();
$account = Account::with('clients')->find($entry->account_id);
$not_suspended_clients = Client::where('is_suspended', 0)->where('account_id', $account->id)->get();
Notification::send($not_suspended_clients, new ApplicationRefined());
\Alert::add('success', '<strong>Success!</strong><br>Application is refined')->flash();
return redirect()->back();
}
public function approveApplication(Request $request){
$application = Application::find($request->id);
$application->state = 'approved';
$application->save();
$account = Account::with('clients')->find($application->account_id);
$account->legalization_number = $request->legalization_number;
$account->expires_at = $request->expires_at;
$account->save();
$not_suspended_clients = Client::where('is_suspended', 0)->where('account_id', $account->id)->get();
Notification::send($not_suspended_clients, new ApplicationApproved());
\Alert::add('success', '<strong>Success!</strong><br>Application is approved')->flash();
return redirect()->back();
}
public function previewApplicationAdmin($id){
$application = Application::with(['account', 'attachments', 'ticket'])->find($id);
return view('admin.application_preview',[
'application' => $application
]);
}
}