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

240 lines
9.1 KiB
PHP
Raw Normal View History

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\ApplicationRequest;
2022-11-22 07:50:54 +00:00
use App\Models\Account;
2022-11-18 20:20:50 +00:00
use App\Models\Application;
2022-12-01 10:28:14 +00:00
use App\Notifications\ApplicationApproved;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
2022-11-19 06:06:49 +00:00
use Carbon\Carbon;
2022-11-22 07:50:54 +00:00
use Illuminate\Http\Request;
2022-12-01 10:28:14 +00:00
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;
2022-11-08 08:11:10 +00:00
// use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
2022-11-19 06:06:49 +00:00
// use \Backpack\CRUD\app\Http\Controllers\Operations\UpdateOperation;
use \Backpack\CRUD\app\Http\Controllers\Operations\DeleteOperation;
/**
* Configure the CrudPanel object. Apply settings to all operations.
*
* @return void
*/
2022-11-19 07:33:19 +00:00
public function setup(){
2022-11-14 09:54:17 +00:00
if(!(backpack_user()->hasPermissionTo('applications'))){
2022-11-29 08:56:58 +00:00
$this->crud->denyAccess(['update']);
}
if(!(backpack_user()->hasRole('Super Admin'))){
$this->crud->denyAccess(['delete']);
2022-11-14 09:54:17 +00:00
}
2022-11-18 20:20:50 +00:00
CRUD::setModel(Application::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/application');
2022-11-08 07:37:23 +00:00
CRUD::setEntityNameStrings('application', trans('app.application.list_title'));
2022-11-18 20:20:50 +00:00
Application::updating(function($entry) {
2022-11-22 07:42:37 +00:00
$entry->modified_by = backpack_user()->name;
2022-11-18 20:20:50 +00:00
});
$this->crud->addClause('where', 'state', '!=', 'Draft');
$this->crud->addFilter([
'name' => 'state',
'type' => 'dropdown',
2022-11-08 07:37:23 +00:00
'label' => trans('app.application.state')
], [
2022-11-08 07:37:23 +00:00
'new' => trans('app.application.new'),
2022-11-19 06:06:49 +00:00
'accepted' => trans('app.application.accepted'),
2022-11-08 07:37:23 +00:00
'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',
2022-11-08 07:37:23 +00:00
'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');
});
2022-11-18 20:20:50 +00:00
$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 . '%');
2022-11-18 20:20:50 +00:00
});
}
/**
* 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');
2022-09-23 13:26:59 +00:00
$this->crud->addColumns([
2022-11-29 08:56:58 +00:00
2022-09-23 13:26:59 +00:00
[
2022-11-29 08:56:58 +00:00
'label' => trans('app.application.name'),
2022-09-30 07:52:56 +00:00
'type' => 'select',
2022-11-19 07:33:19 +00:00
'name' => 'account_type',
'entity' => 'account',
'model' => "App\Models\Account",
2022-11-29 08:56:58 +00:00
'attribute' => 'type_and_name',
2022-09-30 07:52:56 +00:00
],
2022-11-19 07:33:19 +00:00
[
2022-11-08 07:37:23 +00:00
'label' => trans('app.application.leg_number'),
2022-09-30 07:52:56 +00:00
'type' => 'select',
2022-11-19 07:33:19 +00:00
'name' => 'account_legnumber',
'entity' => 'account',
'model' => "App\Models\Account",
2022-09-30 07:52:56 +00:00
'attribute' => 'legalization_number',
],
2022-11-19 07:33:19 +00:00
[
2022-11-08 07:37:23 +00:00
'label' => trans('app.application.expires_at'),
2022-09-30 07:52:56 +00:00
'type' => 'select',
2022-11-19 07:33:19 +00:00
'name' => 'account_exp_date',
'entity' => 'account',
'model' => "App\Models\Account",
2022-09-30 07:52:56 +00:00
'attribute' => 'expires_at',
],
2022-09-23 13:26:59 +00:00
[
'name' => 'state',
2022-11-08 07:37:23 +00:00
'label' => trans('app.application.state'),
2022-11-22 06:57:47 +00:00
'type' => 'badge',
],
2022-11-18 20:52:48 +00:00
[
'name' => 'accepted_by',
'label' => trans('app.last_updates.accepted_by'),
'type' => 'text',
],
[
'name' => 'created_at',
2022-11-08 07:37:23 +00:00
'label' => trans('app.application.created_at'),
2022-09-23 13:26:59 +00:00
]
]);
2022-11-29 09:00:16 +00:00
// CRUD::addColumn(['name'=>'country_id', 'type'=>'select','label'=> trans('app.account.country'), 'entity' => 'country' ,'model' => 'App\Model\Country','attribute' => 'name']);
2022-09-30 08:58:33 +00:00
$this->crud->addButtonFromModelFunction('line', 'preview_button', 'preview', 'beginning');
2022-11-19 06:06:49 +00:00
// $this->crud->addButtonFromModelFunction('line', 'accept_button', 'accept', 'beginning');
}
2022-11-19 06:06:49 +00:00
public function accept($id){
2022-11-19 06:41:20 +00:00
$entry = Application::findOrfail($id);
$entry->accepted_by = backpack_user()->name;
$entry->state = 'accepted';
2022-11-19 06:06:49 +00:00
$entry->accepted_date = Carbon::now();
$entry->save();
2022-11-23 11:56:28 +00:00
//todo fix alert
2022-12-01 11:51:17 +00:00
\Alert::add('success', '<strong>Success!</strong><br>Application is accepted')->flash();
2022-11-19 06:06:49 +00:00
return redirect()->back();
}
2022-11-22 07:32:37 +00:00
public function refine($id){
$entry = Application::findOrfail($id);
2022-11-22 07:42:37 +00:00
$entry->state = 'refine';
$entry->refine_note = request()->get('note');
2022-11-22 07:50:54 +00:00
$entry->save();
2022-11-23 11:56:28 +00:00
//todo fix alert
//todo send email to account clients where not suspended {note, application status message}
2022-12-01 11:51:17 +00:00
\Alert::add('success', '<strong>Success!</strong><br>Application is refined')->flash();
2022-11-22 07:32:37 +00:00
return redirect()->back();
}
2022-11-22 07:50:54 +00:00
public function approveApplication(Request $request){
$application = Application::find($request->id);
$application->state = 'approved';
$application->save();
2022-12-01 10:28:14 +00:00
$account = Account::with('clients')->find($application->account_id);
2022-11-22 07:50:54 +00:00
$account->legalization_number = $request->legalization_number;
$account->expires_at = $request->expires_at;
$account->save();
2022-11-23 11:56:28 +00:00
//todo fix alert
2022-12-01 10:28:14 +00:00
//todo check email notification
Notification::send($account->clients, new ApplicationApproved($account));
\Alert::add('success', '<strong>Success!</strong><br>Application is approved')->flash();
2022-11-22 07:50:54 +00:00
return redirect()->back();
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
2022-11-08 08:11:10 +00:00
// protected function setupCreateOperation()
// {
// CRUD::setValidation(ApplicationRequest::class);
// $this->crud->addFields([
// [ // SelectMultiple = n-n relationship (with pivot table)
// 'label' => trans('app.application.account'),
// 'type' => 'custom_select_account',
// 'name' => 'account_id', // the method that defines the relationship in your Model
// 'entity' => 'account', // the method that defines the relationship in your Model
// 'model' => "App\Models\Account", // foreign key model
// 'attribute_1' => 'name', // foreign key attribute that is shown to user
// 'attribute_2' => 'surname',
// ],
// [
// 'name' => 'state',
// 'label' => trans('app.application.state'),
// 'type' => 'select_from_array',
// 'options' => [
// 'new' => trans('app.application.new'),
// 'applied' => trans('app.application.applied'),
// 'refine' => trans('app.application.refine'),
// 'approved' => trans('app.application.approved'),
// 'archive' => trans('app.application.archived')
// ]
// ]
// ]);
// }
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
2022-11-19 06:06:49 +00:00
// protected function setupUpdateOperation()
// {
// CRUD::setValidation(ApplicationRequest::class);
// $this->crud->addFields([
// [
// 'name' => 'state',
// 'label' => 'State',
// 'type' => 'select_from_array',
// 'options' => [
// 'new' => trans('app.application.new'),
// 'applied' => trans('app.application.applied'),
// 'refine' => trans('app.application.refine'),
// 'approved' => trans('app.application.approved'),
// 'archive' => trans('app.application.archived')
// ]
// ]
// ]);
// }
}