GsheetController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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.dob,
  20. care_month.is_client_enrolled_in_rm,
  21. client.most_recent_completed_mcp_note_date,
  22. client.most_recent_completed_mcp_note_id,
  23. mrmnote.effective_dateest::date as most_recent_completed_mcp_note_date_cm,
  24. client.cell_number,
  25. client.is_assigned_cellular_bp_device,
  26. client.is_assigned_cellular_weight_scale_device,
  27. care_month.start_date,
  28. care_month.rm_total_time_in_seconds_by_mcp,
  29. care_month.rm_total_time_in_seconds_by_rmm_pro,
  30. care_month.number_of_days_with_remote_measurements,
  31. care_month.has_anyone_interacted_with_client_about_rm,
  32. care_month.has_mcp_interacted_with_client_about_rm,
  33. care_month.rm_num_measurements_not_stamped_by_mcp,
  34. care_month.rm_num_measurements_not_stamped_by_non_hcp,
  35. care_month.rm_num_measurements_not_stamped_by_rmm,
  36. care_month.rm_num_measurements_not_stamped_by_rme,
  37. client.cell_number,
  38. client.most_recent_cellular_bp_dbp_mm_hg,
  39. client.most_recent_cellular_bp_sbp_mm_hg,
  40. client.most_recent_cellular_bp_measurement_at,
  41. client.most_recent_cellular_weight_value,
  42. client.most_recent_cellular_weight_measurement_at
  43. 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
  44. left join note mrmnote on mrmnote.id = (
  45. select max(n.id) from note n
  46. where
  47. n.client_id = client.id AND
  48. n.is_cancelled = FALSE AND
  49. (n.is_signed_by_hcp IS NOT NULL AND n.is_signed_by_hcp = TRUE) AND
  50. n.effective_dateest::date >= care_month.start_date::date AND
  51. n.effective_dateest::date < (care_month.start_date::date + INTERVAL '1 month')
  52. )
  53. left join pro mcp_pro on care_month.mcp_pro_id = mcp_pro.id
  54. left join pro rmm_pro on care_month.rmm_pro_id = rmm_pro.id
  55. ";
  56. $sqlParams = [];
  57. $values = $this->getValues($sql, $sqlParams);
  58. $spreadsheetId = '1jRCkukeSfZufY8gpKSGYgv9cgeDbA1ZYP4pKJhyPP_o';
  59. $sheetName = 'Sheet1';
  60. return $this->exportToGsheet($sql, $sqlParams, $spreadsheetId, $sheetName);
  61. }
  62. private function exportToGsheet($sql, $sqlParams, $spreadsheetId, $sheetName){
  63. $client = $this->getApiClient();
  64. $service = new \Google\Service\Sheets($client);
  65. $values = $this->getValues($sql, $sqlParams);
  66. $range = $sheetName.'!A1:V1';
  67. $body = new ValueRange([
  68. 'values' => $values
  69. ]);
  70. $params = [
  71. 'valueInputOption' => 'RAW'
  72. ];
  73. $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
  74. return $result;
  75. }
  76. function getApiClient(){
  77. $KEY_FILE_LOCATION = storage_path('stag-gsheets-d9ead2f78b4b.json');
  78. $client = new Google_Client();
  79. $client->setApplicationName("My Rooster Admin");
  80. $client->setAuthConfig($KEY_FILE_LOCATION);
  81. $client->setScopes([\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS]);
  82. return $client;
  83. }
  84. private function getValues($sql, $sqlParams){
  85. $values = [];
  86. $raws = DB::select($sql, $sqlParams);
  87. if(!count($raws)){
  88. throw new \Exception("No data");
  89. }
  90. $firstRaw = $raws[0];
  91. $headers = [];
  92. foreach($firstRaw as $k => $val) {
  93. $headers[] = $k;
  94. }
  95. $values[] = $headers;
  96. foreach($raws as $raw){
  97. $rawStr = '';
  98. foreach($headers as $header ){
  99. $rawStr = $rawStr.$raw->$header.'~~';
  100. }
  101. $values[] = explode('~~', $rawStr);
  102. }
  103. return $values;
  104. }
  105. }