helpers.php 37 KB

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