clientsToGsheet.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Models\BDTDevice;
  4. use App\Models\Client;
  5. use Google\Service\Sheets\ValueRange;
  6. use Google_Client;
  7. use Illuminate\Console\Command;
  8. use Revolution\Google\Sheets\Facades\Sheets;
  9. use Revolution\Google\Sheets\Sheets as SheetsSheets;
  10. class clientsToGsheet extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'clients:export-to-gsheet';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'Export records to gsheet';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return int
  37. */
  38. public function handle()
  39. {
  40. $this->pushSheet();
  41. return 0;
  42. }
  43. function pushSheet(){
  44. $spreadsheetId = config('app.googleSpreadsheetId');
  45. $googleSpreadsheetProductsSheetName = config('app.googleSpreadsheetProductsSheetName');
  46. $client = $this->getApiClient();
  47. $service = new \Google\Service\Sheets($client);
  48. $values = $this->getValues();
  49. $range = $googleSpreadsheetProductsSheetName.'!A1:V1';
  50. $body = new ValueRange([
  51. 'values' => $values
  52. ]);
  53. $params = [
  54. 'valueInputOption' => 'RAW'
  55. ];
  56. $result = $service->spreadsheets_values->append($spreadsheetId, $range, $body, $params);
  57. $this->info(json_encode($result));
  58. }
  59. function getApiClient(){
  60. $KEY_FILE_LOCATION = storage_path('stag-gsheets-d9ead2f78b4b.json');
  61. $client = new Google_Client();
  62. $client->setApplicationName("My Rooster Admin");
  63. $client->setAuthConfig($KEY_FILE_LOCATION);
  64. $client->setScopes([\Google\Service\Sheets::DRIVE, \Google\Service\Sheets::SPREADSHEETS]);
  65. return $client;
  66. }
  67. private function getValues(){
  68. $values = [];
  69. $values[] = [
  70. 'Chart Number',
  71. 'Name',
  72. 'MCP',
  73. 'CC',
  74. 'Mailing Address State',
  75. 'Insurance',
  76. 'Is Part B Primary',
  77. 'Last Visit',
  78. 'Next Appt.',
  79. 'BP/Pulse Timestamp',
  80. 'Weight Timestamp',
  81. 'Created',
  82. 'Assigned On',
  83. 'Notes',
  84. 'Status',
  85. 'Temparature Gun Delivery Status',
  86. 'Pulse Oximeter Delivery Status',
  87. 'Cellular Bp Delivery Status',
  88. 'Weight Scale Delivery Status'
  89. ];
  90. Client::whereNull('shadow_pro_id')->chunk(1, function($clients) use (&$values){
  91. foreach($clients as $patient){
  92. $patientStr =($patient->chart_number).'~~'.
  93. ($patient->displayName()).'~~'.
  94. (@$patient->mcp ? $patient->mcp->displayName() : '--').'~~'.
  95. (@$patient->defaultNaPro ? $patient->defaultNaPro->displayName() : '--').'~~'.
  96. ($patient->mailing_address_state).'~~'.
  97. ($patient->getPrimaryCoverage()?$patient->getPrimaryCoverage()->insuranceDisplayName():'-').'~~'.
  98. ($patient->getPrimaryCoverage()?$patient->getPrimaryCoverage()->is_partbprimary:'-').'~~'.
  99. (friendly_date($patient->most_recent_completed_mcp_note_date) ).'~~'.
  100. ($patient->nextMcpAppointment ? friendly_date_time($patient->nextMcpAppointment->raw_date.' '.$patient->nextMcpAppointment->raw_start_time) : '-').'~~'.
  101. (friendlier_date_time($patient->most_recent_cellular_bp_measurement_at) ).'~~'.
  102. (friendlier_date_time($patient->most_recent_cellular_weight_measurement_at) ).'~~'.
  103. (friendly_date_time($patient->created_at, false)).'~~'.
  104. ($patient->getMcpAssignedOn()).'~~'.
  105. (count($patient->activeNotes)).'~~'.
  106. ($patient->client_engagement_status_category == 'DUMMY'? 'Test Record': $patient->client_engagement_status_category).'~~'.
  107. ($patient->temparatureGunDeliveryStatus()).'~~'.
  108. ($patient->pulseOximeterDeliveryStatus()).'~~'.
  109. ($patient->hasBPDevice()?'DELIVERED':'').'~~'.
  110. ($patient->hasWeightScaleDevice()?'DELIVERED':'').'~~';
  111. $values[] = explode('~~', $patientStr);
  112. }
  113. });
  114. return $values;
  115. }
  116. }