GsheetController.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Exception;
  4. use Google\Service\Sheets\ValueRange;
  5. use Google_Client;
  6. use Illuminate\Console\Command;
  7. use Revolution\Google\Sheets\Facades\Sheets;
  8. use Revolution\Google\Sheets\Sheets as SheetsSheets;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\DB;
  11. class GsheetController extends Controller
  12. {
  13. public function testGsheet(){
  14. $sql = "
  15. SELECT client.name_first, client.name_last,
  16. (client.name_first || ' ' || client.name_last) as client_name,
  17. (mcp_pro.name_first || ' ' || mcp_pro.name_last) as mcp_pro_name,
  18. (rmm_pro.name_first || ' ' || rmm_pro.name_last) as rmm_pro_name,
  19. client.uid as client_uid,
  20. client.dob,
  21. client.is_enrolled_in_rm,
  22. client.most_recent_completed_mcp_note_date,
  23. client.most_recent_completed_mcp_note_id,
  24. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  25. mrmnote.id as most_recent_completed_mcp_note_id_cm,
  26. mrmnote.uid as most_recent_completed_mcp_note_uid_cm,
  27. client.cell_number,
  28. client.is_assigned_cellular_bp_device,
  29. client.is_assigned_cellular_weight_scale_device,
  30. care_month.uid as care_month_uid,
  31. care_month.id as care_month_id,
  32. care_month.start_date,
  33. care_month.rm_total_time_in_seconds_by_mcp,
  34. care_month.rm_total_time_in_seconds_by_rmm_pro,
  35. care_month.number_of_days_with_remote_measurements,
  36. care_month.has_anyone_interacted_with_client_about_rm,
  37. care_month.has_mcp_interacted_with_client_about_rm,
  38. care_month.rm_num_measurements_not_stamped_by_mcp,
  39. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  40. care_month.rm_num_measurements_not_stamped_by_rmm,
  41. care_month.rm_num_measurements_not_stamped_by_rme,
  42. care_month.mcp_pro_id as care_month_mcp_pro_id,
  43. care_month.rmm_pro_id as care_month_rmm_pro_id,
  44. client.mcp_pro_id,
  45. client.default_na_pro_id,
  46. client.rmm_pro_id,
  47. client.rme_pro_id,
  48. client.cell_number,
  49. client.most_recent_cellular_bp_dbp_mm_hg,
  50. client.most_recent_cellular_bp_sbp_mm_hg,
  51. client.most_recent_cellular_bp_measurement_at,
  52. client.most_recent_cellular_weight_value,
  53. client.most_recent_cellular_weight_measurement_at
  54. FROM care_month join client on care_month.client_id = client.id join note mrnote on client.most_recent_completed_mcp_note_id = mrnote.id
  55. left join note mrmnote on mrmnote.id = (
  56. select max(n.id) from note n
  57. where
  58. n.client_id = client.id AND
  59. n.is_cancelled = FALSE AND
  60. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  61. n.effective_dateest::date >= care_month.start_date::date AND
  62. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  63. )
  64. left join pro mcp_pro on care_month.mcp_pro_id = mcp_pro.id
  65. left join pro rmm_pro on care_month.rmm_pro_id = rmm_pro.id
  66. ";
  67. $sqlParams = [];
  68. $values = $this->getValues($sql, $sqlParams);
  69. $spreadsheetId = '1jRCkukeSfZufY8gpKSGYgv9cgeDbA1ZYP4pKJhyPP_o';
  70. $sheetName = 'Sheet1';
  71. return $this->exportToGsheet($sql, $sqlParams, $spreadsheetId, $sheetName);
  72. }
  73. private function exportToGsheet($sql, $sqlParams, $spreadsheetId, $sheetName){
  74. $client = $this->getApiClient();
  75. $service = new \Google\Service\Sheets($client);
  76. $values = $this->getValues($sql, $sqlParams);
  77. $range = $sheetName.'!A1:V1';
  78. $body = new ValueRange([
  79. 'values' => $values
  80. ]);
  81. $params = [
  82. 'valueInputOption' => 'RAW'
  83. ];
  84. $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
  85. return $result;
  86. }
  87. function getApiClient(){
  88. $KEY_FILE_LOCATION = storage_path('stag-gsheets-d9ead2f78b4b.json');
  89. $client = new Google_Client();
  90. $client->setApplicationName("My Rooster Admin");
  91. $client->setAuthConfig($KEY_FILE_LOCATION);
  92. $client->setScopes([\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS]);
  93. return $client;
  94. }
  95. private function getValues($sql, $sqlParams){
  96. $values = [];
  97. $raws = DB::select($sql, $sqlParams);
  98. if(!count($raws)){
  99. throw new \Exception("No data");
  100. }
  101. $firstRaw = $raws[0];
  102. $headers = [];
  103. foreach($firstRaw as $k => $val) {
  104. $headers[] = $k;
  105. }
  106. $values[] = $headers;
  107. foreach($raws as $raw){
  108. $rawStr = '';
  109. foreach($headers as $header ){
  110. $rawStr = $rawStr.$raw->$header.'~~';
  111. }
  112. $values[] = explode('~~', $rawStr);
  113. }
  114. return $values;
  115. }
  116. }