helpers.php 35 KB

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