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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 getPdfView(): string
  9. {
  10. return 'components.company.reports.cash-flow-statement-pdf';
  11. }
  12. public function getTitle(): string
  13. {
  14. return 'Cash Flow Statement';
  15. }
  16. public function getCategories(): array
  17. {
  18. $categories = [];
  19. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  20. // Header for the main category
  21. $header = [];
  22. foreach ($this->getColumns() as $column) {
  23. $header[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
  24. }
  25. // Category-level summary
  26. $categorySummary = [];
  27. foreach ($this->getColumns() as $column) {
  28. $categorySummary[$column->getName()] = match ($column->getName()) {
  29. 'account_name' => 'Total ' . $accountCategoryName,
  30. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  31. default => '',
  32. };
  33. }
  34. // Accounts directly under the main category
  35. $data = array_map(function (AccountDTO $account) {
  36. $row = [];
  37. foreach ($this->getColumns() as $column) {
  38. $row[$column->getName()] = match ($column->getName()) {
  39. 'account_code' => $account->accountCode,
  40. 'account_name' => [
  41. 'name' => $account->accountName,
  42. 'id' => $account->accountId ?? null,
  43. 'start_date' => $account->startDate,
  44. 'end_date' => $account->endDate,
  45. ],
  46. 'net_movement' => $account->balance->netMovement ?? '',
  47. default => '',
  48. };
  49. }
  50. return $row;
  51. }, $accountCategory->accounts ?? []);
  52. // Subcategories (types) under the main category
  53. $types = [];
  54. foreach ($accountCategory->types as $typeName => $type) {
  55. // Header for subcategory (type)
  56. $typeHeader = [];
  57. foreach ($this->getColumns() as $column) {
  58. $typeHeader[$column->getName()] = $column->getName() === 'account_name' ? $typeName : '';
  59. }
  60. // Account data for the subcategory
  61. $typeData = array_map(function (AccountDTO $account) {
  62. $row = [];
  63. foreach ($this->getColumns() as $column) {
  64. $row[$column->getName()] = match ($column->getName()) {
  65. 'account_code' => $account->accountCode,
  66. 'account_name' => [
  67. 'name' => $account->accountName,
  68. 'id' => $account->accountId ?? null,
  69. 'start_date' => $account->startDate,
  70. 'end_date' => $account->endDate,
  71. ],
  72. 'net_movement' => $account->balance->netMovement ?? '',
  73. default => '',
  74. };
  75. }
  76. return $row;
  77. }, $type->accounts ?? []);
  78. // Subcategory (type) summary
  79. $typeSummary = [];
  80. foreach ($this->getColumns() as $column) {
  81. $typeSummary[$column->getName()] = match ($column->getName()) {
  82. 'account_name' => 'Total ' . $typeName,
  83. 'net_movement' => $type->summary->netMovement ?? '',
  84. default => '',
  85. };
  86. }
  87. // Add subcategory (type) to the list
  88. $types[$typeName] = new ReportTypeDTO(
  89. header: $typeHeader,
  90. data: $typeData,
  91. summary: $typeSummary,
  92. );
  93. }
  94. // Add the category to the final array with its direct accounts and subcategories (types)
  95. $categories[$accountCategoryName] = new ReportCategoryDTO(
  96. header: $header,
  97. data: $data, // Direct accounts under the category
  98. summary: $categorySummary,
  99. types: $types, // Subcategories (types) under the category
  100. );
  101. }
  102. return $categories;
  103. }
  104. public function getSummaryCategories(): array
  105. {
  106. $summaryCategories = [];
  107. $columns = $this->getSummaryColumns();
  108. foreach ($this->report->categories as $accountCategoryName => $accountCategory) {
  109. $categoryHeader = [];
  110. foreach ($columns as $column) {
  111. $categoryHeader[$column->getName()] = $column->getName() === 'account_name' ? $accountCategoryName : '';
  112. }
  113. $categorySummary = [];
  114. foreach ($columns as $column) {
  115. $categorySummary[$column->getName()] = match ($column->getName()) {
  116. 'account_name' => 'Total ' . $accountCategoryName,
  117. 'net_movement' => $accountCategory->summary->netMovement ?? '',
  118. default => '',
  119. };
  120. }
  121. $types = [];
  122. // Iterate through each account type and calculate type summaries
  123. foreach ($accountCategory->types as $typeName => $type) {
  124. $typeSummary = [];
  125. foreach ($columns as $column) {
  126. $typeSummary[$column->getName()] = match ($column->getName()) {
  127. 'account_name' => $typeName,
  128. 'net_movement' => $type->summary->netMovement ?? '',
  129. default => '',
  130. };
  131. }
  132. $types[$typeName] = new ReportTypeDTO(
  133. header: [],
  134. data: [],
  135. summary: $typeSummary,
  136. );
  137. }
  138. // Add the category with its types and summary to the final array
  139. $summaryCategories[$accountCategoryName] = new ReportCategoryDTO(
  140. header: $categoryHeader,
  141. data: [],
  142. summary: $categorySummary,
  143. types: $types,
  144. );
  145. }
  146. return $summaryCategories;
  147. }
  148. public function getOverallTotals(): array
  149. {
  150. return [];
  151. }
  152. public function getSummaryOverallTotals(): array
  153. {
  154. return [];
  155. }
  156. public function getSummary(): array
  157. {
  158. return [
  159. [
  160. 'label' => 'Total Cash Inflows',
  161. 'value' => $this->report->overallTotal->debitBalance ?? '',
  162. ],
  163. [
  164. 'label' => 'Total Cash Outflows',
  165. 'value' => $this->report->overallTotal->creditBalance ?? '',
  166. ],
  167. [
  168. 'label' => 'Net Cash Flow',
  169. 'value' => $this->report->overallTotal->netMovement ?? '',
  170. ],
  171. ];
  172. }
  173. public function getOverviewAlignedWithColumns(): array
  174. {
  175. $summary = [];
  176. foreach ($this->getSummary() as $summaryItem) {
  177. $row = [];
  178. foreach ($this->getColumns() as $column) {
  179. $row[$column->getName()] = match ($column->getName()) {
  180. 'account_name' => $summaryItem['label'] ?? '',
  181. 'net_movement' => $summaryItem['value'] ?? '',
  182. default => '',
  183. };
  184. }
  185. $summary[] = $row;
  186. }
  187. return $summary;
  188. }
  189. public function getSummaryOverviewAlignedWithColumns(): array
  190. {
  191. return array_map(static function ($row) {
  192. unset($row['account_code']);
  193. return $row;
  194. }, $this->getOverviewAlignedWithColumns());
  195. }
  196. public function getOverviewHeaders(): array
  197. {
  198. return once(function (): array {
  199. $headers = [];
  200. foreach ($this->getColumns() as $column) {
  201. $headers[$column->getName()] = $column->getName() === 'account_name' ? 'OVERVIEW' : '';
  202. }
  203. return $headers;
  204. });
  205. }
  206. public function getSummaryOverviewHeaders(): array
  207. {
  208. return once(function (): array {
  209. $headers = $this->getOverviewHeaders();
  210. unset($headers['account_code']);
  211. return $headers;
  212. });
  213. }
  214. public function getOverview(): array
  215. {
  216. $categories = [];
  217. foreach ($this->report->overview->categories as $categoryName => $category) {
  218. $header = [];
  219. foreach ($this->getColumns() as $column) {
  220. $header[$column->getName()] = $column->getName() === 'account_name' ? $categoryName : '';
  221. }
  222. $data = array_map(function (AccountDTO $account) {
  223. $row = [];
  224. foreach ($this->getColumns() as $column) {
  225. $row[$column->getName()] = match ($column->getName()) {
  226. 'account_code' => $account->accountCode,
  227. 'account_name' => [
  228. 'name' => $account->accountName,
  229. 'id' => $account->accountId ?? null,
  230. 'start_date' => $account->startDate,
  231. 'end_date' => $account->endDate,
  232. ],
  233. 'net_movement' => $account->balance->startingBalance ?? $account->balance->endingBalance ?? '',
  234. default => '',
  235. };
  236. }
  237. return $row;
  238. }, $category->accounts);
  239. $summary = [];
  240. foreach ($this->getColumns() as $column) {
  241. $summary[$column->getName()] = match ($column->getName()) {
  242. 'account_name' => 'Total ' . $categoryName,
  243. 'net_movement' => $category->summary->startingBalance ?? $category->summary->endingBalance ?? '',
  244. default => '',
  245. };
  246. }
  247. $categories[] = new ReportCategoryDTO(
  248. header: $header,
  249. data: $data,
  250. summary: $summary,
  251. );
  252. }
  253. return $categories;
  254. }
  255. public function getSummaryOverview(): array
  256. {
  257. $summaryCategories = [];
  258. $columns = $this->getSummaryColumns();
  259. foreach ($this->report->overview->categories as $categoryName => $category) {
  260. $categorySummary = [];
  261. foreach ($columns as $column) {
  262. $categorySummary[$column->getName()] = match ($column->getName()) {
  263. 'account_name' => $categoryName,
  264. 'net_movement' => $category->summary->startingBalance ?? $category->summary->endingBalance ?? '',
  265. default => '',
  266. };
  267. }
  268. $summaryCategories[] = new ReportCategoryDTO(
  269. header: [],
  270. data: [],
  271. summary: $categorySummary,
  272. );
  273. }
  274. return $summaryCategories;
  275. }
  276. }