/[openfoncier]/trunk/services/metier/consultationmanager.php
ViewVC logotype

Annotation of /trunk/services/metier/consultationmanager.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 559 - (hide annotations)
Tue Oct 30 17:05:34 2012 UTC (12 years, 3 months ago) by mlimic
File size: 8660 byte(s)
save of services files
1 mlimic 515 <?php
2    
3 mlimic 522 /*
4 mlimic 523 * Processes requests received via services which concern consultations (by
5 mlimic 522 * ERP or other services).
6     *
7     * @author Mirna Limic <[email protected]>
8     * Date: 18/10/2012
9     *
10     * Follow-up:
11     * Bugs:
12     */
13    
14    
15 mlimic 515 require_once ("./metier/metiermanager.php");
16    
17     class ConsultationManager extends MetierManager {
18    
19 mlimic 559
20 mlimic 522 /*
21     * Constructor.
22     *
23     * Calls it's parent constructor to establish a DB connection.
24     */
25 mlimic 526 public function __construct() {
26 mlimic 515 parent::__construct();
27 mlimic 559 $this->date_limite = null;
28     $this->date_retour = null;
29     $this->date_envoi = null;
30     $this->dossier_id = null;
31     $this->decisions = null;
32 mlimic 515 }
33    
34 mlimic 523
35 mlimic 522 /*
36     * Destructor
37     *
38     * Call's its parent's destructor.
39     */
40 mlimic 526 public function __destruct() {
41 mlimic 515 parent::__destruct();
42     }
43    
44 mlimic 522
45 mlimic 559 private function dateValid($date_str, $date_start_str = null,
46     $date_end_str = null) {
47     $dates_str = array($date_start_str, $date_end_str);
48     $dates = array();
49     for ($i = 0; $i < 2; $i++) {
50     if ($dates_str[$i] == null) {
51     $dates[] = null;
52     continue;
53     }
54     $d = explode('-', $dates_str[$i]);
55     $dates[] = strtotime($d[2].'-'.$d[1].'-'.$d[0]);
56     }
57     $d = explode('/', $date_str);
58     $date = strtotime(implode('-', $d));
59    
60     print ' start:'.strval($dates[0]);
61     print ' end:'.strval($dates[1]);
62     print ' sent:'.strval($date);
63     if ($dates[0] && $date < $dates[0]) {
64     return false;
65     }
66     if ($dates[1] && $date > $dates[1]) {
67     return false;
68     }
69     return true;
70     }
71    
72    
73     private function getDataFromDB($id) {
74     $sql = 'SELECT date_envoi, date_retour, date_limite, dossier FROM '.
75     'consultation WHERE consultation = '.$id;
76     $res = $this->db->query($sql);
77     if ($this->checkDBError($res, 'Erreur lors de recuperation des dates '.
78     'de la consultation :'.$id)) {
79     return $this->KO;
80     }
81     while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) {
82     $this->date_envoi = $row[0];;
83     $this->date_retour = $row[1];
84     $this->date_limite = $row[2];
85     $this->dossier_id = $row[3];
86     }
87    
88     print ' date_envoi:'.$this->date_envoi;
89     print ' date_limite:'.$this->date_limite;
90     $res->free();
91     return $this->OK;
92     }
93    
94     private function checkDates($date_str) {
95     // check that the consultation exists in DB
96     if (!$this->date_limite) {
97     $this->setMessage("La consultation n'était pas trouvé");
98     return $this->BAD_DATA;
99     }
100    
101     // check that the date_retour does not already exist
102     if (!empty($this->date_retour)) {
103     $this->setMessage('Le date de retour déjà existe pour la '.
104     'consultation');
105     return $this->BAD_DATA;
106     }
107    
108     // check that the date is valid
109     $date = explode("/", $date_str);
110     print ' date:';
111     print_r($date);
112     if (!checkdate($date[1], $date[0], $date[2])) {
113     $this->setMessage("Le date fourni n'est pas bon (en bon format)");
114     return $this->BAD_DATA;
115     }
116    
117     if (!$this->dateValid($date_str, $this->date_envoi, $this->date_limite)) {
118     $this->setMessage("Le date fourni n'est pas entre le date d'envoi ".
119     "et le date limite de la demande de consultation");
120     return $this->BAD_DATA;
121     }
122     return $this->OK;
123     }
124    
125    
126     private function avisValid($avis) {
127     // get all of the decisions possible from table avis_decision
128     $sql = "SELECT libelle, avis_consultation FROM avis_consultation";
129     $res = $this->db->query($sql);
130     if ($this->checkDBError($res, 'Erreur lors de recuperation des avis '.
131     'possibles pendant traitement de la consultation :'.$id)) {
132     return $this->KO;
133     }
134    
135     $this->decisions = array();
136     while ($row =& $res->fetchRow(DB_FETCHMODE_ORDERED)) {
137     $this->decisions[$row[0]] = $row[1];
138     }
139     $res->free();
140    
141     // check that the decision received corresponds to a decision in DB
142     if (!in_array($avis, array_keys($this->decisions))) {
143     $this->setMessage(__FILE__."::".__FUNCTION__.
144     ": L'avis reçu n'est pas trouvé dans la BD.");
145     $this->KO;
146     }
147     print ' decisions:';
148     print_r($this->decisions);
149     return $this->OK;
150     }
151    
152    
153     private function fichierDataValid(&$data) {
154     // do the check
155     if (!isset($data['nom_fichier']) && isset($data['fichier_base64'])
156     || isset($data['nom_fichier']) && !isset($data['fichier_base64'])) {
157     print ' problem_fichier_data ';
158     $this->setMessage(
159     "Les données concernant le fichier ne sont pas complètes");
160     return $this->KO;
161     }
162     print ' ok_fichier_data ';
163     return $this->OK;
164     }
165    
166    
167    
168     private function storeDecisionFile(&$data) {
169     if (isset($data['fichier_base64'])) {
170     $dir = dirname(__FILE__).'/../trs/'.$dossier_id;
171     if (!file_exists($dir) && !is_dir($dir)) {
172     mkdir($dir, 755);
173     }
174     $file_len = strlen($data['fichier_base64']);
175     $filename = $dir.'/consultation_'.$id.'_'.$data['nom_fichier'];
176     $file = fopen($filename, 'w');
177     if (!$file) {
178     $this->setMessage(__FILE__."::".__FUNCTION__.
179     ": Echec en création de pointeur de fichier");
180     $this->KO;
181     }
182     $num_written = fwrite($file, $data[''], $file_len);
183     if (!$num_writeen || $num_written != $file_len) {
184     $this->setMessage(__FILE__."::".__FUNCTION__.
185     ": Sauvegarde de fichier échouée");
186     $this->KO;
187     }
188     fclose($file);
189     }
190     return $this->OK;
191     }
192    
193 mlimic 522 /*
194     * Called when one of the external services sends its recommendation
195     * as a responde to a demand issued by openfoncier.
196     * @param mixed $data The array containing building number.
197     * @param string $id The ID of the dossier.
198     * @return bool 'OK'
199     */
200 mlimic 559 public function consultationDecision($data, $id) {
201 mlimic 515
202 mlimic 559 // get all of the dates for the consultation from DB
203     // and check the date received is ok with respect to the
204     // time interval set by (date_envoi, date_limite)
205     // equally extract the dossier ID while at it
206     $res = $this->getDataFromDB($id);
207     if ($res != $this->OK) {
208     return $res;
209 mlimic 515 }
210 mlimic 559
211     $res = $this->checkDates($data['date_retour']);
212     if ($res != $this->OK) {
213     return $res;
214     }
215    
216     // check that data regarding fichier is complete
217     $res = $this->fichierDataValid($data);
218     if ($res != $this->OK) {
219     return $res;
220     }
221    
222    
223     // check that the decision sent is one of the decisions found in DB
224     // and store the possible decisions in $this->decisions
225     $res = $this->avisValid($data['avis']);
226     if ($res != $this->OK) {
227     return $res;
228     }
229    
230     // first store the file if needed
231     $res = $this->storeDecisionFile($data);
232     if ($res != $this->OK) {
233     return $res;
234     }
235    
236     // update the DB
237     $date = explode("/", $data['date_retour']);
238     $sql = "UPDATE consultation SET ".
239     "date_retour = DATE '".
240     $date[2]."-".$date[1]."-".$date[0]."', ";
241     if (isset($data['motivation'])) {
242     $sql .= "motivation = " . $data['motivation'] . ', ';
243     }
244     if (isset($data['nom_fichier'])) {
245     $sql .= "fichier = ".$data['nom_fichier'] . ', ';
246     }
247     $sql .= "avis_consultation = ".$this->decisions[$data['avis']].
248     ", lu = false WHERE consultation = ".$id;
249    
250    
251     print ' $sql:'.$sql.' ';
252    
253     $res = $this->db->query($sql);
254     if ($this->checkDBError($res, 'Erreur lors de mis à jour '.
255     'de la consultation '.$id.' dans le DB')) {
256     return $this->KO;
257     }
258    
259     $this->setMessage("L'avis été pris en compte");
260 mlimic 515 return $this->OK;
261     }
262    
263 mlimic 559
264 mlimic 515 }
265    
266     ?>

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26