Pro.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. <?php
  2. namespace App\Models;
  3. # use Illuminate\Database\Eloquent\Model;
  4. use App\Helpers\TimeLine;
  5. use Exception;
  6. use Illuminate\Support\Facades\DB;
  7. class Pro extends Model
  8. {
  9. protected $table = 'pro';
  10. public function displayName() {
  11. $name = [];
  12. if(!empty($this->name_last)) $name[] = $this->name_last;
  13. if(!empty($this->name_first)) $name[] = $this->name_first;
  14. if(!count($name)) {
  15. $name = $this->name_display;
  16. }
  17. else {
  18. $name = implode(", ", $name);
  19. }
  20. return $name;
  21. }
  22. public function initials() {
  23. $characters = [];
  24. if(!empty($this->name_first)) $characters[] = $this->name_first[0];
  25. if(!empty($this->name_last)) $characters[] = $this->name_last[0];
  26. return strtolower(implode("", $characters));
  27. }
  28. public function cmBills()
  29. {
  30. return $this->hasMany(Bill::class, 'cm_pro_id');
  31. }
  32. public function hcpBills()
  33. {
  34. return $this->hasMany(Bill::class, 'hcp_pro_id');
  35. }
  36. public function lastPayment() {
  37. return ProTransaction
  38. ::where('pro_id', $this->id)
  39. ->where('plus_or_minus', 'PLUS')
  40. ->orderBy('created_at', 'desc')
  41. ->first();
  42. }
  43. public function hasRates() {
  44. $numRates = ProRate::where('is_active', true)->where('pro_id', $this->id)->count();
  45. return $numRates > 0;
  46. }
  47. public function cmRates() {
  48. return ProRate::distinct('code')
  49. ->where('is_active', true)
  50. ->where('pro_id', $this->id)
  51. ->where('code', 'LIKE', 'CM%')
  52. ->get();
  53. }
  54. public function rmRates() {
  55. return ProRate::distinct('code')
  56. ->where('is_active', true)
  57. ->where('pro_id', $this->id)
  58. ->where('code', 'LIKE', 'RM%')
  59. ->get();
  60. }
  61. public function noteRates() {
  62. return ProRate::distinct('code')
  63. ->where('is_active', true)
  64. ->where('pro_id', $this->id)
  65. ->where('code', 'NOT LIKE', 'CM%')
  66. ->where('code', 'NOT LIKE', 'RM%')
  67. ->get();
  68. }
  69. public function shortcuts() {
  70. return $this->hasMany(ProTextShortcut::class, 'pro_id')->where('is_removed', false);
  71. }
  72. public function noteTemplates() {
  73. return $this->hasMany(NoteTemplatePro::class, 'pro_id')
  74. ->where('is_removed', false)
  75. ->orderBy('position_index', 'asc');
  76. }
  77. public function currentWork() {
  78. return ProClientWork::where('pro_id', $this->id)->where('is_active', true)->first();
  79. }
  80. public function isWorkingOnClient($_client) {
  81. $count = ProClientWork::where('pro_id', $this->id)->where('client_id', $_client->id)->where('is_active', true)->count();
  82. return $count > 0;
  83. }
  84. public function canvasCustomItems($_key) {
  85. return ClientCanvasDataCustomItem::where('key', $_key)->get();
  86. }
  87. /**
  88. * @param $_start - YYYY-MM-DD
  89. * @param $_end - YYYY-MM-DD
  90. * @param string $_timezone - defaults to EASTERN
  91. * @param string $_availableBG - defaults to #00a
  92. * @param string $_unavailableBG - defaults to #a00
  93. * @return array
  94. * @throws Exception
  95. */
  96. public function getAvailabilityEvents($_start, $_end, $_timezone = 'EASTERN', $_availableBG = '#00a', $_unavailableBG = '#a00') {
  97. $_start .= ' 00:00:00';
  98. $_end .= ' 23:59:59';
  99. // get availability data
  100. $proGenAvail = ProGeneralAvailability
  101. ::where('is_cancelled', false)
  102. ->where('pro_id', $this->id)
  103. ->get();
  104. $proSpecAvail = ProSpecificAvailability
  105. ::where('is_cancelled', false)
  106. ->where('pro_id', $this->id)
  107. ->where(function ($query) use ($_start, $_end) {
  108. $query
  109. ->where(function ($query2) use ($_start, $_end) {
  110. $query2
  111. ->where('start_time', '>=', $_start)
  112. ->where('start_time', '<=', $_end);
  113. })
  114. ->orWhere(function ($query2) use ($_start, $_end) {
  115. $query2
  116. ->where('end_time', '>=', $_start)
  117. ->where('end_time', '<=', $_end);
  118. });
  119. })
  120. ->get();
  121. $proSpecUnavail = ProSpecificUnavailability
  122. ::where('is_cancelled', false)
  123. ->where('pro_id', $this->id)
  124. ->where(function ($query) use ($_start, $_end) {
  125. $query
  126. ->where(function ($query2) use ($_start, $_end) {
  127. $query2
  128. ->where('start_time', '>=', $_start)
  129. ->where('start_time', '<=', $_end);
  130. })
  131. ->orWhere(function ($query2) use ($_start, $_end) {
  132. $query2
  133. ->where('end_time', '>=', $_start)
  134. ->where('end_time', '<=', $_end);
  135. });
  136. })
  137. ->get();
  138. // default GA
  139. // if no gen avail, assume mon to fri, 9 to 7
  140. /*if (count($proGenAvail) === 0) {
  141. $dayNames = ['MONDAY', 'TUESDAY', 'WEDNESDAY', 'THURSDAY', 'FRIDAY'];
  142. foreach ($dayNames as $dayName) {
  143. $item = new \stdClass();
  144. $item->day_of_week = $dayName;
  145. $item->timezone = $_timezone;
  146. $item->start_time = '09:00:00';
  147. $item->end_time = '18:00:00';
  148. $proGenAvail->push($item);
  149. }
  150. }*/
  151. // create timeline
  152. $phpTZ = appTZtoPHPTZ($_timezone);
  153. $phpTZObject = new \DateTimeZone($phpTZ);
  154. $startDate = new \DateTime($_start, $phpTZObject);
  155. $endDate = new \DateTime($_end, $phpTZObject);
  156. $proTimeLine = new TimeLine($startDate, $endDate);
  157. // General availability
  158. $period = new \DatePeriod($startDate, \DateInterval::createFromDateString('1 day'), $endDate);
  159. $days = [];
  160. foreach ($period as $day) {
  161. $days[] = [
  162. "day" => strtoupper($day->format("l")), // SUNDAY, etc.
  163. "date" => $day->format("Y-m-d"), // 2020-10-04, etc.
  164. ];
  165. }
  166. foreach ($days as $day) {
  167. $proGenAvailForTheDay = $proGenAvail->filter(function ($record) use ($day) {
  168. return $record->day_of_week === $day["day"];
  169. });
  170. foreach ($proGenAvailForTheDay as $ga) {
  171. $gaStart = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  172. $parts = explode(":", $ga->start_time);
  173. $gaStart->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  174. $gaStart->setTimezone($phpTZObject);
  175. $gaEnd = new \DateTime($day["date"], new \DateTimeZone(appTZtoPHPTZ($ga->timezone)));
  176. $parts = explode(":", $ga->end_time);
  177. $gaEnd->setTime(intval($parts[0]), intval($parts[1]), intval($parts[2]));
  178. $gaEnd->setTimezone($phpTZObject);
  179. $proTimeLine->addAvailability($gaStart, $gaEnd);
  180. }
  181. }
  182. // specific availability
  183. foreach ($proSpecAvail as $sa) {
  184. $saStart = new \DateTime($sa->start_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  185. $saStart->setTimezone($phpTZObject);
  186. $saEnd = new \DateTime($sa->end_time, new \DateTimeZone(appTZtoPHPTZ($sa->timezone)));
  187. $saEnd->setTimezone($phpTZObject);
  188. $proTimeLine->addAvailability($saStart, $saEnd);
  189. }
  190. // specific unavailability
  191. foreach ($proSpecUnavail as $sua) {
  192. $suaStart = new \DateTime($sua->start_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  193. $suaStart->setTimezone($phpTZObject);
  194. $suaEnd = new \DateTime($sua->end_time, new \DateTimeZone(appTZtoPHPTZ($sua->timezone)));
  195. $suaEnd->setTimezone($phpTZObject);
  196. $proTimeLine->removeAvailability($suaStart, $suaEnd);
  197. }
  198. $events = [];
  199. // availability
  200. foreach ($proTimeLine->getAvailable() as $item) {
  201. $eStart = new \DateTime('@' . $item->start);
  202. $eStart->setTimezone($phpTZObject);
  203. $eEnd = new \DateTime('@' . $item->end);
  204. $eEnd->setTimezone($phpTZObject);
  205. $events[] = [
  206. "type" => "availability",
  207. "start" => $eStart->format('Y-m-d H:i:s'),
  208. "end" => $eEnd->format('Y-m-d H:i:s'),
  209. "editable" => false,
  210. "backgroundColor" => $_availableBG
  211. ];
  212. }
  213. // unavailability
  214. foreach ($proTimeLine->getUnavailable() as $item) {
  215. $eStart = new \DateTime('@' . $item->start);
  216. $eStart->setTimezone($phpTZObject);
  217. $eEnd = new \DateTime('@' . $item->end);
  218. $eEnd->setTimezone($phpTZObject);
  219. $events[] = [
  220. "type" => "unavailability",
  221. "start" => $eStart->format('Y-m-d H:i:s'),
  222. "end" => $eEnd->format('Y-m-d H:i:s'),
  223. "editable" => false,
  224. "backgroundColor" => $_unavailableBG
  225. ];
  226. }
  227. return $events;
  228. }
  229. public function getMyClientIds() {
  230. $clients = $this->getAccessibleClientsQuery()->get();
  231. $clientIds = [];
  232. foreach($clients as $client){
  233. $clientIds[] = $client->id;
  234. }
  235. return $clientIds;
  236. }
  237. public function favoritesByCategory($_category) {
  238. return ProFavorite::where('pro_id', $this->id)
  239. ->where('is_removed', false)
  240. ->where('category', $_category)
  241. ->orderBy('category', 'asc')
  242. ->orderBy('position_index', 'asc')
  243. ->get();
  244. }
  245. public function getAccessibleClientsQuery() {
  246. $proID = $this->id;
  247. if ($this->pro_type === 'ADMIN') {
  248. $query = Client::where('id', '>', 0);
  249. } else {
  250. $query = Client::where(function ($q) use ($proID) {
  251. $q->where('mcp_pro_id', $proID)
  252. ->orWhere('cm_pro_id', $proID)
  253. ->orWhere('rmm_pro_id', $proID)
  254. ->orWhere('rme_pro_id', $proID)
  255. ->orWhere('physician_pro_id', $proID)
  256. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID])
  257. ->orWhereRaw('id IN (SELECT client_id FROM appointment WHERE status NOT IN (\'CANCELLED\', \'ABANDONED\') AND pro_id = ?)', [$proID])
  258. ->orWhereRaw('id IN (SELECT mcp_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  259. ->orWhereRaw('id IN (SELECT manager_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)');
  260. });
  261. }
  262. return $query;
  263. }
  264. public function canAddCPMEntryForMeasurement(Measurement $measurement, Pro $pro)
  265. {
  266. // check if client has any programs where this measurement type is allowed
  267. $allowed = false;
  268. $client = $measurement->client;
  269. $clientPrograms = $client->clientPrograms;
  270. if($pro->pro_type !== 'ADMIN') {
  271. $clientPrograms = $clientPrograms->filter(function($_clientProgram) use ($pro) {
  272. return $_clientProgram->manager_pro_id === $pro->id;
  273. });
  274. }
  275. if(count($clientPrograms)) {
  276. foreach ($clientPrograms as $clientProgram) {
  277. if(strpos(strtolower($clientProgram->measurement_labels), '|' . strtolower($measurement->label) . '|') !== FALSE) {
  278. $allowed = true;
  279. break;
  280. }
  281. }
  282. }
  283. return $allowed ? $clientPrograms : FALSE;
  284. }
  285. public function getMeasurements($_onlyUnstamped = true)
  286. {
  287. $measurementsQuery = Measurement::where('is_removed', false);
  288. if ($this->pro_type != 'ADMIN') {
  289. $measurementsQuery
  290. ->whereIn('client_id', $this->getMyClientIds());
  291. }
  292. if ($_onlyUnstamped) {
  293. $measurementsQuery
  294. ->whereNotNull('client_bdt_measurement_id')
  295. ->whereNotNull('ts')
  296. ->where('is_cellular_zero', false)
  297. ->where(function ($q) {
  298. $q->whereNull('status')
  299. ->orWhere(function ($q2) {
  300. $q2->where('status', '<>', 'ACK')
  301. ->where('status', '<>', 'INVALID_ACK');
  302. });
  303. });
  304. }
  305. $x = [];
  306. $measurements = $measurementsQuery->orderBy('ts', 'desc')->paginate(50);
  307. // eager load stuff needed in JS
  308. foreach ($measurements as $measurement) {
  309. // if ($measurement->client_bdt_measurement_id) {
  310. // $measurement->bdtMeasurement = $measurement->clientBDTMeasurement->measurement;
  311. // }
  312. unset($measurement->clientBDTMeasurement); // we do not need this travelling to the frontend
  313. $client = [
  314. "uid" => $measurement->client->uid,
  315. "name" => $measurement->client->displayName(),
  316. ];
  317. $measurement->patient = $client;
  318. $measurement->careMonth = $measurement->client->currentCareMonth();
  319. $measurement->timestamp = friendly_date_time($measurement->created_at);
  320. unset($measurement->client); // we do not need this travelling to the frontend
  321. // if($measurement->label == 'SBP' || $measurement->label = 'DBP'){
  322. // continue;
  323. // }
  324. $x[] = $measurement;
  325. }
  326. return $measurements;
  327. }
  328. public function hrSections() {
  329. return $this->hasMany(HrSection::class, 'pro_id')->orderBy('created_at', 'ASC');
  330. }
  331. }