/** * Initialize mgqs and return the created mgqs. * * @param string $tableAlias * @return array */ public function initMgqs($tableAlias) { if (is_null(static::$mgqsFactory)) { // Resolve $mgqsFactory $mgqsFactoryClass = Config::get('sedp-mis_base-report.mgqs_factory'); if (!$mgqsFactoryClass) { throw new \Exception('Config `sedp-mis_base-report.mgqs_factory` is not defined'); } static::$mgqsFactory = App::make($mgqsFactoryClass); } // Instantiate all mgqs $mgqs = static::$mgqsFactory->mgqs(); return $this->mgqs = array_start_from($mgqs, function ($mgq) use($tableAlias) { return $mgq->tableAlias() === $tableAlias; }); }
/** * Create the summary columns with aggregate calls in select clause and group aggregates query using $groupByKey. * * @param string $groupByKey * @return $this */ public function summaryGroupBy($groupByKey) { // ReportData is in summary, so set $isDetailed to false $this->isDetailed = false; // Get the modelGridQueries starting from an mgq which it has the groupByKey column. $mgqs = array_start_from($this->gridQuery->getModelGridQueries(), function ($mgq) use($groupByKey) { return !is_null($mgq->{$groupByKey}); }); // Then add the columns of those mgqs in select clause. foreach ($mgqs as $mgq) { // Do not addSelect() if mgq has a static columnsToAddSelect method, since it will be added later. if (!method_exists($mgq, 'columnsToAddSelect')) { $this->query->addSelect($mgq->makeSelect()); } } /* * Start using the defaultGroupByKey() method. * Use the defaultGroupByKey if gridQuery has. */ $defaultGroupByKeys = $this->gridQuery->defaultGroupByKey(); // Convert to array for those single groupByKeys if (!is_array($defaultGroupByKeys)) { $defaultGroupByKeys = [$defaultGroupByKeys]; } foreach ($defaultGroupByKeys as $defaultGroupByKey => $groupByColumn) { // Use the groupByColumn string as the defaultGroupByColumnKey $defaultGroupByKey = is_int($defaultGroupByKey) ? $groupByColumn : $defaultGroupByKey; $this->query->addSelect(DB::raw("{$groupByColumn} as {$defaultGroupByKey}")); // Query group by $defautlGroupByKey $this->query->groupBy($defaultGroupByKey); } // Finally, use the groupByKey to group query for the summary report $this->query->groupBy($groupByKey); return $this; }
/** * Return the columns to be added in select, base from the starting tableAlias. * * @param string $tableAlias * @return array */ public function columnsToAddSelect($tableAlias = null) { $columns = []; $mgqs = is_null($tableAlias) ? $this->getModelGridQueries() : array_start_from($this->getModelGridQueries(), function ($mgq) use($tableAlias) { return $mgq->tableAlias() === $tableAlias; }); foreach ($mgqs as $mgq) { $columns = array_merge($columns, $mgq->columns()); } return $columns; }