helpers.php 19 KB

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