FDBPGController.php 29 KB

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