FDBPGController.php 29 KB

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