FDBPGController.php 23 KB

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