Pro.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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. $accessibleClientIds = [];
  231. $clientProAccesses = ClientProAccess::where('pro_id', $this->id)->get();
  232. foreach($clientProAccesses as $cpa){
  233. $accessibleClientIds[] = $cpa->client_id;
  234. }
  235. $appointmentClientIds = [];
  236. $appointments = Appointment::where('pro_id', $this->id)->get();
  237. foreach($appointments as $appts){
  238. $appointmentClientIds[] = $appts->client_id;
  239. }
  240. $clients = Client::where('mcp_pro_id', $this->id)
  241. ->orWhere('cm_pro_id', $this->id)
  242. ->orWhere('rmm_pro_id', $this->id)
  243. ->orWhere('rme_pro_id', $this->id)
  244. ->orWhere('rd_pro_id', $this->id)
  245. ->orWhereIn('id', $accessibleClientIds)
  246. ->orWhereIn('id', $appointmentClientIds)->get();
  247. $clientIds = [];
  248. foreach($clients as $client){
  249. $clientIds[] = $client->id;
  250. }
  251. return $clientIds;
  252. }
  253. public function favoritesByCategory($_category) {
  254. return ProFavorite::where('pro_id', $this->id)
  255. ->where('is_removed', false)
  256. ->where('category', $_category)
  257. ->orderBy('category', 'asc')
  258. ->orderBy('position_index', 'asc')
  259. ->get();
  260. }
  261. public function getAccessibleClientsQuery() {
  262. $proID = $this->id;
  263. if ($this->pro_type === 'ADMIN') {
  264. $query = Client::where('id', '>', 0);
  265. } else {
  266. $query = Client::where(function ($q) use ($proID) {
  267. $q->where('mcp_pro_id', $proID)
  268. ->orWhere('cm_pro_id', $proID)
  269. ->orWhere('rmm_pro_id', $proID)
  270. ->orWhere('rme_pro_id', $proID)
  271. ->orWhereRaw('id IN (SELECT client_id FROM client_pro_access WHERE is_active AND pro_id = ?)', [$proID])
  272. ->orWhereRaw('id IN (SELECT client_id FROM appointment WHERE pro_id = ?)', [$proID])
  273. ->orWhereRaw('id IN (SELECT mcp_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)')
  274. ->orWhereRaw('id IN (SELECT manager_pro_id FROM client_program WHERE client_id = client.id AND is_active = TRUE)');
  275. });
  276. }
  277. return $query;
  278. }
  279. public function canAddCPMEntryForMeasurement(Measurement $measurement, Pro $pro)
  280. {
  281. // check if client has any programs where this measurement type is allowed
  282. $allowed = false;
  283. $client = $measurement->client;
  284. $clientPrograms = $client->clientPrograms;
  285. if($pro->pro_type !== 'ADMIN') {
  286. $clientPrograms = $clientPrograms->filter(function($_clientProgram) use ($pro) {
  287. return $_clientProgram->manager_pro_id === $pro->id;
  288. });
  289. }
  290. if(count($clientPrograms)) {
  291. foreach ($clientPrograms as $clientProgram) {
  292. if(strpos(strtolower($clientProgram->measurement_labels), '|' . strtolower($measurement->label) . '|') !== FALSE) {
  293. $allowed = true;
  294. break;
  295. }
  296. }
  297. }
  298. return $allowed ? $clientPrograms : FALSE;
  299. }
  300. }