MaterielsController.php
15.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
/**
* Materiels Controller
*
* @property \App\Model\Table\MaterielsTable $Materiels
*/
class MaterielsController extends AppController
{
/**
* Index method
*
* @return \Cake\Network\Response|null
*/
public function index()
{
$this->paginate = [
'contain' => ['SurCategories', 'Categories', 'SousCategories', 'GroupesThematiques', 'GroupesMetiers', 'Organismes', 'Sites']
];
$materiels = $this->paginate($this->Materiels);
$this->set('nbMateriels', $this->Materiels->find('all')->count());
//test my debug
$this->myDebug($this);
$this->set(compact('materiels'));
$this->set('_serialize', ['materiels']);
}
/**
* View method
*
* @param string|null $id Materiel id.
* @return \Cake\Network\Response|null
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function view($id = null)
{
$materiel = $this->Materiels->get($id, [
'contain' => ['SurCategories', 'Categories', 'SousCategories', 'GroupesThematiques', 'GroupesMetiers', 'Organismes', 'Sites', 'Documents', 'Emprunts', 'Suivis']
]);
$this->set('materiel', $materiel);
$this->set('_serialize', ['materiel']);
}
/**
* Add method
*
* @return \Cake\Network\Response|void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$materiel = $this->Materiels->newEntity();
if ($this->request->is('post')) {
$materiel = $this->Materiels->patchEntity($materiel, $this->request->data);
if ($this->Materiels->save($materiel)) {
$this->Flash->success(__('Le matériel a bien été ajouté.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Le matériel n\'a pas pu être ajouté.'));
}
}
$surCategories = $this->Materiels->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SurCategories.nom']);
$categories = $this->Materiels->Categories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Categories.nom']);
$sousCategories = $this->Materiels->SousCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SousCategories.nom']);
$groupesThematiques = $this->Materiels->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']);
$groupesMetiers = $this->Materiels->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']);
$organismes = $this->Materiels->Organismes->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Organismes.nom']);
$sites = $this->Materiels->Sites->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Sites.nom']);
$utilisateurs = TableRegistry::get('Users')->find('list', [ 'keyField' => 'nom', 'valueField' => 'nom']);
$mail_responsable = TableRegistry::get('Users')->find()->select('email')->where(['username =' => $this->Auth->user('username')])->first()['email'];
$this->set(compact('materiel', 'surCategories', 'categories', 'sousCategories', 'groupesThematiques', 'groupesMetiers', 'organismes', 'sites', 'utilisateurs', 'mail_responsable'));
$this->set('_serialize', ['materiel']);
}
/**
* Edit method
*
* @param string|null $id Materiel id.
* @return \Cake\Network\Response|void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$materiel = $this->Materiels->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$materiel = $this->Materiels->patchEntity($materiel, $this->request->data);
if ($this->Materiels->save($materiel)) {
$this->Flash->success(__('Le matériel a bien été édité.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('Le matériel n\'a pas pu être édité.'));
}
}
$surCategories = $this->Materiels->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SurCategories.nom']);
$categories = $this->Materiels->Categories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Categories.nom']);
$sousCategories = $this->Materiels->SousCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SousCategories.nom']);
$groupesThematiques = $this->Materiels->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']);
$groupesMetiers = $this->Materiels->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']);
$organismes = $this->Materiels->Organismes->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Organismes.nom']);
$sites = $this->Materiels->Sites->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Sites.nom']);
$utilisateurs = TableRegistry::get('Users')->find('list', [ 'keyField' => 'nom', 'valueField' => 'nom']);
if(!empty($materiel->get('nom_responsable'))){
if(!in_array($materiel->get('nom_responsable'), $utilisateurs->toArray())){
$nom_ancien_responsable = $materiel->get('nom_responsable');
$this->set(compact('nom_ancien_responsable'));
}
}
$mail_responsable = TableRegistry::get('Users')->find()->select('email')->where(['username =' => $this->Auth->user('username')])->first()['email'];
$this->set(compact('materiel', 'surCategories', 'categories', 'sousCategories', 'groupesThematiques', 'groupesMetiers', 'organismes', 'sites', 'utilisateurs', 'mail_responsable'));
$this->set('_serialize', ['materiel']);
}
/**
* Delete method
*
* @param string|null $id Materiel id.
* @return \Cake\Network\Response|null Redirects to index.
* @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$materiel = $this->Materiels->get($id);
if ($this->Materiels->delete($materiel)) {
$this->Flash->success(__('Le matériel a bien été supprimé.'));
} else {
$this->Flash->error(__('Le matériel n\'a pas pu être supprimé.'));
}
return $this->redirect(['action' => 'index']);
}
private function statusSetTo($newStatus, $message, $id = null, $from = 'index') {
$materiel = $this->Materiels->get($id)->set('status', $newStatus);
if ($this->Materiels->save($materiel)) {
$this->Flash->success(__($message));
}
return $this->redirect(['action'=>$from, $id]);
}
public function statusValidated($id = null, $from = 'index') {
$this->statusSetTo('VALIDATED', 'Le matériel a bien été validé', $id, $from);
}
public function statusToBeArchived($id = null, $from = 'index') {
$this->statusSetTo('TOBEARCHIVED', 'La sortie d\'inventaire a bien été demandée', $id, $from);
}
public function statusArchived($id = null, $from = 'index') {
$this->statusSetTo('ARCHIVED', 'Le matériel a bien été sorti de l\'inventaire', $id, $from);
}
private function getConditionForField($fieldName) {
$searchFieldName = 's_' . $fieldName;
if ( isset($this->request->data[$searchFieldName]) && ($this->request->data[$searchFieldName] != '')) return ["Materiels.$fieldName LIKE" => '%'.$this->request->data[$searchFieldName].'%'];
return NULL;
}
private function getConditionForFieldNumber($fieldName) {
$searchFieldName = 's_' . $fieldName;
if ( isset($this->request->data[$searchFieldName]) && ($this->request->data[$searchFieldName] != '')) return ["Materiels.$fieldName =" => $this->request->data[$searchFieldName]];
return NULL;
}
public function find() {
$s_sur_categories = $this->Materiels->SurCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SurCategories.nom']);
$s_categories = $this->Materiels->Categories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Categories.nom']);
$s_sous_categories = $this->Materiels->SousCategories->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'SousCategories.nom']);
$s_groupes_thematiques = $this->Materiels->GroupesThematiques->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesThematiques.nom']);
$s_groupes_metiers = $this->Materiels->GroupesMetiers->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'GroupesMetiers.nom']);
$s_organismes = $this->Materiels->Organismes->find('list', [ 'keyField' => 'id', 'valueField' => 'nom', 'order' => 'Organismes.nom']);
$this->set(compact('s_sur_categories', 's_categories', 's_sous_categories', 's_groupes_thematiques', 's_groupes_metiers', 's_organismes'));
// some data POSTED (au moins le champ de recherche generale) ?
if ( isset($this->request->data['s_all']) || isset($this->request->data['s_all_2']) || isset($this->request->data['designation'])) {
$generalFieldConditions = NULL;
// if general field set (s_all), then set general request for it
if (isset($this->request->data['s_all'])) {
$all = $this->request->data['s_all'];
$generalFieldConditions = ['OR' => [
'Materiels.designation LIKE' => '%'.$all.'%',
'Materiels.numero_laboratoire LIKE' => '%'.$all.'%',
'Materiels.numero_inventaire_organisme LIKE' => '%'.$all.'%',
'Materiels.numero_inventaire_old LIKE' => '%'.$all.'%',
'Materiels.numero_commande LIKE' => '%'.$all.'%',
'Materiels.description LIKE' => '%'.$all.'%',
'Materiels.fournisseur LIKE' => '%'.$all.'%',
'Materiels.nom_responsable LIKE' => '%'.$all.'%',
'Materiels.email_responsable LIKE' => '%'.$all.'%',
'Materiels.code_comptable LIKE' => '%'.$all.'%',
'Materiels.numero_serie LIKE' => '%'.$all.'%',
'Materiels.date_acquisition LIKE' => '%'.$all.'%',
'Materiels.date_reception LIKE' => '%'.$all.'%',
]];
}
if (isset($this->request->data['s_all_2'])) {
$all = $this->request->data['s_all_2'];
$generalFieldConditions = ['OR' => [
'Materiels.designation LIKE' => '%'.$all.'%',
'Materiels.numero_laboratoire LIKE' => '%'.$all.'%',
'Materiels.numero_inventaire_organisme LIKE' => '%'.$all.'%',
'Materiels.numero_inventaire_old LIKE' => '%'.$all.'%',
'Materiels.numero_commande LIKE' => '%'.$all.'%',
'Materiels.description LIKE' => '%'.$all.'%',
'Materiels.fournisseur LIKE' => '%'.$all.'%',
'Materiels.nom_responsable LIKE' => '%'.$all.'%',
'Materiels.email_responsable LIKE' => '%'.$all.'%',
'Materiels.code_comptable LIKE' => '%'.$all.'%',
'Materiels.numero_serie LIKE' => '%'.$all.'%',
'Materiels.date_acquisition LIKE' => '%'.$all.'%',
'Materiels.date_reception LIKE' => '%'.$all.'%',
]];
}
$specificFieldsConditions = NULL;
// au moins un champ specifique rempli ?
if ( isset($this->request->data['s_designation']) ) {
// Materiel type
$matostype = $this->request->data['s_matostype'];
$matostypeRequest = NULL;
switch ($matostype) {
// Administratif
case 'A':
$matostypeRequest['Materiels.materiel_administratif ='] = '1';
break;
// Technique
case 'T':
$matostypeRequest['Materiels.materiel_technique ='] = '1';
break;
// Admin et Tech
case 'AT':
$matostypeRequest['Materiels.materiel_administratif ='] = '1';
$matostypeRequest['Materiels.materiel_technique ='] = '1';
break;
// Admin ONLY
case 'AO':
$matostypeRequest['Materiels.materiel_administratif ='] = '1';
$matostypeRequest['Materiels.materiel_technique ='] = '0';
break;
// Tech ONLY
case 'TO':
$matostypeRequest['Materiels.materiel_administratif ='] = '0';
$matostypeRequest['Materiels.materiel_technique ='] = '1';
break;
}
$periode_acquisitionRequest = NULL;
if ($this->request->data['s_periode_acquisition1'] != '')
$periode_acquisitionRequest['Materiels.date_acquisition >='] = $this->request->data['s_periode_acquisition1'];
if ($this->request->data['s_periode_acquisition2'] != '')
$periode_acquisitionRequest['Materiels.date_acquisition <='] = $this->request->data['s_periode_acquisition2'];
$montantRequest = [];
if ($this->request->data['s_prix_ht_inf'] != '')
$montantRequest['Materiels.prix_ht <='] = $this->request->data['s_prix_ht_inf'];
if ($this->request->data['s_prix_ht_sup'] != '')
$montantRequest['Materiels.prix_ht >='] = $this->request->data['s_prix_ht_sup'];
$specificFieldsConditions = [
'Materiels.designation LIKE' => '%'.$this->request->data['s_designation'].'%',
'Materiels.numero_laboratoire LIKE' => '%'.$this->request->data['s_numero_laboratoire'].'%',
$this->getConditionForField('numero_commande'),
'Materiels.date_acquisition LIKE' => '%'.$this->request->data['s_date_acquisition'].'%',
$periode_acquisitionRequest,
$this->getConditionForFieldNumber('prix_ht'),
$montantRequest,
$this->getConditionForFieldNumber('sur_categorie_id'),
$this->getConditionForFieldNumber('categorie_id'),
$this->getConditionForFieldNumber('sous_categorie_id'),
$this->getConditionForField('nom_responsable'),
$this->getConditionForField('numero_inventaire_organisme'),
$this->getConditionForField('numero_inventaire_old'),
$this->getConditionForFieldNumber('groupes_metier_id'),
$this->getConditionForFieldNumber('groupes_thematique_id'),
$this->getConditionForField('status'),
$this->getConditionForFieldNumber('organisme_id'),
$matostypeRequest,
];
}
// CONSTRUCTION DE LA REQUETE SQL COMPLETE = $specificFieldsConditions OR $generalFieldConditions (mais entre chaque champ, c'est un AND)
// by default, no sort
$order = [];
if (isset($this->passedArgs[0]) && isset($this->passedArgs[1])) {
$order = $this->passedArgs[0];
$order .= ' '.$this->passedArgs[1];
}
if (isset($this->request->data['s_all_2']) && $this->request->data['s_all_2'] != '') {
$conditions = $generalFieldConditions;
}
else if (isset($this->request->data['s_all']) && $this->request->data['s_all'] != '') {
$conditions = $generalFieldConditions;
}
else {
$conditions = $specificFieldsConditions;
}
$lastResults = $this->Materiels->find('all', ['limit' => 1000, 'order' => $order, 'conditions' => $conditions])->toArray();
$this->set('_results', $lastResults);
} // end if()
}
public function export() {
}
}