FDBPGController.php 28 KB

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