MaterielsTable.php
11.1 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
<?php
namespace App\Model\Table;
use App\Model\Entity\Materiel;
use Cake\ORM\Query;
use Cake\ORM\RulesChecker;
use Cake\ORM\Table;
use Cake\Validation\Validator;
use Cake\ORM\TableRegistry;
/**
* Materiels Model
*
* @property \Cake\ORM\Association\BelongsTo $SurCategories
* @property \Cake\ORM\Association\BelongsTo $Categories
* @property \Cake\ORM\Association\BelongsTo $SousCategories
* @property \Cake\ORM\Association\BelongsTo $GroupesThematiques
* @property \Cake\ORM\Association\BelongsTo $GroupesMetiers
* @property \Cake\ORM\Association\BelongsTo $Organismes
* @property \Cake\ORM\Association\BelongsTo $Sites
* @property \Cake\ORM\Association\HasMany $Documents
* @property \Cake\ORM\Association\HasMany $Emprunts
* @property \Cake\ORM\Association\HasMany $Suivis
*/
class MaterielsTable extends AppTable
{
public $ALL_STATUS = array('CREATED', 'VALIDATED', 'TOBEARCHIVED', 'ARCHIVED');
/**
* Initialize method
*
* @param array $config The configuration for the Table.
* @return void
*/
public function initialize(array $config)
{
parent::initialize($config);
$this->table('materiels');
$this->displayField('id');
$this->primaryKey('id');
$this->addBehavior('Timestamp');
$this->belongsTo('SurCategories', [
'foreignKey' => 'sur_categorie_id'
]);
$this->belongsTo('Categories', [
'foreignKey' => 'categorie_id'
]);
$this->belongsTo('SousCategories', [
'foreignKey' => 'sous_categorie_id'
]);
$this->belongsTo('GroupesThematiques', [
'foreignKey' => 'groupes_thematique_id'
]);
$this->belongsTo('GroupesMetiers', [
'foreignKey' => 'groupes_metier_id'
]);
$this->belongsTo('Organismes', [
'foreignKey' => 'organisme_id'
]);
$this->belongsTo('Sites', [
'foreignKey' => 'site_id'
]);
$this->hasMany('Documents', [
'foreignKey' => 'materiel_id'
]);
$this->hasMany('Emprunts', [
'foreignKey' => 'materiel_id'
]);
$this->hasMany('Suivis', [
'foreignKey' => 'materiel_id'
]);
}
/**
* Default validation rules.
*
* @param \Cake\Validation\Validator $validator Validator instance.
* @return \Cake\Validation\Validator
*/
public function validationDefault(Validator $validator)
{
$validator
->integer('id')
->allowEmpty('id', 'create');
$validator
->notEmpty('designation' ,'Ce champ doit être rempli')
->add('designation', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->notEmpty('sur_categorie_id', 'Vous devez sélectionner une valeur');
$validator
->notEmpty('categorie_id', 'Vous devez sélectionner une valeur');
$validator
->allowEmpty('numero_laboratoire')
->add('numero_laboratoire', 'unique', ['rule' => 'validateUnique', 'provider' => 'table']);
$validator
->allowEmpty('description')
->add('description', 'valid', ['rule' => 'check_string_with_some_special_cars', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->boolean('materiel_administratif')
->allowEmpty('materiel_administratif');
$validator
->boolean('materiel_technique')
->allowEmpty('materiel_technique');
$validator
->add('status', 'valid', ['rule' => 'checkStatus', 'message' => 'Le statut doit prendre une des 4 valeurs CREATED, VALIDATED, TOBEARCHIVED, ou ARCHIVED', 'provider' => 'table']);
$validator
->notEmpty('date_acquisition', 'Ce champ doit être rempli');
$validator
->allowEmpty('fournisseur')
->add('fournisseur', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->numeric('prix_ht')
->allowEmpty('prix_ht')
->add('prix_ht', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('eotp')
->add('eotp', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('numero_commande')
->add('numero_commande', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('code_comptable')
->add('code_comptable', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('numero_serie')
->add('numero_serie', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('numero_inventaire_organisme')
->add('numero_inventaire_organisme', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('numero_inventaire_old')
->add('numero_inventaire_old', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('date_archivage');
$validator
->boolean('etiquette')
->allowEmpty('etiquette');
$validator
->notEmpty('site_id', 'Ce champ doit être rempli');
$validator
->allowEmpty('lieu_detail')
->add('lieu_detail', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('nom_responsable')
->add('nom_responsable', 'valid', ['rule' => 'check_string', 'message' => 'Ce champ contient des caractères interdits', 'provider' => 'table']);
$validator
->allowEmpty('email_responsable')
->email('email_responsable');
$validator
->allowEmpty('nom_createur');
$validator
->allowEmpty('nom_modificateur');
$validator
->allowEmpty('date_reception');
$validator
->allowEmpty('date_fin_garantie');
$validator
->allowEmpty('duree_garantie');
$validator
->allowEmpty('unite_duree_garantie');
return $validator;
}
public function checkStatus($check) {
return ( isset($check) && in_array($check, $this->ALL_STATUS) );
}
/**
* Returns a rules checker object that will be used for validating
* application integrity.
*
* @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
* @return \Cake\ORM\RulesChecker
*/
public function buildRules(RulesChecker $rules)
{
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
$checkAtLeastOneChecked = function($entity) {
return ( $entity->materiel_administratif || $entity->materiel_technique);
};
// return if price >= $configuration->prix_inventaire_administratif € then must be checked as "administratif"
$checkIfIsAdministratifWhenShouldBe = function ($entity) {
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
return ! ( isset($entity->prix_ht) && $entity->prix_ht >= $configuration->prix_inventaire_administratif && !$entity->materiel_administratif );
};
// return if price <800€ then must NOT be checked as "administratif"
$checkIfIsNotAdministratifWhenShouldNotBe = function ($entity) {
$configuration = TableRegistry::get('Configurations')->find()->where(['id =' => 1])->first();
return ! ( isset($entity->prix_ht) && $entity->prix_ht < $configuration->prix_inventaire_administratif && $entity->materiel_administratif );
};
$rules->add($checkAtLeastOneChecked, [
'errorField' => 'materiel_administratif',
'message' => 'Le matériel est obligatoirement inventoriable ou technique.'
]);
$rules->add($checkIfIsAdministratifWhenShouldBe, [
'errorField' => 'materiel_administratif',
'message' => 'Le matériel vaut plus de '.$configuration->prix_inventaire_administratif.'€ HT, il est donc obligatoirement inventoriable.'
]);
$rules->add($checkIfIsNotAdministratifWhenShouldNotBe, [
'errorField' => 'materiel_administratif',
'message' => 'Le matériel vaut moins de '.$configuration->prix_inventaire_administratif.'€ HT, il n\'est donc pas inventoriable.'
]);
$rules->add($rules->isUnique(['numero_laboratoire']));
$rules->add($rules->existsIn(['sur_categorie_id'], 'SurCategories'));
$rules->add($rules->existsIn(['categorie_id'], 'Categories'));
$rules->add($rules->existsIn(['sous_categorie_id'], 'SousCategories'));
$rules->add($rules->existsIn(['groupes_thematique_id'], 'GroupesThematiques'));
$rules->add($rules->existsIn(['groupes_metier_id'], 'GroupesMetiers'));
$rules->add($rules->existsIn(['organisme_id'], 'Organismes'));
$rules->add($rules->existsIn(['site_id'], 'Sites'));
return $rules;
}
public function beforeSave($event, $entity, $options)
{
if(!empty($entity->get('nom_responsable')) && empty($entity->get('nom_responsable'))){
$entity->set('nom_responsable', $entity->get('nom_ancien_responsable'));
}
// numero_laboratoire generator (QC changed this in Jan 2015)
if (empty($entity->get('numero_laboratoire')) && !empty($entity->get('date_acquisition'))) {
$year = substr($entity->get('date_acquisition'), 6, 4);
$labShortName = TableRegistry::get('Configurations')->find()->first()->labNameShort;
$num = TableRegistry::get('Materiels')->find('all', [
'fields' => ['numero_laboratoire'],
'conditions' => ['Materiels.numero_laboratoire LIKE' => $labShortName.'-'.$year.'%'],
'order' => ['Materiels.numero_laboratoire DESC']
])->first()['numero_laboratoire'];
$newId = substr($num, -4)+1;
$labNumber = $labShortName.'-'.$year.'-'.sprintf("%04d", $newId);
$entity->set('numero_laboratoire', $labNumber);
}
if(empty($entity->get('date_acquisition'))){
$entity->set('date_acquisition', null);
}
if(empty($entity->get('date_reception'))){
$entity->set('date_reception', null);
}
return true;
}
}