helpers.php 32 KB

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