|
@@ -5,12 +5,14 @@ namespace App\Http\Controllers;
|
|
|
|
|
|
use App\Models\Clause;
|
|
|
use App\Models\StatTreeLineClause;
|
|
|
+use App\Models\StatTreeLineReportColumn;
|
|
|
use Illuminate\Http\Request;
|
|
|
use App\Models\StatTree;
|
|
|
use App\Models\StatTreeLine;
|
|
|
use App\Models\Client;
|
|
|
use App\Models\Pro;
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
|
|
+use Illuminate\Pagination\Paginator;
|
|
|
use Illuminate\Support\Facades\DB;
|
|
|
use Ramsey\Uuid\Uuid;
|
|
|
|
|
@@ -22,13 +24,44 @@ class StatTreeLineController extends Controller
|
|
|
return view('app.stat-tree.stat-tree-line-reports.list', compact('statTreeLines'));
|
|
|
}
|
|
|
|
|
|
- public function editReport(StatTreeLine $statTreeLine) {
|
|
|
+ public function editReport(Request $request, StatTreeLine $statTreeLine) {
|
|
|
$allClauses = Clause::orderByRaw('position_index ASC NULLS FIRST')->get();
|
|
|
return view('app.stat-tree.stat-tree-line-reports.edit', compact('statTreeLine', 'allClauses'));
|
|
|
}
|
|
|
|
|
|
- public function viewReport(StatTreeLine $statTreeLine) {
|
|
|
-
|
|
|
+ public function viewReport(Request $request, StatTreeLine $statTreeLine) {
|
|
|
+ $total = 0;
|
|
|
+ $rows = [];
|
|
|
+ $columns = [];
|
|
|
+ $selectColumns = [];
|
|
|
+ $paginator = null;
|
|
|
+ $line = $statTreeLine;
|
|
|
+ foreach ($line->reportColumns as $reportColumn) {
|
|
|
+ $columns[] = [
|
|
|
+ "label" => $reportColumn->label,
|
|
|
+ "column" => $reportColumn->display_key,
|
|
|
+ "type" => $reportColumn->field_type,
|
|
|
+ "as" => "v_{$reportColumn->id}"
|
|
|
+ ];
|
|
|
+ $selectColumns[] = "{$reportColumn->display_key} as v_{$reportColumn->id}";
|
|
|
+ }
|
|
|
+ if(count($line->reportColumns)) {
|
|
|
+ $result = $this->queryStatTreeLineData($line, $selectColumns, $columns, $request);
|
|
|
+ if($result[0] === false) {
|
|
|
+ $total = 0;
|
|
|
+ $rows = [];
|
|
|
+ $paginator = new Paginator($rows, 20, 1);
|
|
|
+ $error = $result[1];
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ $total = $result[0];
|
|
|
+ $rows = $result[1];
|
|
|
+ $paginator = new LengthAwarePaginator($rows, $total, $request->input('per_page') ?: 20, $request->input('page') ?: 1);
|
|
|
+ $paginator->setPath(route('practice-management.statTreeLines.view-data', compact('line')));
|
|
|
+ $error = '';
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return view('app.stat-tree.stat-tree-line-reports.view-data', compact('line', 'total', 'rows', 'columns', 'paginator', 'error'));
|
|
|
}
|
|
|
|
|
|
|
|
@@ -178,7 +211,7 @@ class StatTreeLineController extends Controller
|
|
|
protected function queryStatTreeLineData(StatTreeLine $statTreeLine, $selectColumns, $columns, Request $request)
|
|
|
{
|
|
|
|
|
|
- $model = $statTreeLine->statTree->model;
|
|
|
+ $model = $statTreeLine->statTree ? $statTreeLine->statTree->model : $statTreeLine->model;
|
|
|
|
|
|
$clauses = [];
|
|
|
foreach ($statTreeLine->lineClauses as $lineClause) {
|
|
@@ -207,14 +240,16 @@ class StatTreeLineController extends Controller
|
|
|
}
|
|
|
|
|
|
// if stat tree bound to a pro, apply pro_scope_clause
|
|
|
- if($request->input('proUid') && $statTreeLine->statTree->pro_scope_clause) {
|
|
|
- $pro = Pro::where('uid', $request->input('proUid'))->first();
|
|
|
- if($pro) {
|
|
|
- $clauses[] = str_replace('@PRO_ID', $pro->id, $statTreeLine->statTree->pro_scope_clause);
|
|
|
+ if($statTreeLine->statTree) {
|
|
|
+ if($request->input('proUid') && $statTreeLine->statTree->pro_scope_clause) {
|
|
|
+ $pro = Pro::where('uid', $request->input('proUid'))->first();
|
|
|
+ if($pro) {
|
|
|
+ $clauses[] = str_replace('@PRO_ID', $pro->id, $statTreeLine->statTree->pro_scope_clause);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ elseif($statTreeLine->statTree->pro && $statTreeLine->statTree->pro_scope_clause) {
|
|
|
+ $clauses[] = str_replace('@PRO_ID', $statTreeLine->statTree->pro->id, $statTreeLine->statTree->pro_scope_clause);
|
|
|
}
|
|
|
- }
|
|
|
- elseif($statTreeLine->statTree->pro && $statTreeLine->statTree->pro_scope_clause) {
|
|
|
- $clauses[] = str_replace('@PRO_ID', $statTreeLine->statTree->pro->id, $statTreeLine->statTree->pro_scope_clause);
|
|
|
}
|
|
|
|
|
|
// filters from view-data UI
|
|
@@ -275,7 +310,7 @@ class StatTreeLineController extends Controller
|
|
|
$result = [$total, $result];
|
|
|
}
|
|
|
catch (\Exception $ex) {
|
|
|
- $result = 'error';
|
|
|
+ $result = [false, $ex->getMessage()];
|
|
|
}
|
|
|
return $result;
|
|
|
}
|
|
@@ -425,4 +460,48 @@ class StatTreeLineController extends Controller
|
|
|
DB::select("delete from stat_tree_line_clause where id = {$statTreeLineClause->id}");
|
|
|
return $this->pass();
|
|
|
}
|
|
|
+ public function addReportColumn(Request $request) {
|
|
|
+ $statTreeLine = StatTreeLine::where('uid', $request->input('uid'))->first();
|
|
|
+ if(!$statTreeLine) return $this->fail('Stat tree line not found!');
|
|
|
+ $column = new StatTreeLineReportColumn();
|
|
|
+ $nextId = DB::select("select nextval('stat_tree_line_report_column_id_seq')");
|
|
|
+ $column->id = $nextId[0]->nextval;
|
|
|
+ $column->uid = Uuid::uuid4();
|
|
|
+ $column->stat_tree_line_id = $statTreeLine->id;
|
|
|
+ $column->label = $request->input('label');
|
|
|
+ $column->display_key = $request->input('column');
|
|
|
+ $positionIndex = DB::select("select max(position_index) from stat_tree_line_report_column where stat_tree_line_id = {$column->stat_tree_line_id}");
|
|
|
+ $column->position_index = is_numeric($positionIndex[0]->max) ? $positionIndex[0]->max + 1 : 1;
|
|
|
+ $column->field_type = $request->input('fieldType');
|
|
|
+ $column->save();
|
|
|
+ return $this->pass();
|
|
|
+ }
|
|
|
+ public function updateReportColumn(Request $request) {
|
|
|
+ $column = StatTreeLineReportColumn::where('uid', $request->input('uid'))->first();
|
|
|
+ if(!$column) return $this->fail('Stat tree line report column not found!');
|
|
|
+ $column->label = $request->input('label');
|
|
|
+ $column->display_key = $request->input('column');
|
|
|
+ $column->field_type = $request->input('fieldType');
|
|
|
+ $column->save();
|
|
|
+ return $this->pass();
|
|
|
+ }
|
|
|
+ public function removeReportColumn(Request $request) {
|
|
|
+ $column = StatTreeLineReportColumn::where('uid', $request->input('uid'))->first();
|
|
|
+ if(!$column) return $this->fail('Stat tree line report column not found!');
|
|
|
+ DB::select("delete from stat_tree_line_report_column where id = {$column->id}");
|
|
|
+ return $this->pass();
|
|
|
+ }
|
|
|
+ public function reorderReportColumns(Request $request) {
|
|
|
+ $uids = json_decode($request->input('uids'));
|
|
|
+ $position = 1;
|
|
|
+ for ($i = 0; $i < count($uids); $i++) {
|
|
|
+ $column = StatTreeLineReportColumn::where('uid', $uids[$i])->first();
|
|
|
+ if($column) {
|
|
|
+ $column->position_index = $position;
|
|
|
+ $column->save();
|
|
|
+ $position++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return $this->pass();
|
|
|
+ }
|
|
|
}
|