helpers.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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 Soundasleep\Html2Text as Html2Text;
  14. if(!function_exists('genericBills')) {
  15. function genericBills(Pro $performerPro, Client $patient, $entityType, $entityUid) {
  16. $genericBills = Bill::where('bill_service_type', 'GENERIC');
  17. if($performerPro->pro_type !== 'ADMIN') {
  18. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  19. }
  20. if($patient) {
  21. $genericBills = $genericBills->where('client_id', $patient->id);
  22. }
  23. if($entityType && $entityUid) {
  24. $genericBills = $genericBills
  25. ->where('generic_target_entity_type', $entityType)
  26. ->where('generic_target_entity_uid', $entityUid);
  27. }
  28. return $genericBills->orderBy('created_at', 'DESC')->get();
  29. }
  30. }
  31. if(!function_exists('hasActiveGenericBill')) {
  32. function hasActiveGenericBill(Pro $performerPro, Client $patient, $entityType, $entityUid) {
  33. $genericBills = Bill::where('bill_service_type', 'GENERIC')->where('is_cancelled', false);
  34. if($performerPro->pro_type !== 'ADMIN') {
  35. $genericBills = $genericBills->where('generic_pro_id', $performerPro->id);
  36. }
  37. if($patient) {
  38. $genericBills = $genericBills->where('client_id', $patient->id);
  39. }
  40. if($entityType && $entityUid) {
  41. $genericBills = $genericBills
  42. ->where('generic_target_entity_type', $entityType)
  43. ->where('generic_target_entity_uid', $entityUid);
  44. }
  45. return $genericBills->count() > 0;
  46. }
  47. }
  48. if(!function_exists('queryLineExcept')) {
  49. function queryLineExcept($except = []) {
  50. $params = request()->all();
  51. $final = [];
  52. foreach ($params as $k => $v) {
  53. if(in_array($k, $except) === FALSE) {
  54. $final[] = "$k=" . urlencode($v);
  55. }
  56. }
  57. return implode('&', $final);
  58. }
  59. }
  60. if(!function_exists('sortColumnHead')) {
  61. function sortColumnHead($url, $label, $sortKey, $defaultDirection = 'ASC') {
  62. $currentSortKey = request()->input('sort');
  63. $currentDir = request()->input('dir');
  64. $targetDir = $currentDir ? ($currentDir === 'ASC' ? 'DESC' : 'ASC') : $defaultDirection;
  65. echo '<a href="' . $url . '?sort=' . $sortKey . '&dir=' . $targetDir . '&' . queryLineExcept(['sort', 'dir']) . '">' . $label . '</a>';
  66. if($currentSortKey === $sortKey) {
  67. if($currentDir === 'ASC') {
  68. echo "&nbsp;&nbsp;↑";
  69. }
  70. elseif($currentDir === 'DESC') {
  71. echo "&nbsp;&nbsp;↓";
  72. }
  73. }
  74. }
  75. }
  76. if(!function_exists('html2Text')) {
  77. function html2Text($old, $new){
  78. }
  79. }
  80. if(!function_exists('diff')) {
  81. function diff($old, $new){
  82. // return Diff::toHTML(Diff::compare($old, $new));
  83. }
  84. }
  85. if(!function_exists('get_current_session')) {
  86. function get_current_session(){
  87. return AppSession::where('session_key', request()->cookie('sessionKey'))->first();
  88. }
  89. }
  90. if(!function_exists('friendly_date_time')) {
  91. function friendly_date_time($value, $includeTime = true, $default = '-', $long_year=false) {
  92. if(!$value || empty($value)) return $default;
  93. try {
  94. $result = strtotime($value);
  95. if($long_year){
  96. $result = date("m/d/Y" . ($includeTime ? ", h:ia" : ""), $result);
  97. }else{
  98. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  99. }
  100. return $result;
  101. }
  102. catch (Exception $e) {
  103. return $value;
  104. }
  105. }
  106. }
  107. if(!function_exists('friendlier_date_time')) {
  108. function friendlier_date_time($value, $includeTime = true, $default = '-') {
  109. if(!$value || empty($value)) return $default;
  110. try {
  111. $result = strtotime($value);
  112. $result = date("j M, y" . ($includeTime ? ", h:i a" : ""), $result);
  113. return $result;
  114. }
  115. catch (Exception $e) {
  116. return $value;
  117. }
  118. }
  119. }
  120. if(!function_exists('friendly_date_time_short')) {
  121. function friendly_date_time_short($value, $includeTime = true, $default = '-') {
  122. if(!$value || empty($value)) return $default;
  123. try {
  124. $result = strtotime($value);
  125. $result = date("m/d/y" . ($includeTime ? ", h:ia" : ""), $result);
  126. return $result;
  127. }
  128. catch (Exception $e) {
  129. return $value;
  130. }
  131. }
  132. }
  133. if(!function_exists('friendly_date_time_short_with_tz')) {
  134. function friendly_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  135. if(!$value || empty($value)) return $default;
  136. try {
  137. $realTimezone = resolve_timezone($tz);
  138. $date = new DateTime($value);
  139. $date->setTimezone(new DateTimeZone($realTimezone));
  140. return $date->format("m/d/y" . ($includeTime ? ", h:iA" : ""));
  141. }
  142. catch (Exception $e) {
  143. return $e->getMessage();
  144. }
  145. }
  146. }
  147. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp')) {
  148. function friendly_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  149. if(!$value || empty($value)) return $default;
  150. try {
  151. $realTimezone = resolve_timezone($tz);
  152. $date = new DateTime("@$value");
  153. $date->setTimezone(new DateTimeZone($realTimezone));
  154. return $date->format("m/d/y, h:iA");
  155. }
  156. catch (Exception $e) {
  157. return $e->getMessage();
  158. }
  159. }
  160. }
  161. if(!function_exists('postgres_date_time_short_with_tz')) {
  162. function postgres_date_time_short_with_tz($value, $includeTime = true, $tz='UTC', $default = '-') {
  163. if(!$value || empty($value)) return $default;
  164. try {
  165. $realTimezone = resolve_timezone($tz);
  166. $date = new DateTime($value);
  167. $date->setTimezone(new DateTimeZone($realTimezone));
  168. return $date->format("Y-m-d" . ($includeTime ? " h:i:s" : ""));
  169. }
  170. catch (Exception $e) {
  171. return $e->getMessage();
  172. }
  173. }
  174. }
  175. if(!function_exists('postgres_date_time_short_with_tz_from_timestamp')) {
  176. function postgres_date_time_short_with_tz_from_timestamp($value, $tz='UTC', $default = '-') {
  177. if(!$value || empty($value)) return $default;
  178. try {
  179. $realTimezone = resolve_timezone($tz);
  180. $date = new DateTime("@$value");
  181. $date->setTimezone(new DateTimeZone($realTimezone));
  182. return $date->format("Y-m-d h:i:s");
  183. }
  184. catch (Exception $e) {
  185. return $e->getMessage();
  186. }
  187. }
  188. }
  189. if(!function_exists('friendly_date_time_short_with_tz_from_timestamp_divide1000')) {
  190. function friendly_date_time_short_with_tz_from_timestamp_divide1000($value, $tz='UTC', $default = '-') {
  191. if(!$value || empty($value)) return $default;
  192. try {
  193. $value = (floor($value / 1000));
  194. $realTimezone = resolve_timezone($tz);
  195. $date = new DateTime("@$value");
  196. $date->setTimezone(new DateTimeZone($realTimezone));
  197. return $date->format("m/d/y, h:iA");
  198. }
  199. catch (Exception $e) {
  200. return $e->getMessage();
  201. }
  202. }
  203. }
  204. if(!function_exists('friendly_date')) {
  205. function friendly_date($value) {
  206. if(!$value || empty($value)) return '';
  207. try {
  208. $result = strtotime($value);
  209. $result = date("m/d/Y", $result);
  210. return $result;
  211. }
  212. catch (Exception $e) {
  213. return $value;
  214. }
  215. }
  216. }
  217. if(!function_exists('friendlier_date')) {
  218. function friendlier_date($value) {
  219. if(!$value || empty($value)) return '';
  220. try {
  221. $result = strtotime($value);
  222. $result = date("j M Y", $result);
  223. return $result;
  224. }
  225. catch (Exception $e) {
  226. return $value;
  227. }
  228. }
  229. }
  230. if(!function_exists('unfriendly_date')) {
  231. function unfriendly_date($value) {
  232. if(!$value || empty($value)) return '';
  233. try {
  234. $result = strtotime($value);
  235. $result = date("Y-m-d", $result);
  236. return $result;
  237. }
  238. catch (Exception $e) {
  239. return $value;
  240. }
  241. }
  242. }
  243. if(!function_exists('friendly_time')) {
  244. function friendly_time($value, $default = '-') {
  245. if(!$value || empty($value)) return $default;
  246. try {
  247. $result = strtotime($value);
  248. $result = date("h:i a", $result);
  249. return $result;
  250. }
  251. catch (Exception $e) {
  252. return $value;
  253. }
  254. }
  255. }
  256. if(!function_exists('military_time')) {
  257. function military_time($value, $tz='UTC', $default = '-') {
  258. if(!$value || empty($value)) return $default;
  259. try {
  260. $realTimezone = resolve_timezone($tz);
  261. $date = new DateTime($value);
  262. $date->setTimezone(new DateTimeZone($realTimezone));
  263. return $date->format("H:i");
  264. }
  265. catch (Exception $e) {
  266. return $value;
  267. }
  268. }
  269. }
  270. if(!function_exists('resolve_timezone')) {
  271. function resolve_timezone($value) {
  272. try {
  273. switch ($value) {
  274. case 'ALASKA': {
  275. return 'US/Alaska';
  276. }
  277. case 'CENTRAL': {
  278. return 'US/Central';
  279. }
  280. case 'EASTERN': {
  281. return 'US/Eastern';
  282. }
  283. case 'HAWAII': {
  284. return 'US/Hawaii';
  285. }
  286. case 'MOUNTAIN': {
  287. return 'US/Mountain';
  288. }
  289. case 'PACIFIC': {
  290. return 'US/Pacific';
  291. }
  292. case 'PUERTO_RICO': {
  293. return 'America/Puerto_Rico';
  294. }
  295. case 'UTC': {
  296. return 'UTC';
  297. }
  298. }
  299. }
  300. catch (Exception $e) {
  301. return $value;
  302. }
  303. }
  304. }
  305. // $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
  306. // echo $date->format('Y-m-d H:i:sP') . "\n";
  307. if(!function_exists('friendly_month')) {
  308. function friendly_month($value) {
  309. if(!$value || empty($value)) return "-";
  310. try {
  311. $result = strtotime($value);
  312. $result = date("M o", $result);
  313. return $result;
  314. }
  315. catch (Exception $e) {
  316. return $value;
  317. }
  318. }
  319. }
  320. if(!function_exists('friendly_money')){
  321. function friendly_money($value){
  322. return number_format((float)$value, 2, '.', '');
  323. }
  324. }
  325. if(!function_exists('time_in_hrminsec')) {
  326. function time_in_hrminsec($value, $default = '-') {
  327. if(!$value || empty($value)) return $default;
  328. $value = intval($value);
  329. $minutes = intval($value / 60);
  330. $seconds = $value % 60;
  331. $hours = 0;
  332. if($minutes >= 60) {
  333. $hours = intval($minutes / 60);
  334. $minutes = $minutes % 60;
  335. }
  336. $output = [];
  337. if($hours > 0) {
  338. $output[] = "{$hours}h";
  339. }
  340. if($minutes > 0) {
  341. $output[] = "{$minutes}m";
  342. }
  343. if($seconds > 0) {
  344. $output[] = "{$seconds}s";
  345. }
  346. return implode(" ", $output);
  347. }
  348. }
  349. if(!function_exists('sanitize_field_name')) {
  350. function sanitize_field_name($name) {
  351. $result = strtolower($name);
  352. return preg_replace("/[^0-9a-z]/i", "_", $result);
  353. }
  354. }
  355. if(!function_exists('renderNoteTemplate')) {
  356. function renderNoteTemplate($template, $topLevel)
  357. {
  358. echo
  359. '<div class="note-template-item" ' .
  360. 'template="' . (isset($template->template) ? $template->template : $template->text) . '" ' .
  361. 'type="' . (isset($template->type) ? $template->type : "value") . '" ' .
  362. '>' .
  363. '<div class="note-template-text d-flex align-items-center">' .
  364. '<span class="label">' .
  365. '<input type="checkbox" />' .
  366. '<span>' . $template->text . '</span>' .
  367. '</span>';
  368. if (isset($template->type) && $template->type === 'plus-minus') {
  369. echo '<div class="ml-auto mr-2 text-nowrap">';
  370. echo '<a href="#" class="plus-trigger"><i class="fa fa-plus-circle"></i></a>';
  371. echo '<a href="#" class="minus-trigger ml-1"><i class="fa fa-minus-circle"></i></a>';
  372. echo '</div>';
  373. }
  374. echo '</div>';
  375. if (isset($template->children) && count($template->children)) {
  376. echo '<i class="fa fa-chevron-right has-children"></i>';
  377. echo '<div class="note-template-children">';
  378. foreach ($template->children as $t) {
  379. renderNoteTemplate($t, false);
  380. }
  381. echo '</div>';
  382. } else if (isset($template->type) && $template->type !== 'plus-minus') {
  383. echo '<i class="fa fa-chevron-right has-children"></i>';
  384. echo '<div class="note-template-children">';
  385. if ($template->type === 'alpha') {
  386. echo '<textarea class="form-control form-control-sm"></textarea>';
  387. } else {
  388. echo '<input type="' . $template->type . '" class="form-control form-control-sm">';
  389. }
  390. echo '</div>';
  391. }
  392. echo '</div>';
  393. }
  394. }
  395. if(!function_exists('renderNoteTemplates')) {
  396. function renderNoteTemplates($path)
  397. {
  398. $templates = json_decode(file_get_contents($path));
  399. foreach ($templates->templates as $template) {
  400. renderNoteTemplate($template, true);
  401. }
  402. }
  403. }
  404. if(!function_exists('renderNoteExamTemplates')) {
  405. function renderNoteExamTemplates($parentPath, $childPath)
  406. {
  407. $templates = json_decode(file_get_contents($parentPath));
  408. $templates = $templates->templates;
  409. // override as needed with what is in template set
  410. if(file_exists($childPath)) {
  411. $orTemplates = json_decode(file_get_contents($parentPath));
  412. $orTemplates = $orTemplates->templates;
  413. for ($i = 0; $i < count($templates); $i++) {
  414. for ($j = 0; $j < count($orTemplates); $j++) {
  415. if($templates[$i]->text === $orTemplates[$j]->text) {
  416. $templates[$i] = $orTemplates[$j];
  417. }
  418. }
  419. }
  420. }
  421. foreach ($templates as $template) {
  422. renderNoteTemplate($template, true);
  423. }
  424. }
  425. }
  426. if(!function_exists('getVal')) {
  427. function getVal($object, $prop)
  428. {
  429. if (isset($object->$prop)) {
  430. return $object->$prop;
  431. } else {
  432. return '';
  433. }
  434. }
  435. }
  436. if(!function_exists('appTZtoPHPTZ')) {
  437. function appTZtoPHPTZ($_timezone)
  438. {
  439. switch ($_timezone) {
  440. case 'ALASKA':
  441. $timezone = "US/Alaska";
  442. break;
  443. case 'CENTRAL':
  444. $timezone = "US/Central";
  445. break;
  446. case 'HAWAII':
  447. $timezone = "US/Hawaii";
  448. break;
  449. case 'MOUNTAIN':
  450. $timezone = "US/Mountain";
  451. break;
  452. case 'PACIFIC':
  453. $timezone = "US/Pacific";
  454. break;
  455. case 'PUERTO_RICO':
  456. $timezone = "America/Puerto_Rico";
  457. break;
  458. default:
  459. $timezone = "US/Eastern";
  460. break;
  461. }
  462. return $timezone;
  463. }
  464. }
  465. if(!function_exists('convertToTimezone')) {
  466. function convertToTimezone($_dateTime, $_targetTimezone, $_sourceTimezone = 'UTC', $_returnRaw = false)
  467. {
  468. if (!$_dateTime) return $_dateTime;
  469. $date = new \DateTime($_dateTime, new \DateTimeZone($_sourceTimezone));
  470. $date->setTimezone(new \DateTimeZone(appTZtoPHPTZ($_targetTimezone)));
  471. return $_returnRaw ? $date : $date->format('Y-m-d H:i:s');
  472. }
  473. }
  474. if(!function_exists('minutes_to_hhmm')) {
  475. function minutes_to_hhmm($_minutes)
  476. {
  477. $h = intval(floor($_minutes / 60));
  478. $m = $_minutes;
  479. if($h > 0) {
  480. $m = $_minutes - $h * 60;
  481. }
  482. $h = ($h < 10 ? '0' : '') . $h;
  483. $m = ($m < 10 ? '0' : '') . $m;
  484. return $h . ':' . $m;
  485. }
  486. }