- Add trial balance and general ledger pages to client resource with interactive tables - Implement sales and expenses relation managers for client-specific transactions - Enhance transaction handling with proper tax and withholding calculations - Add date casting to Transaction model and define client relationships - Configure super admin role bypass in AppServiceProvider - Update Filament components and fix JavaScript formatting issues
136 lines
5.0 KiB
PHP
136 lines
5.0 KiB
PHP
<?php
|
|
|
|
namespace App\Actions\Transactions;
|
|
|
|
use App\Actions\Balances\CreateBalanceAction;
|
|
use App\Actions\BaseAction;
|
|
use App\Actions\Ledgers\CreateLedgerAction;
|
|
use App\DataObjects\CreateLedgerDTO;
|
|
use App\DataObjects\CreateTransactionDTO;
|
|
use App\Models\Account;
|
|
use Closure;
|
|
use Illuminate\Support\Facades\Pipeline;
|
|
use Spatie\LaravelData\Data;
|
|
|
|
class CreateTransactionAction extends BaseAction
|
|
{
|
|
public function __invoke(CreateTransactionDTO|Data $payload, Closure $next)
|
|
{
|
|
$payload->transaction = $payload->transactionable->transactions()->create($payload->data);
|
|
|
|
$this->transactionAccountLedger($payload);
|
|
|
|
$this->withHoldingAccountLedger($payload);
|
|
|
|
$this->cashAccountLedger($payload);
|
|
|
|
return $next($payload);
|
|
}
|
|
|
|
public function transactionAccountLedger($payload): void
|
|
{
|
|
$branch = $payload->transaction->branch;
|
|
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
|
|
$type = $isExpense ? 'debit' : 'credit';
|
|
|
|
if ($branch->isClientVatable) {
|
|
//create transaction account ledger
|
|
$ledgerPayload = new CreateLedgerDTO(
|
|
branch_id: $payload->transactionable->branch_id,
|
|
amount: $payload->transaction->net_amount ?? 0.00,
|
|
transaction: $payload->transaction,
|
|
account: $payload->transaction->account,
|
|
type: $type,
|
|
);
|
|
$this->ledgerPipe($ledgerPayload);
|
|
|
|
$this->taxAccountLedger($payload, $isExpense);
|
|
} else {
|
|
//create transaction account ledger
|
|
$ledgerPayload = new CreateLedgerDTO(
|
|
branch_id: $payload->transactionable->branch_id,
|
|
amount: $payload->transaction->gross_amount ?? 0.00,
|
|
transaction: $payload->transaction,
|
|
account: $payload->transaction->account,
|
|
type: $type,
|
|
);
|
|
$this->ledgerPipe($ledgerPayload);
|
|
}
|
|
|
|
}
|
|
|
|
public function ledgerPipe(CreateLedgerDTO $ledgerPayload): mixed
|
|
{
|
|
return Pipeline::send(passable: $ledgerPayload)->through([
|
|
CreateLedgerAction::class,
|
|
CreateBalanceAction::class,
|
|
])->thenReturn();
|
|
}
|
|
|
|
public function taxAccountLedger($payload, bool $isExpense): void
|
|
{
|
|
$accountName = $isExpense ? 'Input Tax' : 'Output Tax';
|
|
$type = $isExpense ? 'debit' : 'credit';
|
|
|
|
$taxAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) {
|
|
return $balance->where('branch_id', $payload->transactionable->branch_id);
|
|
})->first();
|
|
|
|
if ($taxAccount) {
|
|
$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?
|
|
transaction: $payload->transaction,
|
|
account: $taxAccount,
|
|
type: $type,
|
|
);
|
|
$this->ledgerPipe($ledgerPayload);
|
|
}
|
|
}
|
|
|
|
public function withHoldingAccountLedger($payload): void
|
|
{
|
|
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
|
|
$accountName = $isExpense ? 'Payable Withholding Tax' : 'Creditable Withholding Tax';
|
|
$type = $isExpense ? 'credit' : 'debit';
|
|
|
|
$withholdingAccount = Account::query()->where('account', $accountName)->whereHas('balances', function ($balance) use ($payload) {
|
|
return $balance->where('branch_id', $payload->transactionable->branch_id);
|
|
})->first();
|
|
|
|
if ($withholdingAccount) {
|
|
$ledgerPayload = new CreateLedgerDTO(
|
|
branch_id: $payload->transactionable->branch_id,
|
|
amount: $payload->transaction->payable_withholding_tax ?? 0.00,
|
|
transaction: $payload->transaction,
|
|
account: $withholdingAccount,
|
|
type: $type,
|
|
);
|
|
$this->ledgerPipe($ledgerPayload);
|
|
}
|
|
}
|
|
|
|
public function cashAccountLedger($payload): void
|
|
{
|
|
$isExpense = $payload->transactionable instanceof \App\Models\Expense;
|
|
$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;
|
|
|
|
$cashAccount = Account::query()->where('account', 'Cash')->whereHas('balances', function ($balance) use ($payload) {
|
|
return $balance->where('branch_id', $payload->transactionable->branch_id);
|
|
})->first();
|
|
|
|
if ($cashAccount) {
|
|
$ledgerPayload = new CreateLedgerDTO(
|
|
branch_id: $payload->transactionable->branch_id,
|
|
amount: $amount,
|
|
transaction: $payload->transaction,
|
|
account: $cashAccount,
|
|
type: $type,
|
|
);
|
|
$this->ledgerPipe($ledgerPayload);
|
|
}
|
|
}
|
|
}
|