FDBPGController.php 26 KB

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