helpers.php 16 KB

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