helpers.php 38 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: tatu
  5. * Date: 6/23/20
  6. * Time: 12:10 AM
  7. */
  8. use App\Models\AppSession;
  9. use App\Models\Client;
  10. use App\Models\Pro;
  11. use App\Models\Bill;
  12. use App\Models\Survey;
  13. //require_once './class.Diff.php';
  14. use Illuminate\Support\Facades\Http;
  15. use Soundasleep\Html2Text as Html2Text;
  16. use Illuminate\Support\Facades\File;
  17. use Illuminate\Support\Facades\View;
  18. if(!function_exists('chargeForCPT')) {
  19. function chargeForCPT($cpt) {
  20. switch($cpt) {
  21. case "99202": return '$' . 172;
  22. case "99203": return '$' . 262;
  23. case "99204": return '$' . 387;
  24. case "99205": return '$' . 512;
  25. case "99211": return '$' . 56;
  26. case "99212": return '$' . 133;
  27. case "99213": return '$' . 212;
  28. case "99214": return '$' . 297;
  29. case "99215": return '$' . 418;
  30. case "99354": return '$' . 289;
  31. case "G0506": return '$' . 144;
  32. case "99441": return '$' . 132;
  33. case "99442": return '$' . 211;
  34. case "99443": return '$' . 297;
  35. case "99454": return '$' . 138;
  36. case "99457": return '$' . 117;
  37. case "99458": return '$' . 93;
  38. case "90791": return '$' . 394;
  39. case "90792": return '$' . 444;
  40. case "90832": return '$' . 171;
  41. case "90834": return '$' . 226;
  42. case "90837": return '$' . 332;
  43. case "90833": return '$' . 158;
  44. case "90836": return '$' . 199;
  45. case "90838": return '$' . 261;
  46. case "99355": return '$' . 130;
  47. case "97802": return '$' . 60;
  48. case "97803": return '$' . 50;
  49. case "99495": return '$' . 275;
  50. case "99406": return '$' . 35.56;
  51. case "99091": return '$' . 75;
  52. case "99453": return '$' . 40;
  53. }
  54. return '-';
  55. }
  56. }
  57. if(!function_exists('currentSortForKey')) {
  58. function currentSortForKey($current, $key) {
  59. $found = false;
  60. $current = $current ? $current : [];
  61. for ($i = 0; $i < count($current); $i++) {
  62. if($current[$i]['key'] === $key) { // if exists, reverse
  63. return ["index" => ($i + 1), "order" => $current[$i]['order']];
  64. }
  65. }
  66. return FALSE;
  67. }
  68. }
  69. if(!function_exists('includeOrReverseInMultiSort')) {
  70. function includeOrReverseInMultiSort($current, $key) {
  71. $found = false;
  72. $current = $current ? $current : [];
  73. for ($i = 0; $i < count($current); $i++) {
  74. if($current[$i]['key'] === $key) { // if exists, reverse
  75. $current[$i]['order'] = ($current[$i]['order'] === 'DESC' ? 'ASC' : 'DESC');
  76. $found = true;
  77. break;
  78. }
  79. }
  80. if (!$found) { // if not exists, include
  81. $current[] = ["key" => $key, "order" => 'ASC'];
  82. }
  83. return $current;
  84. }
  85. }
  86. if(!function_exists('removeFromMultiSort')) {
  87. function removeFromMultiSort($current, $key) {
  88. $found = false;
  89. $current = $current ? $current : [];
  90. $current = array_filter($current, function($_x) use ($key) {
  91. return $_x['key'] !== $key;
  92. });
  93. return array_values($current);
  94. }
  95. }
  96. if(!function_exists('toFeetAndInches')) {
  97. function toFeetAndInches($value, $ftLabel = ' ft.', $inLabel = ' in.') {
  98. if(!$value) return '-';
  99. $value = round($value);
  100. $ft = round(floor($value / 12));
  101. $in = $value % 12;
  102. return "$ft$ftLabel $in$inLabel";
  103. }
  104. }
  105. if(!function_exists('callJava')) {
  106. function callJava($endPoint, $data, $sessionKey) {
  107. $url = config('stag.backendUrl') . $endPoint;
  108. $response = Http::asForm()
  109. ->withHeaders([
  110. 'sessionKey' => $sessionKey
  111. ])
  112. ->post($url, $data)
  113. ->body();
  114. return $response;
  115. }
  116. }
  117. if(!function_exists('feetFromInches')) {
  118. function feetFromInches($value) {
  119. if(!$value) return 0;
  120. $value = round($value);
  121. return round(floor($value / 12));
  122. }
  123. }
  124. if(!function_exists('inchesAfterFeetFromInches')) {
  125. function inchesAfterFeetFromInches($value) {
  126. if(!$value) return 0;
  127. $value = round($value);
  128. return round($value % 12);
  129. }
  130. }
  131. if(!function_exists('genericBills')) {
  132. function genericBills(Pro $performerPro, $patient,$careMonth, $entityType, $entityUid, $paginate = false) {
  133. $genericBills = Bill::where('bill_service_type', 'GENERIC');
  134. if($performerPro->pro_type !== 'ADMIN') {
  135. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  136. }
  137. if($patient) {
  138. $genericBills = $genericBills->where('client_id', $patient->id);
  139. }
  140. if($careMonth){
  141. $genericBills = $genericBills->where('care_month_id', $careMonth->id);
  142. }
  143. if($entityType && $entityUid) {
  144. $genericBills = $genericBills
  145. ->where('generic_target_entity_type', $entityType)
  146. ->where('generic_target_entity_uid', $entityUid);
  147. }
  148. if($paginate) {
  149. return $genericBills->orderBy('created_at', 'DESC')->paginate(25);
  150. }
  151. return $genericBills->orderBy('created_at', 'DESC')->get();
  152. }
  153. }
  154. if(!function_exists('hasActiveGenericBill')) {
  155. function hasActiveGenericBill(Pro $performerPro, $patient, $entityType, $entityUid) {
  156. $genericBills = Bill::where('bill_service_type', 'GENERIC')->where('is_cancelled', false);
  157. if($performerPro->pro_type !== 'ADMIN') {
  158. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  159. }
  160. if($patient) {
  161. $genericBills = $genericBills->where('client_id', $patient->id);
  162. }
  163. if($entityType && $entityUid) {
  164. $genericBills = $genericBills
  165. ->where('generic_target_entity_type', $entityType)
  166. ->where('generic_target_entity_uid', $entityUid);
  167. }
  168. return $genericBills->count() > 0;
  169. }
  170. }
  171. if(!function_exists('queryLineExcept')) {
  172. function queryLineExcept($except = []) {
  173. $params = request()->all();
  174. $final = [];
  175. foreach ($params as $k => $v) {
  176. if(in_array($k, $except) === FALSE) {
  177. if(is_array($v)) $v = implode(',', $v);
  178. $final[] = "$k=" . urlencode($v);
  179. }
  180. }
  181. return implode('&', $final);
  182. }
  183. }
  184. if(!function_exists('sortColumnHead')) {
  185. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  186. $currentSortKey = request()->input('sort');
  187. $currentDir = request()->input('dir');
  188. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  189. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  190. if($currentSortKey === $sortKey) {
  191. if($currentDir === 'ASC') {
  192. echo "&nbsp;&nbsp;↑";
  193. }
  194. elseif($currentDir === 'DESC') {
  195. echo "&nbsp;&nbsp;↓";
  196. }
  197. }
  198. }
  199. }
  200. if(!function_exists('html2Text')) {
  201. function html2Text($old, $new){
  202. }
  203. }
  204. if(!function_exists('diff')) {
  205. function diff($old, $new){
  206. // return Diff::toHTML(Diff::compare($old, $new));
  207. }
  208. }
  209. if(!function_exists('get_current_session')) {
  210. function get_current_session(){
  211. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  212. }
  213. }
  214. if(!function_exists('get_current_date')) {
  215. function get_current_date($timezone = null){
  216. if(!$timezone){
  217. $timezone = 'US\Eastern';
  218. }
  219. $date = new DateTime("now", new DateTimeZone('US/Eastern') );
  220. return $date->format('Y-m-d');
  221. }
  222. }
  223. if(!function_exists('friendly_date_est')) {
  224. function friendly_date_est($value, $default = '-') {
  225. if(!$value || empty($value)) return $default;
  226. try {
  227. $realTimezone = resolve_timezone('EASTERN');
  228. $date = new DateTime($value);
  229. $date->setTimezone(new DateTimeZone($realTimezone));
  230. return $date->format("m/d/y");
  231. }
  232. catch (Exception $e) {
  233. return $e->getMessage();
  234. }
  235. }
  236. }
  237. if(!function_exists('friendly_date_est_compact')) {
  238. function friendly_date_est_compact($value, $default = '-') {
  239. if(!$value || empty($value)) return $default;
  240. try {
  241. $realTimezone = resolve_timezone('EASTERN');
  242. $date = new DateTime($value);
  243. $date->setTimezone(new DateTimeZone($realTimezone));
  244. return $date->format('y') === date('y') ? $date->format("m/d") : $date->format("m/d/y");
  245. }
  246. catch (Exception $e) {
  247. return $e->getMessage();
  248. }
  249. }
  250. }
  251. if(!function_exists('friendly_date_compact')) {
  252. function friendly_date_compact($value, $default = '-') {
  253. if(!$value || empty($value)) return $default;
  254. try {
  255. $date = new DateTime($value);
  256. return $date->format('y') === date('y') ? $date->format("m/d") : $date->format("m/d/y");
  257. }
  258. catch (Exception $e) {
  259. return $e->getMessage();
  260. }
  261. }
  262. }
  263. if(!function_exists('friendly_date_time')) {
  264. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  265. if(!$value || empty($value)) return $default;
  266. try {
  267. if($includeTime) {
  268. $realTimezone = 'US/Eastern';
  269. $date = new DateTime($value);
  270. $date->setTimezone(new DateTimeZone($realTimezone));
  271. return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
  272. }
  273. else {
  274. $result = strtotime($value);
  275. if($long_year){
  276. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  277. }else{
  278. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  279. }
  280. return $result;
  281. }
  282. }
  283. catch (Exception $e) {
  284. return $value;
  285. }
  286. }
  287. }
  288. if(!function_exists('friendly_date_time_with_seconds')) {
  289. function friendly_date_time_with_seconds($value, $includeTime = true, $default = '-', $long_year=false) {
  290. if(!$value || empty($value)) return $default;
  291. try {
  292. if($includeTime) {
  293. $realTimezone = 'US/Eastern';
  294. $date = new DateTime($value);
  295. $date->setTimezone(new DateTimeZone($realTimezone));
  296. return $date->format("m/d/y" . ($includeTime ? ", h:i:sa" : "")) . ($includeTime ? ' EST' : '');
  297. }
  298. else {
  299. $result = strtotime($value);
  300. if($long_year){
  301. $result = date("m/d/Y" . ($includeTime ? ", h:i:sa" : ""), $result);
  302. }else{
  303. $result = date("m/d/y" . ($includeTime ? ", h:i:sa" : ""), $result);
  304. }
  305. return $result;
  306. }
  307. }
  308. catch (Exception $e) {
  309. return $value;
  310. }
  311. }
  312. }
  313. if(!function_exists('friendly_date_month_year')) {
  314. function friendly_date_month_year($value, $default = '-', $long_year=true) {
  315. if(!$value || empty($value)) return $default;
  316. try {
  317. $result = strtotime($value);
  318. if($long_year){
  319. $result = date("m/Y", $result);
  320. }else{
  321. $result = date("m/y", $result);
  322. }
  323. return $result;
  324. }
  325. catch (Exception $e) {
  326. return $value;
  327. }
  328. }
  329. }
  330. if(!function_exists('friendlier_date_time')) {
  331. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  332. return friendly_date_time($value, $includeTime, $default);
  333. }
  334. }
  335. if(!function_exists('friendly_date_time_short')) {
  336. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  337. if(!$value || empty($value)) return $default;
  338. try {
  339. $result = strtotime($value);
  340. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  341. return $result;
  342. }
  343. catch (Exception $e) {
  344. return $value;
  345. }
  346. }
  347. }
  348. if(!function_exists('friendly_date_time_short_with_tz')) {
  349. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  350. if(!$value || empty($value)) return $default;
  351. try {
  352. $realTimezone = resolve_timezone($tz);
  353. $date = new DateTime($value);
  354. $date->setTimezone(new DateTimeZone($realTimezone));
  355. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  356. }
  357. catch (Exception $e) {
  358. return $e->getMessage();
  359. }
  360. }
  361. }
  362. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  363. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-', $format = "m/d/y, h:iA") {
  364. if(!$value || empty($value)) return $default;
  365. try {
  366. $realTimezone = resolve_timezone($tz);
  367. $date = new DateTime("@$value");
  368. $date->setTimezone(new DateTimeZone($realTimezone));
  369. return $date->format($format);
  370. }
  371. catch (Exception $e) {
  372. return $e->getMessage();
  373. }
  374. }
  375. }
  376. if(!function_exists('postgres_date_time_short_with_tz')) {
  377. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  378. if(!$value || empty($value)) return $default;
  379. try {
  380. $realTimezone = resolve_timezone($tz);
  381. $date = new DateTime($value);
  382. $date->setTimezone(new DateTimeZone($realTimezone));
  383. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  384. }
  385. catch (Exception $e) {
  386. return $e->getMessage();
  387. }
  388. }
  389. }
  390. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  391. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  392. if(!$value || empty($value)) return $default;
  393. try {
  394. $realTimezone = resolve_timezone($tz);
  395. $date = new DateTime("@$value");
  396. $date->setTimezone(new DateTimeZone($realTimezone));
  397. return $date->format("Y-m-d h:i:s");
  398. }
  399. catch (Exception $e) {
  400. return $e->getMessage();
  401. }
  402. }
  403. }
  404. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  405. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  406. if(!$value || empty($value)) return $default;
  407. try {
  408. $value = (floor($value / 1000));
  409. $realTimezone = resolve_timezone($tz);
  410. $date = new DateTime("@$value");
  411. $date->setTimezone(new DateTimeZone($realTimezone));
  412. return $date->format("m/d/y, h:iA");
  413. }
  414. catch (Exception $e) {
  415. return $e->getMessage();
  416. }
  417. }
  418. }
  419. if(!function_exists('friendly_date_short_with_tz_from_timestamp_divide1000')) {
  420. function friendly_date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  421. if(!$value || empty($value)) return $default;
  422. try {
  423. $value = (floor($value / 1000));
  424. $realTimezone = resolve_timezone($tz);
  425. $date = new DateTime("@$value");
  426. $date->setTimezone(new DateTimeZone($realTimezone));
  427. return $date->format("m/d/y");
  428. }
  429. catch (Exception $e) {
  430. return $e->getMessage();
  431. }
  432. }
  433. }
  434. if(!function_exists('date_short_with_tz_from_timestamp_divide1000')) {
  435. function date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  436. if(!$value || empty($value)) return $default;
  437. try {
  438. $value = (floor($value / 1000));
  439. $realTimezone = resolve_timezone($tz);
  440. $date = new DateTime("@$value");
  441. $date->setTimezone(new DateTimeZone($realTimezone));
  442. return $date->format("Y-m-d");
  443. }
  444. catch (Exception $e) {
  445. return $e->getMessage();
  446. }
  447. }
  448. }
  449. if(!function_exists('get_end_of_month')) {
  450. function get_end_of_month($value) {
  451. if(!$value || empty($value)) return $default;
  452. try {
  453. $date = new DateTime($value);
  454. $date->modify('last day of this month');
  455. return $date->format("m/d/Y");
  456. }
  457. catch (Exception $e) {
  458. return $e->getMessage();
  459. }
  460. }
  461. }
  462. if(!function_exists('friendly_date')) {
  463. function friendly_date($value) {
  464. if(!$value || empty($value)) return '';
  465. try {
  466. $result = strtotime($value);
  467. $result = date("m/d/Y", $result);
  468. return $result;
  469. }
  470. catch (Exception $e) {
  471. return $value;
  472. }
  473. }
  474. }
  475. if(!function_exists('friendly_date_short')) {
  476. function friendly_date_short($value) {
  477. if(!$value || empty($value)) return '';
  478. try {
  479. $result = strtotime($value);
  480. $result = date("m/d/y", $result);
  481. return $result;
  482. }
  483. catch (Exception $e) {
  484. return $value;
  485. }
  486. }
  487. }
  488. if(!function_exists('friendly_date_month_year')) {
  489. function friendly_date_month_year($value) {
  490. if(!$value || empty($value)) return '';
  491. try {
  492. $result = strtotime($value);
  493. $result = date("M Y", $result);
  494. return $result;
  495. }
  496. catch (Exception $e) {
  497. return $value;
  498. }
  499. }
  500. }
  501. if(!function_exists('friendlier_date')) {
  502. function friendlier_date($value) {
  503. if(!$value || empty($value)) return '';
  504. try {
  505. $result = strtotime($value);
  506. $result = date("m/d/Y", $result);
  507. return $result;
  508. }
  509. catch (Exception $e) {
  510. return $value;
  511. }
  512. }
  513. }
  514. if(!function_exists('relative_friendly_date')) {
  515. function relative_friendly_date($value) {
  516. if(!$value || empty($value)) return '';
  517. try {
  518. $result = date_diff(date_create($value), date_create('now'))->days;
  519. if ($result > 30) $result = date('F Y', strtotime($value));
  520. else if ($result > 1) $result .= ' days ago';
  521. else if ($result === 1) $result = 'Yesterday';
  522. else if ($result === 0) $result = 'Today';
  523. return $result;
  524. }
  525. catch (Exception $e) {
  526. return $value;
  527. }
  528. }
  529. }
  530. if(!function_exists('unfriendly_date')) {
  531. function unfriendly_date($value) {
  532. if(!$value || empty($value)) return '';
  533. try {
  534. $result = strtotime($value);
  535. $result = date("Y-m-d", $result);
  536. return $result;
  537. }
  538. catch (Exception $e) {
  539. return $value;
  540. }
  541. }
  542. }
  543. if(!function_exists('friendly_time')) {
  544. function friendly_time($value, $default = '-') {
  545. if(!$value || empty($value)) return $default;
  546. try {
  547. $result = strtotime($value);
  548. $result = date("h:i a", $result);
  549. return $result;
  550. }
  551. catch (Exception $e) {
  552. return $value;
  553. }
  554. }
  555. }
  556. if(!function_exists('military_time')) {
  557. function military_time($value, $tz='UTC', $default = '-') {
  558. if(!$value || empty($value)) return $default;
  559. try {
  560. $realTimezone = resolve_timezone($tz);
  561. $date = new DateTime($value);
  562. $date->setTimezone(new DateTimeZone($realTimezone));
  563. return $date->format("H:i");
  564. }
  565. catch (Exception $e) {
  566. return $value;
  567. }
  568. }
  569. }
  570. if(!function_exists('resolve_timezone')) {
  571. function resolve_timezone($value) {
  572. try {
  573. switch ($value) {
  574. case 'ALASKA': {
  575. return 'US/Alaska';
  576. }
  577. case 'CENTRAL': {
  578. return 'US/Central';
  579. }
  580. case 'EASTERN': {
  581. return 'US/Eastern';
  582. }
  583. case 'HAWAII': {
  584. return 'US/Hawaii';
  585. }
  586. case 'MOUNTAIN': {
  587. return 'US/Mountain';
  588. }
  589. case 'PACIFIC': {
  590. return 'US/Pacific';
  591. }
  592. case 'PUERTO_RICO': {
  593. return 'America/Puerto_Rico';
  594. }
  595. case 'UTC': {
  596. return 'UTC';
  597. }
  598. }
  599. }
  600. catch (Exception $e) {
  601. return $value;
  602. }
  603. }
  604. }
  605. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  606. // echo $date->format('Y-m-d H:i:sP') . "\n";
  607. if(!function_exists('friendly_month')) {
  608. function friendly_month($value) {
  609. if(!$value || empty($value)) return "-";
  610. try {
  611. $result = strtotime($value);
  612. $result = date("F o", $result);
  613. return $result;
  614. }
  615. catch (Exception $e) {
  616. return $value;
  617. }
  618. }
  619. }
  620. if(!function_exists('friendly_month_short')) {
  621. function friendly_month_short($value) {
  622. if(!$value || empty($value)) return "-";
  623. try {
  624. $result = strtotime($value);
  625. $result = date("M y", $result);
  626. return $result;
  627. }
  628. catch (Exception $e) {
  629. return $value;
  630. }
  631. }
  632. }
  633. if(!function_exists('day_part_from_date')) {
  634. function day_part_from_date($value) {
  635. if(!$value || empty($value)) return "-";
  636. try {
  637. $result = strtotime($value);
  638. $result = date("d", $result);
  639. return $result;
  640. }
  641. catch (Exception $e) {
  642. return $value;
  643. }
  644. }
  645. }
  646. if(!function_exists('month_part_from_date')) {
  647. function month_part_from_date($value) {
  648. if(!$value || empty($value)) return "-";
  649. try {
  650. $result = strtotime($value);
  651. $result = date("m", $result);
  652. return $result;
  653. }
  654. catch (Exception $e) {
  655. return $value;
  656. }
  657. }
  658. }
  659. if(!function_exists('year_part_from_date')) {
  660. function year_part_from_date($value) {
  661. if(!$value || empty($value)) return "-";
  662. try {
  663. $result = strtotime($value);
  664. $result = date("Y", $result);
  665. return $result;
  666. }
  667. catch (Exception $e) {
  668. return $value;
  669. }
  670. }
  671. }
  672. if(!function_exists('friendly_money')){
  673. function friendly_money($value, $decimals = 2) {
  674. // if($decimals === 0) return intval($value);
  675. return number_format((float)$value, $decimals, '.', '');
  676. }
  677. }
  678. if(!function_exists('time_in_hrminsec')) {
  679. function time_in_hrminsec($value, $default = '-') {
  680. if(!$value || empty($value)) return $default;
  681. $value = intval($value);
  682. $minutes = intval($value / 60);
  683. $seconds = $value % 60;
  684. $hours = 0;
  685. if($minutes >= 60) {
  686. $hours = intval($minutes / 60);
  687. $minutes = $minutes % 60;
  688. }
  689. $output = [];
  690. if($hours > 0) {
  691. $output[] = "{$hours}h";
  692. }
  693. if($minutes > 0) {
  694. $output[] = "{$minutes}m";
  695. }
  696. if($seconds > 0) {
  697. $output[] = "{$seconds}s";
  698. }
  699. return implode(" ", $output);
  700. }
  701. }
  702. if(!function_exists('sanitize_field_name')) {
  703. function sanitize_field_name($name) {
  704. $result = strtolower($name);
  705. return preg_replace("/[^0-9a-z]/i", "_", $result);
  706. }
  707. }
  708. if(!function_exists('sanitize_state_name')) {
  709. function sanitize_state_name($name) {
  710. $result = strtolower($name);
  711. return ucwords(preg_replace("/_/i", " ", $result));
  712. }
  713. }
  714. if(!function_exists('renderNoteTemplate')) {
  715. function renderNoteTemplate($template, $topLevel)
  716. {
  717. echo
  718. '<div class="note-template-item" ' .
  719. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  720. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  721. '>' .
  722. '<div class="note-template-text d-flex align-items-center">' .
  723. '<span class="label">' .
  724. '<input type="checkbox" />' .
  725. '<span>' . $template->text . '</span>' .
  726. '</span>';
  727. if (isset($template->type) && $template->type === 'plus-minus') {
  728. echo '<div class="ml-auto mr-2 text-nowrap">';
  729. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  730. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  731. echo '</div>';
  732. }
  733. echo '</div>';
  734. if (isset($template->children) && count($template->children)) {
  735. echo '<i class="fa fa-chevron-right has-children"></i>';
  736. echo '<div class="note-template-children">';
  737. foreach ($template->children as $t) {
  738. renderNoteTemplate($t, false);
  739. }
  740. echo '</div>';
  741. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  742. echo '<i class="fa fa-chevron-right has-children"></i>';
  743. echo '<div class="note-template-children">';
  744. if ($template->type === 'alpha') {
  745. echo '<textarea class="form-control form-control-sm"></textarea>';
  746. } else {
  747. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  748. }
  749. echo '</div>';
  750. }
  751. echo '</div>';
  752. }
  753. }
  754. if(!function_exists('renderNoteTemplates')) {
  755. function renderNoteTemplates($path)
  756. {
  757. $templates = json_decode(file_get_contents($path));
  758. foreach ($templates->templates as $template) {
  759. renderNoteTemplate($template, true);
  760. }
  761. }
  762. }
  763. if(!function_exists('renderNoteExamTemplates')) {
  764. function renderNoteExamTemplates($parentPath, $childPath)
  765. {
  766. $templates = json_decode(file_get_contents($parentPath));
  767. $templates = $templates->templates;
  768. // override as needed with what is in template set
  769. if(file_exists($childPath)) {
  770. $orTemplates = json_decode(file_get_contents($parentPath));
  771. $orTemplates = $orTemplates->templates;
  772. for ($i = 0; $i < count($templates); $i++) {
  773. for ($j = 0; $j < count($orTemplates); $j++) {
  774. if($templates[$i]->text === $orTemplates[$j]->text) {
  775. $templates[$i] = $orTemplates[$j];
  776. }
  777. }
  778. }
  779. }
  780. foreach ($templates as $template) {
  781. renderNoteTemplate($template, true);
  782. }
  783. }
  784. }
  785. if(!function_exists('getVal')) {
  786. function getVal($object, $prop)
  787. {
  788. if (isset($object->$prop)) {
  789. return $object->$prop;
  790. } else {
  791. return '';
  792. }
  793. }
  794. }
  795. if(!function_exists('appTZtoPHPTZ')) {
  796. function appTZtoPHPTZ($_timezone)
  797. {
  798. switch ($_timezone) {
  799. case 'ALASKA':
  800. $timezone = "US/Alaska";
  801. break;
  802. case 'CENTRAL':
  803. $timezone = "US/Central";
  804. break;
  805. case 'HAWAII':
  806. $timezone = "US/Hawaii";
  807. break;
  808. case 'MOUNTAIN':
  809. $timezone = "US/Mountain";
  810. break;
  811. case 'PACIFIC':
  812. $timezone = "US/Pacific";
  813. break;
  814. case 'PUERTO_RICO':
  815. $timezone = "America/Puerto_Rico";
  816. break;
  817. default:
  818. $timezone = "US/Eastern";
  819. break;
  820. }
  821. return $timezone;
  822. }
  823. }
  824. if(!function_exists('convertToTimezone')) {
  825. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  826. {
  827. if (!$_dateTime) return $_dateTime;
  828. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  829. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  830. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  831. }
  832. }
  833. if(!function_exists('minutes_to_hhmm')) {
  834. function minutes_to_hhmm($_minutes)
  835. {
  836. $h = intval(floor($_minutes / 60));
  837. $m = $_minutes;
  838. if($h > 0) {
  839. $m = $_minutes - $h * 60;
  840. }
  841. $h = ($h < 10 ? '0' : '') . $h;
  842. $m = ($m < 10 ? '0' : '') . $m;
  843. return $h . ':' . $m;
  844. }
  845. }
  846. if(!function_exists('vsValue')) {
  847. function vsValue($_v, $patient = null, $_direct = false)
  848. {
  849. if ($_direct) {
  850. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  851. }
  852. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  853. }
  854. }
  855. if(!function_exists('vsElement')) {
  856. function vsElement($_v, $type, $name, $patient, $class = '')
  857. {
  858. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  859. 'name="' . $name . '" ' .
  860. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  861. }
  862. }
  863. if(!function_exists('vsRoElement')) {
  864. function vsRoElement($_v, $type, $name, $patient, $class = '')
  865. {
  866. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  867. 'name="' . $name . '" ' .
  868. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  869. }
  870. }
  871. if(!function_exists('str_compact')) {
  872. function str_compact($_str)
  873. {
  874. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  875. }
  876. }
  877. if(!function_exists('noteMethodDisplay')) {
  878. function noteMethodDisplay($method)
  879. {
  880. if($method === 'IN_CLINIC') return 'In-Clinic';
  881. $method = str_replace('_', ' ', $method);
  882. return ucwords(strtolower($method));
  883. }
  884. }
  885. if(!function_exists('friendly_timezone')) {
  886. function friendly_timezone($tz) {
  887. $map = [
  888. 'EASTERN' => 'EST',
  889. 'CENTRAL' => 'CST',
  890. 'MOUNTAIN' => 'MST',
  891. 'PACIFIC' => 'PST',
  892. 'ALASKA' => 'AST',
  893. 'HAWAII' => 'HST',
  894. 'PUERTO_RICO' => 'PR'
  895. ];
  896. return $map[$tz] ?? str_replace("_", " ", $tz);
  897. }
  898. }
  899. if(!function_exists('segment_template_summary_value_display')) {
  900. function segment_template_summary_value_display($value, $default='________', $class = '') {
  901. if($value && strlen($value)) return '<span class="segment-template-summary-value '.$class.'">' . $value . '</span>';
  902. return $default;
  903. }
  904. }
  905. if(!function_exists('starts_with')) {
  906. function starts_with($haystack, $needle) {
  907. $length = strlen($needle);
  908. return substr($haystack, 0, $length) === $needle;
  909. }
  910. }
  911. if(!function_exists('get_doc_templates')){
  912. function get_doc_templates(){
  913. $basePath = 'views/document-templates-generic';
  914. $dir = opendir(resource_path($basePath));
  915. $templates = [];
  916. if($dir) {
  917. while ( $entry = readdir($dir) ) {
  918. if($entry !== '.' && $entry !== '..' && is_dir(resource_path($basePath . '/' . $entry))) {
  919. $spec = json_decode(file_get_contents(resource_path($basePath . '/' . $entry) . '/spec.json'));
  920. if(isset($spec->active) && $spec->active) {
  921. $html = file_get_contents(resource_path($basePath . '/' . $entry) . '/content.blade.php');
  922. // get fields
  923. $proVariables = [];
  924. $htmlDisplay = preg_replace_callback(
  925. '/{([^}]+)}/',
  926. function ($match) use (&$proVariables) {
  927. $token = $match[1];
  928. $replacement = '';
  929. if(strpos($token, '.') === FALSE) {
  930. $adminField = false;
  931. if($token[0] === '@') {
  932. // $token = substr($token, 1);
  933. $adminField = true;
  934. }
  935. switch ($token) {
  936. case 'TODAY':
  937. $replacement = date('m/d/Y');
  938. break;
  939. default:
  940. $replacement = '<span class="text-info document-variable" data-variable="' . $token . '">' .
  941. ($token[0] === '@' ? substr($token, 1) : $token) .
  942. '</span>';
  943. if (!isset($proVariables[$token])) {
  944. $proVariables[$token] = [
  945. 'token' => $token,
  946. 'adminField' => $adminField
  947. ];
  948. }
  949. break;
  950. }
  951. }
  952. return $replacement;
  953. },
  954. $html
  955. );
  956. $templates[] = [
  957. "name" => $entry,
  958. "title" => $spec->title,
  959. "html" => $html,
  960. "htmlDisplay" => $htmlDisplay,
  961. "proVariables" => $proVariables
  962. ];
  963. }
  964. }
  965. }
  966. }
  967. closedir($dir);
  968. // sort by document name
  969. $names = array_column($templates, 'title');
  970. array_multisort($names, SORT_ASC, $templates);
  971. return $templates;
  972. }
  973. }
  974. if(!function_exists('format_phone_number')) {
  975. function format_phone_number($number, $default = '-') {
  976. if(empty($number)) return $default;
  977. return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
  978. }
  979. }
  980. if(!function_exists('format_number')) {
  981. function format_number($number, $places = 2) {
  982. return number_format((float)$number, $places, '.', '');
  983. }
  984. }
  985. if(!function_exists('toHumanReadable')) {
  986. function toHumanReadable($name) {
  987. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  988. }
  989. }
  990. if(!function_exists('parseRender')) {
  991. function parseRender($_data) {
  992. if($_data) {
  993. $type = gettype($_data);
  994. if(is_string($_data) || is_numeric($_data)) {
  995. echo $_data;
  996. }
  997. else {
  998. echo "<table class='table table-sm border w-100'>";
  999. foreach($_data as $k => $v) {
  1000. echo "<tr>";
  1001. echo "<td><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
  1002. echo "<td>";
  1003. if(is_object($v)) {
  1004. parseRender($v);
  1005. }
  1006. elseif(is_array($v)) {
  1007. foreach($v as $k2 => $v2) {
  1008. parseRender($v2);
  1009. }
  1010. }
  1011. else {
  1012. echo $v;
  1013. }
  1014. echo "</td>";
  1015. echo "</tr>";
  1016. }
  1017. echo "</table>";
  1018. }
  1019. }
  1020. }
  1021. }
  1022. if(!function_exists('mask_string')) {
  1023. function mask_string($string, $mask = "*", $lengthFromLastCharacter = null) {
  1024. if(!$lengthFromLastCharacter){
  1025. return str_repeat($mask, strlen($string));
  1026. }
  1027. $firstCharacters = substr($string, ($lengthFromLastCharacter));
  1028. $lastCharacters = substr($string, -$lengthFromLastCharacter);
  1029. $maskedFirstCharacters = str_repeat($mask, strlen($firstCharacters));
  1030. return $maskedFirstCharacters . $lastCharacters;
  1031. }
  1032. }
  1033. if(!function_exists('limit_string_length')) {
  1034. function limit_string_length($string, $length = 30) {
  1035. return substr($string,0, $length);
  1036. }
  1037. }
  1038. if(!function_exists('get_custom_problems')) {
  1039. function get_custom_problems() {
  1040. $customProblems = [
  1041. [
  1042. 'sub_text' => "G47. 33",
  1043. 'text' => "Sleep Apnea"
  1044. ]
  1045. ] ;
  1046. return $customProblems;
  1047. }
  1048. }
  1049. if(!function_exists('camel_case')) {
  1050. function camel_case($string, $dontStrip = []) {
  1051. return lcfirst(str_replace(' ', '', ucwords(preg_replace('/[^a-z0-9'.implode('',$dontStrip).']+/', ' ',$string))));
  1052. }
  1053. }
  1054. if(!function_exists('getDefaultCompanyClientUidSession')) {
  1055. function getDefaultCompanyClientUidSession($patientID) {
  1056. return session()->get('DEFAULT_COMPANY_CLIENT_UID_'.$patientID);
  1057. }
  1058. }
  1059. if(!function_exists('formatAsTitle')) {
  1060. function formatAsTitle($inputString) {
  1061. $cleanString = preg_replace('/[^a-zA-Z0-9\s]/', '', $inputString);
  1062. $titleCaseString = ucwords(strtolower($cleanString));
  1063. return $titleCaseString;
  1064. }
  1065. }
  1066. if(!function_exists('getSurveyData')) {
  1067. function getSurveyData($survey) {
  1068. $surveyFormPath = resource_path(Survey::FORM_PATH . '/' . $survey->internal_name . '.blade.php');
  1069. if(!file_exists($surveyFormPath)) return null;
  1070. $entity = null;
  1071. if($survey->entity_type === 'Client'){
  1072. $entity = Client::where('uid', $survey->entity_uid)->first();
  1073. }
  1074. $preview = true;
  1075. $layout = 'app.patient.surveys.partials.preview-layout';
  1076. $html = (string) view('app.patient.surveys.forms.'.$survey->internal_name, compact('entity', 'survey', 'preview', 'layout'));
  1077. return $html;
  1078. }
  1079. }
  1080. if(!function_exists('determineTier')) {
  1081. function determineTier($careMonth) {
  1082. $tier = null;
  1083. if($careMonth->number_of_days_with_remote_measurements >= 7 && $careMonth->rm_total_time_in_seconds_by_mcp >= 10*60 && $careMonth->has_mcp_interacted_with_client_about_rm ){
  1084. $tier = 'TIER_1';
  1085. }
  1086. if($careMonth->number_of_days_with_remote_measurements >= 16 && $careMonth->rm_total_time_in_seconds_by_mcp >= 20*60 && $careMonth->has_mcp_interacted_with_client_about_rm) {
  1087. $tier = 'TIER_2';
  1088. }
  1089. return $tier;
  1090. }
  1091. }
  1092. function getLogo(){
  1093. if(config('app.internalName')) return asset('/img/'.config('app.internalName').'/logo_white.svg');
  1094. return asset('/img/logo_white.svg');
  1095. }