fdb.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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. // convert to simple array
  268. $drugsArray = [];
  269. foreach ($_drugs as $drug) {
  270. $drugsArray[] = $drug;
  271. }
  272. for ($i=$leftIndex; $i<count($drugsArray) - 1; $i++) {
  273. for ($j=$i + 1; $j<count($drugsArray); $j++) {
  274. $output[] = drugDrugInteractionSinglePair($drugsArray[$i], $drugsArray[$j]);
  275. }
  276. }
  277. $output = array_filter($output, function($_x) {
  278. return !!$_x;
  279. });
  280. return implode("<br>", $output);
  281. }
  282. }
  283. if(!function_exists('drug_drug_interaction_info_with_pivot')) {
  284. function drug_drug_interaction_info_with_pivot($_pivot, $_drugs) {
  285. if(!count($_drugs)) return "";
  286. $leftIndex = 0;
  287. $output = [];
  288. foreach ($_drugs as $drug) {
  289. $output[] = drugDrugInteractionSinglePair($_pivot, $drug);
  290. }
  291. $output = array_filter($output, function($_x) {
  292. return !!$_x;
  293. });
  294. if(!count($output)) return '';
  295. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  296. return "<li class='mb-1'>" . $_x . "</li>";
  297. }, $output)) . "</ol>";
  298. }
  299. }
  300. if(!function_exists('drugDrugInteractionSinglePair')) {
  301. function drugDrugInteractionSinglePair($_rx1, $_rx2)
  302. {
  303. $output = [];
  304. // get active ingredient DDI_CODEX values for drug 1 and 2
  305. $rx1ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx1->data->gcnSeqno);
  306. $rx2ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx2->data->gcnSeqno);
  307. if (!$rx1ActiveDdiCodex || !$rx2ActiveDdiCodex || !count($rx1ActiveDdiCodex) || !count($rx2ActiveDdiCodex)) return "";
  308. // dump($rx1_DDI_CODEX);
  309. // dump($rx2_DDI_CODEX);
  310. // get inactive ingredient DDI_CODEX values for drug 1 and 2
  311. // to get this we need to first get the NDCs of the drugs
  312. $rx1Ndc = getNdcFromMedId($_rx1->data->medId);
  313. $rx2Ndc = getNdcFromMedId($_rx2->data->medId);
  314. // dump($rx1Ndc);
  315. // dump($rx2Ndc);
  316. $rx1InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx1Ndc);
  317. $rx2InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx2Ndc);
  318. // if(!$rx1InactiveDdiCodex || !$rx2InactiveDdiCodex || !count($rx1InactiveDdiCodex) || !count($rx2InactiveDdiCodex)) return "";
  319. // dump($rx1InactiveDdiCodex);
  320. // dump($rx2InactiveDdiCodex);
  321. // get ddi codex - monox pairs for drug 1 & 2
  322. $rx1ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1ActiveDdiCodex);
  323. $rx1InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1InactiveDdiCodex);
  324. $rx2ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2ActiveDdiCodex);
  325. $rx2InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2InactiveDdiCodex);
  326. // dump($rx1ActiveDdiCodexMonoxPairs);
  327. // dump($rx1InactiveDdiCodexMonoxPairs);
  328. // dump($rx2ActiveDdiCodexMonoxPairs);
  329. // dump($rx2InactiveDdiCodexMonoxPairs);
  330. // compare 1-active to 2-active and 2-inactive
  331. $activeCatches = [];
  332. foreach ($rx1ActiveDdiCodexMonoxPairs as $compareLeft) {
  333. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  334. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  335. $activeCatches[] = $compareLeft->ddi_codex;
  336. }
  337. }
  338. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  339. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  340. $activeCatches[] = $compareLeft->ddi_codex;
  341. }
  342. }
  343. }
  344. // compare 1-inactive to 2-active and 2-inactive
  345. $inactiveCatches = [];
  346. foreach ($rx1InactiveDdiCodexMonoxPairs as $compareLeft) {
  347. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  348. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  349. $inactiveCatches[] = $compareLeft->ddi_codex;
  350. }
  351. }
  352. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  353. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  354. $inactiveCatches[] = $compareLeft->ddi_codex;
  355. }
  356. }
  357. }
  358. if (count($activeCatches)) {
  359. $t = "<b>{$_rx2->data->name}</b> interacts with one or more active ingredients in <b>{$_rx1->data->name}</b>.";
  360. $output[] = "<span class='events-all' title=\"" . strip_tags(str_replace('"', '', $t)) . "\">" . $t . "</span>";
  361. }
  362. if (count($inactiveCatches)) {
  363. $t = "<b>{$_rx2->data->name}</b> interacts with one or more inactive ingredients in <b>{$_rx1->data->name}</b>.";
  364. $output[] = "<span class='events-all' title=\"" . strip_tags(str_replace('"', '', $t)) . "\">" . $t . "</span>";
  365. }
  366. // TODO: find out and show the names of the actual ingredients causing interaction
  367. return implode("<br>", $output);
  368. }
  369. }
  370. if(!function_exists('getActiveDdiCodexFromGcnSeqNo')) {
  371. function getActiveDdiCodexFromGcnSeqNo($_gcnSeqNo)
  372. {
  373. $ddiCodexArray = [];
  374. $query = DB::connection('pgsql_fdb')->select("
  375. SELECT r1.ddi_codex
  376. FROM RADIMGC4_GCNSEQNO_LINK r1
  377. WHERE r1.gcn_seqno = :gcnSeqNo
  378. ",
  379. ['gcnSeqNo' => $_gcnSeqNo]
  380. );
  381. if (count($query)) {
  382. $ddiCodexArray = array_map(function ($_x) {
  383. return $_x->ddi_codex;
  384. }, $query);
  385. }
  386. return $ddiCodexArray;
  387. }
  388. }
  389. if(!function_exists('getNdcFromMedId')) {
  390. function getNdcFromMedId($_medId)
  391. {
  392. $ndcArray = [];
  393. $query = DB::connection('pgsql_fdb')->select("
  394. select ndc from rmindc1_ndc_medid where medid = :medId
  395. ",
  396. ['medId' => $_medId]
  397. );
  398. if (count($query)) {
  399. $ndcArray = array_map(function ($_x) {
  400. return $_x->ndc;
  401. }, $query);
  402. }
  403. return $ndcArray;
  404. }
  405. }
  406. if(!function_exists('getInactiveDdiCodexFromNdc')) {
  407. function getInactiveDdiCodexFromNdc($_ndc)
  408. {
  409. $ddiCodexArray = [];
  410. if(!count($_ndc)) return $ddiCodexArray;
  411. $query = DB::connection('pgsql_fdb')->select("
  412. SELECT distinct r1.ddi_codex
  413. FROM RDDIMIN0_NDC_INACTV_DDIM_LINK r1
  414. WHERE r1.ddi_ndc IN (" . implode(',', array_map(function ($_x) {
  415. return "'" . $_x . "'";
  416. }, $_ndc)) . ")
  417. "
  418. );
  419. if (count($query)) {
  420. $ddiCodexArray = array_map(function ($_x) {
  421. return $_x->ddi_codex;
  422. }, $query);
  423. }
  424. return $ddiCodexArray;
  425. }
  426. }
  427. if(!function_exists('getDdiCodexMonoxPairs')) {
  428. function getDdiCodexMonoxPairs($_ddiCodexArray)
  429. {
  430. $ddiCodexMonoxPairsArray = [];
  431. if(!count($_ddiCodexArray)) return $ddiCodexMonoxPairsArray;
  432. if (count($_ddiCodexArray)) {
  433. $ddiCodexMonoxPairsArray = DB::connection('pgsql_fdb')->select("
  434. SELECT r1.ddi_codex, r1.ddi_monox
  435. FROM RADIMMA5_MSTR r1
  436. WHERE r1.ddi_codex IN (" . implode(',', array_map(function ($_x) {
  437. return "'" . $_x . "'";
  438. }, $_ddiCodexArray)) . ")
  439. "
  440. );
  441. }
  442. return $ddiCodexMonoxPairsArray;
  443. }
  444. }
  445. if(!function_exists('duplicate_therapy_info')) {
  446. function duplicate_therapy_info($_drugs) {
  447. if(!$_drugs || count($_drugs) < 2) return '';
  448. $dptClasses = [];
  449. foreach ($_drugs as $drug) {
  450. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  451. }
  452. $leftIndex = 0;
  453. $matches = [];
  454. // convert to simple array
  455. $drugsArray = [];
  456. foreach ($_drugs as $drug) {
  457. $drugsArray[] = $drug;
  458. }
  459. for ($i = $leftIndex; $i < count($drugsArray) - 1; $i++) {
  460. for ($j = $i + 1; $j < count($drugsArray); $j++) {
  461. $compareResult = compareDPTs($drugsArray[$i]->dpt, $drugsArray[$j]->dpt);
  462. foreach ($compareResult as $c) {
  463. $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})";
  464. }
  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 events-all' title=\"" . strip_tags(str_replace('"', '', $_x)) . "\">" . $_x . "</li>";
  470. }, $matches)) . "</ol>";
  471. }
  472. }
  473. if(!function_exists('duplicate_therapy_info_with_pivot')) {
  474. function duplicate_therapy_info_with_pivot($_pivot, $_drugs) {
  475. if(!$_drugs || count($_drugs) < 1) return '';
  476. $_pivot->dpt = getDptClassFromGcnSeqNo($_pivot->data->gcnSeqno);
  477. $dptClasses = [];
  478. foreach ($_drugs as $drug) {
  479. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  480. }
  481. $leftIndex = 0;
  482. $matches = [];
  483. foreach ($_drugs as $drug) {
  484. $compareResult = compareDPTs($_pivot->dpt, $drug->dpt);
  485. foreach ($compareResult as $c) {
  486. $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})";
  487. }
  488. }
  489. if(!count($matches)) return '';
  490. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  491. return "<li class='mb-1'>" . $_x . "</li>";
  492. }, $matches)) . "</ol>";
  493. }
  494. }
  495. if (!function_exists('getDptClassFromGcnSeqNo')) {
  496. function getDptClassFromGcnSeqNo($_gcnSeqNo)
  497. {
  498. return DB::connection('pgsql_fdb')->select("
  499. SELECT distinct r1.dpt_class_id, r2.dpt_allowance, r2.dpt_class_desc
  500. FROM RDPTGC0_GCNSEQNO_LINK r1
  501. JOIN RDPTCL0_CLASS_ID r2 on r1.dpt_class_id = r2.dpt_class_id
  502. WHERE r1.gcn_seqno = :gcnSeqNo
  503. ",
  504. ['gcnSeqNo' => $_gcnSeqNo]
  505. );
  506. }
  507. }
  508. if (!function_exists('compareDPTs')) {
  509. function compareDPTs($_dptArray1, $_dptArray2)
  510. {
  511. $output = [];
  512. for ($i = 0; $i < count($_dptArray1); $i++) {
  513. for ($j = 0; $j < count($_dptArray2); $j++) {
  514. if ($_dptArray1[$i]->dpt_class_id == $_dptArray2[$j]->dpt_class_id) {
  515. $output[] = json_decode(json_encode([
  516. "dpt_allowance" => $_dptArray1[$i]->dpt_allowance,
  517. "dpt_class_desc" => $_dptArray1[$i]->dpt_class_desc
  518. ]));
  519. }
  520. }
  521. }
  522. return $output;
  523. }
  524. }
  525. if(!function_exists('coadministration_info')) {
  526. function coadministration_info($_drugs) {
  527. if(count($_drugs) < 2) return "";
  528. // collect gcn-seq-nos
  529. $gcnSeqNos = [];
  530. $rxMap = [];
  531. foreach ($_drugs as $drug) {
  532. if(@$drug->data && $drug->data->gcnSeqno) {
  533. $gcnSeqNos[] = $drug->data->gcnSeqno;
  534. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  535. }
  536. }
  537. if(!count($gcnSeqNos)) return $result;
  538. $gcnSeqNos = implode(',', array_map(function($_x) {
  539. return "'" . $_x . "'";
  540. }, $gcnSeqNos));
  541. $coadministration = DB::connection('pgsql_fdb')->select("
  542. SELECT distinct r1.coadmin_dosing_text
  543. FROM radige0_ddi_gcnseqno_except r1
  544. WHERE r1.side_a_gcn_seqno in ($gcnSeqNos) AND r1.side_b_gcn_seqno in ($gcnSeqNos)
  545. "
  546. );
  547. if(!$coadministration || !count($coadministration)) return '';
  548. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  549. return "<li class='mb-1'>" . $_x->coadmin_dosing_text . "</li>";
  550. }, $coadministration)) . "</ol>";
  551. return $coadministration;
  552. }
  553. }