fdb.php 22 KB

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