helpers.php 35 KB

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