Skip to content

Commit

Permalink
New admin: clients (#251)
Browse files Browse the repository at this point in the history
* Update PaymentService.php

* Fix BelpostLabelService

* Format Oneami code

* Format Oneami code

* 416. Index page

* Format Oneami code

* #430 Internal movement: contact person

* Format Oneami code

* Update PaymentService.php

* Update InstallmentOrderService.php

* Update InstallmentOrderService.php

* New admin: clients

* Format Oneami code

* Fix InstallmentOrderService

* User blacklist

* Format Oneami code

* Fix fb8e8ba

---------

Co-authored-by: Oneami <Oneami@users.noreply.github.com>
  • Loading branch information
Oneami and Oneami authored Jul 3, 2024
1 parent a58f818 commit 04afcbe
Show file tree
Hide file tree
Showing 16 changed files with 593 additions and 6 deletions.
5 changes: 4 additions & 1 deletion src/app/Admin/Controllers/OrderController.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,10 @@ protected function form(?int $id = null)
$form = new Form(new Order());
$order = $id ? Order::query()->where('id', $id)->with([
'country',
'user' => fn ($query) => $query->with(['lastAddress' => fn ($q) => $q->with('country')]),
'user' => fn ($query) => $query->with([
'blacklist',
'lastAddress.country',
]),
])->first() : null;

$administratorService = app(AdministratorService::class);
Expand Down
69 changes: 69 additions & 0 deletions src/app/Filament/Components/Forms/RelationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php

namespace App\Filament\Components\Forms;

use Closure;
use Filament\Forms\Components\Component;

class RelationManager extends Component
{
protected string $view = 'filament.forms.relation-manager';

protected string|Closure $relationManager;

protected bool|Closure $isLazy = false;

/**
* Creates a new instance of the RelationManager class using the service container.
*
* @return static The newly created instance of the RelationManager class.
*/
public static function make(): static
{
return app(static::class);
}

/**
* Sets the relation manager.
*
* @param string|Closure $relationManager The relation manager to set.
* @return static The instance of the relation manager for method chaining.
*/
public function manager(string|Closure $relationManager): static
{
$this->relationManager = $relationManager;

return $this;
}

/**
* Get the relation manager value by evaluating the relationManager property.
*
* @return string The evaluated relation manager value.
*/
public function getRelationManager(): string
{
return $this->evaluate($this->relationManager);
}

/**
* Sets the lazy flag for the relation manager.
*
* @param bool|Closure $condition The condition to determine if the relation manager should be lazy. Defaults to true.
* @return static The instance of the relation manager for method chaining.
*/
public function lazy(bool|Closure $condition = true): static
{
$this->isLazy = $condition;

return $this;
}

/**
* Determine if the relation manager is lazy.
*/
public function isLazy(): bool
{
return (bool)$this->evaluate($this->isLazy);
}
}
218 changes: 218 additions & 0 deletions src/app/Filament/Resources/User/UserResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
<?php

namespace App\Filament\Resources\User;

use App\Filament\Components\Forms\RelationManager;
use App\Filament\Resources\User\UserResource\Pages;
use App\Filament\Resources\User\UserResource\RelationManagers\BlacklistRelationManager;
use App\Filament\Resources\User\UserResource\RelationManagers\PaymentsRelationManager;
use App\Models\User\Group;
use App\Models\User\User;
use Filament\Forms\Components\DatePicker;
use Filament\Forms\Components\Fieldset;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\Tabs;
use Filament\Forms\Components\Tabs\Tab;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Enums\FiltersLayout;
use Filament\Tables\Filters\QueryBuilder;
use Filament\Tables\Filters\QueryBuilder\Constraints\SelectConstraint;
use Filament\Tables\Filters\QueryBuilder\Constraints\TextConstraint;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;

class UserResource extends Resource
{
protected static ?string $model = User::class;

protected static ?string $navigationGroup = 'user';

protected static ?string $modelLabel = 'Пользователи';

protected static ?string $pluralModelLabel = 'Пользователи';

protected static ?int $navigationSort = 1;

public static function form(Form $form): Form
{
return $form
->schema([
Tabs::make('Tabs')->tabs([
Tab::make('Основная информация')->schema([
TextInput::make('first_name')
->label('Имя')
->required()
->maxLength(255),
TextInput::make('last_name')
->label('Фамилия')
->required()
->maxLength(255),
TextInput::make('patronymic_name')
->label('Отчество')
->maxLength(255),
TextInput::make('email')
->label('E-mail')
->maxLength(255),
TextInput::make('phone')
->label('Телефон')
->tel()
->maxLength(255),
DatePicker::make('birth_date')
->label('Дата рождения'),
Select::make('group_id')
->options(Group::all()->pluck('name', 'id'))
->label('Группа'),
Section::make('Адреса')->schema([
Repeater::make('addresses')
->label('')
->relationship()
->schema([
TextInput::make('zip')
->label('Почтовый индекс'),
TextInput::make('region')
->label('Область/край'),
TextInput::make('city')
->label('Город'),
TextInput::make('district')
->label('Район'),
TextInput::make('street')
->label('Улица'),
TextInput::make('house')
->label('Дом'),
TextInput::make('corpus')
->label('Корпус'),
TextInput::make('room')
->label('Квартира'),
TextInput::make('address')
->label('Адрес'),
Toggle::make('approve')
->label('Подтверждение о проверке'),
])
->columns(3),
]),

Section::make('Отзывы')->schema([
Repeater::make('reviews')
->label('')
->relationship()
->schema([
Textarea::make('text')
->label('Отзыв')
->rows(2)
->readOnly(),
])
->addable(false)
->deletable(false)
->columns(1),
]),

]),
Tab::make('Паспортные данные')->schema([
Fieldset::make('passport')
->relationship('passport')
->schema([
TextInput::make('passport_number')
->label('Номер паспорта')
->maxLength(255),
TextInput::make('series')
->label('Серия паспорта')
->maxLength(255),
TextInput::make('issued_by')
->label('Кем выдан')
->maxLength(255),
TextInput::make('issued_date')
->label('Когда выдан')
->maxLength(255),
TextInput::make('personal_number')
->label('Личный номер')
->maxLength(255),
TextInput::make('registration_address')
->label('Адрес прописки')
->maxLength(255),
]),
]),
Tab::make('Платежи')->schema([
RelationManager::make()->manager(PaymentsRelationManager::class)->lazy(true),
])
->columns(1),
Tab::make('Черный список (Лог)')->schema([
RelationManager::make()->manager(BlacklistRelationManager::class)->lazy(true),
])->columns(1),
])->columns(2)->persistTabInQueryString(),
])->columns(1);
}

public static function table(Table $table): Table
{
$table->modifyQueryUsing(
fn (Builder $query) => $query->with(['orders.data'])->orderBy('id', 'desc')
);

return $table
->columns([
Tables\Columns\TextColumn::make('first_name')
->label('Имя')
->searchable(),
Tables\Columns\TextColumn::make('last_name')
->label('Фамилия')
->searchable(),
Tables\Columns\TextColumn::make('patronymic_name')
->label('Отчество'),
Tables\Columns\TextColumn::make('email')
->label('E-mail'),
Tables\Columns\TextColumn::make('phone')
->label('Телефон'),
Tables\Columns\TextColumn::make('orders')
->label('Сумма покупок')
->getStateUsing(function (User $user) {
return $user->completedOrdersCost() . ' руб.';
}),
Tables\Columns\TextColumn::make('group.name')
->label('Группа'),
Tables\Columns\TextColumn::make('reviews_count')
->label('Кол-во отзывов')
->counts('reviews'),
Tables\Columns\TextColumn::make('lastAddress.address')
->label('Адрес'),
Tables\Columns\TextColumn::make('created_at')
->label('Дата регистрации')
->dateTime('d.m.Y H:i:s'),
])
->actions([
Tables\Actions\EditAction::make(),
])
->filters([
QueryBuilder::make()
->constraints([
TextConstraint::make('first_name')->label('Имя'),
TextConstraint::make('last_name')->label('Фамилия'),
TextConstraint::make('patronymic_name')->label('Отчество'),
TextConstraint::make('phone')->label('Телефон'),
TextConstraint::make('email')->label('E-mail'),
SelectConstraint::make('group.id')
->options(Group::query()->pluck('name', 'id'))
->multiple(),
TextConstraint::make('addresses.city')->label('Город'),
TextConstraint::make('addresses.address')->label('Адрес'),
]),
], layout: FiltersLayout::AboveContentCollapsible)
->deferFilters()
->defaultPaginationPageOption(50);
}

public static function getPages(): array
{
return [
'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'),
];
}
}
11 changes: 11 additions & 0 deletions src/app/Filament/Resources/User/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace App\Filament\Resources\User\UserResource\Pages;

use App\Filament\Resources\User\UserResource;
use Filament\Resources\Pages\CreateRecord;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;
}
19 changes: 19 additions & 0 deletions src/app/Filament/Resources/User/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\User\UserResource\Pages;

use App\Filament\Resources\User\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;

class EditUser extends EditRecord
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\DeleteAction::make(),
];
}
}
19 changes: 19 additions & 0 deletions src/app/Filament/Resources/User/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Filament\Resources\User\UserResource\Pages;

use App\Filament\Resources\User\UserResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;

class ListUsers extends ListRecords
{
protected static string $resource = UserResource::class;

protected function getHeaderActions(): array
{
return [
Actions\CreateAction::make(),
];
}
}
Loading

0 comments on commit 04afcbe

Please sign in to comment.