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

196 lines
6.3 KiB
PHP

<?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('account', 'accounts');
}
/**
* 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' => 'contacts',
'type' => 'json',
'label' => 'Contacts'
]);
CRUD::addColumn([
'name' => 'bank',
'type' => 'json',
'label' => 'Bank'
]);
//CRUD::column('profileable_id');
CRUD::addColumn([
// 1-n relationship
'label' => 'Profile ID', // Table column heading
'type' => 'closure',
'function' => function ($entry) {
if ($entry->profileable_type == 'App\Models\Business') {
$data = Business::find($entry->profileable_id);
if($data){
return json_decode($data->personal)[0]['name'];
}
else{
return '-';
}
}
else{
$data = Company::find($entry->profileable_id);
if($data){
return $data->name;
}
else{
return '-';
}
}
},
]);
CRUD::addColumn([
// 1-n relationship
'label' => 'Profile type', // Table column heading
'type' => 'closure',
'function' => function ($entry) {
if ($entry->profileable_type == 'App\Models\Business') {
$data = Business::find($entry->profileable_id);
if($data){
return 'App\Models\Business';
}
else{
return '-';
}
}
else{
$data = Company::find($entry->profileable_id);
if($data){
return 'App\Models\Company';
}
else{
return '-';
}
}
},
]);
CRUD::column('vat');
CRUD::column('country_id');
CRUD::column('legalization_number');
// dropdown filter
$this->crud->addFilter([
'name' => 'status',
'type' => 'dropdown',
'label' => 'Account type'
], [
'App\Models\Company' => 'Company',
'App\Models\Business' => 'Business'
], function($value) { // if the filter is active
$this->crud->addClause('where', 'profileable_type', $value);
});
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
}
/**
* 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' => 'contacts',
'label' => 'Contacts',
'type' => 'table',
'entity_singular' => 'option', // used on the "Add X" button
'columns' => [
'address' => 'Address',
'phone' => 'Phone',
'email' => 'Email',
'fax' => 'Fax'
],
'max' => 15, // maximum rows allowed in the table
'min' => 0, // minimum rows allowed in the table
],
[
'name' => 'bank',
'label' => 'Bank',
'type' => 'table',
'entity_singular' => 'option', // used on the "Add X" button
'columns' => [
'account_number' => 'Account number',
'account_date' => 'Account date',
'currency' => 'Currency',
'iban' => 'IBAN',
'bank_name' => 'Bank name',
'country' => 'Country'
],
'max' => 15, // maximum rows allowed in the table
'min' => 0, // minimum rows allowed in the table
]
]);
CRUD::field('profileable_id');
CRUD::field('profileable_type');
CRUD::field('vat');
CRUD::field('country_id');
CRUD::field('legalization_number');
/**
* 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();
}
}