- Add many-to-many relationships between Sale/Expense and Account models - Create pivot tables for account_sale and account_expense with migrations - Implement account syncing during sale/expense creation and editing - Add accounts_list attribute to display comma-separated account names - Introduce SaleService with DTO for sale creation logic - Simplify sales table columns to show branch, reference, date, and creator - Calculate and store aggregated financial fields from transactions - Make series field read-only instead of disabled in sale form
128 lines
4.2 KiB
PHP
128 lines
4.2 KiB
PHP
<?php
|
|
|
|
namespace App\Filament\Resources\SaleResource\Pages;
|
|
|
|
use App\Actions\Transactions\CreateTransactionAction;
|
|
use App\DataObjects\CreateTransactionDTO;
|
|
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\Resources\Pages\CreateRecord;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Support\Arr;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Illuminate\Support\Facades\Pipeline;
|
|
|
|
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
|
|
{
|
|
try {
|
|
DB::beginTransaction();
|
|
$record = app(SaleService::class)->create($this->getFormDataMutation($data));
|
|
$branch = $record->branch;
|
|
|
|
foreach ($transactions as $transaction) {
|
|
$tData = [
|
|
'branch_id' => $branch->id,
|
|
'happened_on' => $record->happened_on,
|
|
...$transaction,
|
|
];
|
|
|
|
$payload = new CreateTransactionDTO(data: $tData, transactionable: $record);
|
|
|
|
Pipeline::send(passable: $payload)->through(
|
|
[
|
|
CreateTransactionAction::class,
|
|
]
|
|
)->thenReturn();
|
|
}
|
|
|
|
$accountIds = collect($transactions)
|
|
->pluck('account_id')
|
|
->filter()
|
|
->unique()
|
|
->values()
|
|
->all();
|
|
|
|
$record->accounts()->sync($accountIds);
|
|
|
|
DB::commit();
|
|
} catch (\Exception $exception) {
|
|
DB::rollBack();
|
|
throw new \Exception('Failed to save transactions : '.$exception->getMessage());
|
|
}
|
|
|
|
return $record;
|
|
}
|
|
|
|
protected function afterCreate(): void
|
|
{
|
|
$branch = Branch::find($this->data['branch_id']);
|
|
}
|
|
}
|