Move the logic for generating base accounts for a new client from the ClientObserver into a dedicated command class (GenerateBaseAccountCommand). This improves code organization and reusability. - The command is now used in the ClientObserver::created method. - The command is also made available as a manual action in the AccountsRelationManager table header, allowing admins to generate base accounts for existing clients that lack them. - Added necessary imports to the CreateSale page, though the command is not directly used there in this diff, suggesting preparatory work for future integration.
97 lines
3.3 KiB
PHP
97 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\SaleResource\Pages;
|
|
|
|
use App\Actions\Sales\CreateSaleAction;
|
|
use App\Actions\Sales\SyncAccountsAction;
|
|
use App\Actions\Transactions\CreateRecordTransactionsAction;
|
|
use App\Commands\Clients\GenerateBaseAccountCommand;
|
|
use App\Filament\Resources\ClientResource;
|
|
use App\Filament\Resources\SaleResource;
|
|
use App\Models\Branch;
|
|
use App\Models\Client;
|
|
use App\Models\Sale;
|
|
use App\Services\Sales\SaleService;
|
|
use Filament\Actions\Action;
|
|
use Filament\Forms\Components\Actions;
|
|
use Filament\Resources\Pages\CreateRecord;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class CreateSale extends CreateRecord
|
|
{
|
|
protected static string $resource = SaleResource::class;
|
|
|
|
public ?int $clientId = null;
|
|
|
|
public function mount(): void
|
|
{
|
|
parent::mount();
|
|
|
|
$this->clientId = request()->integer('client_id');
|
|
}
|
|
|
|
public function getBreadcrumbs(): array
|
|
{
|
|
$client = $this->getClient();
|
|
|
|
if (! $client) {
|
|
return parent::getBreadcrumbs();
|
|
}
|
|
|
|
return [
|
|
ClientResource::getUrl('view', ['record' => $client->id]) => $client->company,
|
|
ClientResource::getUrl('view', ['record' => $client->id]).'?activeRelationManager=3' => 'Sales',
|
|
$this->getResource()::getUrl('create', ['client_id' => $client->id]) => 'Create',
|
|
];
|
|
}
|
|
|
|
protected function getClient(): Client|null
|
|
{
|
|
if (! $this->clientId) {
|
|
return null;
|
|
}
|
|
|
|
return Client::find($this->clientId);
|
|
}
|
|
|
|
protected function mutateFormDataBeforeCreate(array $data): array
|
|
{
|
|
return $this->getFormDataMutation($data);
|
|
}
|
|
|
|
protected function handleRecordCreation(array $data): Model
|
|
{
|
|
$transactions = $this->data['transactions'] ?? [];
|
|
return $this->processCreate($data, $transactions);
|
|
}
|
|
|
|
public function getFormDataMutation(array $data): array
|
|
{
|
|
$transactions = $data['transactions'] ?? [];
|
|
|
|
$data['gross_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['gross_amount'] ?? 0));
|
|
$data['exempt'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['exempt'] ?? 0));
|
|
$data['vatable_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['vatable_amount'] ?? 0));
|
|
$data['output_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['output_tax'] ?? 0));
|
|
$data['payable_withholding_tax'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['payable_withholding_tax'] ?? 0));
|
|
$data['discount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['discount'] ?? 0));
|
|
$data['net_amount'] = collect($transactions)->sum(fn (array $transaction) => (float) ($transaction['net_amount'] ?? 0));
|
|
|
|
return Arr::except($data, ['client', 'transactions', 'with_discount']);
|
|
}
|
|
|
|
public function processCreate(array $data, array $transactions): Model
|
|
{
|
|
$record = app(CreateSaleAction::class)($this->getFormDataMutation($data), $transactions);
|
|
|
|
return $record;
|
|
}
|
|
|
|
protected function afterCreate(): void
|
|
{
|
|
$branch = Branch::find($this->data['branch_id']);
|
|
}
|
|
}
|