helpers.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605
  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('friendlier_date')) {
  233. function friendlier_date($value) {
  234. if(!$value || empty($value)) return '';
  235. try {
  236. $result = strtotime($value);
  237. $result = date("j M Y", $result);
  238. return $result;
  239. }
  240. catch (Exception $e) {
  241. return $value;
  242. }
  243. }
  244. }
  245. if(!function_exists('unfriendly_date')) {
  246. function unfriendly_date($value) {
  247. if(!$value || empty($value)) return '';
  248. try {
  249. $result = strtotime($value);
  250. $result = date("Y-m-d", $result);
  251. return $result;
  252. }
  253. catch (Exception $e) {
  254. return $value;
  255. }
  256. }
  257. }
  258. if(!function_exists('friendly_time')) {
  259. function friendly_time($value, $default = '-') {
  260. if(!$value || empty($value)) return $default;
  261. try {
  262. $result = strtotime($value);
  263. $result = date("h:i a", $result);
  264. return $result;
  265. }
  266. catch (Exception $e) {
  267. return $value;
  268. }
  269. }
  270. }
  271. if(!function_exists('military_time')) {
  272. function military_time($value, $tz='UTC', $default = '-') {
  273. if(!$value || empty($value)) return $default;
  274. try {
  275. $realTimezone = resolve_timezone($tz);
  276. $date = new DateTime($value);
  277. $date->setTimezone(new DateTimeZone($realTimezone));
  278. return $date->format("H:i");
  279. }
  280. catch (Exception $e) {
  281. return $value;
  282. }
  283. }
  284. }
  285. if(!function_exists('resolve_timezone')) {
  286. function resolve_timezone($value) {
  287. try {
  288. switch ($value) {
  289. case 'ALASKA': {
  290. return 'US/Alaska';
  291. }
  292. case 'CENTRAL': {
  293. return 'US/Central';
  294. }
  295. case 'EASTERN': {
  296. return 'US/Eastern';
  297. }
  298. case 'HAWAII': {
  299. return 'US/Hawaii';
  300. }
  301. case 'MOUNTAIN': {
  302. return 'US/Mountain';
  303. }
  304. case 'PACIFIC': {
  305. return 'US/Pacific';
  306. }
  307. case 'PUERTO_RICO': {
  308. return 'America/Puerto_Rico';
  309. }
  310. case 'UTC': {
  311. return 'UTC';
  312. }
  313. }
  314. }
  315. catch (Exception $e) {
  316. return $value;
  317. }
  318. }
  319. }
  320. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  321. // echo $date->format('Y-m-d H:i:sP') . "\n";
  322. if(!function_exists('friendly_month')) {
  323. function friendly_month($value) {
  324. if(!$value || empty($value)) return "-";
  325. try {
  326. $result = strtotime($value);
  327. $result = date("F o", $result);
  328. return $result;
  329. }
  330. catch (Exception $e) {
  331. return $value;
  332. }
  333. }
  334. }
  335. if(!function_exists('day_part_from_date')) {
  336. function day_part_from_date($value) {
  337. if(!$value || empty($value)) return "-";
  338. try {
  339. $result = strtotime($value);
  340. $result = date("d", $result);
  341. return $result;
  342. }
  343. catch (Exception $e) {
  344. return $value;
  345. }
  346. }
  347. }
  348. if(!function_exists('month_part_from_date')) {
  349. function month_part_from_date($value) {
  350. if(!$value || empty($value)) return "-";
  351. try {
  352. $result = strtotime($value);
  353. $result = date("m", $result);
  354. return $result;
  355. }
  356. catch (Exception $e) {
  357. return $value;
  358. }
  359. }
  360. }
  361. if(!function_exists('year_part_from_date')) {
  362. function year_part_from_date($value) {
  363. if(!$value || empty($value)) return "-";
  364. try {
  365. $result = strtotime($value);
  366. $result = date("Y", $result);
  367. return $result;
  368. }
  369. catch (Exception $e) {
  370. return $value;
  371. }
  372. }
  373. }
  374. if(!function_exists('friendly_money')){
  375. function friendly_money($value){
  376. return number_format((float)$value, 2, '.', '');
  377. }
  378. }
  379. if(!function_exists('time_in_hrminsec')) {
  380. function time_in_hrminsec($value, $default = '-') {
  381. if(!$value || empty($value)) return $default;
  382. $value = intval($value);
  383. $minutes = intval($value / 60);
  384. $seconds = $value % 60;
  385. $hours = 0;
  386. if($minutes >= 60) {
  387. $hours = intval($minutes / 60);
  388. $minutes = $minutes % 60;
  389. }
  390. $output = [];
  391. if($hours > 0) {
  392. $output[] = "{$hours}h";
  393. }
  394. if($minutes > 0) {
  395. $output[] = "{$minutes}m";
  396. }
  397. if($seconds > 0) {
  398. $output[] = "{$seconds}s";
  399. }
  400. return implode(" ", $output);
  401. }
  402. }
  403. if(!function_exists('sanitize_field_name')) {
  404. function sanitize_field_name($name) {
  405. $result = strtolower($name);
  406. return preg_replace("/[^0-9a-z]/i", "_", $result);
  407. }
  408. }
  409. if(!function_exists('renderNoteTemplate')) {
  410. function renderNoteTemplate($template, $topLevel)
  411. {
  412. echo
  413. '<div class="note-template-item" ' .
  414. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  415. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  416. '>' .
  417. '<div class="note-template-text d-flex align-items-center">' .
  418. '<span class="label">' .
  419. '<input type="checkbox" />' .
  420. '<span>' . $template->text . '</span>' .
  421. '</span>';
  422. if (isset($template->type) && $template->type === 'plus-minus') {
  423. echo '<div class="ml-auto mr-2 text-nowrap">';
  424. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  425. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  426. echo '</div>';
  427. }
  428. echo '</div>';
  429. if (isset($template->children) && count($template->children)) {
  430. echo '<i class="fa fa-chevron-right has-children"></i>';
  431. echo '<div class="note-template-children">';
  432. foreach ($template->children as $t) {
  433. renderNoteTemplate($t, false);
  434. }
  435. echo '</div>';
  436. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  437. echo '<i class="fa fa-chevron-right has-children"></i>';
  438. echo '<div class="note-template-children">';
  439. if ($template->type === 'alpha') {
  440. echo '<textarea class="form-control form-control-sm"></textarea>';
  441. } else {
  442. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  443. }
  444. echo '</div>';
  445. }
  446. echo '</div>';
  447. }
  448. }
  449. if(!function_exists('renderNoteTemplates')) {
  450. function renderNoteTemplates($path)
  451. {
  452. $templates = json_decode(file_get_contents($path));
  453. foreach ($templates->templates as $template) {
  454. renderNoteTemplate($template, true);
  455. }
  456. }
  457. }
  458. if(!function_exists('renderNoteExamTemplates')) {
  459. function renderNoteExamTemplates($parentPath, $childPath)
  460. {
  461. $templates = json_decode(file_get_contents($parentPath));
  462. $templates = $templates->templates;
  463. // override as needed with what is in template set
  464. if(file_exists($childPath)) {
  465. $orTemplates = json_decode(file_get_contents($parentPath));
  466. $orTemplates = $orTemplates->templates;
  467. for ($i = 0; $i < count($templates); $i++) {
  468. for ($j = 0; $j < count($orTemplates); $j++) {
  469. if($templates[$i]->text === $orTemplates[$j]->text) {
  470. $templates[$i] = $orTemplates[$j];
  471. }
  472. }
  473. }
  474. }
  475. foreach ($templates as $template) {
  476. renderNoteTemplate($template, true);
  477. }
  478. }
  479. }
  480. if(!function_exists('getVal')) {
  481. function getVal($object, $prop)
  482. {
  483. if (isset($object->$prop)) {
  484. return $object->$prop;
  485. } else {
  486. return '';
  487. }
  488. }
  489. }
  490. if(!function_exists('appTZtoPHPTZ')) {
  491. function appTZtoPHPTZ($_timezone)
  492. {
  493. switch ($_timezone) {
  494. case 'ALASKA':
  495. $timezone = "US/Alaska";
  496. break;
  497. case 'CENTRAL':
  498. $timezone = "US/Central";
  499. break;
  500. case 'HAWAII':
  501. $timezone = "US/Hawaii";
  502. break;
  503. case 'MOUNTAIN':
  504. $timezone = "US/Mountain";
  505. break;
  506. case 'PACIFIC':
  507. $timezone = "US/Pacific";
  508. break;
  509. case 'PUERTO_RICO':
  510. $timezone = "America/Puerto_Rico";
  511. break;
  512. default:
  513. $timezone = "US/Eastern";
  514. break;
  515. }
  516. return $timezone;
  517. }
  518. }
  519. if(!function_exists('convertToTimezone')) {
  520. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  521. {
  522. if (!$_dateTime) return $_dateTime;
  523. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  524. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  525. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  526. }
  527. }
  528. if(!function_exists('minutes_to_hhmm')) {
  529. function minutes_to_hhmm($_minutes)
  530. {
  531. $h = intval(floor($_minutes / 60));
  532. $m = $_minutes;
  533. if($h > 0) {
  534. $m = $_minutes - $h * 60;
  535. }
  536. $h = ($h < 10 ? '0' : '') . $h;
  537. $m = ($m < 10 ? '0' : '') . $m;
  538. return $h . ':' . $m;
  539. }
  540. }