helpers.php 38 KB

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