helpers.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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_time')) {
  151. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  152. if(!$value || empty($value)) return $default;
  153. try {
  154. if($includeTime) {
  155. $realTimezone = 'US/Eastern';
  156. $date = new DateTime($value);
  157. $date->setTimezone(new DateTimeZone($realTimezone));
  158. return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
  159. }
  160. else {
  161. $result = strtotime($value);
  162. if($long_year){
  163. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  164. }else{
  165. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  166. }
  167. return $result;
  168. }
  169. }
  170. catch (Exception $e) {
  171. return $value;
  172. }
  173. }
  174. }
  175. if(!function_exists('friendly_date_month_year')) {
  176. function friendly_date_month_year($value, $default = '-', $long_year=true) {
  177. if(!$value || empty($value)) return $default;
  178. try {
  179. $result = strtotime($value);
  180. if($long_year){
  181. $result = date("m/Y", $result);
  182. }else{
  183. $result = date("m/y", $result);
  184. }
  185. return $result;
  186. }
  187. catch (Exception $e) {
  188. return $value;
  189. }
  190. }
  191. }
  192. if(!function_exists('friendlier_date_time')) {
  193. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  194. return friendly_date_time($value, $includeTime, $default);
  195. }
  196. }
  197. if(!function_exists('friendly_date_time_short')) {
  198. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  199. if(!$value || empty($value)) return $default;
  200. try {
  201. $result = strtotime($value);
  202. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  203. return $result;
  204. }
  205. catch (Exception $e) {
  206. return $value;
  207. }
  208. }
  209. }
  210. if(!function_exists('friendly_date_time_short_with_tz')) {
  211. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  212. if(!$value || empty($value)) return $default;
  213. try {
  214. $realTimezone = resolve_timezone($tz);
  215. $date = new DateTime($value);
  216. $date->setTimezone(new DateTimeZone($realTimezone));
  217. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  218. }
  219. catch (Exception $e) {
  220. return $e->getMessage();
  221. }
  222. }
  223. }
  224. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  225. function friendly_date_time_short_with_tz_from_timestamp($value, $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, h:iA");
  232. }
  233. catch (Exception $e) {
  234. return $e->getMessage();
  235. }
  236. }
  237. }
  238. if(!function_exists('postgres_date_time_short_with_tz')) {
  239. function postgres_date_time_short_with_tz($value, $includeTime = true, $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("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  246. }
  247. catch (Exception $e) {
  248. return $e->getMessage();
  249. }
  250. }
  251. }
  252. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  253. function postgres_date_time_short_with_tz_from_timestamp($value, $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 h:i:s");
  260. }
  261. catch (Exception $e) {
  262. return $e->getMessage();
  263. }
  264. }
  265. }
  266. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  267. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  268. if(!$value || empty($value)) return $default;
  269. try {
  270. $value = (floor($value / 1000));
  271. $realTimezone = resolve_timezone($tz);
  272. $date = new DateTime("@$value");
  273. $date->setTimezone(new DateTimeZone($realTimezone));
  274. return $date->format("m/d/y, h:iA");
  275. }
  276. catch (Exception $e) {
  277. return $e->getMessage();
  278. }
  279. }
  280. }
  281. if(!function_exists('friendly_date_short_with_tz_from_timestamp_divide1000')) {
  282. function friendly_date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  283. if(!$value || empty($value)) return $default;
  284. try {
  285. $value = (floor($value / 1000));
  286. $realTimezone = resolve_timezone($tz);
  287. $date = new DateTime("@$value");
  288. $date->setTimezone(new DateTimeZone($realTimezone));
  289. return $date->format("m/d/y");
  290. }
  291. catch (Exception $e) {
  292. return $e->getMessage();
  293. }
  294. }
  295. }
  296. if(!function_exists('date_short_with_tz_from_timestamp_divide1000')) {
  297. function date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  298. if(!$value || empty($value)) return $default;
  299. try {
  300. $value = (floor($value / 1000));
  301. $realTimezone = resolve_timezone($tz);
  302. $date = new DateTime("@$value");
  303. $date->setTimezone(new DateTimeZone($realTimezone));
  304. return $date->format("Y-m-d");
  305. }
  306. catch (Exception $e) {
  307. return $e->getMessage();
  308. }
  309. }
  310. }
  311. if(!function_exists('friendly_date')) {
  312. function friendly_date($value) {
  313. if(!$value || empty($value)) return '';
  314. try {
  315. $result = strtotime($value);
  316. $result = date("m/d/Y", $result);
  317. return $result;
  318. }
  319. catch (Exception $e) {
  320. return $value;
  321. }
  322. }
  323. }
  324. if(!function_exists('friendly_date_short')) {
  325. function friendly_date_short($value) {
  326. if(!$value || empty($value)) return '';
  327. try {
  328. $result = strtotime($value);
  329. $result = date("m/d/y", $result);
  330. return $result;
  331. }
  332. catch (Exception $e) {
  333. return $value;
  334. }
  335. }
  336. }
  337. if(!function_exists('friendly_date_month_year')) {
  338. function friendly_date_month_year($value) {
  339. if(!$value || empty($value)) return '';
  340. try {
  341. $result = strtotime($value);
  342. $result = date("M Y", $result);
  343. return $result;
  344. }
  345. catch (Exception $e) {
  346. return $value;
  347. }
  348. }
  349. }
  350. if(!function_exists('friendlier_date')) {
  351. function friendlier_date($value) {
  352. if(!$value || empty($value)) return '';
  353. try {
  354. $result = strtotime($value);
  355. $result = date("m/d/Y", $result);
  356. return $result;
  357. }
  358. catch (Exception $e) {
  359. return $value;
  360. }
  361. }
  362. }
  363. if(!function_exists('relative_friendly_date')) {
  364. function relative_friendly_date($value) {
  365. if(!$value || empty($value)) return '';
  366. try {
  367. $result = date_diff(date_create($value), date_create('now'))->days;
  368. if ($result > 30) $result = date('F Y', strtotime($value));
  369. else if ($result > 1) $result .= ' days ago';
  370. else if ($result === 1) $result = 'Yesterday';
  371. else if ($result === 0) $result = 'Today';
  372. return $result;
  373. }
  374. catch (Exception $e) {
  375. return $value;
  376. }
  377. }
  378. }
  379. if(!function_exists('unfriendly_date')) {
  380. function unfriendly_date($value) {
  381. if(!$value || empty($value)) return '';
  382. try {
  383. $result = strtotime($value);
  384. $result = date("Y-m-d", $result);
  385. return $result;
  386. }
  387. catch (Exception $e) {
  388. return $value;
  389. }
  390. }
  391. }
  392. if(!function_exists('friendly_time')) {
  393. function friendly_time($value, $default = '-') {
  394. if(!$value || empty($value)) return $default;
  395. try {
  396. $result = strtotime($value);
  397. $result = date("h:i a", $result);
  398. return $result;
  399. }
  400. catch (Exception $e) {
  401. return $value;
  402. }
  403. }
  404. }
  405. if(!function_exists('military_time')) {
  406. function military_time($value, $tz='UTC', $default = '-') {
  407. if(!$value || empty($value)) return $default;
  408. try {
  409. $realTimezone = resolve_timezone($tz);
  410. $date = new DateTime($value);
  411. $date->setTimezone(new DateTimeZone($realTimezone));
  412. return $date->format("H:i");
  413. }
  414. catch (Exception $e) {
  415. return $value;
  416. }
  417. }
  418. }
  419. if(!function_exists('resolve_timezone')) {
  420. function resolve_timezone($value) {
  421. try {
  422. switch ($value) {
  423. case 'ALASKA': {
  424. return 'US/Alaska';
  425. }
  426. case 'CENTRAL': {
  427. return 'US/Central';
  428. }
  429. case 'EASTERN': {
  430. return 'US/Eastern';
  431. }
  432. case 'HAWAII': {
  433. return 'US/Hawaii';
  434. }
  435. case 'MOUNTAIN': {
  436. return 'US/Mountain';
  437. }
  438. case 'PACIFIC': {
  439. return 'US/Pacific';
  440. }
  441. case 'PUERTO_RICO': {
  442. return 'America/Puerto_Rico';
  443. }
  444. case 'UTC': {
  445. return 'UTC';
  446. }
  447. }
  448. }
  449. catch (Exception $e) {
  450. return $value;
  451. }
  452. }
  453. }
  454. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  455. // echo $date->format('Y-m-d H:i:sP') . "\n";
  456. if(!function_exists('friendly_month')) {
  457. function friendly_month($value) {
  458. if(!$value || empty($value)) return "-";
  459. try {
  460. $result = strtotime($value);
  461. $result = date("F o", $result);
  462. return $result;
  463. }
  464. catch (Exception $e) {
  465. return $value;
  466. }
  467. }
  468. }
  469. if(!function_exists('day_part_from_date')) {
  470. function day_part_from_date($value) {
  471. if(!$value || empty($value)) return "-";
  472. try {
  473. $result = strtotime($value);
  474. $result = date("d", $result);
  475. return $result;
  476. }
  477. catch (Exception $e) {
  478. return $value;
  479. }
  480. }
  481. }
  482. if(!function_exists('month_part_from_date')) {
  483. function month_part_from_date($value) {
  484. if(!$value || empty($value)) return "-";
  485. try {
  486. $result = strtotime($value);
  487. $result = date("m", $result);
  488. return $result;
  489. }
  490. catch (Exception $e) {
  491. return $value;
  492. }
  493. }
  494. }
  495. if(!function_exists('year_part_from_date')) {
  496. function year_part_from_date($value) {
  497. if(!$value || empty($value)) return "-";
  498. try {
  499. $result = strtotime($value);
  500. $result = date("Y", $result);
  501. return $result;
  502. }
  503. catch (Exception $e) {
  504. return $value;
  505. }
  506. }
  507. }
  508. if(!function_exists('friendly_money')){
  509. function friendly_money($value){
  510. return number_format((float)$value, 2, '.', '');
  511. }
  512. }
  513. if(!function_exists('time_in_hrminsec')) {
  514. function time_in_hrminsec($value, $default = '-') {
  515. if(!$value || empty($value)) return $default;
  516. $value = intval($value);
  517. $minutes = intval($value / 60);
  518. $seconds = $value % 60;
  519. $hours = 0;
  520. if($minutes >= 60) {
  521. $hours = intval($minutes / 60);
  522. $minutes = $minutes % 60;
  523. }
  524. $output = [];
  525. if($hours > 0) {
  526. $output[] = "{$hours}h";
  527. }
  528. if($minutes > 0) {
  529. $output[] = "{$minutes}m";
  530. }
  531. if($seconds > 0) {
  532. $output[] = "{$seconds}s";
  533. }
  534. return implode(" ", $output);
  535. }
  536. }
  537. if(!function_exists('sanitize_field_name')) {
  538. function sanitize_field_name($name) {
  539. $result = strtolower($name);
  540. return preg_replace("/[^0-9a-z]/i", "_", $result);
  541. }
  542. }
  543. if(!function_exists('sanitize_state_name')) {
  544. function sanitize_state_name($name) {
  545. $result = strtolower($name);
  546. return ucwords(preg_replace("/_/i", " ", $result));
  547. }
  548. }
  549. if(!function_exists('renderNoteTemplate')) {
  550. function renderNoteTemplate($template, $topLevel)
  551. {
  552. echo
  553. '<div class="note-template-item" ' .
  554. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  555. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  556. '>' .
  557. '<div class="note-template-text d-flex align-items-center">' .
  558. '<span class="label">' .
  559. '<input type="checkbox" />' .
  560. '<span>' . $template->text . '</span>' .
  561. '</span>';
  562. if (isset($template->type) && $template->type === 'plus-minus') {
  563. echo '<div class="ml-auto mr-2 text-nowrap">';
  564. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  565. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  566. echo '</div>';
  567. }
  568. echo '</div>';
  569. if (isset($template->children) && count($template->children)) {
  570. echo '<i class="fa fa-chevron-right has-children"></i>';
  571. echo '<div class="note-template-children">';
  572. foreach ($template->children as $t) {
  573. renderNoteTemplate($t, false);
  574. }
  575. echo '</div>';
  576. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  577. echo '<i class="fa fa-chevron-right has-children"></i>';
  578. echo '<div class="note-template-children">';
  579. if ($template->type === 'alpha') {
  580. echo '<textarea class="form-control form-control-sm"></textarea>';
  581. } else {
  582. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  583. }
  584. echo '</div>';
  585. }
  586. echo '</div>';
  587. }
  588. }
  589. if(!function_exists('renderNoteTemplates')) {
  590. function renderNoteTemplates($path)
  591. {
  592. $templates = json_decode(file_get_contents($path));
  593. foreach ($templates->templates as $template) {
  594. renderNoteTemplate($template, true);
  595. }
  596. }
  597. }
  598. if(!function_exists('renderNoteExamTemplates')) {
  599. function renderNoteExamTemplates($parentPath, $childPath)
  600. {
  601. $templates = json_decode(file_get_contents($parentPath));
  602. $templates = $templates->templates;
  603. // override as needed with what is in template set
  604. if(file_exists($childPath)) {
  605. $orTemplates = json_decode(file_get_contents($parentPath));
  606. $orTemplates = $orTemplates->templates;
  607. for ($i = 0; $i < count($templates); $i++) {
  608. for ($j = 0; $j < count($orTemplates); $j++) {
  609. if($templates[$i]->text === $orTemplates[$j]->text) {
  610. $templates[$i] = $orTemplates[$j];
  611. }
  612. }
  613. }
  614. }
  615. foreach ($templates as $template) {
  616. renderNoteTemplate($template, true);
  617. }
  618. }
  619. }
  620. if(!function_exists('getVal')) {
  621. function getVal($object, $prop)
  622. {
  623. if (isset($object->$prop)) {
  624. return $object->$prop;
  625. } else {
  626. return '';
  627. }
  628. }
  629. }
  630. if(!function_exists('appTZtoPHPTZ')) {
  631. function appTZtoPHPTZ($_timezone)
  632. {
  633. switch ($_timezone) {
  634. case 'ALASKA':
  635. $timezone = "US/Alaska";
  636. break;
  637. case 'CENTRAL':
  638. $timezone = "US/Central";
  639. break;
  640. case 'HAWAII':
  641. $timezone = "US/Hawaii";
  642. break;
  643. case 'MOUNTAIN':
  644. $timezone = "US/Mountain";
  645. break;
  646. case 'PACIFIC':
  647. $timezone = "US/Pacific";
  648. break;
  649. case 'PUERTO_RICO':
  650. $timezone = "America/Puerto_Rico";
  651. break;
  652. default:
  653. $timezone = "US/Eastern";
  654. break;
  655. }
  656. return $timezone;
  657. }
  658. }
  659. if(!function_exists('convertToTimezone')) {
  660. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  661. {
  662. if (!$_dateTime) return $_dateTime;
  663. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  664. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  665. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  666. }
  667. }
  668. if(!function_exists('minutes_to_hhmm')) {
  669. function minutes_to_hhmm($_minutes)
  670. {
  671. $h = intval(floor($_minutes / 60));
  672. $m = $_minutes;
  673. if($h > 0) {
  674. $m = $_minutes - $h * 60;
  675. }
  676. $h = ($h < 10 ? '0' : '') . $h;
  677. $m = ($m < 10 ? '0' : '') . $m;
  678. return $h . ':' . $m;
  679. }
  680. }
  681. if(!function_exists('vsValue')) {
  682. function vsValue($_v, $patient = null, $_direct = false)
  683. {
  684. if ($_direct) {
  685. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  686. }
  687. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  688. }
  689. }
  690. if(!function_exists('vsElement')) {
  691. function vsElement($_v, $type, $name, $patient, $class = '')
  692. {
  693. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  694. 'name="' . $name . '" ' .
  695. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  696. }
  697. }
  698. if(!function_exists('vsRoElement')) {
  699. function vsRoElement($_v, $type, $name, $patient, $class = '')
  700. {
  701. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  702. 'name="' . $name . '" ' .
  703. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  704. }
  705. }
  706. if(!function_exists('str_compact')) {
  707. function str_compact($_str)
  708. {
  709. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  710. }
  711. }
  712. if(!function_exists('noteMethodDisplay')) {
  713. function noteMethodDisplay($method)
  714. {
  715. if($method === 'IN_CLINIC') return 'In-Clinic';
  716. $method = str_replace('_', ' ', $method);
  717. return ucwords(strtolower($method));
  718. }
  719. }
  720. if(!function_exists('friendly_timezone')) {
  721. function friendly_timezone($tz) {
  722. $map = [
  723. 'EASTERN' => 'EST',
  724. 'CENTRAL' => 'CST',
  725. 'MOUNTAIN' => 'MST',
  726. 'PACIFIC' => 'PST',
  727. 'ALASKA' => 'AST',
  728. 'HAWAII' => 'HST',
  729. 'PUERTO_RICO' => 'PR'
  730. ];
  731. return $map[$tz] ?? str_replace("_", " ", $tz);
  732. }
  733. }
  734. if(!function_exists('segment_template_summary_value_display')) {
  735. function segment_template_summary_value_display($value, $default='________', $class = '') {
  736. if($value && strlen($value)) return '<span class="segment-template-summary-value '.$class.'">' . $value . '</span>';
  737. return $default;
  738. }
  739. }
  740. if(!function_exists('starts_with')) {
  741. function starts_with($haystack, $needle) {
  742. $length = strlen($needle);
  743. return substr($haystack, 0, $length) === $needle;
  744. }
  745. }
  746. if(!function_exists('get_doc_templates')){
  747. function get_doc_templates(){
  748. $basePath = 'views/document-templates-generic';
  749. $dir = opendir(resource_path($basePath));
  750. $templates = [];
  751. if($dir) {
  752. while ( $entry = readdir($dir) ) {
  753. if($entry !== '.' && $entry !== '..' && is_dir(resource_path($basePath . '/' . $entry))) {
  754. $spec = json_decode(file_get_contents(resource_path($basePath . '/' . $entry) . '/spec.json'));
  755. if(isset($spec->active) && $spec->active) {
  756. $html = file_get_contents(resource_path($basePath . '/' . $entry) . '/content.blade.php');
  757. // get fields
  758. $proVariables = [];
  759. $htmlDisplay = preg_replace_callback(
  760. '/{([^}]+)}/',
  761. function ($match) use (&$proVariables) {
  762. $token = $match[1];
  763. $replacement = '';
  764. if(strpos($token, '.') === FALSE) {
  765. $adminField = false;
  766. if($token[0] === '@') {
  767. // $token = substr($token, 1);
  768. $adminField = true;
  769. }
  770. switch ($token) {
  771. case 'TODAY':
  772. $replacement = date('m/d/Y');
  773. break;
  774. default:
  775. $replacement = '<span class="text-info document-variable" data-variable="' . $token . '">' .
  776. ($token[0] === '@' ? substr($token, 1) : $token) .
  777. '</span>';
  778. if (!isset($proVariables[$token])) {
  779. $proVariables[$token] = [
  780. 'token' => $token,
  781. 'adminField' => $adminField
  782. ];
  783. }
  784. break;
  785. }
  786. }
  787. return $replacement;
  788. },
  789. $html
  790. );
  791. $templates[] = [
  792. "name" => $entry,
  793. "title" => $spec->title,
  794. "html" => $html,
  795. "htmlDisplay" => $htmlDisplay,
  796. "proVariables" => $proVariables
  797. ];
  798. }
  799. }
  800. }
  801. }
  802. closedir($dir);
  803. // sort by document name
  804. $names = array_column($templates, 'title');
  805. array_multisort($names, SORT_ASC, $templates);
  806. return $templates;
  807. }
  808. }
  809. if(!function_exists('format_phone_number')) {
  810. function format_phone_number($number, $default = '-') {
  811. if(empty($number)) return $default;
  812. return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
  813. }
  814. }
  815. if(!function_exists('format_number')) {
  816. function format_number($number, $places = 2) {
  817. return number_format((float)$number, $places, '.', '');
  818. }
  819. }