You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CashFlowStatementReportTransformer.php 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. <?php
  2. namespace App\Transformers;
  3. use App\DTO\AccountDTO;
  4. use App\DTO\ReportCategoryDTO;
  5. use App\DTO\ReportTypeDTO;
  6. class CashFlowStatementReportTransformer extends SummaryReportTransformer
  7. {
  8. public function getTitle(): string
  9. {
  10. return 'Cash Flow Statement';
  11. }
  12. public function getCategories(): array
  13. {
  14. $categories = [];
  15. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  16. // Header for the main category
  17. $header = [];
  18. foreach ($this->getColumns() as $column) {
  19. $header[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
  20. }
  21. // Category-level summary
  22. $categorySummary = [];
  23. foreach ($this->getColumns() as $column) {
  24. $categorySummary[$column->getName()] = match ($column->getName()) {
  25. 'account_name' => 'Total ' . $accountCategoryName,
  26. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  27. default => '',
  28. };
  29. }
  30. // Accounts directly under the main category
  31. $data = array_map(function (AccountDTO $account) {
  32. $row = [];
  33. foreach ($this->getColumns() as $column) {
  34. $row[$column->getName()] = match ($column->getName()) {
  35. 'account_code' => $account->accountCode,
  36. 'account_name' => [
  37. 'name' => $account->accountName,
  38. 'id' => $account->accountId ?? null,
  39. 'start_date' => $account->startDate,
  40. 'end_date' => $account->endDate,
  41. ],
  42. 'net_movement' => $account->balance->netMovement ?? '',
  43. default => '',
  44. };
  45. }
  46. return $row;
  47. }, $accountCategory->accounts ?? []);
  48. // Subcategories (types) under the main category
  49. $types = [];
  50. foreach ($accountCategory->types as $typeName => $type) {
  51. // Header for subcategory (type)
  52. $typeHeader = [];
  53. foreach ($this->getColumns() as $column) {
  54. $typeHeader[$column->getName()] = $column->getName() === 'account_name' ? $typeName : '';
  55. }
  56. // Account data for the subcategory
  57. $typeData = array_map(function (AccountDTO $account) {
  58. $row = [];
  59. foreach ($this->getColumns() as $column) {
  60. $row[$column->getName()] = match ($column->getName()) {
  61. 'account_code' => $account->accountCode,
  62. 'account_name' => [
  63. 'name' => $account->accountName,
  64. 'id' => $account->accountId ?? null,
  65. 'start_date' => $account->startDate,
  66. 'end_date' => $account->endDate,
  67. ],
  68. 'net_movement' => $account->balance->netMovement ?? '',
  69. default => '',
  70. };
  71. }
  72. return $row;
  73. }, $type->accounts ?? []);
  74. // Subcategory (type) summary
  75. $typeSummary = [];
  76. foreach ($this->getColumns() as $column) {
  77. $typeSummary[$column->getName()] = match ($column->getName()) {
  78. 'account_name' => 'Total ' . $typeName,
  79. 'net_movement' => $type->summary->netMovement ?? '',
  80. default => '',
  81. };
  82. }
  83. // Add subcategory (type) to the list
  84. $types[$typeName] = new ReportTypeDTO(
  85. header: $typeHeader,
  86. data: $typeData,
  87. summary: $typeSummary,
  88. );
  89. }
  90. // Add the category to the final array with its direct accounts and subcategories (types)
  91. $categories[$accountCategoryName] = new ReportCategoryDTO(
  92. header: $header,
  93. data: $data, // Direct accounts under the category
  94. summary: $categorySummary,
  95. types: $types, // Subcategories (types) under the category
  96. );
  97. }
  98. return $categories;
  99. }
  100. public function getSummaryCategories(): array
  101. {
  102. $summaryCategories = [];
  103. $columns = $this->getSummaryColumns();
  104. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  105. $categoryHeader = [];
  106. foreach ($columns as $column) {
  107. $categoryHeader[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
  108. }
  109. $categorySummary = [];
  110. foreach ($columns as $column) {
  111. $categorySummary[$column->getName()] = match ($column->getName()) {
  112. 'account_name' => 'Total ' . $accountCategoryName,
  113. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  114. default => '',
  115. };
  116. }
  117. $types = [];
  118. // Iterate through each account type and calculate type summaries
  119. foreach ($accountCategory->types as $typeName => $type) {
  120. $typeSummary = [];
  121. foreach ($columns as $column) {
  122. $typeSummary[$column->getName()] = match ($column->getName()) {
  123. 'account_name' => 'Total ' . $typeName,
  124. 'net_movement' => $type->summary->netMovement ?? '',
  125. default => '',
  126. };
  127. }
  128. $types[$typeName] = new ReportTypeDTO(
  129. header: [],
  130. data: [],
  131. summary: $typeSummary,
  132. );
  133. }
  134. // Add the category with its types and summary to the final array
  135. $summaryCategories[$accountCategoryName] = new ReportCategoryDTO(
  136. header: $categoryHeader,
  137. data: [],
  138. summary: $categorySummary,
  139. types: $types,
  140. );
  141. }
  142. return $summaryCategories;
  143. }
  144. public function getOverallTotals(): array
  145. {
  146. return [];
  147. }
  148. public function getSummaryOverallTotals(): array
  149. {
  150. return [];
  151. }
  152. public function getSummary(): array
  153. {
  154. return [
  155. [
  156. 'label' => 'Gross Cash Inflow',
  157. 'value' => $this->report->overallTotal->debitBalance ?? '',
  158. ],
  159. [
  160. 'label' => 'Gross Cash Outflow',
  161. 'value' => $this->report->overallTotal->creditBalance ?? '',
  162. ],
  163. [
  164. 'label' => 'Net Cash Change',
  165. 'value' => $this->report->overallTotal->netMovement ?? '',
  166. ],
  167. ];
  168. }
  169. public function getSummaryAlignedWithColumns(): array
  170. {
  171. $summary = [];
  172. foreach ($this->getSummary() as $summaryItem) {
  173. $row = [];
  174. foreach ($this->getColumns() as $column) {
  175. $row[$column->getName()] = match ($column->getName()) {
  176. 'account_name' => $summaryItem['label'] ?? '',
  177. 'net_movement' => $summaryItem['value'] ?? '',
  178. default => '',
  179. };
  180. }
  181. $summary[] = $row;
  182. }
  183. return $summary;
  184. }
  185. public function getOverview(): array
  186. {
  187. $categories = [];
  188. foreach ($this->report->overview->categories as $categoryName => $category) {
  189. $header = [];
  190. foreach ($this->getColumns() as $column) {
  191. $header[$column->getName()] = $column->getName() === 'account_name' ? $categoryName : '';
  192. }
  193. $data = array_map(function (AccountDTO $account) {
  194. $row = [];
  195. foreach ($this->getColumns() as $column) {
  196. $row[$column->getName()] = match ($column->getName()) {
  197. 'account_code' => $account->accountCode,
  198. 'account_name' => [
  199. 'name' => $account->accountName,
  200. 'id' => $account->accountId ?? null,
  201. 'start_date' => $account->startDate,
  202. 'end_date' => $account->endDate,
  203. ],
  204. 'net_movement' => $account->balance->startingBalance ?? $account->balance->endingBalance ?? '',
  205. default => '',
  206. };
  207. }
  208. return $row;
  209. }, $category->accounts);
  210. $summary = [];
  211. foreach ($this->getColumns() as $column) {
  212. $summary[$column->getName()] = match ($column->getName()) {
  213. 'account_name' => 'Total ' . $categoryName,
  214. 'net_movement' => $category->summary->startingBalance ?? $category->summary->endingBalance ?? '',
  215. default => '',
  216. };
  217. }
  218. $categories[] = new ReportCategoryDTO(
  219. header: $header,
  220. data: $data,
  221. summary: $summary,
  222. );
  223. }
  224. return $categories;
  225. }
  226. }