helpers.php 17 KB

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