account API fixed

This commit is contained in:
ilmedova 2022-08-04 23:18:19 +05:00
parent e56c8577c4
commit 6054438d59
9 changed files with 266 additions and 79 deletions

View File

@ -6,10 +6,12 @@
use App\Http\Requests\BankAccountRequest; use App\Http\Requests\BankAccountRequest;
use App\Http\Requests\ContactsRequest; use App\Http\Requests\ContactsRequest;
use App\Http\Resources\AccountResource; use App\Http\Resources\AccountResource;
use App\Models\Account;
use Illuminate\Http\Request; use Illuminate\Http\Request;
class AccountController extends Controller class AccountController extends Controller
{ {
/** /**
* @OA\GET( * @OA\GET(
* path="/api/account", * path="/api/account",
@ -28,11 +30,11 @@ class AccountController extends Controller
* ) * )
* ) * )
*/ */
public function account(Request $request) public function account(Request $request){
{
$account = $request->user() $account = Account::with('profile')->find($request->user()->account_id);
->account()
->with('profile'); //return $account;
if(!empty($account)){ if(!empty($account)){
return AccountResource::make($account); return AccountResource::make($account);

View File

@ -40,27 +40,22 @@ public function setup()
*/ */
protected function setupListOperation() protected function setupListOperation()
{ {
CRUD::column('type');
CRUD::column('vat'); CRUD::column('vat');
CRUD::column('country_id'); CRUD::column('country_id');
CRUD::column('legalization_number'); CRUD::column('legalization_number');
// dropdown filter // $this->crud->addFilter([
$this->crud->addFilter([ // 'name' => 'status',
'name' => 'status', // 'type' => 'dropdown',
'type' => 'dropdown', // 'label' => 'Account type'
'label' => 'Account type' // ], [
], [ // 'company' => 'Company',
'company' => 'Company', // 'business' => 'Business'
'business' => 'Business' // ], function($value) { // if the filter is active
], function($value) { // if the filter is active // $this->crud->addClause('where', 'type', $value);
$this->crud->addClause('where', 'type', $value); // });
});
/**
* Columns can be defined using the fluent syntax or array syntax:
* - CRUD::column('price')->type('number');
* - CRUD::addColumn(['name' => 'price', 'type' => 'number']);
*/
} }
/** /**

View File

@ -2,7 +2,9 @@
namespace App\Http\Resources; namespace App\Http\Resources;
use App\Models\Business;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
use Symfony\Component\HttpKernel\Profiler\Profile;
class AccountResource extends JsonResource class AccountResource extends JsonResource
{ {
@ -18,15 +20,17 @@ public function toArray($request)
'id' => $this->id, 'id' => $this->id,
'bank_account' => $this->bank_account, 'bank_account' => $this->bank_account,
'contacts' => $this->contacts, 'contacts' => $this->contacts,
'account_type' => $this->account_type, 'account_type' => $this->type,
'profile' => $this->profile() 'profile' => $this->profile()
]; ];
} }
private function profile() : JsonResource private function profile() : JsonResource
{ {
$type = config('account.'.$this->account_type.'.resource'); if($this->profile){
$type = config('account.'.$this->type.'.resource');
return $type::make($this->profile); return $type::make($this->profile);
}
return new JsonResource([]);
} }
} }

View File

@ -14,6 +14,10 @@ class BusinessProfileResource extends JsonResource
*/ */
public function toArray($request) public function toArray($request)
{ {
return parent::toArray($request); return [
'personal' => $this->personal,
'document' => $this->document,
'job' => $this->job
];
} }
} }

View File

@ -20,7 +20,7 @@ class Account extends Model
// public $timestamps = false; // public $timestamps = false;
protected $guarded = ['id']; protected $guarded = ['id'];
protected $fillable = [ protected $fillable = [
'contacts', 'bank', 'vat', 'country_id', 'legalization_number','type' 'contacts', 'bank', 'vat', 'country_id', 'legalization_number', 'type',
]; ];
protected $casts = [ protected $casts = [
'contacts' => 'array', 'contacts' => 'array',
@ -57,6 +57,10 @@ public function application(){
return $this->hasOne(Application::class); return $this->hasOne(Application::class);
} }
public function clients(){
return $this->hasMany(Client::class);
}
/* /*
|-------------------------------------------------------------------------- |--------------------------------------------------------------------------
| SCOPES | SCOPES

View File

@ -25,7 +25,7 @@ class Client extends Authenticatable
// public $timestamps = false; // public $timestamps = false;
protected $guarded = ['id']; protected $guarded = ['id'];
protected $fillable = [ protected $fillable = [
'firstname', 'lastname', 'email', 'password', 'is_verified','verification_token', 'is_suspended' 'firstname', 'lastname', 'email', 'password', 'is_verified','verification_token', 'is_suspended', 'account_id'
]; ];
protected $hidden = [ protected $hidden = [
'password', 'password',

View File

@ -8,10 +8,10 @@
return [ return [
'business' => [ 'business' => [
'class' => App\AccountTypes\BusinessAccount::class, 'class' => App\AccountTypes\BusinessAccount::class,
'resources' => App\Http\Resources\BusinessProfileResource::class 'resource' => App\Http\Resources\BusinessProfileResource::class
], ],
'company' => [ 'company' => [
'class' => App\AccountTypes\CompanyAccount::class, 'class' => App\AccountTypes\CompanyAccount::class,
'resources' => App\Http\Resources\CompanyProfileResource::class 'resource' => App\Http\Resources\CompanyProfileResource::class
] ]
]; ];

View File

@ -1,49 +0,0 @@
<!-- select -->
@php
$current_value = old_empty_or_null($field['name'], '') ?? $field['value'] ?? $field['default'] ?? '';
$entity_model = $crud->getRelationModel($field['entity'], - 1);
$field['allows_null'] = $field['allows_null'] ?? $entity_model::isColumnNullable($field['name']);
//if it's part of a relationship here we have the full related model, we want the key.
if (is_object($current_value) && is_subclass_of(get_class($current_value), 'Illuminate\Database\Eloquent\Model') ) {
$current_value = $current_value->getKey();
}
if (!isset($field['options'])) {
$options = $field['model']::all();
} else {
$options = call_user_func($field['options'], $field['model']::query());
}
@endphp
@include('crud::fields.inc.wrapper_start')
<label>{!! $field['label'] !!}</label>
@include('crud::fields.inc.translatable_icon')
<select
name="{{ $field['name'] }}"
@include('crud::fields.inc.attributes')
>
@if ($field['allows_null'])
<option value="">-</option>
@endif
@if (count($options))
@foreach ($options as $connected_entity_entry)
@if($current_value == $connected_entity_entry->getKey())
<option value="{{ $connected_entity_entry->getKey() }}" selected>{{ $connected_entity_entry->{$field['attribute']} }}</option>
@else
<option value="{{ $connected_entity_entry->getKey() }}">{{ $connected_entity_entry->{$field['attribute']} }}</option>
@endif
@endforeach
@endif
</select>
{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif
@include('crud::fields.inc.wrapper_end')

View File

@ -0,0 +1,227 @@
<!-- Backpack Table Field Type -->
<?php
$max = isset($field['max']) && (int) $field['max'] > 0 ? $field['max'] : -1;
$min = isset($field['min']) && (int) $field['min'] > 0 ? $field['min'] : -1;
$item_name = strtolower(isset($field['entity_singular']) && ! empty($field['entity_singular']) ? $field['entity_singular'] : $field['label']);
$items = old(square_brackets_to_dots($field['name'])) ?? $field['value'] ?? $field['default'] ?? '';
// make sure no matter the attribute casting
// the $items variable contains a properly defined JSON string
if (is_array($items)) {
if (count($items)) {
$items = json_encode($items);
} else {
$items = '[]';
}
} elseif (is_string($items) && ! is_array(json_decode($items))) {
$items = '[]';
}
// make sure columns are defined
if (! isset($field['columns'])) {
$field['columns'] = ['value' => 'Value'];
}
$field['wrapper'] = $field['wrapper'] ?? $field['wrapperAttributes'] ?? [];
$field['wrapper']['data-field-type'] = 'table';
$field['wrapper']['data-field-name'] = $field['name'];
?>
@include('crud::fields.inc.wrapper_start')
<label>{!! $field['label'] !!}</label>
@include('crud::fields.inc.translatable_icon')
<input class="array-json"
type="hidden"
data-init-function="bpFieldInitTableElement"
name="{{ $field['name'] }}"
value="{{ $items }}"
data-max="{{$max}}"
data-min="{{$min}}"
data-maxErrorTitle="{{trans('backpack::crud.table_cant_add', ['entity' => $item_name])}}"
data-maxErrorMessage="{{trans('backpack::crud.table_max_reached', ['max' => $max])}}">
<div class="array-container form-group">
<table class="table table-sm table-striped m-b-0">
<thead>
<tr>
@foreach( $field['columns'] as $column )
<th style="font-weight: 600!important;">
{{ $column }}
</th>
@endforeach
<th class="text-center"> {{-- <i class="la la-sort"></i> --}} </th>
<th class="text-center"> {{-- <i class="la la-trash"></i> --}} </th>
</tr>
</thead>
<tbody class="table-striped items sortableOptions">
<tr class="array-row clonable" style="display: none;">
@foreach( $field['columns'] as $column => $label)
<td>
<input class="form-control form-control-sm" type="text" data-cell-name="item.{{ $column }}">
</td>
@endforeach
<td>
<span class="btn btn-sm btn-light sort-handle pull-right"><span class="sr-only">sort item</span><i class="la la-sort" role="presentation" aria-hidden="true"></i></span>
</td>
<td>
<button class="btn btn-sm btn-light removeItem" type="button"><span class="sr-only">delete item</span><i class="la la-trash" role="presentation" aria-hidden="true"></i></button>
</td>
</tr>
</tbody>
</table>
<div class="array-controls btn-group m-t-10">
<button class="btn btn-sm btn-light" type="button" data-button-type="addItem"><i class="la la-plus"></i> {{trans('backpack::crud.add')}} {{ $item_name }}</button>
</div>
</div>
{{-- HINT --}}
@if (isset($field['hint']))
<p class="help-block">{!! $field['hint'] !!}</p>
@endif
@include('crud::fields.inc.wrapper_end')
{{-- ########################################## --}}
{{-- Extra CSS and JS for this particular field --}}
{{-- If a field type is shown multiple times on a form, the CSS and JS will only be loaded once --}}
@if ($crud->fieldTypeNotLoaded($field))
@php
$crud->markFieldTypeAsLoaded($field);
@endphp
{{-- FIELD JS - will be loaded in the after_scripts section --}}
@push('crud_fields_scripts')
{{-- YOUR JS HERE --}}
<script type="text/javascript" src="{{ asset('packages/jquery-ui-dist/jquery-ui.min.js') }}"></script>
<script>
function bpFieldInitTableElement(element) {
var $tableWrapper = element.parent('[data-field-type=table]');
var $rows = (element.attr('value') != '') ? $.parseJSON(element.attr('value')) : '';
var $max = element.attr('data-max');
var $min = element.attr('data-min');
var $maxErrorTitle = element.attr('data-maxErrorTitle');
var $maxErrorMessage = element.attr('data-maxErrorMessage');
// add rows with the information from the database
if($rows != '[]') {
$.each($rows, function(key) {
addItem();
$.each(this, function(column , value) {
$tableWrapper.find('tbody tr:last').find('input[data-cell-name="item.' + column + '"]').val(value);
});
// if it's the last row, update the JSON
if ($rows.length == key+1) {
updateTableFieldJson();
}
});
}
// add minimum rows if needed
var itemCount = $tableWrapper.find('tbody tr').not('.clonable').length;
if($min > 0 && itemCount < $min) {
$rowsToAdd = Number($min) - Number(itemCount);
for(var i = 0; i < $rowsToAdd; i++){
addItem();
}
}
$tableWrapper.find('.sortableOptions').sortable({
handle: '.sort-handle',
axis: 'y',
helper: function(e, ui) {
ui.children().each(function() {
$(this).width($(this).width());
});
return ui;
},
update: function( event, ui ) {
updateTableFieldJson();
}
});
$tableWrapper.find('[data-button-type=addItem]').click(function() {
if($max > -1) {
var totalRows = $tableWrapper.find('tbody tr').not('.clonable').length;
if(totalRows < $max) {
addItem();
updateTableFieldJson();
} else {
new Noty({
type: "warning",
text: "<strong>"+$maxErrorTitle+"</strong><br>"+$maxErrorMessage
}).show();
}
} else {
addItem();
updateTableFieldJson();
}
});
function addItem() {
$tableWrapper.find('tbody').append($tableWrapper.find('tbody .clonable').clone().show().removeClass('clonable'));
}
$tableWrapper.on('click', '.removeItem', function() {
var totalRows = $tableWrapper.find('tbody tr').not('.clonable').length;
if (totalRows > $min) {
$(this).closest('tr').remove();
updateTableFieldJson();
return false;
}
});
$tableWrapper.find('tbody').on('keyup', function() {
updateTableFieldJson();
});
function updateTableFieldJson() {
var $rows = $tableWrapper.find('tbody tr').not('.clonable');
var $hiddenField = $tableWrapper.find('input.array-json');
var json = '[';
var otArr = [];
var tbl2 = $rows.each(function(i) {
x = $(this).children().closest('td').find('input');
var itArr = [];
x.each(function() {
if(this.value.length > 0) {
var key = $(this).attr('data-cell-name').replace('item.','');
itArr.push('"' + key + '":' + JSON.stringify(this.value));
}
});
otArr.push('{' + itArr.join(',') + '}');
})
json += otArr.join(",") + ']';
var totalRows = $rows.length;
$hiddenField.val( totalRows ? json : null );
}
// on page load, make sure the input has the old values
updateTableFieldJson();
}
</script>
@endpush
@endif
{{-- End of Extra CSS and JS --}}
{{-- ########################################## --}}