helpers.php 35 KB

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