feat(client): add journals relation and improve transaction ledger handling

- Add journals relation to Client model via Branch
- Update branch selection in JournalsRelationManager to use code instead of name
- Improve tax, withholding, and cash account ledger queries by adding client_id filter and amount checks
- Add missing import for GenerateVoucher command in ExpensesRelationManager
- Label 'Normal Balance' column in AccountsRelationManager
This commit is contained in:
Jp
2026-02-09 22:16:10 +08:00
parent 207f4c1609
commit 1548e178bc
5 changed files with 34 additions and 16 deletions

View File

@@ -71,15 +71,21 @@ class CreateTransactionAction extends BaseAction
{ {
$accountName = $isExpense ? 'Input Tax' : 'Output Tax'; $accountName = $isExpense ? 'Input Tax' : 'Output Tax';
$type = $isExpense ? 'debit' : 'credit'; $type = $isExpense ? 'debit' : 'credit';
$clientId = $payload->transactionable->branch->client_id;
$taxAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) { $taxAccount = Account::query()
return $balance->where('branch_id', $payload->transactionable->branch_id); ->where('account', $accountName)
})->first(); ->where('client_id', $clientId)
->first();
if ($taxAccount) { $amount = $isExpense
? ($payload->transactionable->input_tax ?? 0.00)
: ($payload->transactionable->output_tax ?? 0.00);
if ($taxAccount && $amount > 0) {
$ledgerPayload = new CreateLedgerDTO( $ledgerPayload = new CreateLedgerDTO(
branch_id: $payload->transactionable->branch_id, branch_id: $payload->transactionable->branch_id,
amount: $payload->transactionable->input_tax ?? 0.00, // Assuming input_tax holds the tax amount for both? Or output_tax? amount: $amount,
transaction: $payload->transaction, transaction: $payload->transaction,
account: $taxAccount, account: $taxAccount,
type: $type, type: $type,
@@ -93,15 +99,19 @@ class CreateTransactionAction extends BaseAction
$isExpense = $payload->transactionable instanceof \App\Models\Expense; $isExpense = $payload->transactionable instanceof \App\Models\Expense;
$accountName = $isExpense ? 'Payable Withholding Tax' : 'Creditable Withholding Tax'; $accountName = $isExpense ? 'Payable Withholding Tax' : 'Creditable Withholding Tax';
$type = $isExpense ? 'credit' : 'debit'; $type = $isExpense ? 'credit' : 'debit';
$clientId = $payload->transactionable->branch->client_id;
$withholdingAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) { $withholdingAccount = Account::query()
return $balance->where('branch_id', $payload->transactionable->branch_id); ->where('account', $accountName)
})->first(); ->where('client_id', $clientId)
->first();
if ($withholdingAccount) { $amount = $payload->transaction->payable_withholding_tax ?? 0.00;
if ($withholdingAccount && $amount > 0) {
$ledgerPayload = new CreateLedgerDTO( $ledgerPayload = new CreateLedgerDTO(
branch_id: $payload->transactionable->branch_id, branch_id: $payload->transactionable->branch_id,
amount: $payload->transaction->payable_withholding_tax ?? 0.00, amount: $amount,
transaction: $payload->transaction, transaction: $payload->transaction,
account: $withholdingAccount, account: $withholdingAccount,
type: $type, type: $type,
@@ -116,12 +126,14 @@ class CreateTransactionAction extends BaseAction
$type = $isExpense ? 'credit' : 'debit'; $type = $isExpense ? 'credit' : 'debit';
$wht = $isExpense ? ($payload->transaction->payable_withholding_tax ?? 0) : ($payload->transaction->creditable_withholding_tax ?? 0); $wht = $isExpense ? ($payload->transaction->payable_withholding_tax ?? 0) : ($payload->transaction->creditable_withholding_tax ?? 0);
$amount = ($payload->transaction->gross_amount ?? 0) - $wht; $amount = ($payload->transaction->gross_amount ?? 0) - $wht;
$clientId = $payload->transactionable->branch->client_id;
$cashAccount = Account::query()->where('account', 'Cash')->whereHas('balances', function ($balance) use ($payload) { $cashAccount = Account::query()
return $balance->where('branch_id', $payload->transactionable->branch_id); ->where('account', 'Cash')
})->first(); ->where('client_id', $clientId)
->first();
if ($cashAccount) { if ($cashAccount && $amount > 0) {
$ledgerPayload = new CreateLedgerDTO( $ledgerPayload = new CreateLedgerDTO(
branch_id: $payload->transactionable->branch_id, branch_id: $payload->transactionable->branch_id,
amount: $amount, amount: $amount,

View File

@@ -37,7 +37,7 @@ class AccountsRelationManager extends RelationManager
->columns([ ->columns([
Tables\Columns\TextColumn::make('account')->description(fn (Account $record): string => $record->description ?? ''), Tables\Columns\TextColumn::make('account')->description(fn (Account $record): string => $record->description ?? ''),
Tables\Columns\TextColumn::make('accountType.type'), Tables\Columns\TextColumn::make('accountType.type'),
Tables\Columns\TextColumn::make('accountType.normal_balance'), Tables\Columns\TextColumn::make('accountType.normal_balance')->label('Normal Balance'),
]) ])
->filters([ ->filters([
// //

View File

@@ -3,6 +3,7 @@
namespace App\Filament\Resources\ClientResource\RelationManagers; namespace App\Filament\Resources\ClientResource\RelationManagers;
use App\Actions\Transactions\CreateTransactionAction; use App\Actions\Transactions\CreateTransactionAction;
use App\Commands\Expenses\GenerateVoucher;
use App\DataObjects\CreateTransactionDTO; use App\DataObjects\CreateTransactionDTO;
use App\Models\Account; use App\Models\Account;
use App\Models\Branch; use App\Models\Branch;

View File

@@ -35,7 +35,7 @@ class JournalsRelationManager extends RelationManager
->schema([ ->schema([
Select::make('branch_id') Select::make('branch_id')
->label('Branch') ->label('Branch')
->options(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->pluck('name', 'id')) ->options(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->pluck('code', 'id'))
->required() ->required()
->default(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->first()?->id), ->default(fn () => Branch::where('client_id', $this->getOwnerRecord()->id)->first()?->id),

View File

@@ -73,4 +73,9 @@ class Client extends Model
{ {
return $this->hasManyThrough(Expense::class, Branch::class); return $this->hasManyThrough(Expense::class, Branch::class);
} }
public function journals(): HasManyThrough
{
return $this->hasManyThrough(Journal::class, Branch::class);
}
} }