helpers.php 27 KB

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