Skip to content

Commit

Permalink
Merge pull request #6 from devajmeireles/feature/presence
Browse files Browse the repository at this point in the history
Lista de Presença
  • Loading branch information
devajmeireles authored Aug 29, 2023
2 parents 4391fe4 + 856485f commit 8eb4bc1
Show file tree
Hide file tree
Showing 50 changed files with 1,487 additions and 245 deletions.
30 changes: 24 additions & 6 deletions app/Actions/Signature/CreateSignature.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace App\Actions\Signature;

use App\Models\{Item, Signature};
use App\Notifications\SignatureCreated;
use App\Enums\DeliveryType;
use App\Models\{Item, Presence, Signature};
use App\Services\Settings\Facades\Settings;
use Exception;
use Illuminate\Support\Collection;
use Throwable;
Expand All @@ -13,24 +14,41 @@ class CreateSignature
/** @throws Exception|Throwable */
public function execute(Item $item, Signature $signature, int $quantity = 1): bool
{
$this->validations($item, $signature, $quantity);
$this->validations($item, $quantity);

$presence = $this->presence($signature);

$item->signatures()
->createMany(Collection::times($quantity, fn () => $signature->toArray())->toArray());
->createMany(Collection::times($quantity, fn () => array_merge($signature->toArray(), $presence))->toArray());

$item->is_active = $item->available();
$item->last_signed_at = now();
$item->save();
$item->notify(new SignatureCreated($item, $signature, $quantity));

return true;
}

/** @throws Exception|Throwable */
private function validations(Item $item, Signature $signature, int $quantity = 1): void
private function validations(Item $item, int $quantity = 1): void
{
throw_if(!$item->is_active, new Exception("Item ({$item->id}) não está ativo"));

throw_if($quantity > $item->availableQuantity(), new Exception("Quantidade selecionada ($quantity) é superior a quantidade de itens ({$item->quantity})"));
}

private function presence(Signature $signature): array
{
if (!Settings::get('converter_assinaturas_em_presenca') || $signature->delivery !== DeliveryType::InPerson) {
return [];
}

$presence = Presence::create([
'name' => $signature->name,
'phone' => $signature->phone,
'is_confirmed' => true,
'observation' => 'Presença criada via assinatura',
]);

return ['presence_id' => $presence->id];
}
}
35 changes: 35 additions & 0 deletions app/Console/Commands/CreateDevelopmentUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\Console\Commands;

use App\Enums\UserRole;
use App\Models\User;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Hash;

class CreateDevelopmentUser extends Command
{
protected $signature = 'make:dev';

protected $description = 'Create a random dev user';

public function handle(): int
{
$name = fake()->name();
$nickname = fake()->userName();

User::create([
'role' => UserRole::Admin,
'name' => $name,
'username' => $nickname,
'password' => Hash::make('password'),
]);

$this->table(
['Name', 'Username', 'Password'],
[[$name, $nickname, 'password']]
);

return self::SUCCESS;
}
}
1 change: 1 addition & 0 deletions app/Console/Commands/CreateSettingCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public function handle(): int
'date' => 'date',
'time' => 'time',
'phone' => 'phone (text, masked)',
'boolean' => 'boolean',
]
);

Expand Down
2 changes: 1 addition & 1 deletion app/Exports/Item/ItemExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function headings(): array
];
}

public function map($row): array
public function map(mixed $row): array
{
/** @var Item $row */
return [
Expand Down
42 changes: 42 additions & 0 deletions app/Exports/Presence/PresenceExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Exports\Presence;

use App\Models\Presence;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\{FromCollection, WithHeadings, WithMapping};

class PresenceExport implements FromCollection, WithMapping, WithHeadings
{
public function collection(): Collection
{
return Presence::with('signatures')->get();
}

public function headings(): array
{
return [
'#',
'Nome',
'Telefone',
'Confirmado',
'Assinatura',
'Criado',
'Atualizado',
];
}

public function map(mixed $row): array
{
/** @var Presence $row */
return [
$row->id,
$row->name,
$row->phone,
$row->is_confirmed ? 'Sim' : 'Não',
$row->signatures?->first()->id ?? 'N/A', // @phpstan-ignore-line
$row->created_at->format('d/m/Y H:i:s'),
$row->updated_at->format('d/m/Y H:i:s'),
];
}
}
2 changes: 1 addition & 1 deletion app/Exports/Signature/SignatureExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function headings(): array
];
}

