FDBPGController.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738
  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. r2.dam_concept_id_typ_desc as sub_text
  230. FROM rdamca0_concept r1 join rdamcd0_picklist_con_typ_desc r2 on r1.dam_concept_id_typ = r2.dam_concept_id_typ
  231. WHERE (r1.dam_concept_id_desc ILIKE :term)
  232. ORDER BY r1.dam_concept_id_desc
  233. ",
  234. ['term' => '%' . $term . '%']
  235. );
  236. return json_encode([
  237. "success" => true,
  238. "data" => $matches
  239. ]);
  240. }
  241. // drug <-> allergy match making
  242. public function drugAllergies(Request $request) {
  243. // override
  244. if($request->input('test')) {
  245. $x = new \stdClass();
  246. $x->allergen = 'Pollen';
  247. $x->dam_concept_id_typ = 6;
  248. $allergies = [$x];
  249. $y = new \stdClass();
  250. $y->rx = 'Brufen';
  251. $rx = [$y];
  252. }
  253. else {
  254. $input = json_decode($request->input('data'));
  255. $allergies = $input->allergies;
  256. $rx = $input->rx;
  257. }
  258. $output = [];
  259. /*
  260. for each allergy
  261. if dam_concept_id_typ = 1 // allergen-group-id
  262. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+a+DAM_ALRGN_GRP+Allergen+-+Illustration+of+Scenario+C
  263. ...
  264. elseif dam_concept_id_typ = 2 // medication-name-id
  265. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+a+MED_NAME_ID+Allergen+-+Illustration+of+Scenario+B
  266. ...
  267. elseif dam_concept_id_typ = 6 // base-ingredient-id
  268. // https://docs.fdbhealth.com/display/MKDOCUS/Screening+an+NDC+for+an+Ingredient+Allergen+-+Illustration+of+Scenario+A
  269. ...
  270. endif
  271. endfor
  272. */
  273. foreach ($allergies as $allergy) {
  274. foreach ($rx as $rxItem) {
  275. if($allergy->dam_concept_id_typ == 6) { // ingredient
  276. if ($this->drugAllergyIngredientAllergenVsSingleRx($allergy, $rxItem)) {
  277. $output[] = "<b>{$rxItem->rx}</b> can cause allergic reactions since the patient is allergic to <b>{$allergy->allergen}</b>.";
  278. }
  279. }
  280. else if($allergy->dam_concept_id_typ == 2) { // medication
  281. if ($this->drugAllergyMedicationAllergenVsSingleRx($allergy, $rxItem)) {
  282. $output[] = "<b>{$rxItem->rx}</b> can cause allergic reactions since the patient is allergic to <b>{$allergy->allergen}</b>.";
  283. }
  284. }
  285. else if($allergy->dam_concept_id_typ == 1) { // allergen group
  286. if ($this->drugAllergyGroupAllergenVsSingleRx($allergy, $rxItem)) {
  287. $output[] = "<b>{$rxItem->rx}</b> can cause allergic reactions since the patient is allergic to <b>{$allergy->allergen}</b>.";
  288. }
  289. }
  290. }
  291. }
  292. return implode("<br>", $output);
  293. }
  294. private function drugAllergyIngredientAllergenVsSingleRx($_allergen, $_rx) {
  295. $matches = DB::connection('pgsql_fdb')->select("
  296. (
  297. -- ingredients from medication
  298. SELECT R1.related_hic_seqn as hic_seqn, R2.hic_desc
  299. FROM RHICHCR0_HIC_HIC_LINK R1
  300. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  301. WHERE R1.hic_seqn IN (
  302. SELECT S2.dam_alrgn_hic_seqn
  303. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  304. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  305. WHERE S1.med_concept_id = :medid
  306. AND S1.med_concept_id_typ = 3
  307. )
  308. )
  309. INTERSECT
  310. (
  311. -- all ingredients directly and related from allergens
  312. (
  313. SELECT R1.related_hic_seqn as hic_seqn, R2.hic_desc
  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 = :allergenConceptID
  317. )
  318. UNION
  319. -- all ingredients via related dam allergen groups
  320. (
  321. SELECT r3.hic_seqn, r4.hic_desc
  322. FROM RDAMGHC0_HIC_ALRGN_GRP_LINK R1
  323. JOIN rdamagd1_alrgn_grp_desc R2 on R1.dam_alrgn_grp = R2.dam_alrgn_grp
  324. JOIN RDAMGHC0_HIC_ALRGN_GRP_LINK R3 on R1.dam_alrgn_grp = R3.dam_alrgn_grp
  325. JOIN RHICD5_HIC_DESC R4 on r3.hic_seqn = r4.hic_seqn
  326. WHERE R1.hic_seqn = :allergenConceptID
  327. AND R2.dam_alrgn_grp_status_cd = 0
  328. ORDER BY r3.hic_seqn
  329. )
  330. )
  331. ",
  332. ['medid' => $_rx->medid, 'allergenConceptID' => $_allergen->dam_concept_id]
  333. );
  334. return !!count($matches);
  335. }
  336. private function drugAllergyMedicationAllergenVsSingleRx($_allergen, $_rx) {
  337. $matches = DB::connection('pgsql_fdb')->select("
  338. (
  339. -- ingredients from medication
  340. SELECT R1.related_hic_seqn as hic_seqn
  341. FROM RHICHCR0_HIC_HIC_LINK R1
  342. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  343. WHERE R1.hic_seqn IN (
  344. SELECT S2.dam_alrgn_hic_seqn
  345. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  346. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  347. WHERE S1.med_concept_id = :medid
  348. AND S1.med_concept_id_typ = 3
  349. )
  350. )
  351. INTERSECT
  352. (
  353. -- all ingredients directly and related from allergens
  354. SELECT R1.dam_alrgn_hic_seqn as hic_seqn
  355. FROM RDAMHHA0_HIC_HICL_ALG_LINK R1
  356. WHERE R1.hicl_seqno IN (
  357. SELECT R1.hicl_seqno
  358. FROM RMEDMHL0_MED_HICLSEQNO_LINK R1
  359. WHERE R1.med_concept_id_typ = 1
  360. AND R1.med_concept_id = :allergenConceptID
  361. AND MED_CONCEPT_HICL_SRC_CD = 0
  362. )
  363. )
  364. ",
  365. ['medid' => $_rx->medid, 'allergenConceptID' => $_allergen->dam_concept_id]
  366. );
  367. return !!count($matches);
  368. }
  369. private function drugAllergyGroupAllergenVsSingleRx($_allergen, $_rx) {
  370. $matches = DB::connection('pgsql_fdb')->select("
  371. (
  372. -- ingredients from medication
  373. SELECT R1.related_hic_seqn as hic_seqn
  374. FROM RHICHCR0_HIC_HIC_LINK R1
  375. JOIN RHICD5_HIC_DESC R2 ON R1.related_hic_seqn = R2.hic_seqn
  376. WHERE R1.hic_seqn IN (
  377. SELECT S2.dam_alrgn_hic_seqn
  378. FROM RMEDMHL0_MED_HICLSEQNO_LINK S1
  379. JOIN RDAMHHA0_HIC_HICL_ALG_LINK S2 ON S1.hicl_seqno = S2.hicl_seqno
  380. WHERE S1.med_concept_id = :medid
  381. AND S1.med_concept_id_typ = 3
  382. )
  383. )
  384. INTERSECT
  385. (
  386. -- ingredients from medication allergen
  387. (
  388. SELECT R1.hic_seqn as hic_seqn
  389. FROM RDAMGHC0_HIC_ALRGN_GRP_LINK R1
  390. WHERE R1.DAM_ALRGN_GRP = :allergenConceptID
  391. )
  392. UNION
  393. (
  394. SELECT distinct s1.hic_seqn
  395. FROM RDAMXHC0_HIC_ALRGN_XSENSE_LINK S1
  396. WHERE S1.dam_alrgn_xsense IN (
  397. SELECT R1.dam_alrgn_xsense
  398. FROM RDAMGX0_ALRGN_GRP_XSENSE_LINK R1
  399. JOIN RDAMCSD1_XSENSIT_ALLERGY_DESC R2 on R1.dam_alrgn_xsense = R2.dam_alrgn_xsense
  400. WHERE R1.dam_alrgn_grp = :allergenConceptID
  401. AND R2.dam_alrgn_xsense_status_cd = 0
  402. )
  403. )
  404. )
  405. ",
  406. ['medid' => $_rx->medid, 'allergenConceptID' => $_allergen->dam_concept_id]
  407. );
  408. return !!count($matches);
  409. }
  410. // drug <-> drug match making
  411. public function drugDrugInteraction(Request $request) {
  412. if($request->input('test')) {
  413. // override
  414. $rx = json_decode(json_encode([
  415. [
  416. "gcn_seqno" => "45190",
  417. "med_name_id" => "18604",
  418. "medid" => "234539",
  419. "routed_dosage_form_med_id" => "95130",
  420. "routed_med_id" => "19876",
  421. "rx" => "Zyprexa Zydis",
  422. ],
  423. [
  424. "gcn_seqno" => "49853",
  425. "med_name_id" => "26164",
  426. "medid" => "400058",
  427. "routed_dosage_form_med_id" => "36194",
  428. "routed_med_id" => "32562",
  429. "rx" => "Orfadin",
  430. ],
  431. ]));
  432. }
  433. else {
  434. $input = json_decode($request->input('data'));
  435. $rx = $input->rx;
  436. }
  437. if(count($rx) < 2) return "";
  438. $leftIndex = 0;
  439. $output = [];
  440. for ($i=$leftIndex; $i<count($rx) - 1; $i++) {
  441. for ($j=$i + 1; $j<count($rx); $j++) {
  442. $output[] = $this->drugDrugInteractionSinglePair($rx[$i], $rx[$j]);
  443. }
  444. }
  445. $output = array_filter($output, function($_x) {
  446. return !!$_x;
  447. });
  448. return implode("<br>", $output);
  449. }
  450. private function drugDrugInteractionSinglePair($_rx1, $_rx2) {
  451. $output = [];
  452. // get active ingredient DDI_CODEX values for drug 1 and 2
  453. $rx1ActiveDdiCodex = $this->getActiveDdiCodexFromGcnSeqNo($_rx1->gcn_seqno);
  454. $rx2ActiveDdiCodex = $this->getActiveDdiCodexFromGcnSeqNo($_rx2->gcn_seqno);
  455. if(!$rx1ActiveDdiCodex || !$rx2ActiveDdiCodex || !count($rx1ActiveDdiCodex) || !count($rx2ActiveDdiCodex)) return "";
  456. // dump($rx1_DDI_CODEX);
  457. // dump($rx2_DDI_CODEX);
  458. // get inactive ingredient DDI_CODEX values for drug 1 and 2
  459. // to get this we need to first get the NDCs of the drugs
  460. $rx1Ndc = $this->getNdcFromMedId($_rx1->medid);
  461. $rx2Ndc = $this->getNdcFromMedId($_rx2->medid);
  462. // dump($rx1Ndc);
  463. // dump($rx2Ndc);
  464. $rx1InactiveDdiCodex = $this->getInactiveDdiCodexFromNdc($rx1Ndc);
  465. $rx2InactiveDdiCodex = $this->getInactiveDdiCodexFromNdc($rx2Ndc);
  466. // if(!$rx1InactiveDdiCodex || !$rx2InactiveDdiCodex || !count($rx1InactiveDdiCodex) || !count($rx2InactiveDdiCodex)) return "";
  467. // dump($rx1InactiveDdiCodex);
  468. // dump($rx2InactiveDdiCodex);
  469. // get ddi codex - monox pairs for drug 1 & 2
  470. $rx1ActiveDdiCodexMonoxPairs = $this->getDdiCodexMonoxPairs($rx1ActiveDdiCodex);
  471. $rx1InactiveDdiCodexMonoxPairs = $this->getDdiCodexMonoxPairs($rx1InactiveDdiCodex);
  472. $rx2ActiveDdiCodexMonoxPairs = $this->getDdiCodexMonoxPairs($rx2ActiveDdiCodex);
  473. $rx2InactiveDdiCodexMonoxPairs = $this->getDdiCodexMonoxPairs($rx2InactiveDdiCodex);
  474. // dump($rx1ActiveDdiCodexMonoxPairs);
  475. // dump($rx1InactiveDdiCodexMonoxPairs);
  476. // dump($rx2ActiveDdiCodexMonoxPairs);
  477. // dump($rx2InactiveDdiCodexMonoxPairs);
  478. // compare 1-active to 2-active and 2-inactive
  479. $activeCatches = [];
  480. foreach ($rx1ActiveDdiCodexMonoxPairs as $compareLeft) {
  481. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  482. if($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  483. $activeCatches[] = $compareLeft->ddi_codex;
  484. }
  485. }
  486. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  487. if($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  488. $activeCatches[] = $compareLeft->ddi_codex;
  489. }
  490. }
  491. }
  492. // compare 1-inactive to 2-active and 2-inactive
  493. $inactiveCatches = [];
  494. foreach ($rx1InactiveDdiCodexMonoxPairs as $compareLeft) {
  495. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  496. if($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  497. $inactiveCatches[] = $compareLeft->ddi_codex;
  498. }
  499. }
  500. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  501. if($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  502. $inactiveCatches[] = $compareLeft->ddi_codex;
  503. }
  504. }
  505. }
  506. if(count($activeCatches)) {
  507. $output[] = "<b>{$_rx2->rx}</b> interacts with one or more active ingredients in <b>{$_rx1->rx}</b>.";
  508. }
  509. if(count($inactiveCatches)) {
  510. $output[] = "<b>{$_rx2->rx}</b> interacts with one or more inactive ingredients in <b>{$_rx1->rx}</b>.";
  511. }
  512. // TODO: find out and show the names of the actual ingredients causing interaction
  513. return implode("<br>", $output);
  514. }
  515. private function getActiveDdiCodexFromGcnSeqNo($_gcnSeqNo) {
  516. $ddiCodexArray = [];
  517. $query = DB::connection('pgsql_fdb')->select("
  518. SELECT r1.ddi_codex
  519. FROM RADIMGC4_GCNSEQNO_LINK r1
  520. WHERE r1.gcn_seqno = :gcnSeqNo
  521. ",
  522. ['gcnSeqNo' => $_gcnSeqNo]
  523. );
  524. if(count($query)) {
  525. $ddiCodexArray = array_map(function($_x) {
  526. return $_x->ddi_codex;
  527. }, $query);
  528. }
  529. return $ddiCodexArray;
  530. }
  531. private function getNdcFromMedId($_medId) {
  532. $ndcArray = [];
  533. $query = DB::connection('pgsql_fdb')->select("
  534. select ndc from rmindc1_ndc_medid where medid = :medId
  535. ",
  536. ['medId' => $_medId]
  537. );
  538. if(count($query)) {
  539. $ndcArray = array_map(function($_x) {
  540. return $_x->ndc;
  541. }, $query);
  542. }
  543. return $ndcArray;
  544. }
  545. private function getInactiveDdiCodexFromNdc($_ndc) {
  546. $ddiCodexArray = [];
  547. $query = DB::connection('pgsql_fdb')->select("
  548. SELECT distinct r1.ddi_codex
  549. FROM RDDIMIN0_NDC_INACTV_DDIM_LINK r1
  550. WHERE r1.ddi_ndc IN (" . implode(',', array_map(function($_x) {return "'" . $_x . "'";}, $_ndc)) . ")
  551. "
  552. );
  553. if(count($query)) {
  554. $ddiCodexArray = array_map(function($_x) {
  555. return $_x->ddi_codex;
  556. }, $query);
  557. }
  558. return $ddiCodexArray;
  559. }
  560. private function getDdiCodexMonoxPairs($_ddiCodexArray) {
  561. $ddiCodexMonoxPairsArray = [];
  562. if(count($_ddiCodexArray)) {
  563. $ddiCodexMonoxPairsArray = DB::connection('pgsql_fdb')->select("
  564. SELECT r1.ddi_codex, r1.ddi_monox
  565. FROM RADIMMA5_MSTR r1
  566. WHERE r1.ddi_codex IN (" . implode(',', array_map(function($_x) {return "'" . $_x . "'";}, $_ddiCodexArray)) . ")
  567. "
  568. );
  569. }
  570. return $ddiCodexMonoxPairsArray;
  571. }
  572. // drug <-> drug coadministration notes
  573. public function drugCoadministration(Request $request) {
  574. $gcnSeqnos = $request->input('gcn-seqnos') ? trim($request->input('gcn-seqnos')) : '';
  575. if (empty($gcnSeqnos)) return '';
  576. //$gcnSeqnos = explode(",", $gcnSeqnos);
  577. $coadministration = DB::connection('pgsql_fdb')->select("
  578. SELECT distinct r1.coadmin_dosing_text
  579. FROM radige0_ddi_gcnseqno_except r1
  580. WHERE r1.side_a_gcn_seqno in ($gcnSeqnos) AND r1.side_b_gcn_seqno in ($gcnSeqnos)
  581. "
  582. );
  583. return view('app.fdb-pg.fdb-coadministration', compact('coadministration'));
  584. }
  585. // duplicate therapy indications
  586. public function duplicateTherapy(Request $request) {
  587. if($request->input('test')) {
  588. // override
  589. $rx = json_decode(json_encode([
  590. [
  591. "gcn_seqno" => "4376",
  592. "med_name_id" => "1076",
  593. "medid" => "172480",
  594. "routed_dosage_form_med_id" => "5870",
  595. "routed_med_id" => "1082",
  596. "rx" => "aspirin 325",
  597. ],
  598. [
  599. "gcn_seqno" => "4377",
  600. "med_name_id" => "1076",
  601. "medid" => "216092",
  602. "routed_dosage_form_med_id" => "5870",
  603. "routed_med_id" => "1082",
  604. "rx" => "aspirin 500",
  605. ],
  606. ]));
  607. }
  608. else {
  609. $input = json_decode($request->input('data'));
  610. $rx = $input->rx;
  611. }
  612. $dptClasses = [];
  613. foreach ($rx as $rxItem) {
  614. $rxItem->dpt = $this->getDptClassFromGcnSeqNo($rxItem->gcn_seqno);
  615. }
  616. // dd($rx);
  617. $leftIndex = 0;
  618. $matches = [];
  619. for ($i=$leftIndex; $i<count($rx) - 1; $i++) {
  620. for ($j=$i + 1; $j<count($rx); $j++) {
  621. $compareResult = $this->compareDPTs($rx[$i]->dpt, $rx[$j]->dpt);
  622. foreach ($compareResult as $c) {
  623. $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})";
  624. }
  625. }
  626. }
  627. return "<ol class='pl-0 ml-3'>" . implode("", array_map(function($_x) {
  628. return "<li class='mb-2'>" . $_x . "</li>";
  629. }, $matches)) . "</ol>";
  630. }
  631. private function getDptClassFromGcnSeqNo($_gcnSeqNo) {
  632. return DB::connection('pgsql_fdb')->select("
  633. SELECT distinct r1.dpt_class_id, r2.dpt_allowance, r2.dpt_class_desc
  634. FROM RDPTGC0_GCNSEQNO_LINK r1
  635. JOIN RDPTCL0_CLASS_ID r2 on r1.dpt_class_id = r2.dpt_class_id
  636. WHERE r1.gcn_seqno = :gcnSeqNo
  637. ",
  638. ['gcnSeqNo' => $_gcnSeqNo]
  639. );
  640. }
  641. private function compareDPTs($_dptArray1, $_dptArray2) {
  642. $output = [];
  643. for ($i = 0; $i < count($_dptArray1); $i++) {
  644. for ($j = 0; $j < count($_dptArray2); $j++) {
  645. if ($_dptArray1[$i]->dpt_class_id == $_dptArray2[$j]->dpt_class_id) {
  646. $output[] = json_decode(json_encode([
  647. "dpt_allowance" => $_dptArray1[$i]->dpt_allowance,
  648. "dpt_class_desc" => $_dptArray1[$i]->dpt_class_desc
  649. ]));
  650. }
  651. }
  652. }
  653. return $output;
  654. }
  655. public function rxVigilance(Request $request, Client $patient) {
  656. return view('app.fdb-pg.rx-vigilance', compact('patient'));
  657. }
  658. public function dxVigilance(Request $request, Client $patient) {
  659. return view('app.fdb-pg.dx-vigilance', compact('patient'));
  660. }
  661. public function allergyVigilance(Request $request, Client $patient) {
  662. return view('app.fdb-pg.allergy-vigilance', compact('patient'));
  663. }
  664. }