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';
$type = $isExpense ? 'debit' : 'credit';
$clientId = $payload->transactionable->branch->client_id;
$taxAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) {
return $balance->where('branch_id', $payload->transactionable->branch_id);
})->first();
$taxAccount = Account::query()
->where('account', $accountName)
->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(
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,
account: $taxAccount,
type: $type,
@@ -93,15 +99,19 @@ class CreateTransactionAction extends BaseAction
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
$accountName = $isExpense ? 'Payable Withholding Tax' : 'Creditable Withholding Tax';
$type = $isExpense ? 'credit' : 'debit';
$clientId = $payload->transactionable->branch->client_id;
$withholdingAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) {
return $balance->where('branch_id', $payload->transactionable->branch_id);
})->first();
$withholdingAccount = Account::query()
->where('account', $accountName)
->where('client_id', $clientId)
->first();
if ($withholdingAccount) {
$amount = $payload->transaction->payable_withholding_tax ?? 0.00;
if ($withholdingAccount && $amount > 0) {
$ledgerPayload = new CreateLedgerDTO(
branch_id: $payload->transactionable->branch_id,
amount: $payload->transaction->payable_withholding_tax ?? 0.00,
amount: $amount,
transaction: $payload->transaction,
account: $withholdingAccount,
type: $type,
@@ -116,12 +126,14 @@ class CreateTransactionAction extends BaseAction
$type = $isExpense ? 'credit' : 'debit';
$wht = $isExpense ? ($payload->transaction->payable_withholding_tax ?? 0) : ($payload->transaction->creditable_withholding_tax ?? 0);
$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) {
return $balance->where('branch_id', $payload->transactionable->branch_id);
})->first();
$cashAccount = Account::query()
->where('account', 'Cash')
->where('client_id', $clientId)
->first();
if ($cashAccount) {
if ($cashAccount && $amount > 0) {
$ledgerPayload = new CreateLedgerDTO(
branch_id: $payload->transactionable->branch_id,
amount: $amount,