<?php
 namespace Modules\Reports\AriaReports\Model\GetPivotTableStructure; class Response extends \Modules\Reports\AriaReports\Model\ResponseAbstract { protected ?string $type = null; protected ?object $pivotStructure = null; protected ?string $displayCurrency = null; public function getPivotStructure(): ?object { return $this->pivotStructure; } protected function processResponseObject(): void { $output = $this->getRelevantOutput(); if ($output->type=='message') { $firstContent = $output->content[0]; $this->textMessage = $firstContent->text; } elseif ($output->type=='function_call') { $pivotDefObj = new \Modules\Reports\AriaReports\Model\PivotTableDefinition(); $this->populatePivotDefObject($pivotDefObj, json_decode($output->arguments, true)); $this->pivotStructure = $pivotDefObj; } else { throw new \RuntimeException('Unknown output type from the AI.'); } } protected function populatePivotDefObject(\Modules\Reports\AriaReports\Model\PivotTableDefinition $pivotDefObj, array $aiGeneratedPivotStructure): void { if ($aiGeneratedPivotStructure['filters']) { foreach($aiGeneratedPivotStructure['filters'] as $filter) { $operation = $filter['operation']; $value = $filter['value']; if ($filter['value']=='null') { if ($operation=='=') { $operation = 'IS NULL'; } else { $operation = 'IS NOT NULL'; } $value= null; } $pivotDefObj->addFilter($filter['field'], $operation, $value); } } if ($aiGeneratedPivotStructure['dataCategory']) { $pivotDefObj->dataField = $aiGeneratedPivotStructure['dataCategory']['field']; $pivotDefObj->dataAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction']; if ($aiGeneratedPivotStructure['dataCategory']['currency']) { $pivotDefObj->dataCurrency = $aiGeneratedPivotStructure['dataCategory']['currency']; } } if ($aiGeneratedPivotStructure['rowsCategory']) { $pivotDefObj->rowsField = $aiGeneratedPivotStructure['rowsCategory']['field']; $pivotDefObj->rowsDisplayOthers = $aiGeneratedPivotStructure['rowsCategory']['displayOthers']; $pivotDefObj->rowsLimit = $aiGeneratedPivotStructure['rowsCategory']['limit']; if ($aiGeneratedPivotStructure['rowsCategory']['orderField']=='currentField') { $pivotDefObj->rowsOrderField = $aiGeneratedPivotStructure['rowsCategory']['field']; } else { $pivotDefObj->rowsOrderField = $aiGeneratedPivotStructure['dataCategory']['field']; $pivotDefObj->rowsOrderAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction']; } $pivotDefObj->rowsOrderDirection = $aiGeneratedPivotStructure['rowsCategory']['orderDirection']; } if ($aiGeneratedPivotStructure['colsCategory']) { $pivotDefObj->colsField = $aiGeneratedPivotStructure['colsCategory']['field']; $pivotDefObj->colsDisplayOthers = $aiGeneratedPivotStructure['colsCategory']['displayOthers']; $pivotDefObj->colsLimit = $aiGeneratedPivotStructure['colsCategory']['limit']; if ($aiGeneratedPivotStructure['colsCategory']['orderField']=='currentField') { $pivotDefObj->colsOrderField = $aiGeneratedPivotStructure['colsCategory']['field']; } else { $pivotDefObj->colsOrderField = $aiGeneratedPivotStructure['dataCategory']['field']; $pivotDefObj->colsOrderAggFunction = $aiGeneratedPivotStructure['dataCategory']['aggregateFunction']; } $pivotDefObj->colsOrderDirection = $aiGeneratedPivotStructure['colsCategory']['orderDirection']; } } }