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

181 lines
6.8 KiB
PHP
Executable File

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Requests\AccountRequest;
use App\Models\Business;
use App\Models\Company;
use Backpack\CRUD\app\Http\Controllers\CrudController;
use Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD;
/**
* Class AccountCrudController
* @package App\Http\Controllers\Admin
* @property-read \Backpack\CRUD\app\Library\CrudPanel\CrudPanel $crud
*/
class AccountCrudController extends CrudController
{
use \Backpack\CRUD\app\Http\Controllers\Operations\ListOperation;
// use \Backpack\CRUD\app\Http\Controllers\Operations\CreateOperation;
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
*/
public function setup()
{
CRUD::setModel(\App\Models\Account::class);
CRUD::setRoute(config('backpack.base.route_prefix') . '/account');
CRUD::setEntityNameStrings(trans('app.account.title'), trans('app.account.list_title'));
$this->crud->addFilter([
'name' => 'type',
'type' => 'dropdown',
'label' => trans('app.account.filter.type')
], [
'business' => trans('app.account.filter.business'),
'company' => trans('app.account.filter.company'),
], function ($value) { // if the filter is active
$this->crud->addClause('where', 'type', $value);
});
if(!(backpack_user()->hasPermissionTo('accounts'))){
$this->crud->denyAccess(['delete', 'update']);
}
$this->crud->addFilter([
'name' => 'country',
'type' => 'select2',
'label' => trans('app.account.country')
], function () {
App\Models\Country::get()->pluck('name', 'id');
}, function ($value) { // if the filter is active
$this->crud->addClause('where', 'country_id', $value);
});
}
/**
* Define what happens when the List operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-list-entries
* @return void
*/
protected function setupListOperation()
{
CRUD::addColumn([
'name'=>'account_type',
'type'=>'text',
'label'=> trans('app.account.filter.type'),
]);
CRUD::addColumn([
'name' => 'profile',
'label' => trans('app.account.name'),
'type' => 'profile_name',
'searchLogic' => function ($query, $column, $searchTerm) {
$query->whereHas('profile', function ($q) use ($column, $searchTerm) {
$q->where('name', 'like', '%'.$searchTerm.'%')
->orWhere('surname', 'like', '%'.$searchTerm.'%')
->orWhere('short_name', 'like', '%'.$searchTerm.'%');
});
}
]);
CRUD::addColumn(['name'=>'legalization_number', 'type'=>'text','label'=> trans('app.account.legalization_number')]);
CRUD::addColumn(['name'=>'expires_at', 'type'=>'text','label'=> trans('app.account.expires_at')]);
CRUD::addColumn(['name'=>'country_id', 'type'=>'select','label'=> trans('app.account.country'), 'entity' => 'country' ,'model' => 'App\Model\Country','attribute' => 'name']);
$this->crud->addButtonFromModelFunction('line', 'preview_button', 'preview', 'beginning');
// $this->crud->addButtonFromModelFunction('line', 'export_button', 'export_account', 'beginning');
}
/**
* Define what happens when the Create operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-create
* @return void
*/
protected function setupCreateOperation()
{
CRUD::setValidation(AccountRequest::class);
$this->crud->addFields([
[
'name' => 'legalization_number',
'label' => trans('app.account.legalization_number'),
'type' => 'text',
'tab' => trans('app.account.tab_legalization')
],
[
'name' => 'expires_at',
'label' => trans('app.account.expires_at'),
'type' => 'date',
'tab' => trans('app.account.tab_legalization')
],
[
'name' => 'bank',
'label' => trans('app.account.bank'),
'type' => 'table',
'entity_singular' => 'option', // used on the "Add X" button
'columns' => [
'account_number' => trans('app.account.account_number'),
'account_date' => trans('app.account.account_date'),
'currency' => trans('app.account.currency'),
'iban' => trans('app.account.iban'),
'bank_name' => trans('app.account.bank_name'),
'country' => trans('app.account.country'),
],
'max' => 15, // maximum rows allowed in the table
'min' => 0, // minimum rows allowed in the table
'tab' => trans('app.account.tab_bank'),
],
[
'name' => 'contacts',
'label' => trans('app.account.contacts'),
'type' => 'table',
'entity_singular' => 'option', // used on the "Add X" button
'columns' => [
'address' => trans('app.account.address'),
'phone' => trans('app.account.phone'),
'email' => trans('app.account.email'),
'fax' => trans('app.account.fax'),
],
'max' => 15, // maximum rows allowed in the table
'min' => 0, // minimum rows allowed in the table
'tab' => trans('app.account.tab_contacts'),
],
[
'name' => 'country_id',
'label' => trans('app.account.country'),
'type' => 'select',
'entity' => 'country',
'model' => "App\Models\Country", // related model
'attribute' => 'name', // foreign key attribute that is shown to user
'tab' => trans('app.account.tab_country'),
],
]);
/**
* Fields can be defined using the fluent syntax or array syntax:
* - CRUD::field('price')->type('number');
* - CRUD::addField(['name' => 'price', 'type' => 'number']));
*/
}
/**
* Define what happens when the Update operation is loaded.
*
* @see https://backpackforlaravel.com/docs/crud-operation-update
* @return void
*/
protected function setupUpdateOperation()
{
$this->setupCreateOperation();
}
}