clientsToGsheet.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. 'Issued Yet?',
  74. 'Client',
  75. 'Used?',
  76. 'MCP',
  77. 'Issue Date',
  78. 'Last Meas. Date',
  79. 'Last Meas. Days Ago',
  80. 'Days with Remote Meas. this Month',
  81. 'Days with Remote Meas. last Month',
  82. 'Client Engagement Status Category',
  83. 'Admin Engagement Assessment Status',
  84. 'Mcp Engagement Assessment Status',
  85. 'Default Na Engagement Assessment Status'
  86. ];
  87. Client::whereNull('shadow_pro_id')->chunk(function($clients) use ($values){
  88. foreach($clients as $patient){
  89. $patientStr =($patient->chart_number).'~~'.
  90. ($patient->displayName()).'~~'.
  91. (@$patient->mcp ? $patient->mcp->displayName() : '--').'~~'.
  92. ($patient->mailing_address_state).'~~'.
  93. ($patient->getPrimaryCoverage()?$patient->getPrimaryCoverage()->insuranceDisplayName():'-').'~~'.
  94. (friendly_date($patient->most_recent_completed_mcp_note_date) ).'~~'.
  95. ($patient->nextMcpAppointment ? friendly_date_time($patient->nextMcpAppointment->raw_date.' '.$patient->nextMcpAppointment->raw_start_time) : '-').'~~'.
  96. (friendlier_date_time($patient->most_recent_cellular_bp_measurement_at) ).'~~'.
  97. (friendlier_date_time($patient->most_recent_cellular_weight_measurement_at) ).'~~'.
  98. (friendly_date_time($patient->created_at, false)).'~~'.
  99. ($patient->getMcpAssignedOn()).'~~'.
  100. (count($patient->activeNotes)).'~~'.
  101. ($patient->client_engagement_status_category == 'DUMMY'? 'Test Record': $patient->client_engagement_status_category).'~~'.
  102. ($patient->temparatureGunDeliveryStatus()).'~~'.
  103. ($patient->pulseOximeterDeliveryStatus()).'~~'.
  104. ($patient->hasBPDevice()?'DELIVERED':'').'~~'.
  105. ($patient->hasWeightScaleDevice()?'DELIVERED':'').'~~';
  106. $values[] = explode('~~', $patientStr);
  107. }
  108. });
  109. return $values;
  110. }
  111. }