helpers.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709
  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 Soundasleep\Html2Text as Html2Text;
  14. if(!function_exists('toFeetAndInches')) {
  15. function toFeetAndInches($value) {
  16. if(!$value) return '-';
  17. $value = round($value);
  18. $ft = round(floor($value / 12));
  19. $in = $value % 12;
  20. return "$ft ft. $in in.";
  21. }
  22. }
  23. if(!function_exists('feetFromInches')) {
  24. function feetFromInches($value) {
  25. if(!$value) return 0;
  26. $value = round($value);
  27. return round(floor($value / 12));
  28. }
  29. }
  30. if(!function_exists('inchesAfterFeetFromInches')) {
  31. function inchesAfterFeetFromInches($value) {
  32. if(!$value) return 0;
  33. $value = round($value);
  34. return round($value % 12);
  35. }
  36. }
  37. if(!function_exists('genericBills')) {
  38. function genericBills(Pro $performerPro, $patient, $entityType, $entityUid) {
  39. $genericBills = Bill::where('bill_service_type', 'GENERIC');
  40. if($performerPro->pro_type !== 'ADMIN') {
  41. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  42. }
  43. if($patient) {
  44. $genericBills = $genericBills->where('client_id', $patient->id);
  45. }
  46. if($entityType && $entityUid) {
  47. $genericBills = $genericBills
  48. ->where('generic_target_entity_type', $entityType)
  49. ->where('generic_target_entity_uid', $entityUid);
  50. }
  51. return $genericBills->orderBy('created_at', 'DESC')->get();
  52. }
  53. }
  54. if(!function_exists('hasActiveGenericBill')) {
  55. function hasActiveGenericBill(Pro $performerPro, $patient, $entityType, $entityUid) {
  56. $genericBills = Bill::where('bill_service_type', 'GENERIC')->where('is_cancelled', false);
  57. if($performerPro->pro_type !== 'ADMIN') {
  58. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  59. }
  60. if($patient) {
  61. $genericBills = $genericBills->where('client_id', $patient->id);
  62. }
  63. if($entityType && $entityUid) {
  64. $genericBills = $genericBills
  65. ->where('generic_target_entity_type', $entityType)
  66. ->where('generic_target_entity_uid', $entityUid);
  67. }
  68. return $genericBills->count() > 0;
  69. }
  70. }
  71. if(!function_exists('queryLineExcept')) {
  72. function queryLineExcept($except = []) {
  73. $params = request()->all();
  74. $final = [];
  75. foreach ($params as $k => $v) {
  76. if(in_array($k, $except) === FALSE) {
  77. $final[] = "$k=" . urlencode($v);
  78. }
  79. }
  80. return implode('&', $final);
  81. }
  82. }
  83. if(!function_exists('sortColumnHead')) {
  84. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  85. $currentSortKey = request()->input('sort');
  86. $currentDir = request()->input('dir');
  87. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  88. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  89. if($currentSortKey === $sortKey) {
  90. if($currentDir === 'ASC') {
  91. echo "&nbsp;&nbsp;↑";
  92. }
  93. elseif($currentDir === 'DESC') {
  94. echo "&nbsp;&nbsp;↓";
  95. }
  96. }
  97. }
  98. }
  99. if(!function_exists('html2Text')) {
  100. function html2Text($old, $new){
  101. }
  102. }
  103. if(!function_exists('diff')) {
  104. function diff($old, $new){
  105. // return Diff::toHTML(Diff::compare($old, $new));
  106. }
  107. }
  108. if(!function_exists('get_current_session')) {
  109. function get_current_session(){
  110. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  111. }
  112. }
  113. if(!function_exists('friendly_date_time')) {
  114. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  115. if(!$value || empty($value)) return $default;
  116. try {
  117. $result = strtotime($value);
  118. if($long_year){
  119. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  120. }else{
  121. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  122. }
  123. return $result;
  124. }
  125. catch (Exception $e) {
  126. return $value;
  127. }
  128. }
  129. }
  130. if(!function_exists('friendlier_date_time')) {
  131. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  132. if(!$value || empty($value)) return $default;
  133. try {
  134. $result = strtotime($value);
  135. $result = date("m/d/Y" . ($includeTime ? ", h:i a" : ""), $result);
  136. return $result;
  137. }
  138. catch (Exception $e) {
  139. return $value;
  140. }
  141. }
  142. }
  143. if(!function_exists('friendly_date_time_short')) {
  144. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  145. if(!$value || empty($value)) return $default;
  146. try {
  147. $result = strtotime($value);
  148. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  149. return $result;
  150. }
  151. catch (Exception $e) {
  152. return $value;
  153. }
  154. }
  155. }
  156. if(!function_exists('friendly_date_time_short_with_tz')) {
  157. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  158. if(!$value || empty($value)) return $default;
  159. try {
  160. $realTimezone = resolve_timezone($tz);
  161. $date = new DateTime($value);
  162. $date->setTimezone(new DateTimeZone($realTimezone));
  163. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  164. }
  165. catch (Exception $e) {
  166. return $e->getMessage();
  167. }
  168. }
  169. }
  170. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  171. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  172. if(!$value || empty($value)) return $default;
  173. try {
  174. $realTimezone = resolve_timezone($tz);
  175. $date = new DateTime("@$value");
  176. $date->setTimezone(new DateTimeZone($realTimezone));
  177. return $date->format("m/d/y, h:iA");
  178. }
  179. catch (Exception $e) {
  180. return $e->getMessage();
  181. }
  182. }
  183. }
  184. if(!function_exists('postgres_date_time_short_with_tz')) {
  185. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  186. if(!$value || empty($value)) return $default;
  187. try {
  188. $realTimezone = resolve_timezone($tz);
  189. $date = new DateTime($value);
  190. $date->setTimezone(new DateTimeZone($realTimezone));
  191. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  192. }
  193. catch (Exception $e) {
  194. return $e->getMessage();
  195. }
  196. }
  197. }
  198. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  199. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  200. if(!$value || empty($value)) return $default;
  201. try {
  202. $realTimezone = resolve_timezone($tz);
  203. $date = new DateTime("@$value");
  204. $date->setTimezone(new DateTimeZone($realTimezone));
  205. return $date->format("Y-m-d h:i:s");
  206. }
  207. catch (Exception $e) {
  208. return $e->getMessage();
  209. }
  210. }
  211. }
  212. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  213. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  214. if(!$value || empty($value)) return $default;
  215. try {
  216. $value = (floor($value / 1000));
  217. $realTimezone = resolve_timezone($tz);
  218. $date = new DateTime("@$value");
  219. $date->setTimezone(new DateTimeZone($realTimezone));
  220. return $date->format("m/d/y, h:iA");
  221. }
  222. catch (Exception $e) {
  223. return $e->getMessage();
  224. }
  225. }
  226. }
  227. if(!function_exists('friendly_date_short_with_tz_from_timestamp_divide1000')) {
  228. function friendly_date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  229. if(!$value || empty($value)) return $default;
  230. try {
  231. $value = (floor($value / 1000));
  232. $realTimezone = resolve_timezone($tz);
  233. $date = new DateTime("@$value");
  234. $date->setTimezone(new DateTimeZone($realTimezone));
  235. return $date->format("m/d/y");
  236. }
  237. catch (Exception $e) {
  238. return $e->getMessage();
  239. }
  240. }
  241. }
  242. if(!function_exists('friendly_date')) {
  243. function friendly_date($value) {
  244. if(!$value || empty($value)) return '';
  245. try {
  246. $result = strtotime($value);
  247. $result = date("m/d/Y", $result);
  248. return $result;
  249. }
  250. catch (Exception $e) {
  251. return $value;
  252. }
  253. }
  254. }
  255. if(!function_exists('friendly_date_month_year')) {
  256. function friendly_date_month_year($value) {
  257. if(!$value || empty($value)) return '';
  258. try {
  259. $result = strtotime($value);
  260. $result = date("M Y", $result);
  261. return $result;
  262. }
  263. catch (Exception $e) {
  264. return $value;
  265. }
  266. }
  267. }
  268. if(!function_exists('friendlier_date')) {
  269. function friendlier_date($value) {
  270. if(!$value || empty($value)) return '';
  271. try {
  272. $result = strtotime($value);
  273. $result = date("m/d/Y", $result);
  274. return $result;
  275. }
  276. catch (Exception $e) {
  277. return $value;
  278. }
  279. }
  280. }
  281. if(!function_exists('unfriendly_date')) {
  282. function unfriendly_date($value) {
  283. if(!$value || empty($value)) return '';
  284. try {
  285. $result = strtotime($value);
  286. $result = date("Y-m-d", $result);
  287. return $result;
  288. }
  289. catch (Exception $e) {
  290. return $value;
  291. }
  292. }
  293. }
  294. if(!function_exists('friendly_time')) {
  295. function friendly_time($value, $default = '-') {
  296. if(!$value || empty($value)) return $default;
  297. try {
  298. $result = strtotime($value);
  299. $result = date("h:i a", $result);
  300. return $result;
  301. }
  302. catch (Exception $e) {
  303. return $value;
  304. }
  305. }
  306. }
  307. if(!function_exists('military_time')) {
  308. function military_time($value, $tz='UTC', $default = '-') {
  309. if(!$value || empty($value)) return $default;
  310. try {
  311. $realTimezone = resolve_timezone($tz);
  312. $date = new DateTime($value);
  313. $date->setTimezone(new DateTimeZone($realTimezone));
  314. return $date->format("H:i");
  315. }
  316. catch (Exception $e) {
  317. return $value;
  318. }
  319. }
  320. }
  321. if(!function_exists('resolve_timezone')) {
  322. function resolve_timezone($value) {
  323. try {
  324. switch ($value) {
  325. case 'ALASKA': {
  326. return 'US/Alaska';
  327. }
  328. case 'CENTRAL': {
  329. return 'US/Central';
  330. }
  331. case 'EASTERN': {
  332. return 'US/Eastern';
  333. }
  334. case 'HAWAII': {
  335. return 'US/Hawaii';
  336. }
  337. case 'MOUNTAIN': {
  338. return 'US/Mountain';
  339. }
  340. case 'PACIFIC': {
  341. return 'US/Pacific';
  342. }
  343. case 'PUERTO_RICO': {
  344. return 'America/Puerto_Rico';
  345. }
  346. case 'UTC': {
  347. return 'UTC';
  348. }
  349. }
  350. }
  351. catch (Exception $e) {
  352. return $value;
  353. }
  354. }
  355. }
  356. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  357. // echo $date->format('Y-m-d H:i:sP') . "\n";
  358. if(!function_exists('friendly_month')) {
  359. function friendly_month($value) {
  360. if(!$value || empty($value)) return "-";
  361. try {
  362. $result = strtotime($value);
  363. $result = date("F o", $result);
  364. return $result;
  365. }
  366. catch (Exception $e) {
  367. return $value;
  368. }
  369. }
  370. }
  371. if(!function_exists('day_part_from_date')) {
  372. function day_part_from_date($value) {
  373. if(!$value || empty($value)) return "-";
  374. try {
  375. $result = strtotime($value);
  376. $result = date("d", $result);
  377. return $result;
  378. }
  379. catch (Exception $e) {
  380. return $value;
  381. }
  382. }
  383. }
  384. if(!function_exists('month_part_from_date')) {
  385. function month_part_from_date($value) {
  386. if(!$value || empty($value)) return "-";
  387. try {
  388. $result = strtotime($value);
  389. $result = date("m", $result);
  390. return $result;
  391. }
  392. catch (Exception $e) {
  393. return $value;
  394. }
  395. }
  396. }
  397. if(!function_exists('year_part_from_date')) {
  398. function year_part_from_date($value) {
  399. if(!$value || empty($value)) return "-";
  400. try {
  401. $result = strtotime($value);
  402. $result = date("Y", $result);
  403. return $result;
  404. }
  405. catch (Exception $e) {
  406. return $value;
  407. }
  408. }
  409. }
  410. if(!function_exists('friendly_money')){
  411. function friendly_money($value){
  412. return number_format((float)$value, 2, '.', '');
  413. }
  414. }
  415. if(!function_exists('time_in_hrminsec')) {
  416. function time_in_hrminsec($value, $default = '-') {
  417. if(!$value || empty($value)) return $default;
  418. $value = intval($value);
  419. $minutes = intval($value / 60);
  420. $seconds = $value % 60;
  421. $hours = 0;
  422. if($minutes >= 60) {
  423. $hours = intval($minutes / 60);
  424. $minutes = $minutes % 60;
  425. }
  426. $output = [];
  427. if($hours > 0) {
  428. $output[] = "{$hours}h";
  429. }
  430. if($minutes > 0) {
  431. $output[] = "{$minutes}m";
  432. }
  433. if($seconds > 0) {
  434. $output[] = "{$seconds}s";
  435. }
  436. return implode(" ", $output);
  437. }
  438. }
  439. if(!function_exists('sanitize_field_name')) {
  440. function sanitize_field_name($name) {
  441. $result = strtolower($name);
  442. return preg_replace("/[^0-9a-z]/i", "_", $result);
  443. }
  444. }
  445. if(!function_exists('sanitize_state_name')) {
  446. function sanitize_state_name($name) {
  447. $result = strtolower($name);
  448. return ucwords(preg_replace("/_/i", " ", $result));
  449. }
  450. }
  451. if(!function_exists('renderNoteTemplate')) {
  452. function renderNoteTemplate($template, $topLevel)
  453. {
  454. echo
  455. '<div class="note-template-item" ' .
  456. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  457. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  458. '>' .
  459. '<div class="note-template-text d-flex align-items-center">' .
  460. '<span class="label">' .
  461. '<input type="checkbox" />' .
  462. '<span>' . $template->text . '</span>' .
  463. '</span>';
  464. if (isset($template->type) && $template->type === 'plus-minus') {
  465. echo '<div class="ml-auto mr-2 text-nowrap">';
  466. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  467. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  468. echo '</div>';
  469. }
  470. echo '</div>';
  471. if (isset($template->children) && count($template->children)) {
  472. echo '<i class="fa fa-chevron-right has-children"></i>';
  473. echo '<div class="note-template-children">';
  474. foreach ($template->children as $t) {
  475. renderNoteTemplate($t, false);
  476. }
  477. echo '</div>';
  478. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  479. echo '<i class="fa fa-chevron-right has-children"></i>';
  480. echo '<div class="note-template-children">';
  481. if ($template->type === 'alpha') {
  482. echo '<textarea class="form-control form-control-sm"></textarea>';
  483. } else {
  484. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  485. }
  486. echo '</div>';
  487. }
  488. echo '</div>';
  489. }
  490. }
  491. if(!function_exists('renderNoteTemplates')) {
  492. function renderNoteTemplates($path)
  493. {
  494. $templates = json_decode(file_get_contents($path));
  495. foreach ($templates->templates as $template) {
  496. renderNoteTemplate($template, true);
  497. }
  498. }
  499. }
  500. if(!function_exists('renderNoteExamTemplates')) {
  501. function renderNoteExamTemplates($parentPath, $childPath)
  502. {
  503. $templates = json_decode(file_get_contents($parentPath));
  504. $templates = $templates->templates;
  505. // override as needed with what is in template set
  506. if(file_exists($childPath)) {
  507. $orTemplates = json_decode(file_get_contents($parentPath));
  508. $orTemplates = $orTemplates->templates;
  509. for ($i = 0; $i < count($templates); $i++) {
  510. for ($j = 0; $j < count($orTemplates); $j++) {
  511. if($templates[$i]->text === $orTemplates[$j]->text) {
  512. $templates[$i] = $orTemplates[$j];
  513. }
  514. }
  515. }
  516. }
  517. foreach ($templates as $template) {
  518. renderNoteTemplate($template, true);
  519. }
  520. }
  521. }
  522. if(!function_exists('getVal')) {
  523. function getVal($object, $prop)
  524. {
  525. if (isset($object->$prop)) {
  526. return $object->$prop;
  527. } else {
  528. return '';
  529. }
  530. }
  531. }
  532. if(!function_exists('appTZtoPHPTZ')) {
  533. function appTZtoPHPTZ($_timezone)
  534. {
  535. switch ($_timezone) {
  536. case 'ALASKA':
  537. $timezone = "US/Alaska";
  538. break;
  539. case 'CENTRAL':
  540. $timezone = "US/Central";
  541. break;
  542. case 'HAWAII':
  543. $timezone = "US/Hawaii";
  544. break;
  545. case 'MOUNTAIN':
  546. $timezone = "US/Mountain";
  547. break;
  548. case 'PACIFIC':
  549. $timezone = "US/Pacific";
  550. break;
  551. case 'PUERTO_RICO':
  552. $timezone = "America/Puerto_Rico";
  553. break;
  554. default:
  555. $timezone = "US/Eastern";
  556. break;
  557. }
  558. return $timezone;
  559. }
  560. }
  561. if(!function_exists('convertToTimezone')) {
  562. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  563. {
  564. if (!$_dateTime) return $_dateTime;
  565. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  566. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  567. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  568. }
  569. }
  570. if(!function_exists('minutes_to_hhmm')) {
  571. function minutes_to_hhmm($_minutes)
  572. {
  573. $h = intval(floor($_minutes / 60));
  574. $m = $_minutes;
  575. if($h > 0) {
  576. $m = $_minutes - $h * 60;
  577. }
  578. $h = ($h < 10 ? '0' : '') . $h;
  579. $m = ($m < 10 ? '0' : '') . $m;
  580. return $h . ':' . $m;
  581. }
  582. }
  583. if(!function_exists('vsValue')) {
  584. function vsValue($_v, $patient = null, $_direct = false)
  585. {
  586. if ($_direct) {
  587. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  588. }
  589. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  590. }
  591. }
  592. if(!function_exists('vsElement')) {
  593. function vsElement($_v, $type, $name, $patient)
  594. {
  595. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0" ' .
  596. 'name="' . $name . '" ' .
  597. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  598. }
  599. }
  600. if(!function_exists('vsRoElement')) {
  601. function vsRoElement($_v, $type, $name, $patient)
  602. {
  603. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0" ' .
  604. 'name="' . $name . '" ' .
  605. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  606. }
  607. }
  608. if(!function_exists('str_compact')) {
  609. function str_compact($_str)
  610. {
  611. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  612. }
  613. }
  614. if(!function_exists('noteMethodDisplay')) {
  615. function noteMethodDisplay($method)
  616. {
  617. if($method === 'IN_CLINIC') return 'In-Clinic';
  618. $method = str_replace('_', ' ', $method);
  619. return ucwords(strtolower($method));
  620. }
  621. }
  622. if(!function_exists('friendly_timezone')) {
  623. function friendly_timezone($tz) {
  624. $map = [
  625. 'EASTERN' => 'EST',
  626. 'CENTRAL' => 'CST',
  627. 'MOUNTAIN' => 'MST',
  628. 'PACIFIC' => 'PST',
  629. 'ALASKA' => 'AST',
  630. 'HAWAII' => 'HST',
  631. 'PUERTO_RICO' => 'PR'
  632. ];
  633. return $map[$tz] ?? str_replace("_", " ", $tz);
  634. }
  635. }