fdb.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617
  1. <?php
  2. use Illuminate\Http\Request;
  3. use Illuminate\Support\Facades\DB;
  4. if(!function_exists('side_effects_info')) {
  5. function side_effects_info($_drugs) {
  6. $result = [];
  7. // collect gcn-seq-nos
  8. $gcnSeqNos = [];
  9. $rxMap = [];
  10. foreach ($_drugs as $drug) {
  11. if(@$drug->data && $drug->data->gcnSeqno) {
  12. $gcnSeqNos[] = $drug->data->gcnSeqno;
  13. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  14. }
  15. }
  16. if(!count($gcnSeqNos)) return $result;
  17. $gcnSeqNos = implode(',', array_map(function($_x) {
  18. return "'" . $_x . "'";
  19. }, $gcnSeqNos));
  20. $sides = DB::connection('pgsql_fdb')->select("
  21. SELECT r1.gcn_seqno, r1.side, sm.side_freq, sm.side_sev, sm.dxid, dx.dxid_desc56
  22. FROM rsidegc0_gcnseqno_link r1
  23. JOIN rsidema3_mstr sm ON r1.side = sm.side
  24. JOIN rfmldx0_dxid dx ON sm.dxid = dx.dxid
  25. WHERE r1.gcn_seqno IN (" . $gcnSeqNos . ")
  26. ORDER BY r1.gcn_seqno, sm.side_sev DESC, sm.side_freq ASC
  27. "
  28. );
  29. $seMap = [];
  30. foreach ($sides as $se) {
  31. if(!isset($seMap[$rxMap[$se->gcn_seqno]])) {
  32. $seMap[$rxMap[$se->gcn_seqno]] = [];
  33. }
  34. $seMap[$rxMap[$se->gcn_seqno]][] = [
  35. "gcn_seqno" => $se->gcn_seqno,
  36. "side_freq" => $se->side_freq,
  37. "side_sev" => $se->side_sev,
  38. "dxid_desc56" => $se->dxid_desc56,
  39. ];
  40. }
  41. $result = $seMap;
  42. return $result;
  43. }
  44. }
  45. if(!function_exists('precautions_info')) {
  46. function precautions_info($_drugs) {
  47. }
  48. }
  49. if(!function_exists('contraindications_info')) {
  50. function contraindications_info($_drugs) {
  51. $result = [];
  52. // collect routed med ids
  53. $routedMedIds = [];
  54. $rxMap = [];
  55. foreach ($_drugs as $drug) {
  56. if(@$drug->data && $drug->data->routedMedId) {
  57. $routedMedIds[] = $drug->data->routedMedId;
  58. $rxMap[$drug->data->routedMedId] = $drug->data->name;
  59. }
  60. }
  61. if(!count($routedMedIds)) return $result;
  62. $routedMedIds = implode(',', array_map(function($_x) {
  63. return "'" . $_x . "'";
  64. }, $routedMedIds));
  65. $contraindications = DB::connection('pgsql_fdb')->select("
  66. SELECT r1.routed_med_id, r1.ddxcn, r2.dxid, r2.ddxcn_sl, r3.dxid_desc56
  67. FROM rddcmrm0_routed_med_link r1
  68. JOIN rddcmma1_contra_mstr r2 ON r1.ddxcn = r2.ddxcn
  69. JOIN rfmldx0_dxid r3 ON r2.dxid = r3.dxid
  70. WHERE r1.routed_med_id IN (" . $routedMedIds . ")
  71. ORDER BY r1.routed_med_id, r3.dxid_desc56, r2.ddxcn_sl
  72. "
  73. );
  74. $ciMap = [];
  75. foreach ($contraindications as $ci) {
  76. if(!isset($ciMap[$rxMap[$ci->routed_med_id]])) {
  77. $ciMap[$rxMap[$ci->routed_med_id]] = [];
  78. }
  79. $ciMap[$rxMap[$ci->routed_med_id]][] = [
  80. "dxid_desc56" => $ci->dxid_desc56,
  81. ];
  82. }
  83. $result = $ciMap;
  84. return $result;
  85. }
  86. }
  87. if(!function_exists('drug_allergy_info')) {
  88. function drug_allergy_info($_drugs, $_allergies, $_rxPivot = true) {
  89. $output = [];
  90. /*
  91. for each allergy
  92. if damConceptIdType = 1 // allergen-group-id
  93. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+a+DAM_ALRGN_GRP+Allergen+-+Illustration+of+Scenario+C
  94. ...
  95. elseif damConceptIdType = 2 // medication-name-id
  96. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+a+MED_NAME_ID+Allergen+-+Illustration+of+Scenario+B
  97. ...
  98. elseif damConceptIdType = 6 // base-ingredient-id
  99. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+an+Ingredient+Allergen+-+Illustration+of+Scenario+A
  100. ...
  101. endif
  102. endfor
  103. */
  104. foreach ($_allergies as $allergy) {
  105. foreach ($_drugs as $rxItem) {
  106. if($allergy->data->damConceptIdType == 6) { // ingredient
  107. if (drugAllergyIngredientAllergenVsSingleRx($allergy, $rxItem)) {
  108. $output[] = "<b>{$rxItem->data->name}</b> can cause allergic reactions " . ($_rxPivot ? 'since' : 'if') . " the patient is allergic to <b>{$allergy->data->name}</b>.";
  109. }
  110. }
  111. else if($allergy->data->damConceptIdType == 2) { // medication
  112. if (drugAllergyMedicationAllergenVsSingleRx($allergy, $rxItem)) {
  113. $output[] = "<b>{$rxItem->data->name}</b> (medication) can cause allergic reactions " . ($_rxPivot ? 'since' : 'if') . " the patient is allergic to <b>{$allergy->data->name}</b>.";
  114. }
  115. }
  116. else if($allergy->data->damConceptIdType == 1) { // allergen group
  117. if (drugAllergyGroupAllergenVsSingleRx($allergy, $rxItem)) {
  118. $output[] = "<b>{$rxItem->data->name}</b> can cause allergic reactions " . ($_rxPivot ? 'since' : 'if') . " the patient is allergic to <b>{$allergy->data->name}</b>.";
  119. }
  120. }
  121. }
  122. }
  123. if(!count($output)) return '';
  124. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  125. return "<li class='mb-1'>" . $_x . "</li>";
  126. }, $output)) . "</ol>";
  127. }
  128. }
  129. if(!function_exists('drugAllergyIngredientAllergenVsSingleRx')) {
  130. function drugAllergyIngredientAllergenVsSingleRx($_allergen, $_rx)
  131. {
  132. $matches = DB::connection('pgsql_fdb')->select("
  133. (
  134. -- ingredients from medication
  135. SELECT R1.related_hic_seqn as hic_seqn, R2.hic_desc
  136. FROM RHICHCR0_HIC_HIC_LINK R1
  137. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  138. WHERE R1.hic_seqn IN (
  139. SELECT S2.dam_alrgn_hic_seqn
  140. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  141. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  142. WHERE S1.med_concept_id = :medid
  143. AND S1.med_concept_id_typ = 3
  144. )
  145. )
  146. INTERSECT
  147. (
  148. -- all ingredients directly and related from allergens
  149. (
  150. SELECT R1.related_hic_seqn as hic_seqn, R2.hic_desc
  151. FROM RHICHCR0_HIC_HIC_LINK R1
  152. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  153. WHERE R1.hic_seqn = :allergenConceptID
  154. )
  155. UNION
  156. -- all ingredients via related dam allergen groups
  157. (
  158. SELECT r3.hic_seqn, r4.hic_desc
  159. FROM RDAMGHC0_HIC_ALRGN_GRP_LINK R1
  160. JOIN rdamagd1_alrgn_grp_desc R2 on R1.dam_alrgn_grp = R2.dam_alrgn_grp
  161. JOIN RDAMGHC0_HIC_ALRGN_GRP_LINK R3 on R1.dam_alrgn_grp = R3.dam_alrgn_grp
  162. JOIN RHICD5_HIC_DESC R4 on r3.hic_seqn = r4.hic_seqn
  163. WHERE R1.hic_seqn = :allergenConceptID
  164. AND R2.dam_alrgn_grp_status_cd = 0
  165. ORDER BY r3.hic_seqn
  166. )
  167. )
  168. ",
  169. ['medid' => $_rx->data->medId, 'allergenConceptID' => $_allergen->data->damConceptId]
  170. );
  171. return !!count($matches);
  172. }
  173. }
  174. if(!function_exists('drugAllergyMedicationAllergenVsSingleRx')) {
  175. function drugAllergyMedicationAllergenVsSingleRx($_allergen, $_rx)
  176. {
  177. $matches = DB::connection('pgsql_fdb')->select("
  178. (
  179. -- ingredients from medication
  180. SELECT R1.related_hic_seqn as hic_seqn
  181. FROM RHICHCR0_HIC_HIC_LINK R1
  182. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  183. WHERE R1.hic_seqn IN (
  184. SELECT S2.dam_alrgn_hic_seqn
  185. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  186. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  187. WHERE S1.med_concept_id = :medid
  188. AND S1.med_concept_id_typ = 3
  189. )
  190. )
  191. INTERSECT
  192. (
  193. -- all ingredients directly and related from allergens
  194. SELECT R1.dam_alrgn_hic_seqn as hic_seqn
  195. FROM RDAMHHA0_HIC_HICL_ALG_LINK R1
  196. WHERE R1.hicl_seqno IN (
  197. SELECT R1.hicl_seqno
  198. FROM RMEDMHL0_MED_HICLSEQNO_LINK R1
  199. WHERE R1.med_concept_id_typ = 1
  200. AND R1.med_concept_id = :allergenConceptID
  201. AND MED_CONCEPT_HICL_SRC_CD = 0
  202. )
  203. )
  204. ",
  205. ['medid' => $_rx->data->medId, 'allergenConceptID' => $_allergen->data->damConceptId]
  206. );
  207. return !!count($matches);
  208. }
  209. }
  210. if(!function_exists('drugAllergyGroupAllergenVsSingleRx')) {
  211. function drugAllergyGroupAllergenVsSingleRx($_allergen, $_rx)
  212. {
  213. $matches = DB::connection('pgsql_fdb')->select("
  214. (
  215. -- ingredients from medication
  216. SELECT R1.related_hic_seqn as hic_seqn
  217. FROM RHICHCR0_HIC_HIC_LINK R1
  218. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  219. WHERE R1.hic_seqn IN (
  220. SELECT S2.dam_alrgn_hic_seqn
  221. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  222. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  223. WHERE S1.med_concept_id = :medid
  224. AND S1.med_concept_id_typ = 3
  225. )
  226. )
  227. INTERSECT
  228. (
  229. -- ingredients from medication allergen
  230. (
  231. SELECT R1.hic_seqn as hic_seqn
  232. FROM RDAMGHC0_HIC_ALRGN_GRP_LINK R1
  233. WHERE R1.DAM_ALRGN_GRP = :allergenConceptID
  234. )
  235. UNION
  236. (
  237. SELECT distinct s1.hic_seqn
  238. FROM RDAMXHC0_HIC_ALRGN_XSENSE_LINK S1
  239. WHERE S1.dam_alrgn_xsense IN (
  240. SELECT R1.dam_alrgn_xsense
  241. FROM RDAMGX0_ALRGN_GRP_XSENSE_LINK R1
  242. JOIN RDAMCSD1_XSENSIT_ALLERGY_DESC R2 on R1.dam_alrgn_xsense = R2.dam_alrgn_xsense
  243. WHERE R1.dam_alrgn_grp = :allergenConceptID
  244. AND R2.dam_alrgn_xsense_status_cd = 0
  245. )
  246. )
  247. )
  248. ",
  249. ['medid' => $_rx->data->medId, 'allergenConceptID' => $_allergen->data->damConceptId]
  250. );
  251. return !!count($matches);
  252. }
  253. }
  254. if(!function_exists('drug_drug_interaction_info')) {
  255. function drug_drug_interaction_info($_drugs) {
  256. if(count($_drugs) < 2) return "";
  257. $leftIndex = 0;
  258. $output = [];
  259. for ($i=$leftIndex; $i<count($_drugs) - 1; $i++) {
  260. for ($j=$i + 1; $j<count($_drugs); $j++) {
  261. $output[] = drugDrugInteractionSinglePair($_drugs[$i], $_drugs[$j]);
  262. }
  263. }
  264. $output = array_filter($output, function($_x) {
  265. return !!$_x;
  266. });
  267. return implode("<br>", $output);
  268. }
  269. }
  270. if(!function_exists('drug_drug_interaction_info_with_pivot')) {
  271. function drug_drug_interaction_info_with_pivot($_pivot, $_drugs) {
  272. if(!count($_drugs)) return "";
  273. $leftIndex = 0;
  274. $output = [];
  275. for ($i=0; $i<count($_drugs); $i++) {
  276. $output[] = drugDrugInteractionSinglePair($_pivot, $_drugs[$i]);
  277. }
  278. if(!count($output)) return '';
  279. $output = array_filter($output, function($_x) {
  280. return !!$_x;
  281. });
  282. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  283. return "<li class='mb-1'>" . $_x . "</li>";
  284. }, $output)) . "</ol>";
  285. }
  286. }
  287. if(!function_exists('drugDrugInteractionSinglePair')) {
  288. function drugDrugInteractionSinglePair($_rx1, $_rx2)
  289. {
  290. $output = [];
  291. // get active ingredient DDI_CODEX values for drug 1 and 2
  292. $rx1ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx1->data->gcnSeqno);
  293. $rx2ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx2->data->gcnSeqno);
  294. if (!$rx1ActiveDdiCodex || !$rx2ActiveDdiCodex || !count($rx1ActiveDdiCodex) || !count($rx2ActiveDdiCodex)) return "";
  295. // dump($rx1_DDI_CODEX);
  296. // dump($rx2_DDI_CODEX);
  297. // get inactive ingredient DDI_CODEX values for drug 1 and 2
  298. // to get this we need to first get the NDCs of the drugs
  299. $rx1Ndc = getNdcFromMedId($_rx1->data->medId);
  300. $rx2Ndc = getNdcFromMedId($_rx2->data->medId);
  301. // dump($rx1Ndc);
  302. // dump($rx2Ndc);
  303. $rx1InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx1Ndc);
  304. $rx2InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx2Ndc);
  305. // if(!$rx1InactiveDdiCodex || !$rx2InactiveDdiCodex || !count($rx1InactiveDdiCodex) || !count($rx2InactiveDdiCodex)) return "";
  306. // dump($rx1InactiveDdiCodex);
  307. // dump($rx2InactiveDdiCodex);
  308. // get ddi codex - monox pairs for drug 1 & 2
  309. $rx1ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1ActiveDdiCodex);
  310. $rx1InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1InactiveDdiCodex);
  311. $rx2ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2ActiveDdiCodex);
  312. $rx2InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2InactiveDdiCodex);
  313. // dump($rx1ActiveDdiCodexMonoxPairs);
  314. // dump($rx1InactiveDdiCodexMonoxPairs);
  315. // dump($rx2ActiveDdiCodexMonoxPairs);
  316. // dump($rx2InactiveDdiCodexMonoxPairs);
  317. // compare 1-active to 2-active and 2-inactive
  318. $activeCatches = [];
  319. foreach ($rx1ActiveDdiCodexMonoxPairs as $compareLeft) {
  320. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  321. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  322. $activeCatches[] = $compareLeft->ddi_codex;
  323. }
  324. }
  325. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  326. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  327. $activeCatches[] = $compareLeft->ddi_codex;
  328. }
  329. }
  330. }
  331. // compare 1-inactive to 2-active and 2-inactive
  332. $inactiveCatches = [];
  333. foreach ($rx1InactiveDdiCodexMonoxPairs as $compareLeft) {
  334. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  335. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  336. $inactiveCatches[] = $compareLeft->ddi_codex;
  337. }
  338. }
  339. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  340. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  341. $inactiveCatches[] = $compareLeft->ddi_codex;
  342. }
  343. }
  344. }
  345. if (count($activeCatches)) {
  346. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more active ingredients in <b>{$_rx1->data->name}</b>.";
  347. }
  348. if (count($inactiveCatches)) {
  349. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more inactive ingredients in <b>{$_rx1->data->name}</b>.";
  350. }
  351. // TODO: find out and show the names of the actual ingredients causing interaction
  352. return implode("<br>", $output);
  353. }
  354. }
  355. if(!function_exists('getActiveDdiCodexFromGcnSeqNo')) {
  356. function getActiveDdiCodexFromGcnSeqNo($_gcnSeqNo)
  357. {
  358. $ddiCodexArray = [];
  359. $query = DB::connection('pgsql_fdb')->select("
  360. SELECT r1.ddi_codex
  361. FROM RADIMGC4_GCNSEQNO_LINK r1
  362. WHERE r1.gcn_seqno = :gcnSeqNo
  363. ",
  364. ['gcnSeqNo' => $_gcnSeqNo]
  365. );
  366. if (count($query)) {
  367. $ddiCodexArray = array_map(function ($_x) {
  368. return $_x->ddi_codex;
  369. }, $query);
  370. }
  371. return $ddiCodexArray;
  372. }
  373. }
  374. if(!function_exists('getNdcFromMedId')) {
  375. function getNdcFromMedId($_medId)
  376. {
  377. $ndcArray = [];
  378. $query = DB::connection('pgsql_fdb')->select("
  379. select ndc from rmindc1_ndc_medid where medid = :medId
  380. ",
  381. ['medId' => $_medId]
  382. );
  383. if (count($query)) {
  384. $ndcArray = array_map(function ($_x) {
  385. return $_x->ndc;
  386. }, $query);
  387. }
  388. return $ndcArray;
  389. }
  390. }
  391. if(!function_exists('getInactiveDdiCodexFromNdc')) {
  392. function getInactiveDdiCodexFromNdc($_ndc)
  393. {
  394. $ddiCodexArray = [];
  395. if(!count($_ndc)) return $ddiCodexArray;
  396. $query = DB::connection('pgsql_fdb')->select("
  397. SELECT distinct r1.ddi_codex
  398. FROM RDDIMIN0_NDC_INACTV_DDIM_LINK r1
  399. WHERE r1.ddi_ndc IN (" . implode(',', array_map(function ($_x) {
  400. return "'" . $_x . "'";
  401. }, $_ndc)) . ")
  402. "
  403. );
  404. if (count($query)) {
  405. $ddiCodexArray = array_map(function ($_x) {
  406. return $_x->ddi_codex;
  407. }, $query);
  408. }
  409. return $ddiCodexArray;
  410. }
  411. }
  412. if(!function_exists('getDdiCodexMonoxPairs')) {
  413. function getDdiCodexMonoxPairs($_ddiCodexArray)
  414. {
  415. $ddiCodexMonoxPairsArray = [];
  416. if(!count($_ddiCodexArray)) return $ddiCodexMonoxPairsArray;
  417. if (count($_ddiCodexArray)) {
  418. $ddiCodexMonoxPairsArray = DB::connection('pgsql_fdb')->select("
  419. SELECT r1.ddi_codex, r1.ddi_monox
  420. FROM RADIMMA5_MSTR r1
  421. WHERE r1.ddi_codex IN (" . implode(',', array_map(function ($_x) {
  422. return "'" . $_x . "'";
  423. }, $_ddiCodexArray)) . ")
  424. "
  425. );
  426. }
  427. return $ddiCodexMonoxPairsArray;
  428. }
  429. }
  430. if(!function_exists('duplicate_therapy_info')) {
  431. function duplicate_therapy_info($_drugs) {
  432. $dptClasses = [];
  433. foreach ($_drugs as $drug) {
  434. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  435. }
  436. $leftIndex = 0;
  437. $matches = [];
  438. for ($i = $leftIndex; $i < count($_drugs) - 1; $i++) {
  439. for ($j = $i + 1; $j < count($_drugs); $j++) {
  440. $compareResult = compareDPTs($_drugs[$i]->dpt, $_drugs[$j]->dpt);
  441. foreach ($compareResult as $c) {
  442. $matches[] = "<b>{$_drugs[$i]->data->name}</b> and <b>{$_drugs[$j]->data->name}</b> both participate in the duplicate therapy class <b>{$c->dpt_class_desc}</b> (duplicates allowed: {$c->dpt_allowance})";
  443. }
  444. }
  445. }
  446. if(!count($matches)) return '';
  447. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  448. return "<li class='mb-1'>" . $_x . "</li>";
  449. }, $matches)) . "</ol>";
  450. }
  451. }
  452. if(!function_exists('duplicate_therapy_info_with_pivot')) {
  453. function duplicate_therapy_info_with_pivot($_pivot, $_drugs) {
  454. $_pivot->dpt = getDptClassFromGcnSeqNo($_pivot->data->gcnSeqno);
  455. $dptClasses = [];
  456. foreach ($_drugs as $drug) {
  457. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  458. }
  459. $leftIndex = 0;
  460. $matches = [];
  461. for ($i = 0; $i < count($_drugs); $i++) {
  462. $compareResult = compareDPTs($_pivot->dpt, $_drugs[$i]->dpt);
  463. foreach ($compareResult as $c) {
  464. $matches[] = "<b>{$_pivot->data->name}</b> and <b>{$_drugs[$i]->data->name}</b> both participate in the duplicate therapy class <b>{$c->dpt_class_desc}</b> (duplicates allowed: {$c->dpt_allowance})";
  465. }
  466. }
  467. if(!count($matches)) return '';
  468. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  469. return "<li class='mb-1'>" . $_x . "</li>";
  470. }, $matches)) . "</ol>";
  471. }
  472. }
  473. if (!function_exists('getDptClassFromGcnSeqNo')) {
  474. function getDptClassFromGcnSeqNo($_gcnSeqNo)
  475. {
  476. return DB::connection('pgsql_fdb')->select("
  477. SELECT distinct r1.dpt_class_id, r2.dpt_allowance, r2.dpt_class_desc
  478. FROM RDPTGC0_GCNSEQNO_LINK r1
  479. JOIN RDPTCL0_CLASS_ID r2 on r1.dpt_class_id = r2.dpt_class_id
  480. WHERE r1.gcn_seqno = :gcnSeqNo
  481. ",
  482. ['gcnSeqNo' => $_gcnSeqNo]
  483. );
  484. }
  485. }
  486. if (!function_exists('compareDPTs')) {
  487. function compareDPTs($_dptArray1, $_dptArray2)
  488. {
  489. $output = [];
  490. for ($i = 0; $i < count($_dptArray1); $i++) {
  491. for ($j = 0; $j < count($_dptArray2); $j++) {
  492. if ($_dptArray1[$i]->dpt_class_id == $_dptArray2[$j]->dpt_class_id) {
  493. $output[] = json_decode(json_encode([
  494. "dpt_allowance" => $_dptArray1[$i]->dpt_allowance,
  495. "dpt_class_desc" => $_dptArray1[$i]->dpt_class_desc
  496. ]));
  497. }
  498. }
  499. }
  500. return $output;
  501. }
  502. }
  503. if(!function_exists('coadministration_info')) {
  504. function coadministration_info($_drugs) {
  505. if(count($_drugs) < 2) return "";
  506. // collect gcn-seq-nos
  507. $gcnSeqNos = [];
  508. $rxMap = [];
  509. foreach ($_drugs as $drug) {
  510. if(@$drug->data && $drug->data->gcnSeqno) {
  511. $gcnSeqNos[] = $drug->data->gcnSeqno;
  512. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  513. }
  514. }
  515. if(!count($gcnSeqNos)) return $result;
  516. $gcnSeqNos = implode(',', array_map(function($_x) {
  517. return "'" . $_x . "'";
  518. }, $gcnSeqNos));
  519. $coadministration = DB::connection('pgsql_fdb')->select("
  520. SELECT distinct r1.coadmin_dosing_text
  521. FROM radige0_ddi_gcnseqno_except r1
  522. WHERE r1.side_a_gcn_seqno in ($gcnSeqNos) AND r1.side_b_gcn_seqno in ($gcnSeqNos)
  523. "
  524. );
  525. if(!$coadministration || !count($coadministration)) return '';
  526. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  527. return "<li class='mb-1'>" . $_x->coadmin_dosing_text . "</li>";
  528. }, $coadministration)) . "</ol>";
  529. return $coadministration;
  530. }
  531. }