ReportService.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. require_once('vo/com/imt/intimamedia/vo/AbacusVo.php');
  3. require_once('vo/com/imt/intimamedia/vo/NormalValuesVo.php');
  4. require_once('vo/com/imt/intimamedia/vo/ReportDataVo.php');
  5. require_once('vo/com/imt/intimamedia/vo/ReturnObjectVo.php');
  6. require_once('common/SQLServerManager.php');
  7. require_once('common/SecurityManager.php');
  8. require_once('common/PhpMail.php');
  9. require_once('sendgrid-php/sendgrid-php.php');
  10. class ReportService
  11. {
  12. public function saveData(ReportDataVo $reportDataVo, $appointmentId, $token)
  13. {
  14. $returnObjectVo = new ReturnObjectVo();
  15. $returnObjectVo->token = SecurityManager::verifyToken($token);
  16. // check if an entry exist, in case update it
  17. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_report] WHERE fk_appointment = ". $appointmentId .
  18. " AND fk_abacus = " . $reportDataVo->abacus ." AND type = " . $reportDataVo->type . ";";
  19. $results = SQLServerManager::queryOnDatabase( $query );
  20. if ($results)
  21. {
  22. foreach( $results as $value )
  23. $reportId = $value->id;
  24. // update report and delete/insert mails
  25. $query = "UPDATE [intimamedia_physician].[dbo].[t_report] set conclusion = '". $reportDataVo->conclusion ."',".
  26. " signs = '". $reportDataVo->signs ."', indications = '". $reportDataVo->indications ."' WHERE fk_appointment = ". $appointmentId .
  27. " AND fk_abacus = " . $reportDataVo->abacus ." AND fk_type = " . $reportDataVo->type . ";";
  28. SQLServerManager::queryOnDatabase( $query );
  29. $query = "DELETE FROM [intimamedia_physician].[dbo].[t_mail] WHERE fk_report = ". $reportDataVo->id . ";";
  30. SQLServerManager::queryOnDatabase( $query );
  31. }
  32. else
  33. {
  34. // insert
  35. $query = "INSERT INTO [intimamedia_physician].[dbo].[t_report] (type, conclusion, signs, indications, fk_appointment, fk_abacus) VALUES ('".
  36. $reportDataVo->type ."','". $reportDataVo->conclusion ."','". $reportDataVo->signs ."','". $reportDataVo->indications ."','".
  37. $appointmentId ."','". $reportDataVo->abacus . "')";
  38. SQLServerManager::queryOnDatabase( $query );
  39. $query = "SELECT MAX(id) AS lastId FROM [intimamedia_physician].[dbo].[t_report]";
  40. $results = SQLServerManager::queryOnDatabase( $query );
  41. $reportId = -1;
  42. foreach( $results as $value )
  43. $reportId = $value->lastId;
  44. }
  45. foreach( $reportDataVo->mails as $mail )
  46. {
  47. $query = "INSERT INTO [intimamedia_physician].[dbo].[t_mail] (label, fk_report) VALUES ('".$mail."','". $reportId ."')";
  48. SQLServerManager::queryOnDatabase( $query );
  49. }
  50. return $returnObjectVo;
  51. }
  52. public function getData($appointmentId, $token)
  53. {
  54. $returnObjectVo = new ReturnObjectVo();
  55. $returnObjectVo->token = SecurityManager::verifyToken($token);
  56. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_report] WHERE fk_appointment = '". $appointmentId ."';";
  57. $results = SQLServerManager::queryOnDatabase( $query );
  58. if (!$results)
  59. {
  60. $returnObjectVo->value = null;
  61. return $returnObjectVo;
  62. }
  63. $resultArray = array();
  64. foreach( $results as $value )
  65. {
  66. $reportDataVo = new ReportDataVo();
  67. $reportDataVo->id = $value->id;
  68. $reportDataVo->type = $value->type;
  69. $reportDataVo->conclusion = trim($value->conclusion);
  70. $reportDataVo->indications = trim($value->indications);
  71. $reportDataVo->signs = trim($value->signs);
  72. $reportDataVo->abacus = trim($value->fk_abacus);
  73. $reportDataVo->mails = null;
  74. $reportDataVo->previousMails = null;
  75. $currentAppointment = $value->number;
  76. $patientId = $value->fk_patient;
  77. $mailQuery = "SELECT * FROM [intimamedia_physician].[dbo].[t_mail] WHERE fk_report = '". $reportDataVo->id ."';";
  78. $mailResults = SQLServerManager::queryOnDatabase( $mailQuery );
  79. if ($mailResults)
  80. {
  81. $mails = array();
  82. foreach( $mailResults as $mail )
  83. array_push($mails, $mail->label);
  84. $reportDataVo->mails = $mails;
  85. // check if mail from previous appointment exist
  86. if ($currentAppointment - 1 > 0)
  87. {
  88. $q = "SELECT fk_report FROM [intimamedia_physician].[dbo].[tj_appointment] WHERE number = ". $currentAppointment-1 ." AND fk_patient = " . $patientId . ";";
  89. $r = SQLServerManager::queryOnDatabase( $q );
  90. if ($r)
  91. {
  92. foreach( $r as $v )
  93. $reportId = $v->fk_report;
  94. $previousMailQuery = "SELECT * FROM [intimamedia_physician].[dbo].[t_mail] WHERE fk_report = '". $reportId ."';";
  95. $previousMailResults = SQLServerManager::queryOnDatabase( $previousMailQuery );
  96. if ($previousMailResults)
  97. {
  98. $previousMails = array();
  99. foreach( $previousMailResults as $previousMail )
  100. array_push($previousMails, $previousMail->label);
  101. $reportDataVo->previousMails = $previousMails;
  102. }
  103. }
  104. }
  105. }
  106. array_push($resultArray, $reportDataVo);
  107. }
  108. $returnObjectVo->value = $resultArray;
  109. return $returnObjectVo;
  110. }
  111. public function getAbacus($country)
  112. {
  113. $query = "SELECT * FROM [intimamedia_physician].[dbo].[t_abacus] a "
  114. ."INNER JOIN [intimamedia_physician].[dbo].[tr_normal_values] as nv ON a.id = nv.fk_abacus";
  115. /* $ff=fopen("d:/tmp.jd", "a+");
  116. fprintf($ff, "getAbacus = %s\n", $query);
  117. fclose($ff);*/
  118. $results = SQLServerManager::queryOnDatabase( $query );
  119. if( count($results) == 0 )
  120. {
  121. Throw new Exception("E022");
  122. }
  123. $resultArray = array();
  124. $abacusVo = null;
  125. foreach( $results as $value )
  126. {
  127. // pass to another abacus
  128. if ($abacusVo AND strcmp($abacusVo->name, trim($value->name)) != 0)
  129. {
  130. array_push( $resultArray, $abacusVo );
  131. }
  132. // first passage or another abacus
  133. if (!$abacusVo OR strcmp($abacusVo->name, trim($value->name)) != 0)
  134. {
  135. $abacusVo = new AbacusVo();
  136. $abacusVo->normalValues = array();
  137. $abacusVo->id = $value->id;
  138. $abacusVo->name = trim($value->name);
  139. $abacusVo->description = trim($value->description);
  140. $abacusVo->url = trim($value->url);
  141. $abacusVo->country = trim($value->country);
  142. }
  143. $normalValuesVo = new NormalValuesVo();
  144. $normalValuesVo->age = $value->age;
  145. $normalValuesVo->n_age_category = $value->n_age_category;
  146. $normalValuesVo->n_indiv = $value->n_indiv;
  147. $normalValuesVo->r_l_m = $value->r_l_m;
  148. $normalValuesVo->age_min = $value->age_min;
  149. $normalValuesVo->age_max = $value->age_max;
  150. $normalValuesVo->scale_min = $value->scale_min;
  151. $normalValuesVo->scale_max = $value->scale_max;
  152. $normalValuesVo->description_type = $value->description_type;
  153. $normalValuesVo->men_minright = $value->men_minright;
  154. $normalValuesVo->men_meanright = $value->men_meanright;
  155. $normalValuesVo->men_maxright = $value->men_maxright;
  156. $normalValuesVo->men_minleft = $value->men_minleft;
  157. $normalValuesVo->men_meanleft = $value->men_meanleft;
  158. $normalValuesVo->men_maxleft = $value->men_maxleft;
  159. $normalValuesVo->women_minright = $value->women_minright;
  160. $normalValuesVo->women_meanright = $value->women_meanright;
  161. $normalValuesVo->women_maxright = $value->women_maxright;
  162. $normalValuesVo->women_minleft = $value->women_minleft;
  163. $normalValuesVo->women_meanleft = $value->women_meanleft;
  164. $normalValuesVo->women_maxleft = $value->women_maxleft;
  165. $normalValuesVo->abacus_id = $value->id;
  166. array_push ( $abacusVo->normalValues, $normalValuesVo );
  167. }
  168. // for last abacus which is not added to result in loop above
  169. array_push( $resultArray, $abacusVo );
  170. return $resultArray;
  171. }
  172. public function generateReport()
  173. {
  174. $resultArray = array();
  175. sleep(1);
  176. return $resultArray;
  177. }
  178. public function sendReport($pdfData, $pdfName, $userId, $lang, $mails)
  179. {
  180. if ($mails && sizeof($mails))
  181. {
  182. $subject = "IntimaMedia.com : IMT pdf";
  183. if ($lang == 'fr')
  184. $message = "<HTML><HEAD></HEAD><BODY>Bonjour<br/><br/>Vous venez de recevoir un rapport pdf en provenance de l'application IntimaMedia.com.<br/><br/>Cordialement.</BODY></HTML>";
  185. else
  186. $message = "<HTML><HEAD></HEAD><BODY>Hello<br/><br/>Please, find in attachment a IMT pdf report.<br/><br/>Best regards.</BODY></HTML>";
  187. $report = fopen("pdf/".$pdfName, "w");
  188. fwrite($report, $pdfData->data);
  189. fclose($report);
  190. while (sizeof($mails))
  191. {
  192. $mail = array_pop($mails);
  193. if ($mail)
  194. {
  195. $sguser = 'support@iimt.fr';
  196. $sgpass = 'Marignan;/8';
  197. $sendgrid = new SendGrid($sguser, $sgpass);
  198. $email = new SendGrid\Email();
  199. $email
  200. ->addTo($mail)
  201. ->setFrom('support@iimt.fr')
  202. ->setSubject($subject)
  203. ->setText($subject)
  204. ->setHtml($message)
  205. ->addAttachment( "pdf/".$pdfName)
  206. ;
  207. $sendgrid->send($email);
  208. /* try {
  209. $sendgrid->send($email);
  210. } catch(\SendGrid\Exception $e) {
  211. // echo $e->getCode();
  212. // foreach($e->getErrors() as $er) {
  213. // echo $er;
  214. // }
  215. }
  216. */
  217. }
  218. // $ret = PhpMail::sendMail($mail, $subject, $message, "pdf/".$pdfName);
  219. }
  220. }
  221. }
  222. }