Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

CashFlowStatementReportTransformer.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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' => $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' => 'Total Cash Inflows',
  157. 'value' => $this->report->overallTotal->debitBalance ?? '',
  158. ],
  159. [
  160. 'label' => 'Total Cash Outflows',
  161. 'value' => $this->report->overallTotal->creditBalance ?? '',
  162. ],
  163. [
  164. 'label' => 'Net Cash Flow',
  165. 'value' => $this->report->overallTotal->netMovement ?? '',
  166. ],
  167. ];
  168. }
  169. public function getOverviewAlignedWithColumns(): 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 getSummaryOverviewAlignedWithColumns(): array
  186. {
  187. return array_map(static function ($row) {
  188. unset($row['account_code']);
  189. return $row;
  190. }, $this->getOverviewAlignedWithColumns());
  191. }
  192. public function getCashInflowAndOutflowHeaders(): array
  193. {
  194. return once(function (): array {
  195. $headers = [];
  196. $dateRange = $this->getStartDate() && $this->getEndDate()
  197. ? "{$this->getStartDate()} - {$this->getEndDate()}"
  198. : '';
  199. foreach ($this->getColumns() as $column) {
  200. $headers[$column->getName()] = match ($column->getName()) {
  201. 'account_name' => 'CASH INFLOWS AND OUTFLOWS',
  202. 'net_movement' => $dateRange,
  203. default => '',
  204. };
  205. }
  206. return $headers;
  207. });
  208. }
  209. public function getSummaryCashInflowAndOutflowHeaders(): array
  210. {
  211. return once(function (): array {
  212. $headers = $this->getCashInflowAndOutflowHeaders();
  213. // Remove the account_code key if it exists
  214. unset($headers['account_code']);
  215. return $headers;
  216. });
  217. }
  218. public function getOverviewHeaders(): array
  219. {
  220. return once(function (): array {
  221. $headers = [];
  222. foreach ($this->getColumns() as $column) {
  223. $headers[$column->getName()] = $column->getName() === 'account_name' ? 'OVERVIEW' : '';
  224. }
  225. return $headers;
  226. });
  227. }
  228. public function getSummaryOverviewHeaders(): array
  229. {
  230. return once(function (): array {
  231. $headers = $this->getOverviewHeaders();
  232. unset($headers['account_code']);
  233. return $headers;
  234. });
  235. }
  236. public function getOverview(): array
  237. {
  238. $categories = [];
  239. foreach ($this->report->overview->categories as $categoryName => $category) {
  240. $header = [];
  241. foreach ($this->getColumns() as $column) {
  242. $header[$column->getName()] = $column->getName() === 'account_name' ? $categoryName : '';
  243. }
  244. $data = array_map(function (AccountDTO $account) {
  245. $row = [];
  246. foreach ($this->getColumns() as $column) {
  247. $row[$column->getName()] = match ($column->getName()) {
  248. 'account_code' => $account->accountCode,
  249. 'account_name' => [
  250. 'name' => $account->accountName,
  251. 'id' => $account->accountId ?? null,
  252. 'start_date' => $account->startDate,
  253. 'end_date' => $account->endDate,
  254. ],
  255. 'net_movement' => $account->balance->startingBalance ?? $account->balance->endingBalance ?? '',
  256. default => '',
  257. };
  258. }
  259. return $row;
  260. }, $category->accounts);
  261. $summary = [];
  262. foreach ($this->getColumns() as $column) {
  263. $summary[$column->getName()] = match ($column->getName()) {
  264. 'account_name' => 'Total ' . $categoryName,
  265. 'net_movement' => $category->summary->startingBalance ?? $category->summary->endingBalance ?? '',
  266. default => '',
  267. };
  268. }
  269. $categories[] = new ReportCategoryDTO(
  270. header: $header,
  271. data: $data,
  272. summary: $summary,
  273. );
  274. }
  275. return $categories;
  276. }
  277. public function getSummaryOverview(): array
  278. {
  279. $summaryCategories = [];
  280. $columns = $this->getSummaryColumns();
  281. foreach ($this->report->overview->categories as $categoryName => $category) {
  282. $categorySummary = [];
  283. foreach ($columns as $column) {
  284. $categorySummary[$column->getName()] = match ($column->getName()) {
  285. 'account_name' => $categoryName,
  286. 'net_movement' => $category->summary->startingBalance ?? $category->summary->endingBalance ?? '',
  287. default => '',
  288. };
  289. }
  290. $summaryCategories[] = new ReportCategoryDTO(
  291. header: [],
  292. data: [],
  293. summary: $categorySummary,
  294. );
  295. }
  296. return $summaryCategories;
  297. }
  298. }