helpers.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123
  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 = '-', $format = "m/d/y, h:iA") {
  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($format);
  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('friendly_month_short')) {
  590. function friendly_month_short($value) {
  591. if(!$value || empty($value)) return "-";
  592. try {
  593. $result = strtotime($value);
  594. $result = date("M y", $result);
  595. return $result;
  596. }
  597. catch (Exception $e) {
  598. return $value;
  599. }
  600. }
  601. }
  602. if(!function_exists('day_part_from_date')) {
  603. function day_part_from_date($value) {
  604. if(!$value || empty($value)) return "-";
  605. try {
  606. $result = strtotime($value);
  607. $result = date("d", $result);
  608. return $result;
  609. }
  610. catch (Exception $e) {
  611. return $value;
  612. }
  613. }
  614. }
  615. if(!function_exists('month_part_from_date')) {
  616. function month_part_from_date($value) {
  617. if(!$value || empty($value)) return "-";
  618. try {
  619. $result = strtotime($value);
  620. $result = date("m", $result);
  621. return $result;
  622. }
  623. catch (Exception $e) {
  624. return $value;
  625. }
  626. }
  627. }
  628. if(!function_exists('year_part_from_date')) {
  629. function year_part_from_date($value) {
  630. if(!$value || empty($value)) return "-";
  631. try {
  632. $result = strtotime($value);
  633. $result = date("Y", $result);
  634. return $result;
  635. }
  636. catch (Exception $e) {
  637. return $value;
  638. }
  639. }
  640. }
  641. if(!function_exists('friendly_money')){
  642. function friendly_money($value, $decimals = 2) {
  643. // if($decimals === 0) return intval($value);
  644. return number_format((float)$value, $decimals, '.', '');
  645. }
  646. }
  647. if(!function_exists('time_in_hrminsec')) {
  648. function time_in_hrminsec($value, $default = '-') {
  649. if(!$value || empty($value)) return $default;
  650. $value = intval($value);
  651. $minutes = intval($value / 60);
  652. $seconds = $value % 60;
  653. $hours = 0;
  654. if($minutes >= 60) {
  655. $hours = intval($minutes / 60);
  656. $minutes = $minutes % 60;
  657. }
  658. $output = [];
  659. if($hours > 0) {
  660. $output[] = "{$hours}h";
  661. }
  662. if($minutes > 0) {
  663. $output[] = "{$minutes}m";
  664. }
  665. if($seconds > 0) {
  666. $output[] = "{$seconds}s";
  667. }
  668. return implode(" ", $output);
  669. }
  670. }
  671. if(!function_exists('sanitize_field_name')) {
  672. function sanitize_field_name($name) {
  673. $result = strtolower($name);
  674. return preg_replace("/[^0-9a-z]/i", "_", $result);
  675. }
  676. }
  677. if(!function_exists('sanitize_state_name')) {
  678. function sanitize_state_name($name) {
  679. $result = strtolower($name);
  680. return ucwords(preg_replace("/_/i", " ", $result));
  681. }
  682. }
  683. if(!function_exists('renderNoteTemplate')) {
  684. function renderNoteTemplate($template, $topLevel)
  685. {
  686. echo
  687. '<div class="note-template-item" ' .
  688. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  689. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  690. '>' .
  691. '<div class="note-template-text d-flex align-items-center">' .
  692. '<span class="label">' .
  693. '<input type="checkbox" />' .
  694. '<span>' . $template->text . '</span>' .
  695. '</span>';
  696. if (isset($template->type) && $template->type === 'plus-minus') {
  697. echo '<div class="ml-auto mr-2 text-nowrap">';
  698. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  699. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  700. echo '</div>';
  701. }
  702. echo '</div>';
  703. if (isset($template->children) && count($template->children)) {
  704. echo '<i class="fa fa-chevron-right has-children"></i>';
  705. echo '<div class="note-template-children">';
  706. foreach ($template->children as $t) {
  707. renderNoteTemplate($t, false);
  708. }
  709. echo '</div>';
  710. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  711. echo '<i class="fa fa-chevron-right has-children"></i>';
  712. echo '<div class="note-template-children">';
  713. if ($template->type === 'alpha') {
  714. echo '<textarea class="form-control form-control-sm"></textarea>';
  715. } else {
  716. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  717. }
  718. echo '</div>';
  719. }
  720. echo '</div>';
  721. }
  722. }
  723. if(!function_exists('renderNoteTemplates')) {
  724. function renderNoteTemplates($path)
  725. {
  726. $templates = json_decode(file_get_contents($path));
  727. foreach ($templates->templates as $template) {
  728. renderNoteTemplate($template, true);
  729. }
  730. }
  731. }
  732. if(!function_exists('renderNoteExamTemplates')) {
  733. function renderNoteExamTemplates($parentPath, $childPath)
  734. {
  735. $templates = json_decode(file_get_contents($parentPath));
  736. $templates = $templates->templates;
  737. // override as needed with what is in template set
  738. if(file_exists($childPath)) {
  739. $orTemplates = json_decode(file_get_contents($parentPath));
  740. $orTemplates = $orTemplates->templates;
  741. for ($i = 0; $i < count($templates); $i++) {
  742. for ($j = 0; $j < count($orTemplates); $j++) {
  743. if($templates[$i]->text === $orTemplates[$j]->text) {
  744. $templates[$i] = $orTemplates[$j];
  745. }
  746. }
  747. }
  748. }
  749. foreach ($templates as $template) {
  750. renderNoteTemplate($template, true);
  751. }
  752. }
  753. }
  754. if(!function_exists('getVal')) {
  755. function getVal($object, $prop)
  756. {
  757. if (isset($object->$prop)) {
  758. return $object->$prop;
  759. } else {
  760. return '';
  761. }
  762. }
  763. }
  764. if(!function_exists('appTZtoPHPTZ')) {
  765. function appTZtoPHPTZ($_timezone)
  766. {
  767. switch ($_timezone) {
  768. case 'ALASKA':
  769. $timezone = "US/Alaska";
  770. break;
  771. case 'CENTRAL':
  772. $timezone = "US/Central";
  773. break;
  774. case 'HAWAII':
  775. $timezone = "US/Hawaii";
  776. break;
  777. case 'MOUNTAIN':
  778. $timezone = "US/Mountain";
  779. break;
  780. case 'PACIFIC':
  781. $timezone = "US/Pacific";
  782. break;
  783. case 'PUERTO_RICO':
  784. $timezone = "America/Puerto_Rico";
  785. break;
  786. default:
  787. $timezone = "US/Eastern";
  788. break;
  789. }
  790. return $timezone;
  791. }
  792. }
  793. if(!function_exists('convertToTimezone')) {
  794. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  795. {
  796. if (!$_dateTime) return $_dateTime;
  797. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  798. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  799. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  800. }
  801. }
  802. if(!function_exists('minutes_to_hhmm')) {
  803. function minutes_to_hhmm($_minutes)
  804. {
  805. $h = intval(floor($_minutes / 60));
  806. $m = $_minutes;
  807. if($h > 0) {
  808. $m = $_minutes - $h * 60;
  809. }
  810. $h = ($h < 10 ? '0' : '') . $h;
  811. $m = ($m < 10 ? '0' : '') . $m;
  812. return $h . ':' . $m;
  813. }
  814. }
  815. if(!function_exists('vsValue')) {
  816. function vsValue($_v, $patient = null, $_direct = false)
  817. {
  818. if ($_direct) {
  819. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  820. }
  821. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  822. }
  823. }
  824. if(!function_exists('vsElement')) {
  825. function vsElement($_v, $type, $name, $patient, $class = '')
  826. {
  827. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  828. 'name="' . $name . '" ' .
  829. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  830. }
  831. }
  832. if(!function_exists('vsRoElement')) {
  833. function vsRoElement($_v, $type, $name, $patient, $class = '')
  834. {
  835. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  836. 'name="' . $name . '" ' .
  837. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  838. }
  839. }
  840. if(!function_exists('str_compact')) {
  841. function str_compact($_str)
  842. {
  843. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  844. }
  845. }
  846. if(!function_exists('noteMethodDisplay')) {
  847. function noteMethodDisplay($method)
  848. {
  849. if($method === 'IN_CLINIC') return 'In-Clinic';
  850. $method = str_replace('_', ' ', $method);
  851. return ucwords(strtolower($method));
  852. }
  853. }
  854. if(!function_exists('friendly_timezone')) {
  855. function friendly_timezone($tz) {
  856. $map = [
  857. 'EASTERN' => 'EST',
  858. 'CENTRAL' => 'CST',
  859. 'MOUNTAIN' => 'MST',
  860. 'PACIFIC' => 'PST',
  861. 'ALASKA' => 'AST',
  862. 'HAWAII' => 'HST',
  863. 'PUERTO_RICO' => 'PR'
  864. ];
  865. return $map[$tz] ?? str_replace("_", " ", $tz);
  866. }
  867. }
  868. if(!function_exists('segment_template_summary_value_display')) {
  869. function segment_template_summary_value_display($value, $default='________', $class = '') {
  870. if($value && strlen($value)) return '<span class="segment-template-summary-value '.$class.'">' . $value . '</span>';
  871. return $default;
  872. }
  873. }
  874. if(!function_exists('starts_with')) {
  875. function starts_with($haystack, $needle) {
  876. $length = strlen($needle);
  877. return substr($haystack, 0, $length) === $needle;
  878. }
  879. }
  880. if(!function_exists('get_doc_templates')){
  881. function get_doc_templates(){
  882. $basePath = 'views/document-templates-generic';
  883. $dir = opendir(resource_path($basePath));
  884. $templates = [];
  885. if($dir) {
  886. while ( $entry = readdir($dir) ) {
  887. if($entry !== '.' && $entry !== '..' && is_dir(resource_path($basePath . '/' . $entry))) {
  888. $spec = json_decode(file_get_contents(resource_path($basePath . '/' . $entry) . '/spec.json'));
  889. if(isset($spec->active) && $spec->active) {
  890. $html = file_get_contents(resource_path($basePath . '/' . $entry) . '/content.blade.php');
  891. // get fields
  892. $proVariables = [];
  893. $htmlDisplay = preg_replace_callback(
  894. '/{([^}]+)}/',
  895. function ($match) use (&$proVariables) {
  896. $token = $match[1];
  897. $replacement = '';
  898. if(strpos($token, '.') === FALSE) {
  899. $adminField = false;
  900. if($token[0] === '@') {
  901. // $token = substr($token, 1);
  902. $adminField = true;
  903. }
  904. switch ($token) {
  905. case 'TODAY':
  906. $replacement = date('m/d/Y');
  907. break;
  908. default:
  909. $replacement = '<span class="text-info document-variable" data-variable="' . $token . '">' .
  910. ($token[0] === '@' ? substr($token, 1) : $token) .
  911. '</span>';
  912. if (!isset($proVariables[$token])) {
  913. $proVariables[$token] = [
  914. 'token' => $token,
  915. 'adminField' => $adminField
  916. ];
  917. }
  918. break;
  919. }
  920. }
  921. return $replacement;
  922. },
  923. $html
  924. );
  925. $templates[] = [
  926. "name" => $entry,
  927. "title" => $spec->title,
  928. "html" => $html,
  929. "htmlDisplay" => $htmlDisplay,
  930. "proVariables" => $proVariables
  931. ];
  932. }
  933. }
  934. }
  935. }
  936. closedir($dir);
  937. // sort by document name
  938. $names = array_column($templates, 'title');
  939. array_multisort($names, SORT_ASC, $templates);
  940. return $templates;
  941. }
  942. }
  943. if(!function_exists('format_phone_number')) {
  944. function format_phone_number($number, $default = '-') {
  945. if(empty($number)) return $default;
  946. return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
  947. }
  948. }
  949. if(!function_exists('format_number')) {
  950. function format_number($number, $places = 2) {
  951. return number_format((float)$number, $places, '.', '');
  952. }
  953. }
  954. if(!function_exists('toHumanReadable')) {
  955. function toHumanReadable($name) {
  956. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  957. }
  958. }
  959. if(!function_exists('parseRender')) {
  960. function parseRender($_data) {
  961. if($_data) {
  962. $type = gettype($_data);
  963. if(is_string($_data) || is_numeric($_data)) {
  964. echo $_data;
  965. }
  966. else {
  967. echo "<table class='table table-sm border w-100'>";
  968. foreach($_data as $k => $v) {
  969. echo "<tr>";
  970. echo "<td><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
  971. echo "<td>";
  972. if(is_object($v)) {
  973. parseRender($v);
  974. }
  975. elseif(is_array($v)) {
  976. foreach($v as $k2 => $v2) {
  977. parseRender($v2);
  978. }
  979. }
  980. else {
  981. echo $v;
  982. }
  983. echo "</td>";
  984. echo "</tr>";
  985. }
  986. echo "</table>";
  987. }
  988. }
  989. }
  990. }
  991. if(!function_exists('mask_string')) {
  992. function mask_string($string, $mask = "*", $lengthFromLastCharacter = null) {
  993. if(!$lengthFromLastCharacter){
  994. return str_repeat($mask, strlen($string));
  995. }
  996. $firstCharacters = substr($string, ($lengthFromLastCharacter));
  997. $lastCharacters = substr($string, -$lengthFromLastCharacter);
  998. $maskedFirstCharacters = str_repeat($mask, strlen($firstCharacters));
  999. return $maskedFirstCharacters . $lastCharacters;
  1000. }
  1001. }