fdb.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. <?php
  2. use Illuminate\Http\Request;
  3. use Illuminate\Support\Facades\DB;
  4. if(!function_exists('side_effects_info')) {
  5. function side_effects_info($_drugs) {
  6. $result = [];
  7. // collect gcn-seq-nos
  8. $gcnSeqNos = [];
  9. $rxMap = [];
  10. foreach ($_drugs as $drug) {
  11. if(@$drug->data && $drug->data->gcnSeqno) {
  12. $gcnSeqNos[] = $drug->data->gcnSeqno;
  13. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  14. }
  15. }
  16. if(!count($gcnSeqNos)) return $result;
  17. $gcnSeqNos = implode(',', array_map(function($_x) {
  18. return "'" . $_x . "'";
  19. }, $gcnSeqNos));
  20. $sides = DB::connection('pgsql_fdb')->select("
  21. SELECT r1.gcn_seqno, r1.side, sm.side_freq, sm.side_sev, sm.dxid, dx.dxid_desc56
  22. FROM rsidegc0_gcnseqno_link r1
  23. JOIN rsidema3_mstr sm ON r1.side = sm.side
  24. JOIN rfmldx0_dxid dx ON sm.dxid = dx.dxid
  25. WHERE r1.gcn_seqno IN (" . $gcnSeqNos . ")
  26. ORDER BY r1.gcn_seqno, sm.side_sev DESC, sm.side_freq ASC
  27. "
  28. );
  29. $seMap = [];
  30. foreach ($sides as $se) {
  31. if(!isset($seMap[$rxMap[$se->gcn_seqno]])) {
  32. $seMap[$rxMap[$se->gcn_seqno]] = [];
  33. }
  34. $seMap[$rxMap[$se->gcn_seqno]][] = [
  35. "gcn_seqno" => $se->gcn_seqno,
  36. "side_freq" => $se->side_freq,
  37. "side_sev" => $se->side_sev,
  38. "dxid_desc56" => $se->dxid_desc56,
  39. ];
  40. }
  41. $result = $seMap;
  42. return $result;
  43. }
  44. }
  45. if(!function_exists('precautions_info')) {
  46. function precautions_info($_drugs) {
  47. }
  48. }
  49. if(!function_exists('contraindications_info')) {
  50. function contraindications_info($_drugs) {
  51. $result = [];
  52. // collect routed med ids
  53. $routedMedIds = [];
  54. $rxMap = [];
  55. foreach ($_drugs as $drug) {
  56. if(@$drug->data && $drug->data->routedMedId) {
  57. $routedMedIds[] = $drug->data->routedMedId;
  58. $rxMap[$drug->data->routedMedId] = $drug->data->name;
  59. }
  60. }
  61. if(!count($routedMedIds)) return $result;
  62. $routedMedIds = implode(',', array_map(function($_x) {
  63. return "'" . $_x . "'";
  64. }, $routedMedIds));
  65. $contraindications = DB::connection('pgsql_fdb')->select("
  66. SELECT r1.routed_med_id, r1.ddxcn, r2.dxid, r2.ddxcn_sl, r3.dxid_desc56
  67. FROM rddcmrm0_routed_med_link r1
  68. JOIN rddcmma1_contra_mstr r2 ON r1.ddxcn = r2.ddxcn
  69. JOIN rfmldx0_dxid r3 ON r2.dxid = r3.dxid
  70. WHERE r1.routed_med_id IN (" . $routedMedIds . ")
  71. ORDER BY r1.routed_med_id, r3.dxid_desc56, r2.ddxcn_sl
  72. "
  73. );
  74. $ciMap = [];
  75. foreach ($contraindications as $ci) {
  76. if(!isset($ciMap[$rxMap[$ci->routed_med_id]])) {
  77. $ciMap[$rxMap[$ci->routed_med_id]] = [];
  78. }
  79. $ciMap[$rxMap[$ci->routed_med_id]][] = [
  80. "dxid_desc56" => $ci->dxid_desc56,
  81. ];
  82. }
  83. $result = $ciMap;
  84. return $result;
  85. }
  86. }
  87. if(!function_exists('drug_allergy_info')) {
  88. function drug_allergy_info($_drugs, $_allergies) {
  89. }
  90. }
  91. if(!function_exists('drug_drug_interaction_info')) {
  92. function drug_drug_interaction_info($_drugs) {
  93. if(count($_drugs) < 2) return "";
  94. $leftIndex = 0;
  95. $output = [];
  96. for ($i=$leftIndex; $i<count($_drugs) - 1; $i++) {
  97. for ($j=$i + 1; $j<count($_drugs); $j++) {
  98. $output[] = drugDrugInteractionSinglePair($_drugs[$i], $_drugs[$j]);
  99. }
  100. }
  101. $output = array_filter($output, function($_x) {
  102. return !!$_x;
  103. });
  104. return implode("<br>", $output);
  105. }
  106. }
  107. if(!function_exists('drugDrugInteractionSinglePair')) {
  108. function drugDrugInteractionSinglePair($_rx1, $_rx2)
  109. {
  110. $output = [];
  111. // get active ingredient DDI_CODEX values for drug 1 and 2
  112. $rx1ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx1->data->gcnSeqno);
  113. $rx2ActiveDdiCodex = getActiveDdiCodexFromGcnSeqNo($_rx2->data->gcnSeqno);
  114. if (!$rx1ActiveDdiCodex || !$rx2ActiveDdiCodex || !count($rx1ActiveDdiCodex) || !count($rx2ActiveDdiCodex)) return "";
  115. // dump($rx1_DDI_CODEX);
  116. // dump($rx2_DDI_CODEX);
  117. // get inactive ingredient DDI_CODEX values for drug 1 and 2
  118. // to get this we need to first get the NDCs of the drugs
  119. $rx1Ndc = getNdcFromMedId($_rx1->data->medId);
  120. $rx2Ndc = getNdcFromMedId($_rx2->data->medId);
  121. // dump($rx1Ndc);
  122. // dump($rx2Ndc);
  123. $rx1InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx1Ndc);
  124. $rx2InactiveDdiCodex = getInactiveDdiCodexFromNdc($rx2Ndc);
  125. // if(!$rx1InactiveDdiCodex || !$rx2InactiveDdiCodex || !count($rx1InactiveDdiCodex) || !count($rx2InactiveDdiCodex)) return "";
  126. // dump($rx1InactiveDdiCodex);
  127. // dump($rx2InactiveDdiCodex);
  128. // get ddi codex - monox pairs for drug 1 & 2
  129. $rx1ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1ActiveDdiCodex);
  130. $rx1InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx1InactiveDdiCodex);
  131. $rx2ActiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2ActiveDdiCodex);
  132. $rx2InactiveDdiCodexMonoxPairs = getDdiCodexMonoxPairs($rx2InactiveDdiCodex);
  133. // dump($rx1ActiveDdiCodexMonoxPairs);
  134. // dump($rx1InactiveDdiCodexMonoxPairs);
  135. // dump($rx2ActiveDdiCodexMonoxPairs);
  136. // dump($rx2InactiveDdiCodexMonoxPairs);
  137. // compare 1-active to 2-active and 2-inactive
  138. $activeCatches = [];
  139. foreach ($rx1ActiveDdiCodexMonoxPairs as $compareLeft) {
  140. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  141. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  142. $activeCatches[] = $compareLeft->ddi_codex;
  143. }
  144. }
  145. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  146. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  147. $activeCatches[] = $compareLeft->ddi_codex;
  148. }
  149. }
  150. }
  151. // compare 1-inactive to 2-active and 2-inactive
  152. $inactiveCatches = [];
  153. foreach ($rx1InactiveDdiCodexMonoxPairs as $compareLeft) {
  154. foreach ($rx2ActiveDdiCodexMonoxPairs as $compareRight) {
  155. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  156. $inactiveCatches[] = $compareLeft->ddi_codex;
  157. }
  158. }
  159. foreach ($rx2InactiveDdiCodexMonoxPairs as $compareRight) {
  160. if ($compareLeft->ddi_monox == $compareRight->ddi_monox && $compareLeft->ddi_codex != $compareRight->ddi_codex) {
  161. $inactiveCatches[] = $compareLeft->ddi_codex;
  162. }
  163. }
  164. }
  165. if (count($activeCatches)) {
  166. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more active ingredients in <b>{$_rx1->data->name}</b>.";
  167. }
  168. if (count($inactiveCatches)) {
  169. $output[] = "<b>{$_rx2->data->name}</b> interacts with one or more inactive ingredients in <b>{$_rx1->data->name}</b>.";
  170. }
  171. // TODO: find out and show the names of the actual ingredients causing interaction
  172. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  173. return "<li class='mb-1'>" . $_x . "</li>";
  174. }, $output)) . "</ol>";
  175. }
  176. }
  177. if(!function_exists('getActiveDdiCodexFromGcnSeqNo')) {
  178. function getActiveDdiCodexFromGcnSeqNo($_gcnSeqNo)
  179. {
  180. $ddiCodexArray = [];
  181. $query = DB::connection('pgsql_fdb')->select("
  182. SELECT r1.ddi_codex
  183. FROM RADIMGC4_GCNSEQNO_LINK r1
  184. WHERE r1.gcn_seqno = :gcnSeqNo
  185. ",
  186. ['gcnSeqNo' => $_gcnSeqNo]
  187. );
  188. if (count($query)) {
  189. $ddiCodexArray = array_map(function ($_x) {
  190. return $_x->ddi_codex;
  191. }, $query);
  192. }
  193. return $ddiCodexArray;
  194. }
  195. }
  196. if(!function_exists('getNdcFromMedId')) {
  197. function getNdcFromMedId($_medId)
  198. {
  199. $ndcArray = [];
  200. $query = DB::connection('pgsql_fdb')->select("
  201. select ndc from rmindc1_ndc_medid where medid = :medId
  202. ",
  203. ['medId' => $_medId]
  204. );
  205. if (count($query)) {
  206. $ndcArray = array_map(function ($_x) {
  207. return $_x->ndc;
  208. }, $query);
  209. }
  210. return $ndcArray;
  211. }
  212. }
  213. if(!function_exists('getInactiveDdiCodexFromNdc')) {
  214. function getInactiveDdiCodexFromNdc($_ndc)
  215. {
  216. $ddiCodexArray = [];
  217. if(!count($_ndc)) return $ddiCodexArray;
  218. $query = DB::connection('pgsql_fdb')->select("
  219. SELECT distinct r1.ddi_codex
  220. FROM RDDIMIN0_NDC_INACTV_DDIM_LINK r1
  221. WHERE r1.ddi_ndc IN (" . implode(',', array_map(function ($_x) {
  222. return "'" . $_x . "'";
  223. }, $_ndc)) . ")
  224. "
  225. );
  226. if (count($query)) {
  227. $ddiCodexArray = array_map(function ($_x) {
  228. return $_x->ddi_codex;
  229. }, $query);
  230. }
  231. return $ddiCodexArray;
  232. }
  233. }
  234. if(!function_exists('getDdiCodexMonoxPairs')) {
  235. function getDdiCodexMonoxPairs($_ddiCodexArray)
  236. {
  237. $ddiCodexMonoxPairsArray = [];
  238. if(!count($_ddiCodexArray)) return $ddiCodexMonoxPairsArray;
  239. if (count($_ddiCodexArray)) {
  240. $ddiCodexMonoxPairsArray = DB::connection('pgsql_fdb')->select("
  241. SELECT r1.ddi_codex, r1.ddi_monox
  242. FROM RADIMMA5_MSTR r1
  243. WHERE r1.ddi_codex IN (" . implode(',', array_map(function ($_x) {
  244. return "'" . $_x . "'";
  245. }, $_ddiCodexArray)) . ")
  246. "
  247. );
  248. }
  249. return $ddiCodexMonoxPairsArray;
  250. }
  251. }
  252. if(!function_exists('duplicate_therapy_info')) {
  253. function duplicate_therapy_info($_drugs) {
  254. $dptClasses = [];
  255. foreach ($_drugs as $drug) {
  256. $drug->dpt = getDptClassFromGcnSeqNo($drug->data->gcnSeqno);
  257. }
  258. $leftIndex = 0;
  259. $matches = [];
  260. for ($i = $leftIndex; $i < count($_drugs) - 1; $i++) {
  261. for ($j = $i + 1; $j < count($_drugs); $j++) {
  262. $compareResult = compareDPTs($_drugs[$i]->dpt, $_drugs[$j]->dpt);
  263. foreach ($compareResult as $c) {
  264. $matches[] = "<b>{$_drugs[$i]->data->name}</b> and <b>{$_drugs[$j]->data->name}</b> both participate in the duplicate therapy class <b>{$c->dpt_class_desc}</b> (duplicates allowed: {$c->dpt_allowance})";
  265. }
  266. }
  267. }
  268. if(!count($matches)) return '';
  269. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  270. return "<li class='mb-1'>" . $_x . "</li>";
  271. }, $matches)) . "</ol>";
  272. }
  273. }
  274. if (!function_exists('getDptClassFromGcnSeqNo')) {
  275. function getDptClassFromGcnSeqNo($_gcnSeqNo)
  276. {
  277. return DB::connection('pgsql_fdb')->select("
  278. SELECT distinct r1.dpt_class_id, r2.dpt_allowance, r2.dpt_class_desc
  279. FROM RDPTGC0_GCNSEQNO_LINK r1
  280. JOIN RDPTCL0_CLASS_ID r2 on r1.dpt_class_id = r2.dpt_class_id
  281. WHERE r1.gcn_seqno = :gcnSeqNo
  282. ",
  283. ['gcnSeqNo' => $_gcnSeqNo]
  284. );
  285. }
  286. }
  287. if (!function_exists('compareDPTs')) {
  288. function compareDPTs($_dptArray1, $_dptArray2)
  289. {
  290. $output = [];
  291. for ($i = 0; $i < count($_dptArray1); $i++) {
  292. for ($j = 0; $j < count($_dptArray2); $j++) {
  293. if ($_dptArray1[$i]->dpt_class_id == $_dptArray2[$j]->dpt_class_id) {
  294. $output[] = json_decode(json_encode([
  295. "dpt_allowance" => $_dptArray1[$i]->dpt_allowance,
  296. "dpt_class_desc" => $_dptArray1[$i]->dpt_class_desc
  297. ]));
  298. }
  299. }
  300. }
  301. return $output;
  302. }
  303. }
  304. if(!function_exists('coadministration_info')) {
  305. function coadministration_info($_drugs) {
  306. if(count($_drugs) < 2) return "";
  307. // collect gcn-seq-nos
  308. $gcnSeqNos = [];
  309. $rxMap = [];
  310. foreach ($_drugs as $drug) {
  311. if(@$drug->data && $drug->data->gcnSeqno) {
  312. $gcnSeqNos[] = $drug->data->gcnSeqno;
  313. $rxMap[$drug->data->gcnSeqno] = $drug->data->name;
  314. }
  315. }
  316. if(!count($gcnSeqNos)) return $result;
  317. $gcnSeqNos = implode(',', array_map(function($_x) {
  318. return "'" . $_x . "'";
  319. }, $gcnSeqNos));
  320. $coadministration = DB::connection('pgsql_fdb')->select("
  321. SELECT distinct r1.coadmin_dosing_text
  322. FROM radige0_ddi_gcnseqno_except r1
  323. WHERE r1.side_a_gcn_seqno in ($gcnSeqNos) AND r1.side_b_gcn_seqno in ($gcnSeqNos)
  324. "
  325. );
  326. if(!$coadministration || !count($coadministration)) return '';
  327. return "<ol class='pl-0 ml-3 mb-0'>" . implode("", array_map(function ($_x) {
  328. return "<li class='mb-1'>" . $_x->coadmin_dosing_text . "</li>";
  329. }, $coadministration)) . "</ol>";
  330. return $coadministration;
  331. }
  332. }