public function map($row): array
public function map(mixed $row): array
{
/** @var Signature $row */
return [
Expand Down
23 changes: 23 additions & 0 deletions app/Http/Controllers/PresenceController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace App\Http\Controllers;

use App\Exports\Presence\PresenceExport;
use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Facades\Excel;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class PresenceController extends Controller
{
public function index(): View
{
return view('presences');
}

public function export(): BinaryFileResponse
{
$file = sprintf('presenças-%s.xlsx', now()->format('Y-m-d_H:i'));

return Excel::download(new PresenceExport(), $file);
}
}
82 changes: 82 additions & 0 deletions app/Http/Livewire/Presence/Create.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace App\Http\Livewire\Presence;

use App\Models\Presence;
use Exception;
use Illuminate\Contracts\View\View;
use Illuminate\Validation\Rule;
use Livewire\Component;
use WireUi\Traits\Actions;

class Create extends Component
{
use Actions;

public Presence $presence;

public bool $modal = false;

public bool $observation = false;

protected array $validationAttributes = [
'presence.name' => 'nome',
'presence.phone' => 'telefone',
'presence.is_confirmed' => 'presença confirmada',
'presence.observation' => 'observação',
];

public function mount(): void
{
$this->presence();
}

public function render(): View
{
return view('livewire.presence.create');
}

public function rules(bool $observation = null): array
{
$observation ??= $this->observation;

return [
'presence.name' => ['required', 'string', 'max:255'],
'presence.phone' => ['nullable', 'string', 'max:20'],
'presence.is_confirmed' => ['required', 'boolean'],
'presence.observation' => [Rule::when($observation, ['required', 'string', 'max:1024'], ['nullable'])],
];
}

public function create(): void
{
$this->validate();

try {
$this->presence->save();

$this->emitUp('presence::index::refresh');

session()->flash('response', [
'type' => 'green',
'message' => 'Presença criada com sucesso!',
]);

return;
} catch (Exception $e) {
report($e);
} finally {
$this->presence();
}

session()->flash('response', [
'type' => 'red',
'message' => 'Erro ao criar a presença!',
]);
}

private function presence(): void
{
$this->presence = new Presence(['phone' => '', 'is_confirmed' => true]);
}
}
54 changes: 54 additions & 0 deletions app/Http/Livewire/Presence/Delete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace App\Http\Livewire\Presence;

use App\Models\Presence;
use Exception;
use Livewire\Component;
use WireUi\Traits\Actions;

class Delete extends Component
{
use Actions;

public Presence $presence;

public function render(): string
{
return <<<'blade'
<x-button.circle primary
icon="trash"
wire:click="confirmation"
/>
blade;
}

public function confirmation(): void
{
$this->dialog()->confirm([
'title' => 'Confirmação!',
'description' => 'Deseja realmente deletar esta assinatura?',
'icon' => 'error',
'accept' => [
'label' => 'Sim!',
'method' => 'delete',
],
]);
}

public function delete(): void
{
try {
$this->presence->delete();

$this->emitUp('presence::index::refresh');
$this->notification()->success('Presença deletada com sucesso!');

return;
} catch (Exception $e) {
report($e);
}

$this->notification()->error('Erro ao deletar presença!');
}
}
32 changes: 32 additions & 0 deletions app/Http/Livewire/Presence/Filter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace App\Http\Livewire\Presence;

use App\Exports\Contracts\ShouldExport;
use App\Http\Livewire\Traits\InteractWithExportation;
use Illuminate\Contracts\View\View;
use Livewire\Component;
use WireUi\Traits\Actions;

class Filter extends Component implements ShouldExport
{
use Actions;
use InteractWithExportation;

public bool $modal = false;

public function render(): View
{
return view('livewire.presence.filter');
}

public function clear(): void
{
//
}

public function exportable(): array
{
return [];
}
}
38 changes: 38 additions & 0 deletions app/Http/Livewire/Presence/Index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace App\Http\Livewire\Presence;

use App\Http\Livewire\Traits\Table;
use App\Models\Presence;
use Illuminate\Contracts\View\View;
use Illuminate\Pagination\LengthAwarePaginator;
use Livewire\Component;

class Index extends Component
{
use Table;

protected $listeners = [
'presence::index::refresh' => '$refresh',
];

public function render(): View
{
return view('livewire.presence.index', [
'presences' => $this->data(),
]);
}

private function data(): LengthAwarePaginator
{
return Presence::withCount('signatures')
->search($this->search, 'name', 'phone')
->orderBy($this->sort, $this->direction)
->paginate($this->quantity);
}

public function update(Presence $presence): void
{
$this->emitTo(Update::class, 'presence::update::load', $presence);
}
}
Loading

0 comments on commit 8eb4bc1

Please sign in to comment.