PatientService.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863
  1. <?php
  2. require_once('vo/com/imt/intimamedia/vo/PatientVo.php');
  3. require_once('vo/com/imt/intimamedia/vo/AppointmentVo.php');
  4. require_once('vo/com/imt/intimamedia/vo/TobaccoVo.php');
  5. require_once('vo/com/imt/intimamedia/vo/AtsVo.php');
  6. require_once('vo/com/imt/intimamedia/vo/ReturnObjectVo.php');
  7. require_once('vo/com/imt/intimamedia/vo/PersonalHistoryVo.php');
  8. require_once('vo/com/imt/intimamedia/vo/FamilyHistoryVo.php');
  9. require_once('vo/com/imt/intimamedia/vo/CvExaminatorVo.php');
  10. require_once('vo/com/imt/intimamedia/vo/TreatmentVo.php');
  11. require_once('common/SQLServerManager.php');
  12. require_once('common/SecurityManager.php');
  13. class PatientService
  14. {
  15. public function createPatient(PatientVo $patientVo, AppointmentVo $appointmentVo, $personId, $token)
  16. {
  17. $returnObjectVo = new ReturnObjectVo();
  18. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  19. $query = "SELECT fk_organization FROM [evolucare].[dbo].[t_person] WHERE id = '". $personId ."'";
  20. $resultsOrga = SQLServerManager::queryOnDatabase( $query );
  21. foreach( $resultsOrga as $value )
  22. {
  23. $fk_organization = $value->fk_organization;
  24. }
  25. $query = "INSERT INTO [evolucare].[dbo].[t_patient] (uid_physician, first_name, last_name, creation, sex, birth_date, birth_country, ethnic_group, last_visit, fk_physician, fk_organization) VALUES ('".
  26. $patientVo->uid ."','". SQLServerManager::convertStringToSQLServer( $patientVo->firstName ) ."','".
  27. SQLServerManager::convertStringToSQLServer( $patientVo->lastName ) ."','".
  28. date( "Y-m-d", $patientVo->creation/1000 ) ."','". $patientVo->sex ."','".
  29. date( "Y-m-d", $patientVo->birthDate/1000 ) ."', '". $patientVo->birthCountry ."', '". $patientVo->ethnicGroup ."', '".
  30. date( "Y-m-d", $patientVo->lastVisit/1000 )."', " . $personId .", ". $fk_organization .")";
  31. $results = SQLServerManager::queryOnDatabase( $query );
  32. if( $results == "exception")
  33. {
  34. Throw new Exception("E007");
  35. }
  36. $query = "SELECT id FROM [evolucare].[dbo].[t_patient] WHERE uid_physician = '". $patientVo->uid ."'";
  37. $results = SQLServerManager::queryOnDatabase( $query );
  38. foreach( $results as $value )
  39. {
  40. $patientId = $value->id;
  41. }
  42. $query = "INSERT INTO [intimamedia_physician].[dbo].[tj_appointment] ( number, date, weight, height, fk_patient, fk_person, fk_live_country ) VALUES (".
  43. $appointmentVo->number .",'". date( "Y-m-d", $appointmentVo->date/1000 ) ."',". $appointmentVo->weight .",".
  44. $appointmentVo->height .",". $patientId .", ". $personId.", '". $appointmentVo->livingCountry ."')";
  45. $results = SQLServerManager::queryOnDatabase( $query );
  46. if($results == "exception")
  47. {
  48. Throw new Exception("E007");
  49. }
  50. $returnObjectVo->value = $patientId;
  51. return $returnObjectVo;
  52. }
  53. public function searchPatient($criteria, $id_organization, $token)
  54. {
  55. $returnObjectVo = new ReturnObjectVo();
  56. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  57. /*
  58. $names = explode(" ", $criteria);
  59. if (sizeof($names) > 1)
  60. {
  61. $query = "SELECT pa.* FROM [evolucare].[dbo].[t_patient] as pa WHERE
  62. (( pa.last_name LIKE '%". $names[0] ."%'
  63. AND pa.first_name LIKE '%". $names[1] ."%')
  64. OR
  65. ( pa.last_name LIKE '%". $names[1] ."%'
  66. AND pa.first_name LIKE '%". $names[0] ."%'))
  67. AND fk_organization = ". $id_organization .";";
  68. }
  69. else
  70. {
  71. $query = "SELECT pa.* FROM [evolucare].[dbo].[t_patient] as pa WHERE
  72. ( pa.last_name LIKE '%". $names[0] ."%'
  73. OR pa.first_name LIKE '%". $names[0] ."%')
  74. AND fk_organization = ". $id_organization .";";
  75. }
  76. */
  77. $query = "SELECT pa.* FROM [evolucare].[dbo].[t_patient] as pa WHERE fk_organization = ". $id_organization .";";
  78. $results = SQLServerManager::queryOnDatabase( $query );
  79. if ($results == "exception")
  80. {
  81. Throw new Exception("E009");
  82. }
  83. $resultArray = array();
  84. foreach( $results as $value )
  85. {
  86. $patientVo = new PatientVo();
  87. $patientVo->id = $value->id;
  88. $patientVo->firstName = trim( $value->first_name );
  89. $patientVo->lastName = trim( $value->last_name );
  90. $patientVo->sex = trim( $value->sex );
  91. $patientVo->uid = trim( $value->uid_physician );
  92. $patientVo->birthCountry = trim( $value->birth_country );
  93. $patientVo->ethnicGroup = trim( $value->ethnic_group );
  94. $patientVo->creation = $value->creation->format('Y-m-d');
  95. $patientVo->birthDate = $value->birth_date->format('Y-m-d');
  96. $patientVo->lastVisit = $value->last_visit->format('Y-m-d');
  97. array_push( $resultArray, $patientVo );
  98. }
  99. /* $ff=fopen("d:/tmp.jd", "a+");
  100. fprintf($ff, "searchPatient ---------------------------\n");
  101. fclose($ff);*/
  102. $returnObjectVo->value = $resultArray;
  103. return $returnObjectVo;
  104. }
  105. public function searchAppointments($criteria, $token)
  106. {
  107. $returnObjectVo = new ReturnObjectVo();
  108. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  109. $query = "SELECT [intimamedia_physician].[dbo].[tj_appointment].id as appointmentId, * FROM [intimamedia_physician].[dbo].[tj_appointment], [evolucare].[dbo].[t_patient] WHERE".
  110. " tj_appointment.fk_patient = t_patient.id AND t_patient.uid_physician = '". $criteria ."' ORDER BY number ASC";
  111. /* $ff=fopen("d:/tmp.jd", "a+");
  112. fprintf($ff, "-------------------- searchAppointments, patient=%s\n", $criteria);
  113. fprintf($ff, "searchAppointments, query=%s\n", $query);
  114. fclose($ff);*/
  115. $results = SQLServerManager::queryOnDatabase( $query );
  116. $resultArray = array();
  117. foreach( $results as $value )
  118. {
  119. $appointmentVo = new AppointmentVo();
  120. $appointmentVo->id = $value->appointmentId;
  121. $appointmentVo->number = $value->number;
  122. $appointmentVo->date = $value->date->format('Y-m-d');
  123. $appointmentVo->weight = $value->weight;
  124. $appointmentVo->height = $value->height;
  125. $appointmentVo->livingCountry = $value->fk_live_country;
  126. $appointmentVo->patientId = $value->fk_patient;
  127. $query = "SELECT collection FROM [intimamedia_physician].[dbo].[t_image] WHERE fk_appointment = ". $appointmentVo->id;
  128. $resultsType = SQLServerManager::queryOnDatabase( $query );
  129. foreach( $resultsType as $typeValue )
  130. $appointmentVo->type = $typeValue->collection;
  131. $tobaccoVo = new TobaccoVo();
  132. if( $value->fk_tobacco != null )
  133. {
  134. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_tobacco] WHERE id = ". $value->fk_tobacco;
  135. $resultsTobacco = SQLServerManager::queryOnDatabase( $query );
  136. foreach( $resultsTobacco as $tobaccoValue )
  137. {
  138. $tobaccoVo->smoke = $tobaccoValue->smoke;
  139. $tobaccoVo->age = $tobaccoValue->age;
  140. $tobaccoVo->cigarettesPerDay = $tobaccoValue->cigarettes_per_day;
  141. $tobaccoVo->ageStopSmoking = $tobaccoValue->age_stop_smoking;
  142. $tobaccoVo->paquetYear = $tobaccoValue->paquet_year;
  143. }
  144. $appointmentVo->tobacco = $tobaccoVo;
  145. }
  146. $atsVo = new AtsVo();
  147. if( $value->fk_risk_ats != null )
  148. {
  149. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_risk_ats] WHERE id = ". $value->fk_risk_ats;
  150. $resultsAts = SQLServerManager::queryOnDatabase( $query );
  151. foreach( $resultsAts as $atsValue )
  152. {
  153. $atsVo->hta = $atsValue->hta;
  154. $atsVo->htaDuration = $atsValue->hta_duration;
  155. $atsVo->htg = $atsValue->htg;
  156. $atsVo->htgDuration = $atsValue->htg_duration;
  157. $atsVo->hct = $atsValue->hct;
  158. $atsVo->hctDuration = $atsValue->hct_duration;
  159. $atsVo->diabete = $atsValue->diabete;
  160. $atsVo->diabeteDuration = $atsValue->diabete_duration;
  161. }
  162. $appointmentVo->ats = $atsVo;
  163. }
  164. $personalHistoryVo = new PersonalHistoryVo();
  165. if( $value->fk_personal_history != null )
  166. {
  167. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_personal_history] WHERE id = ". $value->fk_personal_history;
  168. $resultsPersonalHistory = SQLServerManager::queryOnDatabase( $query );
  169. foreach( $resultsPersonalHistory as $personalHistoryValue )
  170. {
  171. $personalHistoryVo->coronaryDisease = $personalHistoryValue->coronary_disease;
  172. $personalHistoryVo->typeCoronaryDisease = $personalHistoryValue->fk_coronary_disease;
  173. $personalHistoryVo->lastCoronaryDiseaseAge = strval($personalHistoryValue->last_coronary_disease_age);
  174. $personalHistoryVo->aic = $personalHistoryValue->aic;
  175. $personalHistoryVo->lastAicAge = strval($personalHistoryValue->last_aic_age);
  176. $personalHistoryVo->numberAic = $personalHistoryValue->number_aic;
  177. $personalHistoryVo->typeAic = $personalHistoryValue->fk_type_aic;
  178. $personalHistoryVo->ethiologyAic = strval($personalHistoryValue->fk_type_ethiology);
  179. }
  180. $appointmentVo->personalHistory = $personalHistoryVo;
  181. }
  182. $familyHistoryVo = new FamilyHistoryVo();
  183. if( $value->fk_family_history != null )
  184. {
  185. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_family_history] WHERE id = ". $value->fk_family_history;
  186. $resultsFamilyHistory = SQLServerManager::queryOnDatabase( $query );
  187. foreach( $resultsFamilyHistory as $familyHistoryValue )
  188. {
  189. $familyHistoryVo->fatherCoronaryPathology = $familyHistoryValue->father_coronary_pathology;
  190. $familyHistoryVo->fatherCoronaryPathologyAge = $familyHistoryValue->father_coronary_pathology_age;
  191. $familyHistoryVo->motherCoronaryPathology = $familyHistoryValue->mother_coronary_pathology;
  192. $familyHistoryVo->motherCoronaryPathologyAge = $familyHistoryValue->mother_coronary_pathology_age;
  193. $familyHistoryVo->bsCoronaryPathology = $familyHistoryValue->bs_coronary_pathology;
  194. $familyHistoryVo->bsCoronaryPathologyAge = $familyHistoryValue->bs_coronary_pathology_age;
  195. $familyHistoryVo->fatherVascularPathology = $familyHistoryValue->father_vascular_pathology;
  196. $familyHistoryVo->fatherVascularPathologyAge = $familyHistoryValue->father_vascular_pathology_age;
  197. $familyHistoryVo->motherVascularPathology = $familyHistoryValue->mother_vascular_pathology;
  198. $familyHistoryVo->motherVascularPathologyAge = $familyHistoryValue->mother_vascular_pathology_age;
  199. $familyHistoryVo->bsVascularPathology = $familyHistoryValue->bs_vascular_pathology;
  200. $familyHistoryVo->bsVascularPathologyAge = $familyHistoryValue->bs_vascular_pathology_age;
  201. }
  202. $appointmentVo->familyHistory = $familyHistoryVo;
  203. }
  204. $cvExaminatorVo = new CvExaminatorVo();
  205. if( $value->fk_cv_examination != null )
  206. {
  207. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_cv_examination] WHERE id = ". $value->fk_cv_examination;
  208. $resultsCVExaminator = SQLServerManager::queryOnDatabase( $query );
  209. foreach( $resultsCVExaminator as $cvExaminatorValue )
  210. {
  211. $cvExaminatorVo->frequency = $cvExaminatorValue->frequency;
  212. $cvExaminatorVo->arythmia = $cvExaminatorValue->arythmia;
  213. $cvExaminatorVo->pressureLeftMaxArm = $cvExaminatorValue->pressure_left_max_arm;
  214. $cvExaminatorVo->pressureRightMaxArm = $cvExaminatorValue->pressure_right_max_arm;
  215. $cvExaminatorVo->pressureLeftMaxLeg = $cvExaminatorValue->pressure_left_max_leg;
  216. $cvExaminatorVo->pressureRightMaxLeg = $cvExaminatorValue->pressure_right_max_leg;
  217. $cvExaminatorVo->pressureLeftMinArm = $cvExaminatorValue->pressure_left_min_arm;
  218. $cvExaminatorVo->pressureRightMinArm = $cvExaminatorValue->pressure_right_min_arm;
  219. $cvExaminatorVo->pressureLeftMinLeg = $cvExaminatorValue->pressure_left_min_leg;
  220. $cvExaminatorVo->pressureRightMinLeg = $cvExaminatorValue->pressure_right_min_leg;
  221. }
  222. $appointmentVo->cvExaminator = $cvExaminatorVo;
  223. }
  224. $query = "SELECT * FROM [intimamedia_physician].[dbo].[tj_risk] WHERE fk_appointment = ". $appointmentVo->id;
  225. $resultsRisk = SQLServerManager::queryOnDatabase( $query );
  226. $treatments = array();
  227. foreach( $resultsRisk as $riskValue )
  228. {
  229. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_treatment] WHERE id = ". $riskValue->fk_treatment;
  230. $resultsTreatment = SQLServerManager::queryOnDatabase( $query );
  231. foreach( $resultsTreatment as $treatmentValue )
  232. {
  233. $treatmentVo = new TreatmentVo();
  234. $treatmentVo->type = trim( $treatmentValue->type );
  235. $treatmentVo->name = trim( $treatmentValue->name );
  236. $treatmentVo->posology = trim( $treatmentValue->posology );
  237. $treatmentVo->startDate = trim( $treatmentValue->date );
  238. array_push( $treatments, $treatmentVo );
  239. }
  240. }
  241. $appointmentVo->treatments = $treatments;
  242. array_push( $resultArray, $appointmentVo );
  243. }
  244. if ($results == "exception")
  245. {
  246. Throw new Exception("E010");
  247. }
  248. $returnObjectVo->value = $resultArray;
  249. return $returnObjectVo;
  250. }
  251. public function createAppointment(AppointmentVo $appointmentVo, $patientUid, $token)
  252. {
  253. // check if an empty appointment doesn't exist
  254. $query = "SELECT COUNT(id) as nbImages FROM [intimamedia_physician].[dbo].[t_image] ".
  255. "WHERE fk_appointment IN (SELECT MAX(id) FROM [intimamedia_physician].[dbo].[tj_appointment] WHERE fk_patient = " . $appointmentVo->patientId . " );";
  256. /* $ff=fopen("d:/tmp.jd", "a+");
  257. fprintf($ff, "createAppointment, query=%s\n", $query);
  258. fclose($ff);*/
  259. $results = SQLServerManager::queryOnDatabase( $query );
  260. $isLastAppointmentComplete = 0;
  261. foreach( $results as $value )
  262. {
  263. $isLastAppointmentComplete = $value->nbImages;
  264. }
  265. if (!$isLastAppointmentComplete)
  266. {
  267. /* $ff=fopen("d:/tmp.jd", "a+");
  268. fprintf($ff, "createAppointment, last appointment not complete, patient=%s\n", $patientUid);
  269. fclose($ff);*/
  270. Throw new Exception("E024");
  271. }
  272. $query = "SELECT COUNT(id) as nbAppointment FROM [intimamedia_physician].[dbo].[tj_appointment] ".
  273. "WHERE fk_patient = " . $appointmentVo->patientId . ";";
  274. /* $ff=fopen("d:/tmp.jd", "a+");
  275. fprintf($ff, "createAppointment, query=%s\n", $query);
  276. fclose($ff);*/
  277. $results = SQLServerManager::queryOnDatabase( $query );
  278. $nbAppointment = 0;
  279. foreach( $results as $value )
  280. {
  281. $nbAppointment = $value->nbAppointment + 1;
  282. }
  283. if( $nbAppointment == 0 )
  284. {
  285. Throw new Exception("E008");
  286. }
  287. $query = "INSERT INTO [intimamedia_physician].[dbo].[tj_appointment] ( number, date, weight, height, fk_patient, fk_live_country ) VALUES (".
  288. $nbAppointment .",'". date( "Y-m-d", $appointmentVo->date/1000 ) ."',". $appointmentVo->weight .",".
  289. $appointmentVo->height .",". $appointmentVo->patientId .",'". $appointmentVo->livingCountry ."')";
  290. /* $ff=fopen("d:/tmp.jd", "a+");
  291. fprintf($ff, "createAppointment, new appointment, query=%s\n", $query);
  292. fclose($ff);*/
  293. $results = SQLServerManager::queryOnDatabase( $query );
  294. $appointmentVo->number = $nbAppointment;
  295. if ($results == "exception")
  296. {
  297. Throw new Exception("E008");
  298. }
  299. $query = "SELECT MAX(id) AS lastId FROM [intimamedia_physician].[dbo].[tj_appointment]";
  300. $results = SQLServerManager::queryOnDatabase( $query );
  301. $appointmentId = -1;
  302. foreach( $results as $value )
  303. {
  304. $appointmentId = $value->lastId;
  305. }
  306. /* $ff=fopen("d:/tmp.jd", "a+");
  307. fprintf($ff, "++++++++++ createAppointment, appointmentId=%s\n", $appointmentId);
  308. fclose($ff);*/
  309. if( $appointmentId == -1 )
  310. {
  311. Throw new Exception("E011");
  312. }
  313. $query = "UPDATE [evolucare].[dbo].[t_patient] SET [last_visit] = '". date( "Y-m-d", $appointmentVo->date/1000 ) ."' WHERE id = ". $appointmentVo->patientId;
  314. /* $ff=fopen("d:/tmp.jd", "a+");
  315. fprintf($ff, "createAppointment, query2=%s\n", $query);
  316. fclose($ff);*/
  317. $results = SQLServerManager::queryOnDatabase( $query );
  318. if ($results == "exception")
  319. {
  320. Throw new Exception("E008");
  321. }
  322. return $this->searchAppointments($patientUid, $token);
  323. }
  324. public function addATSRiskToAnAppointment(AtsVo $atsVo, TobaccoVo $tobaccoVo, $id, $token)
  325. {
  326. $returnObjectVo = new ReturnObjectVo();
  327. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  328. if ( $atsVo == null OR $tobaccoVo == null)
  329. {
  330. $returnObjectVo->value = null;
  331. return $returnObjectVo;
  332. }
  333. $query = "SELECT [intimamedia_physician].[dbo].[tj_appointment].fk_tobacco, [intimamedia_physician].[dbo].[tj_appointment].fk_risk_ats FROM [intimamedia_physician].[dbo].[tj_appointment]".
  334. " WHERE id = ". $id;
  335. $results = SQLServerManager::queryOnDatabase( $query );
  336. $resultArray = array();
  337. foreach( $results as $value )
  338. {
  339. $idTobacco = $value->fk_tobacco;
  340. $idAts = $value->fk_risk_ats;
  341. }
  342. $transaction = SQLServerManager::startTransaction();
  343. if ($idTobacco)
  344. {
  345. //update
  346. $tobacco = "UPDATE [intimamedia_physician].[dbo].[t_tobacco] SET smoke = '" . $tobaccoVo->smoke . "', age = '" . $tobaccoVo->age . "', " .
  347. "cigarettes_per_day = '" . $tobaccoVo->cigarettesPerDay . "', age_stop_smoking = '" . $tobaccoVo->ageStopSmoking . "', paquet_year = '" . $tobaccoVo->paquetYear . "' " .
  348. "WHERE id = " . $idTobacco;
  349. }
  350. else
  351. {
  352. $tobacco = "INSERT INTO [intimamedia_physician].[dbo].[t_tobacco] (smoke, age, cigarettes_per_day, age_stop_smoking, paquet_year) VALUES ".
  353. "( '". $tobaccoVo->smoke ."', ". $tobaccoVo->age .", ". $tobaccoVo->cigarettesPerDay .", ". $tobaccoVo->ageStopSmoking .", ".
  354. $tobaccoVo->paquetYear .")";
  355. }
  356. /* $ff=fopen("d:/tmp.jd", "a+");
  357. fprintf($ff, "addATSRiskToAnAppointment, tobacco=%s\n", $tobacco);
  358. fclose($ff);*/
  359. $validTobacco = SQLServerManager::executeQueryForTransaction( $tobacco, $transaction );
  360. if (!$idTobacco)
  361. $idTobacco = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_tobacco]", $transaction );
  362. if ($idAts)
  363. {
  364. //update
  365. $ats = "UPDATE [intimamedia_physician].[dbo].[t_risk_ats] set hta = '". $atsVo->hta ."', hta_duration = '". $atsVo->htaDuration ."', hct = '" . $atsVo->hct . "', ".
  366. "hct_duration = '" . $atsVo->hctDuration . "', diabete = '" . $atsVo->diabete . "', diabete_duration = '" . $atsVo->diabeteDuration . "', ".
  367. "htg = '" . $atsVo->htg ."', htg_duration = '" . $atsVo->htgDuration . "' WHERE id = " . $idAts;
  368. }
  369. else
  370. {
  371. $ats = "INSERT INTO [intimamedia_physician].[dbo].[t_risk_ats] (hta, hta_duration, hct, hct_duration, diabete, diabete_duration, htg, htg_duration) VALUES".
  372. "('". $atsVo->hta ."', ". $atsVo->htaDuration .", '". $atsVo->hct ."', ". $atsVo->hctDuration .", '". $atsVo->diabete ."', '". $atsVo->diabeteDuration ."', '".
  373. $atsVo->htg ."', '". $atsVo->htgDuration ."')";
  374. }
  375. /* $ff=fopen("d:/tmp.jd", "a+");
  376. fprintf($ff, "addATSRiskToAnAppointment, query=%s\n", $ats);
  377. fclose($ff);*/
  378. $validAts = SQLServerManager::executeQueryForTransaction( $ats, $transaction );
  379. if (!$idAts)
  380. $idAts = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_risk_ats]", $transaction );
  381. $appointment = "UPDATE [intimamedia_physician].[dbo].[tj_appointment] SET fk_tobacco = ". $idTobacco .", fk_risk_ats = ". $idAts ." WHERE id = ". $id;
  382. /* $ff=fopen("d:/tmp.jd", "a+");
  383. fprintf($ff, "addATSRiskToAnAppointment, query=%s\n", $appointment);
  384. fclose($ff);*/
  385. $validAppointment = SQLServerManager::executeQueryForTransaction( $appointment, $transaction );
  386. if( $validTobacco && $validAts && $validAppointment )
  387. {
  388. SQLServerManager::commitTransaction( $transaction );
  389. }
  390. else
  391. {
  392. SQLServerManager::rollBackTransaction( $transaction );
  393. Throw new Exception("E012");
  394. }
  395. $returnObjectVo->value = null;
  396. return $returnObjectVo;
  397. }
  398. public function addPersonalHistoryToAnAppointment(PersonalHistoryVo $personalHistoryVo, $id, $token)
  399. {
  400. $returnObjectVo = new ReturnObjectVo();
  401. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  402. if ($personalHistoryVo == null)
  403. {
  404. $returnObjectVo->value = null;
  405. return $returnObjectVo;
  406. }
  407. $query = "SELECT [intimamedia_physician].[dbo].[tj_appointment].fk_personal_history FROM [intimamedia_physician].[dbo].[tj_appointment] WHERE id = ". $id;
  408. $results = SQLServerManager::queryOnDatabase( $query );
  409. $resultArray = array();
  410. foreach( $results as $value )
  411. {
  412. $idHistory = $value->fk_personal_history;
  413. }
  414. $transaction = SQLServerManager::startTransaction();
  415. if ($idHistory)
  416. {
  417. // update
  418. $history = "UPDATE [intimamedia_physician].[dbo].[t_personal_history] ".
  419. "SET coronary_disease = '" . $personalHistoryVo->coronaryDisease . "', aic = '" . $personalHistoryVo->aic . "', number_aic = '" . $personalHistoryVo->numberAic . "', ".
  420. "fk_coronary_disease = '" . $personalHistoryVo->typeCoronaryDisease . "', fk_type_aic = '" . $personalHistoryVo->typeAic . "', ".
  421. "fk_type_ethiology = '" . $personalHistoryVo->ethiologyAic . "', last_aic_age = '" . intval($personalHistoryVo->lastAicAge) . "', ".
  422. "last_coronary_disease_age = '" . intval($personalHistoryVo->lastCoronaryDiseaseAge) . "' WHERE id = " . $idHistory;
  423. }
  424. else
  425. {
  426. $history = "INSERT INTO [intimamedia_physician].[dbo].[t_personal_history]".
  427. "(coronary_disease,aic,number_aic,fk_coronary_disease,fk_type_aic,fk_type_ethiology, last_aic_age, last_coronary_disease_age)".
  428. " VALUES('". $personalHistoryVo->coronaryDisease ."', '". $personalHistoryVo->aic ."', ". $personalHistoryVo->numberAic .
  429. ", '". $personalHistoryVo->typeCoronaryDisease ."', '". $personalHistoryVo->typeAic ."', '". $personalHistoryVo->ethiologyAic ."', '" . intval($personalHistoryVo->lastAicAge) . "', '" . intval($personalHistoryVo->lastCoronaryDiseaseAge) . "')";
  430. }
  431. /* $ff=fopen("d:/tmp.jd", "a+");
  432. fprintf($ff, "addPersonalHistoryToAnAppointment, query=%s\n", $history);
  433. fclose($ff);*/
  434. $validHistory = SQLServerManager::executeQueryForTransaction( $history, $transaction );
  435. if (!$idHistory)
  436. $idHistory = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_personal_history]", $transaction );
  437. $appointment = "UPDATE [intimamedia_physician].[dbo].[tj_appointment] SET fk_personal_history = ". $idHistory ." WHERE id = ". $id;
  438. /* $ff=fopen("d:/tmp.jd", "a+");
  439. fprintf($ff, "addPersonalHistoryToAnAppointment, query=%s\n", $appointment);
  440. fclose($ff);*/
  441. $validAppointment = SQLServerManager::executeQueryForTransaction( $appointment, $transaction );
  442. if( $validHistory && $validAppointment )
  443. {
  444. SQLServerManager::commitTransaction( $transaction );
  445. }
  446. else
  447. {
  448. SQLServerManager::rollBackTransaction( $transaction );
  449. Throw new Exception("E013");
  450. }
  451. $returnObjectVo->value = null;
  452. return $returnObjectVo;
  453. }
  454. public function addFamilyHistoryToAnAppointment(FamilyHistoryVo $familyHistoryVo, $id, $token)
  455. {
  456. $returnObjectVo = new ReturnObjectVo();
  457. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  458. if ($familyHistoryVo == null)
  459. {
  460. $returnObjectVo->value = null;
  461. return $returnObjectVo;
  462. }
  463. $query = "SELECT [intimamedia_physician].[dbo].[tj_appointment].fk_family_history FROM [intimamedia_physician].[dbo].[tj_appointment] WHERE id = ". $id;
  464. $results = SQLServerManager::queryOnDatabase( $query );
  465. $resultArray = array();
  466. foreach( $results as $value )
  467. {
  468. $idHistory = $value->fk_family_history;
  469. }
  470. $transaction = SQLServerManager::startTransaction();
  471. if ($idHistory)
  472. {
  473. //update
  474. $history = "UPDATE [intimamedia_physician].[dbo].[t_family_history] ".
  475. "SET father_coronary_pathology = '" . $familyHistoryVo->fatherCoronaryPathology . "', father_coronary_pathology_age = '" . $familyHistoryVo->fatherCoronaryPathologyAge . "', ".
  476. "mother_coronary_pathology = '" . $familyHistoryVo->motherCoronaryPathology . "', mother_coronary_pathology_age = '" . $familyHistoryVo->motherCoronaryPathologyAge . "', ".
  477. "bs_coronary_pathology = '" . $familyHistoryVo->bsCoronaryPathology . "', bs_coronary_pathology_age = '" . $familyHistoryVo->bsCoronaryPathologyAge . "', ".
  478. "father_vascular_pathology = '" . $familyHistoryVo->fatherVascularPathology . "', father_vascular_pathology_age = '" . $familyHistoryVo->fatherVascularPathologyAge . "', ".
  479. "mother_vascular_pathology = '" . $familyHistoryVo->motherVascularPathology . "', mother_vascular_pathology_age = '" . $familyHistoryVo->motherVascularPathologyAge . "', ".
  480. "bs_vascular_pathology = '" . $familyHistoryVo->bsVascularPathology . "', bs_vascular_pathology_age = '" . $familyHistoryVo->bsVascularPathologyAge . "' WHERE id = " . $idHistory;
  481. }
  482. else
  483. {
  484. $history = "INSERT INTO [intimamedia_physician].[dbo].[t_family_history]".
  485. "(father_coronary_pathology, father_coronary_pathology_age, mother_coronary_pathology, mother_coronary_pathology_age, bs_coronary_pathology, bs_coronary_pathology_age, ".
  486. "father_vascular_pathology, father_vascular_pathology_age, mother_vascular_pathology, mother_vascular_pathology_age, bs_vascular_pathology, bs_vascular_pathology_age)".
  487. " VALUES ('". $familyHistoryVo->fatherCoronaryPathology ."', ". $familyHistoryVo->fatherCoronaryPathologyAge .", '". $familyHistoryVo->motherCoronaryPathology ."', ".
  488. "". $familyHistoryVo->motherCoronaryPathologyAge .", '". $familyHistoryVo->bsCoronaryPathology ."', ". $familyHistoryVo->bsCoronaryPathologyAge .", ".
  489. "'". $familyHistoryVo->fatherVascularPathology ."', ". $familyHistoryVo->fatherVascularPathologyAge .", '". $familyHistoryVo->motherVascularPathology ."', ".
  490. "". $familyHistoryVo->motherVascularPathologyAge .", '". $familyHistoryVo->bsVascularPathology ."', ". $familyHistoryVo->bsVascularPathologyAge .")";
  491. }
  492. /* $ff=fopen("d:/tmp.jd", "a+");
  493. fprintf($ff, "addFamilyHistoryToAnAppointment, query=%s\n", $history);
  494. fclose($ff);*/
  495. $validHistory = SQLServerManager::executeQueryForTransaction( $history, $transaction );
  496. if (!$idHistory)
  497. $idHistory = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_family_history]", $transaction );
  498. $appointment = "UPDATE [intimamedia_physician].[dbo].[tj_appointment] SET fk_family_history = ". $idHistory ." WHERE id = ". $id;
  499. /* $ff=fopen("d:/tmp.jd", "a+");
  500. fprintf($ff, "addFamilyHistoryToAnAppointment, query=%s\n", $appointment);
  501. fclose($ff);*/
  502. $validAppointment = SQLServerManager::executeQueryForTransaction( $appointment, $transaction );
  503. if( $validHistory && $validAppointment )
  504. {
  505. SQLServerManager::commitTransaction( $transaction );
  506. }
  507. else
  508. {
  509. SQLServerManager::rollBackTransaction( $transaction );
  510. Throw new Exception("E014");
  511. }
  512. $returnObjectVo->value = null;
  513. return $returnObjectVo;
  514. }
  515. public function addCardiovascularExaminatorToAnAppointment(CvExaminatorVo $cvExaminatorVo, $id, $token)
  516. {
  517. $returnObjectVo = new ReturnObjectVo();
  518. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  519. if ($cvExaminatorVo == null)
  520. {
  521. $returnObjectVo->value = null;
  522. return $returnObjectVo;
  523. }
  524. $query = "SELECT [intimamedia_physician].[dbo].[tj_appointment].fk_cv_examination FROM [intimamedia_physician].[dbo].[tj_appointment] WHERE id = ". $id;
  525. $results = SQLServerManager::queryOnDatabase( $query );
  526. $resultArray = array();
  527. foreach( $results as $value )
  528. {
  529. $idCVExaminator = $value->fk_cv_examination;
  530. }
  531. $transaction = SQLServerManager::startTransaction();
  532. if ($idCVExaminator)
  533. {
  534. //update
  535. $cvExaminator = "UPDATE [intimamedia_physician].[dbo].[t_cv_examination] ".
  536. "SET frequency = '" . $cvExaminatorVo->frequency . "', arythmia = '" . $cvExaminatorVo->arythmia . "', ".
  537. "pressure_left_max_arm = '" . $cvExaminatorVo->pressureLeftMaxArm . "', pressure_right_max_arm = '" . $cvExaminatorVo->pressureRightMaxArm . "', ".
  538. "pressure_left_max_leg = '" . $cvExaminatorVo->pressureLeftMaxLeg . "', pressure_right_max_leg = '" . $cvExaminatorVo->pressureRightMaxLeg . "', ".
  539. "pressure_left_min_arm = '" . $cvExaminatorVo->pressureLeftMinArm . "', pressure_right_min_arm = '" . $cvExaminatorVo->pressureRightMinArm . "', ".
  540. "pressure_left_min_leg = '" . $cvExaminatorVo->pressureLeftMinLeg . "', pressure_right_min_leg = '" . $cvExaminatorVo->pressureRightMinLeg . "' WHERE id = " . $idCVExaminator;
  541. }
  542. else
  543. {
  544. $cvExaminator = "INSERT INTO [intimamedia_physician].[dbo].[t_cv_examination]".
  545. "(frequency, arythmia, pressure_left_max_arm, pressure_right_max_arm, pressure_left_max_leg, pressure_right_max_leg, ".
  546. "pressure_left_min_arm, pressure_right_min_arm, pressure_left_min_leg, pressure_right_min_leg)".
  547. " VALUES (". $cvExaminatorVo->frequency .", '". $cvExaminatorVo->arythmia ."', ". $cvExaminatorVo->pressureLeftMaxArm .", ".
  548. $cvExaminatorVo->pressureRightMaxArm .", ". $cvExaminatorVo->pressureLeftMaxLeg .", ". $cvExaminatorVo->pressureRightMaxLeg .", ".
  549. $cvExaminatorVo->pressureLeftMinArm .", ". $cvExaminatorVo->pressureRightMinArm .", ". $cvExaminatorVo->pressureLeftMinLeg .", ".
  550. $cvExaminatorVo->pressureRightMinLeg .")";
  551. }
  552. /* $ff=fopen("d:/tmp.jd", "a+");
  553. fprintf($ff, "addCardiovascularExaminatorToAnAppointment = %s\n", $cvExaminator);
  554. fclose($ff);*/
  555. $validCVExaminator = SQLServerManager::executeQueryForTransaction( $cvExaminator, $transaction );
  556. if (!$idCVExaminator)
  557. $idCVExaminator = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_cv_examination]", $transaction );
  558. $appointment = "UPDATE [intimamedia_physician].[dbo].[tj_appointment] SET fk_cv_examination = ". $idCVExaminator ." WHERE id = ". $id;
  559. /* $ff=fopen("d:/tmp.jd", "a+");
  560. fprintf($ff, "addCardiovascularExaminatorToAnAppointment, query=%s\n", $appointment);
  561. fclose($ff);*/
  562. $validAppointment = SQLServerManager::executeQueryForTransaction( $appointment, $transaction );
  563. if( $validCVExaminator && $validAppointment )
  564. {
  565. SQLServerManager::commitTransaction( $transaction );
  566. }
  567. else
  568. {
  569. SQLServerManager::rollBackTransaction( $transaction );
  570. Throw new Exception("E015");
  571. }
  572. $returnObjectVo->value = null;
  573. return $returnObjectVo;
  574. }
  575. public function addTreatmentsToAnAppointment($list, $id, $token)
  576. {
  577. $returnObjectVo = new ReturnObjectVo();
  578. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  579. if ($list == null)
  580. {
  581. $returnObjectVo->value = null;
  582. return $returnObjectVo;
  583. }
  584. $dbTreatmentNameList = array();
  585. $newTreatmentNameList = array();
  586. foreach( $list as $value )
  587. array_push( $newTreatmentNameList, $value->name );
  588. $query = "SELECT name, r.fk_treatment FROM [intimamedia_physician].[dbo].[tj_risk] r
  589. LEFT JOIN [intimamedia_physician].[dbo].[t_treatment] t on t.id = r.fk_treatment WHERE r.fk_appointment = ". $id;
  590. $results = SQLServerManager::queryOnDatabase( $query );
  591. foreach( $results as $value )
  592. {
  593. if (!in_array(trim($value->name), $newTreatmentNameList))
  594. {
  595. $query = "DELETE FROM [intimamedia_physician].[dbo].[tj_risk] WHERE fk_treatment = ". $value->fk_treatment;
  596. SQLServerManager::queryOnDatabase( $query );
  597. $query = "DELETE FROM [intimamedia_physician].[dbo].[tj_treatment] WHERE fk_treatment = ". $value->fk_treatment;
  598. SQLServerManager::queryOnDatabase( $query );
  599. }
  600. else
  601. array_push( $dbTreatmentNameList, trim($value->name) );
  602. }
  603. foreach( $list as $value )
  604. {
  605. if (in_array($value->name, $dbTreatmentNameList) )
  606. continue;
  607. $transaction = SQLServerManager::startTransaction();
  608. $treatment = "INSERT INTO [intimamedia_physician].[dbo].[t_treatment] (date, posology, name, type) VALUES ('". $value->startDate ."', '".
  609. $value->posology ."', '". $value->name ."', '". $value->type ."')";
  610. $validTreatment = SQLServerManager::executeQueryForTransaction( $treatment, $transaction );
  611. $idTreatment = SQLServerManager::getLastId( "[intimamedia_physician].[dbo].[t_treatment]", $transaction );
  612. $risks = "INSERT INTO [intimamedia_physician].[dbo].[tj_risk] (fk_treatment, fk_appointment) VALUES (". $idTreatment .", ". $id .")";
  613. $validRisks = SQLServerManager::executeQueryForTransaction( $risks, $transaction );
  614. /* $ff=fopen("d:/tmp.jd", "a+");
  615. fprintf($ff, "addTreatments, query=%s\n", $risks);
  616. fclose($ff);*/
  617. if( $validTreatment && $validRisks )
  618. {
  619. SQLServerManager::commitTransaction( $transaction );
  620. }
  621. else
  622. {
  623. SQLServerManager::rollBackTransaction( $transaction );
  624. Throw new Exception("E016");
  625. }
  626. }
  627. $returnObjectVo->value = null;
  628. return $returnObjectVo;
  629. }
  630. public function updatePatient(PatientVo $patientVo, $token)
  631. {
  632. $returnObjectVo = new ReturnObjectVo();
  633. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  634. if ( $patientVo != NULL)
  635. {
  636. $updatePatientQuery = "UPDATE [evolucare].[dbo].[t_patient] SET last_name = '" . $patientVo->lastName ."', ".
  637. "first_name = '" . $patientVo->firstName ."', sex = '". $patientVo->sex ."', ".
  638. "birth_date = '". date( "Y-m-d", $patientVo->birthDate/1000 ) ."', birth_country = '". $patientVo->birthCountry ."', ".
  639. "ethnic_group = '". $patientVo->ethnicGroup ."' ".
  640. "WHERE id = " . $patientVo->id;
  641. SQLServerManager::queryOnDatabase( $updatePatientQuery );
  642. }
  643. $returnObjectVo->value = null;
  644. return $returnObjectVo;
  645. }
  646. public function updateAppointment(AppointmentVo $appointmentVo, $patientUid, $token)
  647. {
  648. $returnObjectVo = new ReturnObjectVo();
  649. $returnObjectVo->token = SecurityManager::verifyToken( $token );
  650. $returnObjectVo->value = null;
  651. /* $ff=fopen("d:/tmp.jd", "a+");
  652. fprintf($ff, "updateAppointment, appointmentId=%s, patientId=%s\n", $appointmentVo->id, $patientUid);
  653. fclose($ff);*/
  654. if ( $appointmentVo != NULL)
  655. {
  656. // update height, weight and living country
  657. $query = "UPDATE [intimamedia_physician].[dbo].[tj_appointment] SET weight = ". $appointmentVo->weight .", height = ". $appointmentVo->height .", ".
  658. "fk_live_country = '". $appointmentVo->livingCountry . "' ".
  659. "WHERE id = " . $appointmentVo->id .";";
  660. SQLServerManager::queryOnDatabase( $query );
  661. if ($appointmentVo->ats && $appointmentVo->tobacco)
  662. $this->addATSRiskToAnAppointment($appointmentVo->ats, $appointmentVo->tobacco, $appointmentVo->id, $token);
  663. if ($appointmentVo->personalHistory)
  664. $this->addPersonalHistoryToAnAppointment($appointmentVo->personalHistory, $appointmentVo->id, $token);
  665. if ($appointmentVo->familyHistory)
  666. $this->addFamilyHistoryToAnAppointment($appointmentVo->familyHistory, $appointmentVo->id, $token);
  667. if ($appointmentVo->cvExaminator)
  668. $this->addCardiovascularExaminatorToAnAppointment($appointmentVo->cvExaminator, $appointmentVo->id, $token);
  669. if ($appointmentVo->treatments)
  670. $this->addTreatmentsToAnAppointment($appointmentVo->treatments, $appointmentVo->id, $token);
  671. }
  672. return $returnObjectVo;
  673. }
  674. }
  675. ?>