helpers.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146
  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('chargeForCPT')) {
  16. function chargeForCPT($cpt) {
  17. switch($cpt) {
  18. case "99202": return '$' . 172;
  19. case "99203": return '$' . 262;
  20. case "99204": return '$' . 387;
  21. case "99205": return '$' . 512;
  22. case "99211": return '$' . 56;
  23. case "99212": return '$' . 133;
  24. case "99213": return '$' . 212;
  25. case "99214": return '$' . 297;
  26. case "99215": return '$' . 418;
  27. case "99354": return '$' . 289;
  28. case "G0506": return '$' . 144;
  29. case "99441": return '$' . 132;
  30. case "99442": return '$' . 211;
  31. case "99443": return '$' . 297;
  32. case "99454": return '$' . 138;
  33. case "99457": return '$' . 117;
  34. case "99458": return '$' . 93;
  35. case "90791": return '$' . 394;
  36. case "90792": return '$' . 444;
  37. case "90832": return '$' . 171;
  38. case "90834": return '$' . 226;
  39. case "90837": return '$' . 332;
  40. case "90833": return '$' . 158;
  41. case "90836": return '$' . 199;
  42. case "90838": return '$' . 261;
  43. case "99355": return '$' . 130;
  44. case "97802": return '$' . 60;
  45. case "97803": return '$' . 50;
  46. case "99495": return '$' . 275;
  47. case "99406": return '$' . 35.56;
  48. case "99091": return '$' . 75;
  49. case "99453": return '$' . 40;
  50. }
  51. return '-';
  52. }
  53. }
  54. if(!function_exists('currentSortForKey')) {
  55. function currentSortForKey($current, $key) {
  56. $found = false;
  57. $current = $current ? $current : [];
  58. for ($i = 0; $i < count($current); $i++) {
  59. if($current[$i]['key'] === $key) { // if exists, reverse
  60. return ["index" => ($i + 1), "order" => $current[$i]['order']];
  61. }
  62. }
  63. return FALSE;
  64. }
  65. }
  66. if(!function_exists('includeOrReverseInMultiSort')) {
  67. function includeOrReverseInMultiSort($current, $key) {
  68. $found = false;
  69. $current = $current ? $current : [];
  70. for ($i = 0; $i < count($current); $i++) {
  71. if($current[$i]['key'] === $key) { // if exists, reverse
  72. $current[$i]['order'] = ($current[$i]['order'] === 'DESC' ? 'ASC' : 'DESC');
  73. $found = true;
  74. break;
  75. }
  76. }
  77. if (!$found) { // if not exists, include
  78. $current[] = ["key" => $key, "order" => 'ASC'];
  79. }
  80. return $current;
  81. }
  82. }
  83. if(!function_exists('removeFromMultiSort')) {
  84. function removeFromMultiSort($current, $key) {
  85. $found = false;
  86. $current = $current ? $current : [];
  87. $current = array_filter($current, function($_x) use ($key) {
  88. return $_x['key'] !== $key;
  89. });
  90. return array_values($current);
  91. }
  92. }
  93. if(!function_exists('toFeetAndInches')) {
  94. function toFeetAndInches($value, $ftLabel = ' ft.', $inLabel = ' in.') {
  95. if(!$value) return '-';
  96. $value = round($value);
  97. $ft = round(floor($value / 12));
  98. $in = $value % 12;
  99. return "$ft$ftLabel $in$inLabel";
  100. }
  101. }
  102. if(!function_exists('callJava')) {
  103. function callJava($endPoint, $data, $sessionKey) {
  104. $url = config('stag.backendUrl') . $endPoint;
  105. $response = Http::asForm()
  106. ->withHeaders([
  107. 'sessionKey' => $sessionKey
  108. ])
  109. ->post($url, $data)
  110. ->body();
  111. return $response;
  112. }
  113. }
  114. if(!function_exists('feetFromInches')) {
  115. function feetFromInches($value) {
  116. if(!$value) return 0;
  117. $value = round($value);
  118. return round(floor($value / 12));
  119. }
  120. }
  121. if(!function_exists('inchesAfterFeetFromInches')) {
  122. function inchesAfterFeetFromInches($value) {
  123. if(!$value) return 0;
  124. $value = round($value);
  125. return round($value % 12);
  126. }
  127. }
  128. if(!function_exists('genericBills')) {
  129. function genericBills(Pro $performerPro, $patient,$careMonth, $entityType, $entityUid) {
  130. $genericBills = Bill::where('bill_service_type', 'GENERIC');
  131. if($performerPro->pro_type !== 'ADMIN') {
  132. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  133. }
  134. if($patient) {
  135. $genericBills = $genericBills->where('client_id', $patient->id);
  136. }
  137. if($careMonth){
  138. $genericBills = $genericBills->where('care_month_id', $careMonth->id);
  139. }
  140. if($entityType && $entityUid) {
  141. $genericBills = $genericBills
  142. ->where('generic_target_entity_type', $entityType)
  143. ->where('generic_target_entity_uid', $entityUid);
  144. }
  145. return $genericBills->orderBy('created_at', 'DESC')->get();
  146. }
  147. }
  148. if(!function_exists('hasActiveGenericBill')) {
  149. function hasActiveGenericBill(Pro $performerPro, $patient, $entityType, $entityUid) {
  150. $genericBills = Bill::where('bill_service_type', 'GENERIC')->where('is_cancelled', false);
  151. if($performerPro->pro_type !== 'ADMIN') {
  152. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  153. }
  154. if($patient) {
  155. $genericBills = $genericBills->where('client_id', $patient->id);
  156. }
  157. if($entityType && $entityUid) {
  158. $genericBills = $genericBills
  159. ->where('generic_target_entity_type', $entityType)
  160. ->where('generic_target_entity_uid', $entityUid);
  161. }
  162. return $genericBills->count() > 0;
  163. }
  164. }
  165. if(!function_exists('queryLineExcept')) {
  166. function queryLineExcept($except = []) {
  167. $params = request()->all();
  168. $final = [];
  169. foreach ($params as $k => $v) {
  170. if(in_array($k, $except) === FALSE) {
  171. if(is_array($v)) $v = implode(',', $v);
  172. $final[] = "$k=" . urlencode($v);
  173. }
  174. }
  175. return implode('&', $final);
  176. }
  177. }
  178. if(!function_exists('sortColumnHead')) {
  179. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  180. $currentSortKey = request()->input('sort');
  181. $currentDir = request()->input('dir');
  182. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  183. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  184. if($currentSortKey === $sortKey) {
  185. if($currentDir === 'ASC') {
  186. echo "&nbsp;&nbsp;↑";
  187. }
  188. elseif($currentDir === 'DESC') {
  189. echo "&nbsp;&nbsp;↓";
  190. }
  191. }
  192. }
  193. }
  194. if(!function_exists('html2Text')) {
  195. function html2Text($old, $new){
  196. }
  197. }
  198. if(!function_exists('diff')) {
  199. function diff($old, $new){
  200. // return Diff::toHTML(Diff::compare($old, $new));
  201. }
  202. }
  203. if(!function_exists('get_current_session')) {
  204. function get_current_session(){
  205. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  206. }
  207. }
  208. if(!function_exists('get_current_date')) {
  209. function get_current_date($timezone = null){
  210. if(!$timezone){
  211. $timezone = 'US\Eastern';
  212. }
  213. $date = new DateTime("now", new DateTimeZone('US/Eastern') );
  214. return $date->format('Y-m-d');
  215. }
  216. }
  217. if(!function_exists('friendly_date_est')) {
  218. function friendly_date_est($value, $default = '-') {
  219. if(!$value || empty($value)) return $default;
  220. try {
  221. $realTimezone = resolve_timezone('EASTERN');
  222. $date = new DateTime($value);
  223. $date->setTimezone(new DateTimeZone($realTimezone));
  224. return $date->format("m/d/y");
  225. }
  226. catch (Exception $e) {
  227. return $e->getMessage();
  228. }
  229. }
  230. }
  231. if(!function_exists('friendly_date_est_compact')) {
  232. function friendly_date_est_compact($value, $default = '-') {
  233. if(!$value || empty($value)) return $default;
  234. try {
  235. $realTimezone = resolve_timezone('EASTERN');
  236. $date = new DateTime($value);
  237. $date->setTimezone(new DateTimeZone($realTimezone));
  238. return $date->format('y') === date('y') ? $date->format("m/d") : $date->format("m/d/y");
  239. }
  240. catch (Exception $e) {
  241. return $e->getMessage();
  242. }
  243. }
  244. }
  245. if(!function_exists('friendly_date_compact')) {
  246. function friendly_date_compact($value, $default = '-') {
  247. if(!$value || empty($value)) return $default;
  248. try {
  249. $date = new DateTime($value);
  250. return $date->format('y') === date('y') ? $date->format("m/d") : $date->format("m/d/y");
  251. }
  252. catch (Exception $e) {
  253. return $e->getMessage();
  254. }
  255. }
  256. }
  257. if(!function_exists('friendly_date_time')) {
  258. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  259. if(!$value || empty($value)) return $default;
  260. try {
  261. if($includeTime) {
  262. $realTimezone = 'US/Eastern';
  263. $date = new DateTime($value);
  264. $date->setTimezone(new DateTimeZone($realTimezone));
  265. return $date->format("m/d/y" . ($includeTime ? ", h:ia" : "")) . ($includeTime ? ' EST' : '');
  266. }
  267. else {
  268. $result = strtotime($value);
  269. if($long_year){
  270. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  271. }else{
  272. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  273. }
  274. return $result;
  275. }
  276. }
  277. catch (Exception $e) {
  278. return $value;
  279. }
  280. }
  281. }
  282. if(!function_exists('friendly_date_time_with_seconds')) {
  283. function friendly_date_time_with_seconds($value, $includeTime = true, $default = '-', $long_year=false) {
  284. if(!$value || empty($value)) return $default;
  285. try {
  286. if($includeTime) {
  287. $realTimezone = 'US/Eastern';
  288. $date = new DateTime($value);
  289. $date->setTimezone(new DateTimeZone($realTimezone));
  290. return $date->format("m/d/y" . ($includeTime ? ", h:i:sa" : "")) . ($includeTime ? ' EST' : '');
  291. }
  292. else {
  293. $result = strtotime($value);
  294. if($long_year){
  295. $result = date("m/d/Y" . ($includeTime ? ", h:i:sa" : ""), $result);
  296. }else{
  297. $result = date("m/d/y" . ($includeTime ? ", h:i:sa" : ""), $result);
  298. }
  299. return $result;
  300. }
  301. }
  302. catch (Exception $e) {
  303. return $value;
  304. }
  305. }
  306. }
  307. if(!function_exists('friendly_date_month_year')) {
  308. function friendly_date_month_year($value, $default = '-', $long_year=true) {
  309. if(!$value || empty($value)) return $default;
  310. try {
  311. $result = strtotime($value);
  312. if($long_year){
  313. $result = date("m/Y", $result);
  314. }else{
  315. $result = date("m/y", $result);
  316. }
  317. return $result;
  318. }
  319. catch (Exception $e) {
  320. return $value;
  321. }
  322. }
  323. }
  324. if(!function_exists('friendlier_date_time')) {
  325. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  326. return friendly_date_time($value, $includeTime, $default);
  327. }
  328. }
  329. if(!function_exists('friendly_date_time_short')) {
  330. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  331. if(!$value || empty($value)) return $default;
  332. try {
  333. $result = strtotime($value);
  334. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  335. return $result;
  336. }
  337. catch (Exception $e) {
  338. return $value;
  339. }
  340. }
  341. }
  342. if(!function_exists('friendly_date_time_short_with_tz')) {
  343. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  344. if(!$value || empty($value)) return $default;
  345. try {
  346. $realTimezone = resolve_timezone($tz);
  347. $date = new DateTime($value);
  348. $date->setTimezone(new DateTimeZone($realTimezone));
  349. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  350. }
  351. catch (Exception $e) {
  352. return $e->getMessage();
  353. }
  354. }
  355. }
  356. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  357. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-', $format = "m/d/y, h:iA") {
  358. if(!$value || empty($value)) return $default;
  359. try {
  360. $realTimezone = resolve_timezone($tz);
  361. $date = new DateTime("@$value");
  362. $date->setTimezone(new DateTimeZone($realTimezone));
  363. return $date->format($format);
  364. }
  365. catch (Exception $e) {
  366. return $e->getMessage();
  367. }
  368. }
  369. }
  370. if(!function_exists('postgres_date_time_short_with_tz')) {
  371. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  372. if(!$value || empty($value)) return $default;
  373. try {
  374. $realTimezone = resolve_timezone($tz);
  375. $date = new DateTime($value);
  376. $date->setTimezone(new DateTimeZone($realTimezone));
  377. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  378. }
  379. catch (Exception $e) {
  380. return $e->getMessage();
  381. }
  382. }
  383. }
  384. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  385. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  386. if(!$value || empty($value)) return $default;
  387. try {
  388. $realTimezone = resolve_timezone($tz);
  389. $date = new DateTime("@$value");
  390. $date->setTimezone(new DateTimeZone($realTimezone));
  391. return $date->format("Y-m-d h:i:s");
  392. }
  393. catch (Exception $e) {
  394. return $e->getMessage();
  395. }
  396. }
  397. }
  398. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  399. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  400. if(!$value || empty($value)) return $default;
  401. try {
  402. $value = (floor($value / 1000));
  403. $realTimezone = resolve_timezone($tz);
  404. $date = new DateTime("@$value");
  405. $date->setTimezone(new DateTimeZone($realTimezone));
  406. return $date->format("m/d/y, h:iA");
  407. }
  408. catch (Exception $e) {
  409. return $e->getMessage();
  410. }
  411. }
  412. }
  413. if(!function_exists('friendly_date_short_with_tz_from_timestamp_divide1000')) {
  414. function friendly_date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  415. if(!$value || empty($value)) return $default;
  416. try {
  417. $value = (floor($value / 1000));
  418. $realTimezone = resolve_timezone($tz);
  419. $date = new DateTime("@$value");
  420. $date->setTimezone(new DateTimeZone($realTimezone));
  421. return $date->format("m/d/y");
  422. }
  423. catch (Exception $e) {
  424. return $e->getMessage();
  425. }
  426. }
  427. }
  428. if(!function_exists('date_short_with_tz_from_timestamp_divide1000')) {
  429. function date_short_with_tz_from_timestamp_divide1000($value, $tz='EASTERN', $default = '-') {
  430. if(!$value || empty($value)) return $default;
  431. try {
  432. $value = (floor($value / 1000));
  433. $realTimezone = resolve_timezone($tz);
  434. $date = new DateTime("@$value");
  435. $date->setTimezone(new DateTimeZone($realTimezone));
  436. return $date->format("Y-m-d");
  437. }
  438. catch (Exception $e) {
  439. return $e->getMessage();
  440. }
  441. }
  442. }
  443. if(!function_exists('friendly_date')) {
  444. function friendly_date($value) {
  445. if(!$value || empty($value)) return '';
  446. try {
  447. $result = strtotime($value);
  448. $result = date("m/d/Y", $result);
  449. return $result;
  450. }
  451. catch (Exception $e) {
  452. return $value;
  453. }
  454. }
  455. }
  456. if(!function_exists('friendly_date_short')) {
  457. function friendly_date_short($value) {
  458. if(!$value || empty($value)) return '';
  459. try {
  460. $result = strtotime($value);
  461. $result = date("m/d/y", $result);
  462. return $result;
  463. }
  464. catch (Exception $e) {
  465. return $value;
  466. }
  467. }
  468. }
  469. if(!function_exists('friendly_date_month_year')) {
  470. function friendly_date_month_year($value) {
  471. if(!$value || empty($value)) return '';
  472. try {
  473. $result = strtotime($value);
  474. $result = date("M Y", $result);
  475. return $result;
  476. }
  477. catch (Exception $e) {
  478. return $value;
  479. }
  480. }
  481. }
  482. if(!function_exists('friendlier_date')) {
  483. function friendlier_date($value) {
  484. if(!$value || empty($value)) return '';
  485. try {
  486. $result = strtotime($value);
  487. $result = date("m/d/Y", $result);
  488. return $result;
  489. }
  490. catch (Exception $e) {
  491. return $value;
  492. }
  493. }
  494. }
  495. if(!function_exists('relative_friendly_date')) {
  496. function relative_friendly_date($value) {
  497. if(!$value || empty($value)) return '';
  498. try {
  499. $result = date_diff(date_create($value), date_create('now'))->days;
  500. if ($result > 30) $result = date('F Y', strtotime($value));
  501. else if ($result > 1) $result .= ' days ago';
  502. else if ($result === 1) $result = 'Yesterday';
  503. else if ($result === 0) $result = 'Today';
  504. return $result;
  505. }
  506. catch (Exception $e) {
  507. return $value;
  508. }
  509. }
  510. }
  511. if(!function_exists('unfriendly_date')) {
  512. function unfriendly_date($value) {
  513. if(!$value || empty($value)) return '';
  514. try {
  515. $result = strtotime($value);
  516. $result = date("Y-m-d", $result);
  517. return $result;
  518. }
  519. catch (Exception $e) {
  520. return $value;
  521. }
  522. }
  523. }
  524. if(!function_exists('friendly_time')) {
  525. function friendly_time($value, $default = '-') {
  526. if(!$value || empty($value)) return $default;
  527. try {
  528. $result = strtotime($value);
  529. $result = date("h:i a", $result);
  530. return $result;
  531. }
  532. catch (Exception $e) {
  533. return $value;
  534. }
  535. }
  536. }
  537. if(!function_exists('military_time')) {
  538. function military_time($value, $tz='UTC', $default = '-') {
  539. if(!$value || empty($value)) return $default;
  540. try {
  541. $realTimezone = resolve_timezone($tz);
  542. $date = new DateTime($value);
  543. $date->setTimezone(new DateTimeZone($realTimezone));
  544. return $date->format("H:i");
  545. }
  546. catch (Exception $e) {
  547. return $value;
  548. }
  549. }
  550. }
  551. if(!function_exists('resolve_timezone')) {
  552. function resolve_timezone($value) {
  553. try {
  554. switch ($value) {
  555. case 'ALASKA': {
  556. return 'US/Alaska';
  557. }
  558. case 'CENTRAL': {
  559. return 'US/Central';
  560. }
  561. case 'EASTERN': {
  562. return 'US/Eastern';
  563. }
  564. case 'HAWAII': {
  565. return 'US/Hawaii';
  566. }
  567. case 'MOUNTAIN': {
  568. return 'US/Mountain';
  569. }
  570. case 'PACIFIC': {
  571. return 'US/Pacific';
  572. }
  573. case 'PUERTO_RICO': {
  574. return 'America/Puerto_Rico';
  575. }
  576. case 'UTC': {
  577. return 'UTC';
  578. }
  579. }
  580. }
  581. catch (Exception $e) {
  582. return $value;
  583. }
  584. }
  585. }
  586. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  587. // echo $date->format('Y-m-d H:i:sP') . "\n";
  588. if(!function_exists('friendly_month')) {
  589. function friendly_month($value) {
  590. if(!$value || empty($value)) return "-";
  591. try {
  592. $result = strtotime($value);
  593. $result = date("F o", $result);
  594. return $result;
  595. }
  596. catch (Exception $e) {
  597. return $value;
  598. }
  599. }
  600. }
  601. if(!function_exists('friendly_month_short')) {
  602. function friendly_month_short($value) {
  603. if(!$value || empty($value)) return "-";
  604. try {
  605. $result = strtotime($value);
  606. $result = date("M y", $result);
  607. return $result;
  608. }
  609. catch (Exception $e) {
  610. return $value;
  611. }
  612. }
  613. }
  614. if(!function_exists('day_part_from_date')) {
  615. function day_part_from_date($value) {
  616. if(!$value || empty($value)) return "-";
  617. try {
  618. $result = strtotime($value);
  619. $result = date("d", $result);
  620. return $result;
  621. }
  622. catch (Exception $e) {
  623. return $value;
  624. }
  625. }
  626. }
  627. if(!function_exists('month_part_from_date')) {
  628. function month_part_from_date($value) {
  629. if(!$value || empty($value)) return "-";
  630. try {
  631. $result = strtotime($value);
  632. $result = date("m", $result);
  633. return $result;
  634. }
  635. catch (Exception $e) {
  636. return $value;
  637. }
  638. }
  639. }
  640. if(!function_exists('year_part_from_date')) {
  641. function year_part_from_date($value) {
  642. if(!$value || empty($value)) return "-";
  643. try {
  644. $result = strtotime($value);
  645. $result = date("Y", $result);
  646. return $result;
  647. }
  648. catch (Exception $e) {
  649. return $value;
  650. }
  651. }
  652. }
  653. if(!function_exists('friendly_money')){
  654. function friendly_money($value, $decimals = 2) {
  655. // if($decimals === 0) return intval($value);
  656. return number_format((float)$value, $decimals, '.', '');
  657. }
  658. }
  659. if(!function_exists('time_in_hrminsec')) {
  660. function time_in_hrminsec($value, $default = '-') {
  661. if(!$value || empty($value)) return $default;
  662. $value = intval($value);
  663. $minutes = intval($value / 60);
  664. $seconds = $value % 60;
  665. $hours = 0;
  666. if($minutes >= 60) {
  667. $hours = intval($minutes / 60);
  668. $minutes = $minutes % 60;
  669. }
  670. $output = [];
  671. if($hours > 0) {
  672. $output[] = "{$hours}h";
  673. }
  674. if($minutes > 0) {
  675. $output[] = "{$minutes}m";
  676. }
  677. if($seconds > 0) {
  678. $output[] = "{$seconds}s";
  679. }
  680. return implode(" ", $output);
  681. }
  682. }
  683. if(!function_exists('sanitize_field_name')) {
  684. function sanitize_field_name($name) {
  685. $result = strtolower($name);
  686. return preg_replace("/[^0-9a-z]/i", "_", $result);
  687. }
  688. }
  689. if(!function_exists('sanitize_state_name')) {
  690. function sanitize_state_name($name) {
  691. $result = strtolower($name);
  692. return ucwords(preg_replace("/_/i", " ", $result));
  693. }
  694. }
  695. if(!function_exists('renderNoteTemplate')) {
  696. function renderNoteTemplate($template, $topLevel)
  697. {
  698. echo
  699. '<div class="note-template-item" ' .
  700. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  701. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  702. '>' .
  703. '<div class="note-template-text d-flex align-items-center">' .
  704. '<span class="label">' .
  705. '<input type="checkbox" />' .
  706. '<span>' . $template->text . '</span>' .
  707. '</span>';
  708. if (isset($template->type) && $template->type === 'plus-minus') {
  709. echo '<div class="ml-auto mr-2 text-nowrap">';
  710. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  711. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  712. echo '</div>';
  713. }
  714. echo '</div>';
  715. if (isset($template->children) && count($template->children)) {
  716. echo '<i class="fa fa-chevron-right has-children"></i>';
  717. echo '<div class="note-template-children">';
  718. foreach ($template->children as $t) {
  719. renderNoteTemplate($t, false);
  720. }
  721. echo '</div>';
  722. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  723. echo '<i class="fa fa-chevron-right has-children"></i>';
  724. echo '<div class="note-template-children">';
  725. if ($template->type === 'alpha') {
  726. echo '<textarea class="form-control form-control-sm"></textarea>';
  727. } else {
  728. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  729. }
  730. echo '</div>';
  731. }
  732. echo '</div>';
  733. }
  734. }
  735. if(!function_exists('renderNoteTemplates')) {
  736. function renderNoteTemplates($path)
  737. {
  738. $templates = json_decode(file_get_contents($path));
  739. foreach ($templates->templates as $template) {
  740. renderNoteTemplate($template, true);
  741. }
  742. }
  743. }
  744. if(!function_exists('renderNoteExamTemplates')) {
  745. function renderNoteExamTemplates($parentPath, $childPath)
  746. {
  747. $templates = json_decode(file_get_contents($parentPath));
  748. $templates = $templates->templates;
  749. // override as needed with what is in template set
  750. if(file_exists($childPath)) {
  751. $orTemplates = json_decode(file_get_contents($parentPath));
  752. $orTemplates = $orTemplates->templates;
  753. for ($i = 0; $i < count($templates); $i++) {
  754. for ($j = 0; $j < count($orTemplates); $j++) {
  755. if($templates[$i]->text === $orTemplates[$j]->text) {
  756. $templates[$i] = $orTemplates[$j];
  757. }
  758. }
  759. }
  760. }
  761. foreach ($templates as $template) {
  762. renderNoteTemplate($template, true);
  763. }
  764. }
  765. }
  766. if(!function_exists('getVal')) {
  767. function getVal($object, $prop)
  768. {
  769. if (isset($object->$prop)) {
  770. return $object->$prop;
  771. } else {
  772. return '';
  773. }
  774. }
  775. }
  776. if(!function_exists('appTZtoPHPTZ')) {
  777. function appTZtoPHPTZ($_timezone)
  778. {
  779. switch ($_timezone) {
  780. case 'ALASKA':
  781. $timezone = "US/Alaska";
  782. break;
  783. case 'CENTRAL':
  784. $timezone = "US/Central";
  785. break;
  786. case 'HAWAII':
  787. $timezone = "US/Hawaii";
  788. break;
  789. case 'MOUNTAIN':
  790. $timezone = "US/Mountain";
  791. break;
  792. case 'PACIFIC':
  793. $timezone = "US/Pacific";
  794. break;
  795. case 'PUERTO_RICO':
  796. $timezone = "America/Puerto_Rico";
  797. break;
  798. default:
  799. $timezone = "US/Eastern";
  800. break;
  801. }
  802. return $timezone;
  803. }
  804. }
  805. if(!function_exists('convertToTimezone')) {
  806. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  807. {
  808. if (!$_dateTime) return $_dateTime;
  809. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  810. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  811. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  812. }
  813. }
  814. if(!function_exists('minutes_to_hhmm')) {
  815. function minutes_to_hhmm($_minutes)
  816. {
  817. $h = intval(floor($_minutes / 60));
  818. $m = $_minutes;
  819. if($h > 0) {
  820. $m = $_minutes - $h * 60;
  821. }
  822. $h = ($h < 10 ? '0' : '') . $h;
  823. $m = ($m < 10 ? '0' : '') . $m;
  824. return $h . ':' . $m;
  825. }
  826. }
  827. if(!function_exists('vsValue')) {
  828. function vsValue($_v, $patient = null, $_direct = false)
  829. {
  830. if ($_direct) {
  831. return $_v ? $_v : '<span class="font-weight-normal text-info font-italic">empty</span>';
  832. }
  833. return @($patient->{$_v}) ? $patient->{$_v} : '<span class="font-weight-normal text-info font-italic">empty</span>';
  834. }
  835. }
  836. if(!function_exists('vsElement')) {
  837. function vsElement($_v, $type, $name, $patient, $class = '')
  838. {
  839. return '<input type="' . $type . '" class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  840. 'name="' . $name . '" ' .
  841. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  842. }
  843. }
  844. if(!function_exists('vsRoElement')) {
  845. function vsRoElement($_v, $type, $name, $patient, $class = '')
  846. {
  847. return '<input type="' . $type . '" readonly class="form-control form-control-sm min-width-unset rounded-0 ' . $class . '" ' .
  848. 'name="' . $name . '" ' .
  849. 'value="' . (@($patient->{$_v}) ? $patient->{$_v} : '') . '">';
  850. }
  851. }
  852. if(!function_exists('str_compact')) {
  853. function str_compact($_str)
  854. {
  855. return preg_replace("/[^a-zA-Z0-9]/", "", strip_tags($_str));
  856. }
  857. }
  858. if(!function_exists('noteMethodDisplay')) {
  859. function noteMethodDisplay($method)
  860. {
  861. if($method === 'IN_CLINIC') return 'In-Clinic';
  862. $method = str_replace('_', ' ', $method);
  863. return ucwords(strtolower($method));
  864. }
  865. }
  866. if(!function_exists('friendly_timezone')) {
  867. function friendly_timezone($tz) {
  868. $map = [
  869. 'EASTERN' => 'EST',
  870. 'CENTRAL' => 'CST',
  871. 'MOUNTAIN' => 'MST',
  872. 'PACIFIC' => 'PST',
  873. 'ALASKA' => 'AST',
  874. 'HAWAII' => 'HST',
  875. 'PUERTO_RICO' => 'PR'
  876. ];
  877. return $map[$tz] ?? str_replace("_", " ", $tz);
  878. }
  879. }
  880. if(!function_exists('segment_template_summary_value_display')) {
  881. function segment_template_summary_value_display($value, $default='________', $class = '') {
  882. if($value && strlen($value)) return '<span class="segment-template-summary-value '.$class.'">' . $value . '</span>';
  883. return $default;
  884. }
  885. }
  886. if(!function_exists('starts_with')) {
  887. function starts_with($haystack, $needle) {
  888. $length = strlen($needle);
  889. return substr($haystack, 0, $length) === $needle;
  890. }
  891. }
  892. if(!function_exists('get_doc_templates')){
  893. function get_doc_templates(){
  894. $basePath = 'views/document-templates-generic';
  895. $dir = opendir(resource_path($basePath));
  896. $templates = [];
  897. if($dir) {
  898. while ( $entry = readdir($dir) ) {
  899. if($entry !== '.' && $entry !== '..' && is_dir(resource_path($basePath . '/' . $entry))) {
  900. $spec = json_decode(file_get_contents(resource_path($basePath . '/' . $entry) . '/spec.json'));
  901. if(isset($spec->active) && $spec->active) {
  902. $html = file_get_contents(resource_path($basePath . '/' . $entry) . '/content.blade.php');
  903. // get fields
  904. $proVariables = [];
  905. $htmlDisplay = preg_replace_callback(
  906. '/{([^}]+)}/',
  907. function ($match) use (&$proVariables) {
  908. $token = $match[1];
  909. $replacement = '';
  910. if(strpos($token, '.') === FALSE) {
  911. $adminField = false;
  912. if($token[0] === '@') {
  913. // $token = substr($token, 1);
  914. $adminField = true;
  915. }
  916. switch ($token) {
  917. case 'TODAY':
  918. $replacement = date('m/d/Y');
  919. break;
  920. default:
  921. $replacement = '<span class="text-info document-variable" data-variable="' . $token . '">' .
  922. ($token[0] === '@' ? substr($token, 1) : $token) .
  923. '</span>';
  924. if (!isset($proVariables[$token])) {
  925. $proVariables[$token] = [
  926. 'token' => $token,
  927. 'adminField' => $adminField
  928. ];
  929. }
  930. break;
  931. }
  932. }
  933. return $replacement;
  934. },
  935. $html
  936. );
  937. $templates[] = [
  938. "name" => $entry,
  939. "title" => $spec->title,
  940. "html" => $html,
  941. "htmlDisplay" => $htmlDisplay,
  942. "proVariables" => $proVariables
  943. ];
  944. }
  945. }
  946. }
  947. }
  948. closedir($dir);
  949. // sort by document name
  950. $names = array_column($templates, 'title');
  951. array_multisort($names, SORT_ASC, $templates);
  952. return $templates;
  953. }
  954. }
  955. if(!function_exists('format_phone_number')) {
  956. function format_phone_number($number, $default = '-') {
  957. if(empty($number)) return $default;
  958. return preg_replace('~.*(\d{3})[^\d]{0,7}(\d{3})[^\d]{0,7}(\d{4}).*~', '($1) $2-$3', $number). "\n";
  959. }
  960. }
  961. if(!function_exists('format_number')) {
  962. function format_number($number, $places = 2) {
  963. return number_format((float)$number, $places, '.', '');
  964. }
  965. }
  966. if(!function_exists('toHumanReadable')) {
  967. function toHumanReadable($name) {
  968. return ucwords(preg_replace("/[^0-9a-z]/i", " ", $name));
  969. }
  970. }
  971. if(!function_exists('parseRender')) {
  972. function parseRender($_data) {
  973. if($_data) {
  974. $type = gettype($_data);
  975. if(is_string($_data) || is_numeric($_data)) {
  976. echo $_data;
  977. }
  978. else {
  979. echo "<table class='table table-sm border w-100'>";
  980. foreach($_data as $k => $v) {
  981. echo "<tr>";
  982. echo "<td><b class='text-secondary'>" . toHumanReadable($k) . "</b></td>";
  983. echo "<td>";
  984. if(is_object($v)) {
  985. parseRender($v);
  986. }
  987. elseif(is_array($v)) {
  988. foreach($v as $k2 => $v2) {
  989. parseRender($v2);
  990. }
  991. }
  992. else {
  993. echo $v;
  994. }
  995. echo "</td>";
  996. echo "</tr>";
  997. }
  998. echo "</table>";
  999. }
  1000. }
  1001. }
  1002. }
  1003. if(!function_exists('mask_string')) {
  1004. function mask_string($string, $mask = "*", $lengthFromLastCharacter = null) {
  1005. if(!$lengthFromLastCharacter){
  1006. return str_repeat($mask, strlen($string));
  1007. }
  1008. $firstCharacters = substr($string, ($lengthFromLastCharacter));
  1009. $lastCharacters = substr($string, -$lengthFromLastCharacter);
  1010. $maskedFirstCharacters = str_repeat($mask, strlen($firstCharacters));
  1011. return $maskedFirstCharacters . $lastCharacters;
  1012. }
  1013. }
  1014. if(!function_exists('limit_string_length')) {
  1015. function limit_string_length($string, $length = 30) {
  1016. return substr($string,0, $length);
  1017. }
  1018. }