fdb.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more active ingredients in <b>{$_rx1->data->name}</b>.";
  360. }
  361. if (count($inactiveCatches)) {
  362. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more inactive ingredients in <b>{$_rx1->data->name}</b>.";
  363. }
  364. // TODO: find out and show the names of the actual ingredients causing interaction
  365. return implode("<br>", $output);
  366. }
  367. }
  368. if(!function_exists('getActiveDdiCodexFromGcnSeqNo')) {
  369. function getActiveDdiCodexFromGcnSeqNo($_gcnSeqNo)
  370. {
  371. $ddiCodexArray = [];
  372. $query = DB::connection('pgsql_fdb')->select("
  373. SELECT r1.ddi_codex
  374. FROM RADIMGC4_GCNSEQNO_LINK r1
  375. WHERE r1.gcn_seqno = :gcnSeqNo
  376. ",
  377. ['gcnSeqNo' => $_gcnSeqNo]
  378. );
  379. if (count($query)) {
  380. $ddiCodexArray = array_map(function ($_x) {
  381. return $_x->ddi_codex;
  382. }, $query);
  383. }
  384. return $ddiCodexArray;
  385. }
  386. }
  387. if(!function_exists('getNdcFromMedId')) {
  388. function getNdcFromMedId($_medId)
  389. {
  390. $ndcArray = [];
  391. $query = DB::connection('pgsql_fdb')->select("
  392. select ndc from rmindc1_ndc_medid where medid = :medId
  393. ",
  394. ['medId' => $_medId]
  395. );
  396. if (count($query)) {
  397. $ndcArray = array_map(function ($_x) {
  398. return $_x->ndc;
  399. }, $query);
  400. }
  401. return $ndcArray;
  402. }
  403. }
  404. if(!function_exists('getInactiveDdiCodexFromNdc')) {
  405. function getInactiveDdiCodexFromNdc($_ndc)
  406. {
  407. $ddiCodexArray = [];
  408. if(!count($_ndc)) return $ddiCodexArray;
  409. $query = DB::connection('pgsql_fdb')->select("
  410. SELECT distinct r1.ddi_codex
  411. FROM RDDIMIN0_NDC_INACTV_DDIM_LINK r1
  412. WHERE r1.ddi_ndc IN (" . implode(',', array_map(function ($_x) {
  413. return "'" . $_x . "'";
  414. }, $_ndc)) . ")
  415. "
  416. );
  417. if (count($query)) {
  418. $ddiCodexArray = array_map(function ($_x) {
  419. return $_x->ddi_codex;
  420. }, $query);
  421. }
  422. return $ddiCodexArray;
  423. }
  424. }
  425. if(!function_exists('getDdiCodexMonoxPairs')) {
  426. function getDdiCodexMonoxPairs($_ddiCodexArray)
  427. {
  428. $ddiCodexMonoxPairsArray = [];
  429. if(!count($_ddiCodexArray)) return $ddiCodexMonoxPairsArray;
  430. if (count($_ddiCodexArray)) {
  431. $ddiCodexMonoxPairsArray = DB::connection('pgsql_fdb')->select("
  432. SELECT r1.ddi_codex, r1.ddi_monox
  433. FROM RADIMMA5_MSTR r1
  434. WHERE r1.ddi_codex IN (" . implode(',', array_map(function ($_x) {
  435. return "'" . $_x . "'";
  436. }, $_ddiCodexArray)) . ")
  437. "
  438. );
  439. }
  440. return $ddiCodexMonoxPairsArray;
  441. }
  442. }
  443. if(!function_exists('duplicate_therapy_info')) {
  444. function duplicate_therapy_info($_drugs) {
  445. if(!$_drugs || count($_drugs) < 2) return '';
  446. $dptClasses = [];
  447. foreach ($_drugs as $drug) {
  448. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  449. }
  450. $leftIndex = 0;
  451. $matches = [];
  452. // convert to simple array
  453. $drugsArray = [];
  454. foreach ($_drugs as $drug) {
  455. $drugsArray[] = $drug;
  456. }
  457. for ($i = $leftIndex; $i < count($drugsArray) - 1; $i++) {
  458. for ($j = $i + 1; $j < count($drugsArray); $j++) {
  459. $compareResult = compareDPTs($drugsArray[$i]->dpt, $drugsArray[$j]->dpt);
  460. foreach ($compareResult as $c) {
  461. $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})";
  462. }
  463. }
  464. }
  465. if(!count($matches)) return '';
  466. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  467. return "<li class='mb-1'>" . $_x . "</li>";
  468. }, $matches)) . "</ol>";
  469. }
  470. }
  471. if(!function_exists('duplicate_therapy_info_with_pivot')) {
  472. function duplicate_therapy_info_with_pivot($_pivot, $_drugs) {
  473. if(!$_drugs || count($_drugs) < 1) return '';
  474. $_pivot->dpt = getDptClassFromGcnSeqNo($_pivot->data->gcnSeqno);
  475. $dptClasses = [];
  476. foreach ($_drugs as $drug) {
  477. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  478. }
  479. $leftIndex = 0;
  480. $matches = [];
  481. foreach ($_drugs as $drug) {
  482. $compareResult = compareDPTs($_pivot->dpt, $drug->dpt);
  483. foreach ($compareResult as $c) {
  484. $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})";
  485. }
  486. }
  487. if(!count($matches)) return '';
  488. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  489. return "<li class='mb-1'>" . $_x . "</li>";
  490. }, $matches)) . "</ol>";
  491. }
  492. }
  493. if (!function_exists('getDptClassFromGcnSeqNo')) {
  494. function getDptClassFromGcnSeqNo($_gcnSeqNo)
  495. {
  496. return DB::connection('pgsql_fdb')->select("
  497. SELECT distinct r1.dpt_class_id, r2.dpt_allowance, r2.dpt_class_desc
  498. FROM RDPTGC0_GCNSEQNO_LINK r1
  499. JOIN RDPTCL0_CLASS_ID r2 on r1.dpt_class_id = r2.dpt_class_id
  500. WHERE r1.gcn_seqno = :gcnSeqNo
  501. ",
  502. ['gcnSeqNo' => $_gcnSeqNo]
  503. );
  504. }
  505. }
  506. if (!function_exists('compareDPTs')) {
  507. function compareDPTs($_dptArray1, $_dptArray2)
  508. {
  509. $output = [];
  510. for ($i = 0; $i < count($_dptArray1); $i++) {
  511. for ($j = 0; $j < count($_dptArray2); $j++) {
  512. if ($_dptArray1[$i]->dpt_class_id == $_dptArray2[$j]->dpt_class_id) {
  513. $output[] = json_decode(json_encode([
  514. "dpt_allowance" => $_dptArray1[$i]->dpt_allowance,
  515. "dpt_class_desc" => $_dptArray1[$i]->dpt_class_desc
  516. ]));
  517. }
  518. }
  519. }
  520. return $output;
  521. }
  522. }
  523. if(!function_exists('coadministration_info')) {
  524. function coadministration_info($_drugs) {
  525. if(count($_drugs) < 2) return "";
  526. // collect gcn-seq-nos
  527. $gcnSeqNos = [];
  528. $rxMap = [];
  529. foreach ($_drugs as $drug) {
  530. if(@$drug->data && $drug->data->gcnSeqno) {
  531. $gcnSeqNos[] = $drug->data->gcnSeqno;
  532. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  533. }
  534. }
  535. if(!count($gcnSeqNos)) return $result;
  536. $gcnSeqNos = implode(',', array_map(function($_x) {
  537. return "'" . $_x . "'";
  538. }, $gcnSeqNos));
  539. $coadministration = DB::connection('pgsql_fdb')->select("
  540. SELECT distinct r1.coadmin_dosing_text
  541. FROM radige0_ddi_gcnseqno_except r1
  542. WHERE r1.side_a_gcn_seqno in ($gcnSeqNos) AND r1.side_b_gcn_seqno in ($gcnSeqNos)
  543. "
  544. );
  545. if(!$coadministration || !count($coadministration)) return '';
  546. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  547. return "<li class='mb-1'>" . $_x->coadmin_dosing_text . "</li>";
  548. }, $coadministration)) . "</ol>";
  549. return $coadministration;
  550. }
  551. }