TestCriteriaQueries.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\Client;
  4. use Illuminate\Console\Command;
  5. class TestCriteriaQueries extends Command
  6. {
  7. /**
  8. * The name and signature of the console command.
  9. *
  10. * @var string
  11. */
  12. protected $signature = 'criteria:test';
  13. /**
  14. * The console command description.
  15. *
  16. * @var string
  17. */
  18. protected $description = 'Test criteria queries';
  19. /**
  20. * Create a new command instance.
  21. *
  22. * @return void
  23. */
  24. public function __construct()
  25. {
  26. parent::__construct();
  27. }
  28. /**
  29. * Execute the console command.
  30. *
  31. * @return int
  32. */
  33. public function handle()
  34. {
  35. $testSteps = [
  36. 'MCB Primary? YES',
  37. [
  38. 'criteriaName'=>'MCP',
  39. 'arguments'=>[
  40. 'pro_id'=>'d88b20df-4c2e-430a-a7a1-7d08fbbc1c82'
  41. ]
  42. ]
  43. ];
  44. $query = Client::whereRaw('id > :id', ['id' => 0]);
  45. $query = $this->queryBuilder($query, $testSteps);
  46. $this->info("The count is: ".$query->count());
  47. $records = $query->get();
  48. foreach($records as $record){
  49. $this->info($record->uid);
  50. }
  51. }
  52. function queryBuilder($query, $testCriteria){
  53. foreach($testCriteria as $criterion){
  54. $clause = '';
  55. $params = [];
  56. if(is_string($criterion)){
  57. // $clause = $this->criteria[$criterion]['clause'];
  58. // $params = $this->criteria[$criterion]['arguments'];
  59. $query = $query->whereRaw($this->criteria[$criterion]);
  60. }
  61. if(is_array($criterion)){
  62. $criteriaName = $criterion['criteriaName'];
  63. $clause = $this->criteria[$criteriaName]['clause'];
  64. $params = $criterion['arguments'];
  65. $query = $query->whereRaw($clause, $params);
  66. }
  67. }
  68. return $query;
  69. }
  70. protected $criteria = [
  71. //with preset arguments
  72. 'Client active? YES' => "()",
  73. 'Client active? NO' => "()",
  74. 'MCP assigned? YES' => "()",
  75. 'MCP assigned? NO' => "()",
  76. 'Future MCP appointment? YES' => "()",
  77. 'Future MCP appointment? NO' => "()",
  78. 'MCB Primary? YES' => "(is_part_b_primary = 'YES')",
  79. 'MCB Primary? NO' => "()",
  80. 'Has active RM device? YES' => "()",
  81. 'Has active RM device? NO' => "()",
  82. 'Active device type? Weight' => "()",
  83. 'Active device type? BP' => "()",
  84. 'Active device type? Weight ONLY' => "()",
  85. 'Active device type? BP ONLY' => "()",
  86. 'Active device type? Weight + BP' => "()",
  87. 'Type 2 Diabetic' => "(client.id IN (SELECT client_id FROM dx))",
  88. //with user defined arguments
  89. 'MCP' => [
  90. 'argument_ui_map' => [
  91. 'pro_uid' => [
  92. 'field_type' => 'record',
  93. 'table' => 'pro',
  94. 'display' => '{name_display} - {npi}',
  95. 'value' => 'uid'
  96. ]
  97. ],
  98. 'arguments' => ['pro_uid'],
  99. 'clause' => "(client.mcp_pro_id = (SELECT pro.id FROM pro WHERE pro.uid = :pro_uid))"
  100. ],
  101. 'Chart Creation Date Between' => [
  102. 'argument_ui_map' => [
  103. 'starting_date' => [
  104. 'field_type' => 'date'
  105. ],
  106. 'ending_date' => [
  107. 'field_type' => 'date'
  108. ]
  109. ],
  110. 'arguments' => ['starting_date', 'ending_date'],
  111. 'clause' => "(client.created_at::DATE >= :starting_date AND client.created_at::DATE =< :ending_date)"
  112. ]
  113. ];
  114. }