helpers.php 30 KB

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