Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

TrialBalanceReportTest.php 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. use App\Facades\Accounting;
  3. use App\Factories\ReportDateFactory;
  4. use App\Filament\Company\Pages\Reports\TrialBalance;
  5. use App\Models\Accounting\Transaction;
  6. use App\Services\AccountService;
  7. use App\Services\ReportService;
  8. use function Pest\Livewire\livewire;
  9. it('correctly builds a standard trial balance report', function () {
  10. $testCompany = $this->testCompany;
  11. $reportDates = ReportDateFactory::create($testCompany);
  12. $defaultDateRange = $reportDates->defaultDateRange;
  13. $defaultStartDate = $reportDates->defaultStartDate->toImmutable();
  14. $defaultEndDate = $reportDates->defaultEndDate->toImmutable();
  15. $defaultReportType = 'standard';
  16. $depositAmount = 1000;
  17. $withdrawalAmount = 1000;
  18. $depositCount = 10;
  19. $withdrawalCount = 10;
  20. Transaction::factory()
  21. ->forDefaultBankAccount()
  22. ->forUncategorizedRevenue()
  23. ->asDeposit($depositAmount)
  24. ->count($depositCount)
  25. ->state([
  26. 'posted_at' => $defaultStartDate->subWeek(),
  27. ])
  28. ->create();
  29. Transaction::factory()
  30. ->forDefaultBankAccount()
  31. ->forUncategorizedExpense()
  32. ->asWithdrawal($withdrawalAmount)
  33. ->count($withdrawalCount)
  34. ->state([
  35. 'posted_at' => now()->subWeek(),
  36. ])
  37. ->create();
  38. $defaultBankAccountAccount = $testCompany->default->bankAccount->account;
  39. $earliestTransactionDate = $reportDates->refresh()->earliestTransactionDate;
  40. $fields = $defaultBankAccountAccount->category->getRelevantBalanceFields();
  41. $expectedBalances = Accounting::getBalances(
  42. $defaultBankAccountAccount,
  43. $earliestTransactionDate->toDateString(),
  44. $defaultEndDate->toDateString(),
  45. $fields
  46. );
  47. $calculatedTrialBalances = calculateTrialBalances($defaultBankAccountAccount->category, $expectedBalances['ending_balance']);
  48. $formattedExpectedBalances = formatReportBalances($calculatedTrialBalances);
  49. livewire(TrialBalance::class)
  50. ->assertFormSet([
  51. 'deferredFilters.reportType' => $defaultReportType,
  52. 'deferredFilters.dateRange' => $defaultDateRange,
  53. 'deferredFilters.asOfDate' => $defaultEndDate->toDateTimeString(),
  54. ])
  55. ->assertSet('filters', [
  56. 'reportType' => $defaultReportType,
  57. 'dateRange' => $defaultDateRange,
  58. 'asOfDate' => $defaultEndDate->toDateString(),
  59. ])
  60. ->call('applyFilters')
  61. ->assertDontSeeText('Retained Earnings')
  62. ->assertSeeTextInOrder([
  63. $defaultBankAccountAccount->name,
  64. $formattedExpectedBalances->debitBalance,
  65. $formattedExpectedBalances->creditBalance,
  66. ])
  67. ->assertReportTableData();
  68. });
  69. it('correctly builds a post-closing trial balance report', function () {
  70. $testCompany = $this->testCompany;
  71. $reportDates = ReportDateFactory::create($testCompany);
  72. $defaultDateRange = $reportDates->defaultDateRange;
  73. $defaultStartDate = $reportDates->defaultStartDate->toImmutable();
  74. $defaultEndDate = $reportDates->defaultEndDate->toImmutable();
  75. $defaultReportType = 'postClosing';
  76. $depositAmount = 2000;
  77. $withdrawalAmount = 1000;
  78. $depositCount = 5;
  79. $withdrawalCount = 5;
  80. Transaction::factory()
  81. ->forDefaultBankAccount()
  82. ->forUncategorizedRevenue()
  83. ->asDeposit($depositAmount)
  84. ->count($depositCount)
  85. ->state([
  86. 'posted_at' => $defaultStartDate->subWeek(),
  87. ])
  88. ->create();
  89. Transaction::factory()
  90. ->forDefaultBankAccount()
  91. ->forUncategorizedExpense()
  92. ->asWithdrawal($withdrawalAmount)
  93. ->count($withdrawalCount)
  94. ->state([
  95. 'posted_at' => now()->subWeek(),
  96. ])
  97. ->create();
  98. $defaultBankAccountAccount = $testCompany->default->bankAccount->account;
  99. $earliestTransactionDate = $reportDates->refresh()->earliestTransactionDate->toImmutable();
  100. $fields = $defaultBankAccountAccount->category->getRelevantBalanceFields();
  101. $accountService = app(AccountService::class);
  102. $balances = $accountService->getAccountBalances($earliestTransactionDate->toDateTimeString(), $defaultEndDate->toDateTimeString(), [$defaultBankAccountAccount->id]);
  103. $account = $balances->find($defaultBankAccountAccount->id);
  104. $reportService = app(ReportService::class);
  105. $calculatedBalances = $reportService->calculateAccountBalances($account, $account->category);
  106. $calculatedTrialBalances = calculateTrialBalances($defaultBankAccountAccount->category, $calculatedBalances['ending_balance']);
  107. $formattedExpectedBalances = formatReportBalances($calculatedTrialBalances);
  108. // Use Livewire to assert the report's filters and displayed data
  109. livewire(TrialBalance::class)
  110. ->set('deferredFilters.reportType', $defaultReportType)
  111. ->assertFormSet([
  112. 'deferredFilters.reportType' => $defaultReportType,
  113. 'deferredFilters.dateRange' => $defaultDateRange,
  114. 'deferredFilters.asOfDate' => $defaultEndDate->toDateTimeString(),
  115. ])
  116. ->call('applyFilters')
  117. ->assertSet('filters', [
  118. 'reportType' => $defaultReportType,
  119. 'dateRange' => $defaultDateRange,
  120. 'asOfDate' => $defaultEndDate->toDateString(),
  121. ])
  122. ->assertSeeText('Retained Earnings')
  123. ->assertSeeTextInOrder([
  124. $defaultBankAccountAccount->name,
  125. $formattedExpectedBalances->debitBalance,
  126. $formattedExpectedBalances->creditBalance,
  127. ])
  128. ->assertReportTableData();
  129. });