Skip to content

Commit

Permalink
Merge pull request #62 from andrewdwallo/development-3.x
Browse files Browse the repository at this point in the history
Development 3.x
  • Loading branch information
andrewdwallo authored Jul 28, 2024
2 parents 2e787f8 + bf62ce1 commit a7ed093
Show file tree
Hide file tree
Showing 26 changed files with 856 additions and 613 deletions.
26 changes: 1 addition & 25 deletions app/DTO/AccountBalanceDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\DTO;

use Livewire\Wireable;

class AccountBalanceDTO implements Wireable
class AccountBalanceDTO
{
public function __construct(
public ?string $startingBalance,
Expand All @@ -13,26 +11,4 @@ public function __construct(
public ?string $netMovement,
public ?string $endingBalance,
) {}

public function toLivewire(): array
{
return [
'startingBalance' => $this->startingBalance,
'debitBalance' => $this->debitBalance,
'creditBalance' => $this->creditBalance,
'netMovement' => $this->netMovement,
'endingBalance' => $this->endingBalance,
];
}

public static function fromLivewire($value): static
{
return new static(
$value['startingBalance'],
$value['debitBalance'],
$value['creditBalance'],
$value['netMovement'],
$value['endingBalance'],
);
}
}
20 changes: 1 addition & 19 deletions app/DTO/AccountCategoryDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\DTO;

use Livewire\Wireable;

class AccountCategoryDTO implements Wireable
class AccountCategoryDTO
{
/**
* @param AccountDTO[] $accounts
Expand All @@ -13,20 +11,4 @@ public function __construct(
public array $accounts,
public AccountBalanceDTO $summary,
) {}

public function toLivewire(): array
{
return [
'accounts' => $this->accounts,
'summary' => $this->summary->toLivewire(),
];
}

public static function fromLivewire($value): static
{
return new static(
$value['accounts'],
AccountBalanceDTO::fromLivewire($value['summary']),
);
}
}
23 changes: 2 additions & 21 deletions app/DTO/AccountDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,12 @@

namespace App\DTO;

use Livewire\Wireable;

class AccountDTO implements Wireable
class AccountDTO
{
public function __construct(
public string $accountName,
public string $accountCode,
public ?int $accountId,
public AccountBalanceDTO $balance,
) {}

public function toLivewire(): array
{
return [
'accountName' => $this->accountName,
'accountCode' => $this->accountCode,
'balance' => $this->balance->toLivewire(),
];
}

public static function fromLivewire($value): static
{
return new static(
$value['accountName'],
$value['accountCode'],
AccountBalanceDTO::fromLivewire($value['balance']),
);
}
}
5 changes: 5 additions & 0 deletions app/DTO/AccountTransactionDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,18 @@

namespace App\DTO;

use App\Enums\Accounting\TransactionType;

class AccountTransactionDTO
{
public function __construct(
public ?int $id,
public string $date,
public string $description,
public string $debit,
public string $credit,
public string $balance,
public ?TransactionType $type,
public ?string $tableAction,
) {}
}
22 changes: 1 addition & 21 deletions app/DTO/ReportDTO.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

namespace App\DTO;

use Livewire\Wireable;

class ReportDTO implements Wireable
class ReportDTO
{
public function __construct(
/**
Expand All @@ -14,22 +12,4 @@ public function __construct(
public ?AccountBalanceDTO $overallTotal = null,
public array $fields = [],
) {}

public function toLivewire(): array
{
return [
'categories' => $this->categories,
'overallTotal' => $this->overallTotal?->toLivewire(),
'fields' => $this->fields,
];
}

public static function fromLivewire($value): static
{
return new static(
$value['categories'],
isset($value['overallTotal']) ? AccountBalanceDTO::fromLivewire($value['overallTotal']) : null,
$value['fields'] ?? [],
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ protected function handleRecordCreation(array $data): Model
throw new Halt('No authenticated user found');
}

return $this->handleRecordCreationWithUniqueField($data, new Currency(), $user);
return $this->handleRecordCreationWithUniqueField($data, new Currency, $user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ protected function handleRecordCreation(array $data): Model

$evaluatedTypes = [DiscountType::Sales, DiscountType::Purchase];

return $this->handleRecordCreationWithUniqueField($data, new Discount(), $user, 'type', $evaluatedTypes);
return $this->handleRecordCreationWithUniqueField($data, new Discount, $user, 'type', $evaluatedTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ protected function handleRecordCreation(array $data): Model

$evaluatedTypes = [TaxType::Sales, TaxType::Purchase];

return $this->handleRecordCreationWithUniqueField($data, new Tax(), $user, 'type', $evaluatedTypes);
return $this->handleRecordCreationWithUniqueField($data, new Tax, $user, 'type', $evaluatedTypes);
}
}
2 changes: 2 additions & 0 deletions app/Filament/Company/Pages/Reports/AccountTransactions.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Guava\FilamentClusters\Forms\Cluster;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Support\Collection;
use Livewire\Attributes\Url;
use Symfony\Component\HttpFoundation\StreamedResponse;

class AccountTransactions extends BaseReportPage
Expand All @@ -32,6 +33,7 @@ class AccountTransactions extends BaseReportPage

protected ExportService $exportService;

#[Url]
public ?string $account_id = 'all';

public function boot(ReportService $reportService, ExportService $exportService): void
Expand Down
2 changes: 0 additions & 2 deletions app/Filament/Company/Pages/Reports/TrialBalance.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,9 @@ public function getTable(): array
->alignment(Alignment::Left),
Column::make('debit_balance')
->label('Debit')
->toggleable()
->alignment(Alignment::Right),
Column::make('credit_balance')
->label('Credit')
->toggleable()
->alignment(Alignment::Right),
];
}
Expand Down
71 changes: 59 additions & 12 deletions app/Services/AccountService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@
use App\Contracts\AccountHandler;
use App\Enums\Accounting\AccountCategory;
use App\Models\Accounting\Account;
use App\Models\Accounting\JournalEntry;
use App\Models\Accounting\Transaction;
use App\Models\Banking\BankAccount;
use App\Repositories\Accounting\JournalEntryRepository;
use App\Utilities\Currency\CurrencyAccessor;
use App\ValueObjects\Money;
use Illuminate\Support\Facades\DB;

class AccountService implements AccountHandler
{
Expand Down Expand Up @@ -65,6 +66,31 @@ public function getEndingBalance(Account $account, string $startDate, string $en
return new Money($endingBalance, $account->currency_code);
}

public function getRetainedEarnings(string $startDate): Money
{
$revenue = JournalEntry::whereHas('account', static function ($query) {
$query->where('category', AccountCategory::Revenue);
})
->where('type', 'credit')
->whereHas('transaction', static function ($query) use ($startDate) {
$query->where('posted_at', '<=', $startDate);
})
->sum('amount');

$expense = JournalEntry::whereHas('account', static function ($query) {
$query->where('category', AccountCategory::Expense);
})
->where('type', 'debit')
->whereHas('transaction', static function ($query) use ($startDate) {
$query->where('posted_at', '<=', $startDate);
})
->sum('amount');

$retainedEarnings = $revenue - $expense;

return new Money($retainedEarnings, CurrencyAccessor::getDefaultCurrency());
}

private function calculateNetMovementByCategory(AccountCategory $category, int $debitBalance, int $creditBalance): int
{
return match ($category) {
Expand Down Expand Up @@ -125,20 +151,41 @@ public function getBalances(Account $account, string $startDate, string $endDate

public function getTotalBalanceForAllBankAccounts(string $startDate, string $endDate): Money
{
$bankAccounts = BankAccount::with('account')
->get();
$accountIds = Account::whereHas('bankAccount')
->pluck('id')
->toArray();

$totalBalance = 0;

foreach ($bankAccounts as $bankAccount) {
$account = $bankAccount->account;

if ($account) {
$endingBalance = $this->getEndingBalance($account, $startDate, $endDate)?->getAmount() ?? 0;
$totalBalance += $endingBalance;
}
if (empty($accountIds)) {
return new Money(0, CurrencyAccessor::getDefaultCurrency());
}

$result = DB::table('journal_entries')
->join('transactions', 'journal_entries.transaction_id', '=', 'transactions.id')
->whereIn('journal_entries.account_id', $accountIds)
->where('transactions.posted_at', '<=', $endDate)
->selectRaw('
SUM(CASE
WHEN transactions.posted_at <= ? AND journal_entries.type = "debit" THEN journal_entries.amount
WHEN transactions.posted_at <= ? AND journal_entries.type = "credit" THEN -journal_entries.amount
ELSE 0
END) AS totalStartingBalance,
SUM(CASE
WHEN transactions.posted_at BETWEEN ? AND ? AND journal_entries.type = "debit" THEN journal_entries.amount
WHEN transactions.posted_at BETWEEN ? AND ? AND journal_entries.type = "credit" THEN -journal_entries.amount
ELSE 0
END) AS totalNetMovement
', [
$startDate,
$startDate,
$startDate,
$endDate,
$startDate,
$endDate,
])
->first();

$totalBalance = $result->totalStartingBalance + $result->totalNetMovement;

return new Money($totalBalance, CurrencyAccessor::getDefaultCurrency());
}

Expand Down
Loading

0 comments on commit a7ed093

Please sign in to comment.