ReportsController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Illuminate\Database\Eloquent\Collection;
  4. use Illuminate\Support\Facades\DB;
  5. use Illuminate\Http\Request;
  6. use Yajra\DataTables\Facades\DataTables;
  7. class ReportsController extends Controller
  8. {
  9. public function dynamicDataTable($reportName, Request $request)
  10. {
  11. // Fetch the DataTable configuration
  12. $reportConfig = DB::table('reports')->where('name', $reportName)->first();
  13. if (!$reportConfig) {
  14. return response()->json(['error' => 'Report configuration not found'], 404);
  15. }
  16. // Determine the type of response (default to 'data')
  17. $type = $request->get('type', 'data');
  18. // Start building the query
  19. $query = DB::table($reportConfig->base_table);
  20. // Apply columns
  21. $select_columns = json_decode($reportConfig->select_columns, true);
  22. foreach ($select_columns as $column) {
  23. $query->selectRaw($column['select_exp']);
  24. }
  25. // Apply joins
  26. $joins = json_decode($reportConfig->joins, true);
  27. if (!empty($joins)) {
  28. foreach ($joins as $join) {
  29. if (str_contains($join['table'], '(')) { // Detect subquery joins
  30. $query->join(DB::raw($join['table']), $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
  31. } else {
  32. $query->join($join['table'], $join['on']['first'], $join['on']['operator'], $join['on']['second'], $join['type']);
  33. }
  34. }
  35. }
  36. // Apply conditions (filters)
  37. $conditions = json_decode($reportConfig->conditions, true);
  38. if (!empty($conditions)) {
  39. foreach ($conditions as $condition) {
  40. if (isset($condition['raw'])) {
  41. $query->whereRaw($condition['raw']);
  42. } else {
  43. $query->where($condition['column'], $condition['operator'], $condition['value']);
  44. }
  45. }
  46. }
  47. $columnHeaders = json_decode($reportConfig->column_headers, true);
  48. $columnConfig = json_decode($reportConfig->columns_config, true);
  49. //Apply search filters
  50. $searchTerms = $request->input('searchTerms', []);
  51. foreach ($searchTerms as $index => $value) {
  52. $columnHeader = $columnHeaders[$index];
  53. $config = $columnConfig[$index];
  54. $columnName = $config['name'];
  55. $searchType = $columnHeader['searchtype'];
  56. if ($searchType == 'date' && is_array($value)) {
  57. if (!empty($value[$index]['start'])) {
  58. $query->whereDate($columnName, '>=', $value[$index]['start']);
  59. }
  60. if (!empty($value[$index]['end'])) {
  61. $query->whereDate($columnName, '<=', $value[$index]['end']);
  62. }
  63. } elseif (!empty($value)) {
  64. switch ($searchType) {
  65. case 'text':
  66. $query->where($columnName, 'like', '%' . $value[$index] . '%');
  67. break;
  68. case 'number':
  69. $query->where($columnName, '=', (int) $value[$index]);
  70. break;
  71. case 'dropdown':
  72. if($value[$index] != '') {
  73. $query->where($columnName, '=', $value[$index]);
  74. }
  75. break;
  76. }
  77. }
  78. }
  79. //Get Data for Dropdown header columns
  80. $dropdownData = new Collection();
  81. foreach ($columnHeaders as $index => $columnHeader) {
  82. if($columnHeader['searchtype'] === 'dropdown')
  83. {
  84. $ddColumnConfig = $columnConfig[$index];
  85. $columnSnapshot = (clone $query)
  86. ->select($ddColumnConfig['name'])
  87. ->distinct()->orderBy($ddColumnConfig['name'])
  88. ->pluck($ddColumnConfig['name']);
  89. $dropdownData[$index] = $columnSnapshot;
  90. }
  91. }
  92. // Apply sorting
  93. $sorting = $request->order[0] ?? json_decode($reportConfig->default_sorting, true)[0];
  94. $query->orderBy($sorting['name'], $sorting['dir']);
  95. // Pass query to DataTables
  96. return DataTables::of($query)->with(['dropdowndata' => $dropdownData, 'searchterms' => $searchTerms])->make(true);
  97. }
  98. }