clientsToGsheet.php 4.5 KB

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