1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Application\Service; |
6
|
|
|
|
7
|
|
|
use Application\Api\Exception; |
8
|
|
|
use Doctrine\DBAL\Driver\PDOConnection; |
9
|
|
|
use Doctrine\ORM\EntityManager; |
10
|
|
|
use Interop\Container\ContainerInterface; |
11
|
|
|
|
12
|
|
|
class Importer |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ContainerInterface |
16
|
|
|
*/ |
17
|
|
|
private $container; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var EntityManager |
21
|
|
|
*/ |
22
|
|
|
private $entityManager; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var PDOConnection |
26
|
|
|
*/ |
27
|
|
|
private $filemaker; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var PDOConnection |
31
|
|
|
*/ |
32
|
|
|
private $typo3; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $members = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var array |
41
|
|
|
*/ |
42
|
|
|
private $users = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Storage requests |
46
|
|
|
* |
47
|
|
|
* @var array |
48
|
|
|
*/ |
49
|
|
|
private $storage = []; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Allocated storage |
53
|
|
|
* [idBookable] => number of bookings |
54
|
|
|
* |
55
|
|
|
* @var array |
56
|
|
|
*/ |
57
|
|
|
private $storageAllocated = []; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Import constructor |
61
|
|
|
*/ |
62
|
|
|
public function __construct(ContainerInterface $container, EntityManager $entityManager) |
63
|
|
|
{ |
64
|
|
|
$this->container = $container; |
65
|
|
|
$this->entityManager = $entityManager; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Import the members from FileMaker and TYPO3 |
70
|
|
|
*/ |
71
|
|
|
public function import(): void |
72
|
|
|
{ |
73
|
|
|
$this->loadMembers(); |
74
|
|
|
$this->loadPeople(); |
75
|
|
|
// $this->deleteTestUsers(); |
76
|
|
|
$this->loadStorageRequests(); |
77
|
|
|
$this->insertBookables(); |
78
|
|
|
$this->insertUsers(); |
79
|
|
|
$this->insertBookings(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @return PDOConnection |
84
|
|
|
*/ |
85
|
|
|
private function connectFileMaker(): PDOConnection |
86
|
|
|
{ |
87
|
|
|
if ($this->filemaker) { |
88
|
|
|
return $this->filemaker; |
89
|
|
|
} |
90
|
|
|
$config = $this->container->get('config'); |
91
|
|
|
|
92
|
|
|
$dsn = sprintf('odbc:Driver={%s};Server=%s;Database=%s;charset=UTF-8', $config['filemaker']['driver'], $config['filemaker']['host'], $config['filemaker']['dbname']); |
93
|
|
|
$this->filemaker = new PDOConnection($dsn, $config['filemaker']['user'], $config['filemaker']['password']); |
94
|
|
|
|
95
|
|
|
return $this->filemaker; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @return PDOConnection |
100
|
|
|
*/ |
101
|
|
|
private function connectTypo3(): PDOConnection |
102
|
|
|
{ |
103
|
|
|
if ($this->typo3) { |
104
|
|
|
return $this->typo3; |
105
|
|
|
} |
106
|
|
|
$config = $this->container->get('config'); |
107
|
|
|
|
108
|
|
|
$this->typo3 = new PDOConnection(sprintf('mysql:host=%s;port=%u;dbname=%s', $config['typo3']['host'], $config['typo3']['port'], $config['typo3']['dbname']), $config['typo3']['user'], $config['typo3']['password']); |
109
|
|
|
|
110
|
|
|
return $this->typo3; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Load members from FileMaker "ichtus" table into memory |
115
|
|
|
*/ |
116
|
|
|
private function loadMembers(): void |
117
|
|
|
{ |
118
|
|
|
$this->connectFileMaker(); |
119
|
|
|
$query = <<<EOT |
120
|
|
|
SELECT ID_membre, |
121
|
|
|
"nom_prénom", |
122
|
|
|
"date_entrée ichtus", |
123
|
|
|
remarques, |
124
|
|
|
"liens de famille", |
125
|
|
|
"date_séance d'accueil", |
126
|
|
|
"date_formulaire_adhésion", |
127
|
|
|
membre_new, |
128
|
|
|
membre_actif, |
129
|
|
|
membre_suspension, |
130
|
|
|
"membre_archivé", |
131
|
|
|
assurances, |
132
|
|
|
envoi_papier |
133
|
|
|
FROM ichtus |
134
|
|
|
WHERE ((membre_new>0 AND "date_formulaire_adhésion">=DATE '2018-10-01') OR membre_actif>0 OR membre_suspension>0) AND "membre_archivé"=0 AND remarques NOT LIKE '%Ignorer%' |
135
|
|
|
EOT; |
136
|
|
|
$statement = $this->filemaker->prepare(trim($query)); |
137
|
|
|
if ($statement->execute()) { |
138
|
|
|
echo sprintf('%u membres à importer...', $statement->rowCount()) . PHP_EOL; |
139
|
|
|
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $member) { |
140
|
|
|
foreach ($member as $fieldName => $fieldValue) { |
141
|
|
|
$member[$fieldName] = $this->fromMacRoman($fieldValue); |
142
|
|
|
} |
143
|
|
|
$this->members[$member['ID_membre']] = $member; |
144
|
|
|
echo sprintf('Membre %u importé', $member['ID_membre']) . PHP_EOL; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Load people from TYPO3 fe_users into memory |
151
|
|
|
*/ |
152
|
|
|
private function loadPeople(): void |
153
|
|
|
{ |
154
|
|
|
$this->connectTypo3(); |
155
|
|
|
$query = <<<EOT |
156
|
|
|
SELECT * |
157
|
|
|
FROM fe_users |
158
|
|
|
WHERE FIND_IN_SET(CAST(family_uid as char), :members) AND status_archived=0; |
159
|
|
|
EOT; |
160
|
|
|
$statement = $this->typo3->prepare($query); |
161
|
|
|
$statement->bindValue(':members', implode(',', array_keys($this->members))); |
162
|
|
|
|
163
|
|
|
if ($statement->execute()) { |
164
|
|
|
echo sprintf('%u individus à importer...', $statement->rowCount()) . PHP_EOL; |
165
|
|
|
$withoutLoginCount = 0; |
166
|
|
|
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $user) { |
167
|
|
|
if (($user['family_status'] === 'chef de famille' || $user['family_status'] === 'chef(fe) de famille') && $user['uid'] !== $user['family_uid']) { |
168
|
|
|
echo sprintf('WARN: utilisateur %u ne devrait pas être de chef de la famille %u', $user['uid'], $user['family_uid']) . PHP_EOL; |
169
|
|
|
} |
170
|
|
|
if (empty($user['new_username'])) { |
171
|
|
|
echo sprintf("WARN: utilisateur %u (%s %s) n'a pas de login MyIchtus", $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
172
|
|
|
++$withoutLoginCount; |
173
|
|
|
} |
174
|
|
|
$this->users[$user['uid']] = $user; |
175
|
|
|
echo sprintf('Individu %u importé', $user['uid']) . PHP_EOL; |
176
|
|
|
} |
177
|
|
|
if ($withoutLoginCount > 0) { |
178
|
|
|
echo sprintf('%u individus sans login MyIchtus', $withoutLoginCount) . PHP_EOL; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Create users |
185
|
|
|
*/ |
186
|
|
|
private function insertUsers(): void |
187
|
|
|
{ |
188
|
|
|
$conn = $this->entityManager->getConnection(); |
189
|
|
|
|
190
|
|
|
$typo3 = $this->connectTypo3(); |
191
|
|
|
|
192
|
|
|
$insert = <<<EOT |
193
|
|
|
INSERT INTO user( |
194
|
|
|
id, |
195
|
|
|
login, |
196
|
|
|
password, |
197
|
|
|
first_name, |
198
|
|
|
last_name, |
199
|
|
|
birthday, |
200
|
|
|
sex, |
201
|
|
|
email, |
202
|
|
|
street, |
203
|
|
|
postcode, |
204
|
|
|
locality, |
205
|
|
|
country_id, |
206
|
|
|
mobile_phone, |
207
|
|
|
phone, |
208
|
|
|
internal_remarks, |
209
|
|
|
iban, |
210
|
|
|
has_insurance, |
211
|
|
|
swiss_sailing, |
212
|
|
|
swiss_sailing_type, |
213
|
|
|
swiss_windsurf_type, |
214
|
|
|
receives_newsletter, |
215
|
|
|
role, |
216
|
|
|
family_relationship, |
217
|
|
|
billing_type, |
218
|
|
|
welcome_session_date, |
219
|
|
|
status, |
220
|
|
|
creation_date, |
221
|
|
|
door4 |
222
|
|
|
) VALUES ( |
223
|
|
|
:id, |
224
|
|
|
:login, |
225
|
|
|
:password, |
226
|
|
|
:first_name, |
227
|
|
|
:last_name, |
228
|
|
|
:birthday, |
229
|
|
|
:sex, |
230
|
|
|
:email, |
231
|
|
|
:street, |
232
|
|
|
:postcode, |
233
|
|
|
:locality, |
234
|
|
|
:country_id, |
235
|
|
|
:mobile_phone, |
236
|
|
|
:phone, |
237
|
|
|
:remarks, |
238
|
|
|
:iban, |
239
|
|
|
:has_insurance, |
240
|
|
|
:swiss_sailing, |
241
|
|
|
:swiss_sailing_type, |
242
|
|
|
:swiss_windsurf_type, |
243
|
|
|
:receives_newsletter, |
244
|
|
|
:role, |
245
|
|
|
:family_relationship, |
246
|
|
|
:billing_type, |
247
|
|
|
:welcome_session_date, |
248
|
|
|
:status, |
249
|
|
|
:creation_date, |
250
|
|
|
:door4 |
251
|
|
|
) |
252
|
|
|
EOT; |
253
|
|
|
$insert = $conn->prepare($insert); |
254
|
|
|
|
255
|
|
|
$createAccount = <<<EOT |
256
|
|
|
INSERT INTO account( |
257
|
|
|
owner_id, |
258
|
|
|
creation_date, |
259
|
|
|
balance, |
260
|
|
|
name, |
261
|
|
|
parent_id, |
262
|
|
|
type, |
263
|
|
|
code |
264
|
|
|
) VALUES ( |
265
|
|
|
:owner, |
266
|
|
|
NOW(), |
267
|
|
|
:balance, |
268
|
|
|
:name, |
269
|
|
|
10011, -- Passifs -> 2030 Acomptes de clients |
270
|
|
|
'liability', |
271
|
|
|
:code |
272
|
|
|
) |
273
|
|
|
EOT; |
274
|
|
|
$createAccount = $conn->prepare($createAccount); |
275
|
|
|
|
276
|
|
|
$linkToTag = $conn->prepare('INSERT INTO user_tag_user(user_tag_id, user_id) VALUES (:user_tag_id, :user_id)'); |
277
|
|
|
|
278
|
|
|
$linkToLicense = $conn->prepare('INSERT INTO license_user(license_id, user_id) VALUES (:license_id, :user_id)'); |
279
|
|
|
|
280
|
|
|
$exportPassword = $typo3->prepare('UPDATE fe_users SET new_password=:password WHERE uid=:user_id'); |
281
|
|
|
|
282
|
|
|
foreach ($this->users as $user) { |
283
|
|
|
echo sprintf('Insert user %u (%s %s)', $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
284
|
|
|
$insert->bindValue(':id', $user['uid']); |
285
|
|
|
$insert->bindValue(':login', $user['new_username']); |
286
|
|
|
$insert->bindValue(':first_name', $user['first_name']); |
287
|
|
|
$insert->bindValue(':last_name', $user['last_name']); |
288
|
|
|
$insert->bindValue(':birthday', $user['date_birth'] !== null && $user['date_birth'] !== '0000-00-00' ? $user['date_birth'] : null); |
289
|
|
|
$insert->bindValue(':sex', $user['sexe'] === 'F' ? 2 : 1); |
290
|
|
|
$insert->bindValue(':email', !empty($user['email']) ? $user['email'] : null); |
291
|
|
|
$insert->bindValue(':street', $user['address']); |
292
|
|
|
$insert->bindValue(':postcode', $user['zip']); |
293
|
|
|
$insert->bindValue(':locality', $user['city']); |
294
|
|
|
|
295
|
|
|
switch ($user['country']) { |
296
|
|
|
case 'CH': |
297
|
|
|
$country_id = 1; |
298
|
|
|
|
299
|
|
|
break; |
300
|
|
|
case 'FR': |
301
|
|
|
case 'France': |
302
|
|
|
$country_id = 2; |
303
|
|
|
|
304
|
|
|
break; |
305
|
|
|
case 'DE': |
306
|
|
|
$country_id = 10; |
307
|
|
|
|
308
|
|
|
break; |
309
|
|
|
case 'CA': |
310
|
|
|
$country_id = 6; |
311
|
|
|
|
312
|
|
|
break; |
313
|
|
|
case 'NL': |
314
|
|
|
$country_id = 19; |
315
|
|
|
|
316
|
|
|
break; |
317
|
|
|
default: |
318
|
|
|
$country_id = null; |
319
|
|
|
echo sprintf("WARN: pas de correspondance pour le code pays %s de l\\'individu %u (%s %s)", $user['country'], $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
320
|
|
|
} |
321
|
|
|
$insert->bindValue(':country_id', $country_id); |
322
|
|
|
|
323
|
|
|
$insert->bindValue(':mobile_phone', !empty($user['natel']) ? $user['natel'] : ''); |
324
|
|
|
$insert->bindValue(':phone', $user['telephone']); |
325
|
|
|
|
326
|
|
|
if ($user['uid'] === $user['family_uid']) { |
327
|
|
|
// Si responsable de l'adhésion, fusionne les notes au niveau du membre et de l'individu |
328
|
|
|
$remarks = implode(PHP_EOL, [$this->members[$user['family_uid']]['remarques'], $user['notes']]); |
329
|
|
|
} else { |
330
|
|
|
$remarks = !empty($user['notes']) ? $user['notes'] : ''; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
$insert->bindValue(':remarks', $remarks); |
334
|
|
|
$insert->bindValue(':iban', !empty($user['IBAN']) ? $user['IBAN'] : ''); |
335
|
|
|
|
336
|
|
|
$hasInsurance = false; |
337
|
|
|
if (empty($this->members[$user['family_uid']]['assurances'])) { |
338
|
|
|
echo sprintf('WARN: membre %u n\'a aucune assurance -> fond de réparation forcé', $user['family_uid']) . PHP_EOL; |
339
|
|
|
$this->members[$user['family_uid']]['assurances'] = 'Fonds réparation'; |
340
|
|
|
} elseif (mb_strpos($this->members[$user['family_uid']]['assurances'], 'RC privée') !== false) { |
341
|
|
|
$hasInsurance = true; |
342
|
|
|
} |
343
|
|
|
$insert->bindValue(':has_insurance', $hasInsurance); |
344
|
|
|
|
345
|
|
|
$insert->bindValue(':swiss_sailing', !empty($user['ichtus_swiss_sailing']) ? $user['ichtus_swiss_sailing'] : ''); |
346
|
|
|
switch ($user['ichtus_swiss_sailing_type']) { |
347
|
|
|
case 'A': |
348
|
|
|
$swissSailingType = 'active'; |
349
|
|
|
|
350
|
|
|
break; |
351
|
|
|
case 'P': |
352
|
|
|
$swissSailingType = 'passive'; |
353
|
|
|
|
354
|
|
|
break; |
355
|
|
|
case 'J': |
356
|
|
|
$swissSailingType = 'junior'; |
357
|
|
|
|
358
|
|
|
break; |
359
|
|
|
default: |
360
|
|
|
$swissSailingType = null; |
361
|
|
|
} |
362
|
|
|
$insert->bindValue(':swiss_sailing_type', $swissSailingType); |
363
|
|
|
|
364
|
|
|
switch ($user['ichtus_swiss_windsurf_type']) { |
365
|
|
|
case 'A': |
366
|
|
|
$swissWindsurfType = 'active'; |
367
|
|
|
|
368
|
|
|
break; |
369
|
|
|
case 'P': |
370
|
|
|
$swissWindsurfType = 'passive'; |
371
|
|
|
|
372
|
|
|
break; |
373
|
|
|
default: |
374
|
|
|
$swissWindsurfType = null; |
375
|
|
|
} |
376
|
|
|
$insert->bindValue(':swiss_windsurf_type', $swissWindsurfType); |
377
|
|
|
|
378
|
|
|
$insert->bindValue(':receives_newsletter', !empty($user['email']) ? 1 : 0); |
379
|
|
|
|
380
|
|
|
switch ($user['family_status']) { |
381
|
|
|
case 'chef de famille': |
382
|
|
|
$relationship = 'householder'; |
383
|
|
|
$role = 'member'; |
384
|
|
|
|
385
|
|
|
break; |
386
|
|
|
case 'chef(fe) de famille': |
387
|
|
|
$relationship = 'householder'; |
388
|
|
|
$role = 'member'; |
389
|
|
|
|
390
|
|
|
break; |
391
|
|
|
case 'conjoint': |
392
|
|
|
$relationship = 'partner'; |
393
|
|
|
$role = 'individual'; |
394
|
|
|
|
395
|
|
|
break; |
396
|
|
|
case 'enfant': |
397
|
|
|
$relationship = 'child'; |
398
|
|
|
$role = 'individual'; |
399
|
|
|
|
400
|
|
|
break; |
401
|
|
|
case 'parent': |
402
|
|
|
$relationship = 'parent'; |
403
|
|
|
$role = 'individual'; |
404
|
|
|
|
405
|
|
|
break; |
406
|
|
|
case 'soeur': |
407
|
|
|
$relationship = 'sister'; |
408
|
|
|
$role = 'individual'; |
409
|
|
|
|
410
|
|
|
break; |
411
|
|
|
case 'frère': |
412
|
|
|
$relationship = 'brother'; |
413
|
|
|
$role = 'individual'; |
414
|
|
|
|
415
|
|
|
break; |
416
|
|
|
case 'beau-frère': |
417
|
|
|
$relationship = 'brother'; |
418
|
|
|
$role = 'individual'; |
419
|
|
|
|
420
|
|
|
break; |
421
|
|
|
default: |
422
|
|
|
$relationship = 'householder'; |
423
|
|
|
$role = 'individual'; |
424
|
|
|
echo sprintf("WARN: individu %u (%s %s) n'a pas de statut familial", $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
425
|
|
|
} |
426
|
|
|
if (in_array((int) $user['uid'], [1057, 2738], true)) { |
427
|
|
|
$role = 'administrator'; |
428
|
|
|
} elseif (!empty($user['ichtus_comite_fonction'])) { |
429
|
|
|
$role = 'responsible'; |
430
|
|
|
} |
431
|
|
|
$insert->bindValue(':role', $role); |
432
|
|
|
$insert->bindValue(':door4', (int) in_array($role, ['responsible', 'administrator'], true)); |
433
|
|
|
$insert->bindValue(':family_relationship', $relationship); |
434
|
|
|
|
435
|
|
|
if ($this->members[$user['family_uid']]['envoi_papier'] && empty($user['email'])) { |
436
|
|
|
$insert->bindValue(':billing_type', 'paper'); |
437
|
|
|
} else { |
438
|
|
|
$insert->bindValue(':billing_type', 'electronic'); |
439
|
|
|
} |
440
|
|
|
$insert->bindValue(':welcome_session_date', $this->members[$user['family_uid']]["date_séance d'accueil"]); |
441
|
|
|
|
442
|
|
|
$userStatus = 'new'; |
443
|
|
|
if ($user['status_new'] + $user['status_actif'] + $user['status_archived'] > 1) { |
444
|
|
|
echo sprintf('WARN individu %u (%s %s) a plus d\' un statut actif à la fois', $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
445
|
|
|
} |
446
|
|
|
|
447
|
|
|
if ($this->members[$user['family_uid']]['membre_suspension']) { |
448
|
|
|
$userStatus = 'inactive'; |
449
|
|
|
} elseif ($user['status_actif']) { |
450
|
|
|
$userStatus = 'active'; |
451
|
|
|
} elseif ($user['status_archived']) { |
452
|
|
|
$userStatus = 'archived'; |
453
|
|
|
} |
454
|
|
|
$insert->bindValue(':status', $userStatus); |
455
|
|
|
|
456
|
|
|
$insert->bindValue(':creation_date', $this->members[$user['family_uid']]['date_entrée ichtus']); |
457
|
|
|
|
458
|
|
|
// Generate a new random password and store it in TYPO3 fe_users to send a newsletter to users |
459
|
|
|
$password = $this->generatePassword(); |
460
|
|
|
$exportPassword->bindValue(':password', $password); |
461
|
|
|
$exportPassword->bindValue(':user_id', $user['uid']); |
462
|
|
|
$exportPassword->execute(); |
463
|
|
|
$insert->bindValue(':password', password_hash($password, PASSWORD_DEFAULT)); |
464
|
|
|
|
465
|
|
|
if ($insert->execute() === false) { |
466
|
|
|
echo sprintf('ERROR: création de l\'individu %u (%s %s)', $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
467
|
|
|
|
468
|
|
|
continue; |
469
|
|
|
} |
470
|
|
|
|
471
|
|
|
// Crée un compte débiteur pour le membre |
472
|
|
|
$createAccount->bindValue(':owner', $user['uid']); |
473
|
|
|
$createAccount->bindValue(':name', implode(' ', [$user['first_name'], $user['last_name']])); |
474
|
|
|
$accountNumber = sprintf('2030%04u', $user['uid']); // 2030 (Acomptes de client) & ID User = numéro de compte unique |
475
|
|
|
$createAccount->bindValue(':code', $accountNumber); |
476
|
|
|
$createAccount->bindValue(':balance', 0.00); // Importer le solde des comptes 2018 ? |
477
|
|
|
if ($createAccount->execute() === false) { |
478
|
|
|
echo sprintf('ERROR: échec de création de compte débiteur n°%u pour l\'utilisateur %u (%s %s)', $accountNumber, $user['uid'], $user['first_name'], $user['last_name']) . PHP_EOL; |
479
|
|
|
} |
480
|
|
|
|
481
|
|
|
// Assigne les tags au membre |
482
|
|
|
if (!empty($user['ichtus_comite_fonction'])) { |
483
|
|
|
$userTagId = $this->insertUserTag($user['ichtus_comite_fonction']); |
484
|
|
|
$linkToTag->bindValue(':user_id', $user['uid']); |
485
|
|
|
$linkToTag->bindValue(':user_tag_id', $userTagId); |
486
|
|
|
$linkToTag->execute(); |
487
|
|
|
} |
488
|
|
|
if (!empty($this->users[$user['uid']]['ichtus_NFT'])) { |
489
|
|
|
$userTagId = $this->insertUserTag('Membre NFT'); |
490
|
|
|
$linkToTag->bindValue(':user_id', $user['uid']); |
491
|
|
|
$linkToTag->bindValue(':user_tag_id', $userTagId); |
492
|
|
|
$linkToTag->execute(); |
493
|
|
|
} |
494
|
|
|
|
495
|
|
|
// Assigne les brevets au membre |
496
|
|
|
$linkToLicense->bindValue(':user_id', $user['uid']); |
497
|
|
|
switch ($user['ichtus_autvoile']) { |
498
|
|
|
case 1: $idLicense = 2000; |
499
|
|
|
|
500
|
|
|
break; // Voile niveau 1 |
501
|
|
|
case 2: $idLicense = 2001; |
502
|
|
|
|
503
|
|
|
break; // Voile niveau 2 |
504
|
|
|
case 3: $idLicense = 2002; |
505
|
|
|
|
506
|
|
|
break; // Voile niveau 3 |
507
|
|
|
default: $idLicense = null; |
508
|
|
|
} |
509
|
|
|
if ($idLicense) { |
|
|
|
|
510
|
|
|
echo sprintf('%s %s: brevet voile niveau %u', $user['first_name'], $user['last_name'], $user['ichtus_autvoile']) . PHP_EOL; |
511
|
|
|
$linkToLicense->bindValue(':license_id', $idLicense); |
512
|
|
|
$linkToLicense->execute(); |
513
|
|
|
} |
514
|
|
|
} |
515
|
|
|
|
516
|
|
|
$updateOwner = $conn->prepare('UPDATE user SET owner_id=:owner WHERE id=:id'); |
517
|
|
|
foreach ($this->users as $user) { |
518
|
|
|
if ($user['family_uid'] && $user['family_uid'] !== $user['uid']) { |
519
|
|
|
// Update family ownership |
520
|
|
|
$updateOwner->bindValue(':owner', $user['family_uid']); |
521
|
|
|
$updateOwner->bindValue(':id', $user['uid']); |
522
|
|
|
$updateOwner->execute(); |
523
|
|
|
} |
524
|
|
|
} |
525
|
|
|
} |
526
|
|
|
|
527
|
|
|
public function loadStorageRequests(): void |
528
|
|
|
{ |
529
|
|
|
$this->connectTypo3(); |
530
|
|
|
$query = 'SELECT * FROM tx_speciality_storage_places'; |
531
|
|
|
$statement = $this->typo3->prepare($query); |
532
|
|
|
|
533
|
|
|
if ($statement->execute()) { |
534
|
|
|
echo sprintf('%u demandes d\'emplacement de stockage à importer...', $statement->rowCount()) . PHP_EOL; |
535
|
|
|
foreach ($statement->fetchAll(\PDO::FETCH_ASSOC) as $request) { |
536
|
|
|
if (!array_key_exists($request['uid_link'], $this->members)) { |
537
|
|
|
echo sprintf('WARN: demande de stockage %u pour membre %u (%s %s) introuvable', $request['uid'], $request['uid_link'], $request['first_name'], $request['last_name']) . PHP_EOL; |
538
|
|
|
} |
539
|
|
|
$this->storage[$request['uid_link']] = $request; |
540
|
|
|
} |
541
|
|
|
} |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
/** |
545
|
|
|
* Create bookables (storage lockers, cabinets, space for boats) |
546
|
|
|
*/ |
547
|
|
|
private function insertBookables(): void |
548
|
|
|
{ |
549
|
|
|
$conn = $this->entityManager->getConnection(); |
550
|
|
|
|
551
|
|
|
$insert = <<<EOT |
552
|
|
|
INSERT INTO bookable( |
553
|
|
|
code, |
554
|
|
|
name, |
555
|
|
|
description, |
556
|
|
|
initial_price, |
557
|
|
|
periodic_price, |
558
|
|
|
simultaneous_booking_maximum, |
559
|
|
|
booking_type, |
560
|
|
|
creation_date, |
561
|
|
|
credit_account_id |
562
|
|
|
) VALUES ( |
563
|
|
|
:code, |
564
|
|
|
:name, |
565
|
|
|
:description, |
566
|
|
|
:initial_price, |
567
|
|
|
:periodic_price, |
568
|
|
|
:simultaneous_booking_maximum, |
569
|
|
|
:booking_type, |
570
|
|
|
NOW(), |
571
|
|
|
10036 -- Vente de prestations -> Location casiers |
572
|
|
|
) |
573
|
|
|
EOT; |
574
|
|
|
|
575
|
|
|
$insert = $conn->prepare($insert); |
576
|
|
|
|
577
|
|
|
$linkToTag = $conn->prepare('INSERT INTO bookable_tag_bookable(bookable_tag_id, bookable_id) VALUES (:bookable_tag_id, :bookable_id)'); |
578
|
|
|
|
579
|
|
|
// Armoires |
580
|
|
|
$insert->bindValue(':initial_price', 0); |
581
|
|
|
$insert->bindValue(':periodic_price', 50); |
582
|
|
|
// Une armoire peut être partagée entre 2 ou 3 membres |
583
|
|
|
$insert->bindValue(':simultaneous_booking_maximum', 3); |
584
|
|
|
|
585
|
|
|
/* |
586
|
|
|
* admin_only, because l'attribution d'un casier particulier est faite par l'admin, |
587
|
|
|
* bien que le membre demande un "Casier" il ne doit pas pouvoir réserver "Casier 26" |
588
|
|
|
*/ |
589
|
|
|
$insert->bindValue(':booking_type', 'admin_only'); |
590
|
|
|
|
591
|
|
|
$insert->bindValue(':description', 'Armoire (50 x 200 x 70 cm)'); |
592
|
|
|
for ($i = 1; $i <= 120; ++$i) { |
593
|
|
|
$insert->bindValue(':name', sprintf('Armoire %u', $i)); |
594
|
|
|
$insert->bindValue(':code', sprintf('STA%u', $i)); |
595
|
|
|
$insert->execute(); |
596
|
|
|
$linkToTag->bindValue(':bookable_id', $conn->lastInsertId()); |
597
|
|
|
$linkToTag->bindValue(':bookable_tag_id', $this->insertBookableTag('Armoire')); |
598
|
|
|
$linkToTag->execute(); |
599
|
|
|
$linkToTag->bindValue(':bookable_tag_id', $this->insertBookableTag('Stockage')); |
600
|
|
|
$linkToTag->execute(); |
601
|
|
|
} |
602
|
|
|
|
603
|
|
|
// Casiers |
604
|
|
|
$insert->bindValue(':periodic_price', 30); |
605
|
|
|
$insert->bindValue(':simultaneous_booking_maximum', 1); |
606
|
|
|
$insert->bindValue(':description', 'Casier (50 x 50 x 70 cm)'); |
607
|
|
|
for ($i = 1; $i <= 36; ++$i) { |
608
|
|
|
$insert->bindValue(':name', sprintf('Casier %u', $i)); |
609
|
|
|
$insert->bindValue(':code', sprintf('STC%u', $i)); |
610
|
|
|
$insert->execute(); |
611
|
|
|
$linkToTag->bindValue(':bookable_id', $conn->lastInsertId()); |
612
|
|
|
$linkToTag->bindValue(':bookable_tag_id', $this->insertBookableTag('Casier')); |
613
|
|
|
$linkToTag->execute(); |
614
|
|
|
$linkToTag->bindValue(':bookable_tag_id', $this->insertBookableTag('Stockage')); |
615
|
|
|
$linkToTag->execute(); |
616
|
|
|
} |
617
|
|
|
} |
618
|
|
|
|
619
|
|
|
/** |
620
|
|
|
* Create periodic bookings for all members |
621
|
|
|
*/ |
622
|
|
|
private function insertBookings(): void |
623
|
|
|
{ |
624
|
|
|
$conn = $this->entityManager->getConnection(); |
625
|
|
|
|
626
|
|
|
$insert = <<<EOT |
627
|
|
|
INSERT INTO booking( |
628
|
|
|
owner_id, |
629
|
|
|
creation_date, |
630
|
|
|
status, |
631
|
|
|
participant_count, |
632
|
|
|
start_date, |
633
|
|
|
bookable_id |
634
|
|
|
) VALUES ( |
635
|
|
|
:owner, |
636
|
|
|
NOW(), |
637
|
|
|
:status, |
638
|
|
|
:participant_count, |
639
|
|
|
NOW(), |
640
|
|
|
:bookable |
641
|
|
|
) |
642
|
|
|
EOT; |
643
|
|
|
|
644
|
|
|
$insert = $conn->prepare($insert); |
645
|
|
|
$insert->bindValue(':status', 'booked'); |
646
|
|
|
$insert->bindValue(':participant_count', 1); |
647
|
|
|
|
648
|
|
|
// Réservations liées au chef de famille |
649
|
|
|
foreach ($this->members as $idMember => $member) { |
650
|
|
|
// Cotisation annuelle |
651
|
|
|
$insert->bindValue(':owner', $idMember); |
652
|
|
|
$insert->bindValue(':bookable', 3006); // Cotisation annuelle |
653
|
|
|
$insert->execute(); |
654
|
|
|
|
655
|
|
|
// Fond de réparation (optionnel) |
656
|
|
|
if (!empty($member['assurances']) && mb_strpos($member['assurances'], 'Fonds réparation') !== false) { |
657
|
|
|
$insert->bindValue(':bookable', 3026); // Fonds de réparation interne |
658
|
|
|
$insert->execute(); |
659
|
|
|
} |
660
|
|
|
} |
661
|
|
|
|
662
|
|
|
// Réservations liées aux individus (mais facturées au chef de famille) |
663
|
|
|
foreach ($this->users as $idUser => $user) { |
664
|
|
|
$insert->bindValue(':owner', $idUser); |
665
|
|
|
|
666
|
|
|
// Adhésion NFT (optionnel) |
667
|
|
|
if (!empty($user['ichtus_NFT'])) { |
668
|
|
|
$insert->bindValue(':bookable', 3004); // Cotisation NFT |
669
|
|
|
$insert->execute(); |
670
|
|
|
} |
671
|
|
|
|
672
|
|
|
// Licence Swiss Sailing |
673
|
|
|
if (!empty($user['ichtus_swiss_sailing'])) { |
674
|
|
|
switch ($user['ichtus_swiss_sailing_type']) { |
675
|
|
|
case 'A': |
676
|
|
|
$idBookable = 3027; // Licence Swiss Sailing (voile) |
677
|
|
|
break; |
678
|
|
|
case 'J': |
679
|
|
|
$idBookable = 3030; // Cotisation Swiss Sailing (NFT, junior) |
680
|
|
|
break; |
681
|
|
|
default: |
682
|
|
|
$idBookable = null; |
683
|
|
|
} |
684
|
|
|
if ($idBookable) { |
|
|
|
|
685
|
|
|
$insert->bindValue(':bookable', $idBookable); |
686
|
|
|
$insert->execute(); |
687
|
|
|
} |
688
|
|
|
} |
689
|
|
|
|
690
|
|
|
// Licence Swiss Windsurfing |
691
|
|
|
if (!empty($user['ichtus_swiss_windsurf_type']) && $user['ichtus_swiss_windsurf_type'] === 'A') { |
692
|
|
|
$insert->bindValue(':bookable', 3028); // Cotisation Swiss Windsurfing (NFT) |
693
|
|
|
$insert->execute(); |
694
|
|
|
} |
695
|
|
|
} |
696
|
|
|
|
697
|
|
|
// Attribution des emplacements de stockage |
698
|
|
|
$selectBookableByName = $conn->prepare('SELECT id, name, simultaneous_booking_maximum FROM bookable WHERE name=:name'); |
699
|
|
|
foreach ($this->storage as $request) { |
700
|
|
|
if (empty($request['uid_link']) || !array_key_exists($request['uid_link'], $this->members)) { |
701
|
|
|
echo sprintf('ERROR: UID membre de %s %s inconnu dans fichier de demande de stockage', $request['first_name'], $request['last_name']) . PHP_EOL; |
702
|
|
|
|
703
|
|
|
continue; |
704
|
|
|
} |
705
|
|
|
$insert->bindValue(':owner', $request['uid_link']); |
706
|
|
|
foreach ([1 => 'Armoire %u', 2 => 'Armoire %u', 3 => 'Casier %u'] as $index => $bookableName) { |
707
|
|
|
if ($request["materiel{$index}"] > 0 && !empty($request["materiel{$index}attrib"])) { |
708
|
|
|
$selectBookableByName->bindValue(':name', sprintf($bookableName, $request["materiel{$index}attrib"])); |
709
|
|
|
$bookable = null; |
710
|
|
|
if ($selectBookableByName->execute() && $selectBookableByName->rowCount() === 1) { |
711
|
|
|
$bookable = $selectBookableByName->fetch(\PDO::FETCH_ASSOC); |
712
|
|
|
} |
713
|
|
|
if ($bookable) { |
714
|
|
|
$insert->bindValue(':bookable', $bookable['id']); |
715
|
|
|
$insert->execute(); |
716
|
|
|
if (!array_key_exists($bookable['id'], $this->storageAllocated)) { |
717
|
|
|
$this->storageAllocated[$bookable['id']] = 1; |
718
|
|
|
} else { |
719
|
|
|
$this->storageAllocated[$bookable['id']] += 1; |
720
|
|
|
} |
721
|
|
|
echo sprintf('%s attribué à %s %s', $bookable['name'], $this->users[$request['uid_link']]['first_name'], $this->users[$request['uid_link']]['last_name']) . PHP_EOL; |
722
|
|
|
if ($this->storageAllocated[$bookable['id']] > $bookable['simultaneous_booking_maximum']) { |
723
|
|
|
echo sprintf('WARN: %s attribué %u fois dépassant la limite de %u', $bookable['name'], $this->storageAllocated[$bookable], $bookable['simultaneous_booking_maximum']) . PHP_EOL; |
724
|
|
|
} |
725
|
|
|
} else { |
726
|
|
|
echo sprintf("ERROR: cannot find $bookableName in bookables", $request["materiel{$index}attrib"]) . PHP_EOL; |
727
|
|
|
} |
728
|
|
|
} |
729
|
|
|
} |
730
|
|
|
} |
731
|
|
|
} |
732
|
|
|
|
733
|
|
|
/** |
734
|
|
|
* Insert or find an user tag by name |
735
|
|
|
* |
736
|
|
|
* @param string $name |
737
|
|
|
* |
738
|
|
|
* @return int ID of the existing or newly tag |
739
|
|
|
*/ |
740
|
|
|
private function insertUserTag(string $name): int |
741
|
|
|
{ |
742
|
|
|
$conn = $this->entityManager->getConnection(); |
743
|
|
|
|
744
|
|
|
$existing = $conn->prepare('SELECT id FROM user_tag WHERE name = :name'); |
745
|
|
|
$existing->bindValue(':name', $name); |
746
|
|
|
$existing->execute(); |
747
|
|
|
if ($existing->rowCount()) { |
748
|
|
|
return (int) $existing->fetchColumn(); |
749
|
|
|
} |
750
|
|
|
|
751
|
|
|
$insert = $conn->prepare('INSERT INTO user_tag(creation_date, name) VALUES (NOW(), :name)'); |
752
|
|
|
$insert->bindValue(':name', $name); |
753
|
|
|
if ($insert->execute()) { |
754
|
|
|
return (int) $conn->lastInsertId(); |
755
|
|
|
} |
756
|
|
|
|
757
|
|
|
throw new Exception(sprintf('Cannot find or insert UserTag "%s"', $name)); |
758
|
|
|
} |
759
|
|
|
|
760
|
|
|
/** |
761
|
|
|
* Insert or find a bookable tag by name |
762
|
|
|
* |
763
|
|
|
* @param string $name |
764
|
|
|
* |
765
|
|
|
* @return int ID of the existing or newly tag |
766
|
|
|
*/ |
767
|
|
|
private function insertBookableTag(string $name): int |
768
|
|
|
{ |
769
|
|
|
$conn = $this->entityManager->getConnection(); |
770
|
|
|
|
771
|
|
|
$existing = $conn->prepare('SELECT id FROM bookable_tag WHERE name = :name'); |
772
|
|
|
$existing->bindValue(':name', $name); |
773
|
|
|
$existing->execute(); |
774
|
|
|
if ($existing->rowCount()) { |
775
|
|
|
return (int) $existing->fetchColumn(); |
776
|
|
|
} |
777
|
|
|
|
778
|
|
|
$insert = $conn->prepare('INSERT INTO bookable_tag(creation_date, name) VALUES (NOW(), :name)'); |
779
|
|
|
$insert->bindValue(':name', $name); |
780
|
|
|
if ($insert->execute()) { |
781
|
|
|
return (int) $conn->lastInsertId(); |
782
|
|
|
} |
783
|
|
|
|
784
|
|
|
throw new Exception(sprintf('Cannot find or insert BookableTag "%s"', $name)); |
785
|
|
|
} |
786
|
|
|
|
787
|
|
|
/** |
788
|
|
|
* Convert MacRoman string to UTF-8 |
789
|
|
|
* |
790
|
|
|
* @param mixed $string |
791
|
|
|
* |
792
|
|
|
* @return string |
793
|
|
|
*/ |
794
|
|
|
private function fromMacRoman($string): ?string |
795
|
|
|
{ |
796
|
|
|
return !empty($string) ? iconv('MacRoman', 'UTF-8', $string) : $string; |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
/** |
800
|
|
|
* Delete existing test users prior importing to prevent an ID collision |
801
|
|
|
*/ |
802
|
|
|
private function deleteTestUsers(): void |
803
|
|
|
{ |
804
|
|
|
$conn = $this->entityManager->getConnection(); |
805
|
|
|
$result = $conn->executeQuery('SELECT id FROM account a where a.owner_id < 0'); |
806
|
|
|
$accounts = $result->fetchAll(\PDO::FETCH_COLUMN); |
807
|
|
|
$stmt = $conn->prepare('DELETE tl, t FROM transaction_line tl JOIN transaction t ON tl.transaction_id = t.id WHERE FIND_IN_SET(CAST(tl.debit_id as char), :accounts) OR FIND_IN_SET(CAST(tl.credit_id as char), :accounts)'); |
808
|
|
|
$stmt->bindValue('accounts', implode(',', $accounts)); |
809
|
|
|
$stmt->execute(); |
810
|
|
|
$stmt = $conn->prepare('DELETE FROM account WHERE FIND_IN_SET(CAST(id as char), :accounts)'); |
811
|
|
|
$stmt->bindValue('accounts', implode(',', $accounts)); |
812
|
|
|
$stmt->execute(); |
813
|
|
|
$stmt = $conn->prepare('DELETE FROM message WHERE creator_id < 0 OR owner_id < 0'); |
814
|
|
|
$stmt->execute(); |
815
|
|
|
$stmt = $conn->prepare('DELETE FROM user_tag WHERE creator_id < 0 OR owner_id < 0'); |
816
|
|
|
$stmt->execute(); |
817
|
|
|
$stmt = $conn->prepare('DELETE FROM user_tag WHERE creator_id < 0 OR owner_id < 0'); |
818
|
|
|
$stmt->execute(); |
819
|
|
|
$stmt = $conn->prepare('DELETE FROM expense_claim WHERE creator_id < 0 OR owner_id < 0'); |
820
|
|
|
$stmt->execute(); |
821
|
|
|
$stmt = $conn->prepare('DELETE FROM booking WHERE creator_id < 0 OR owner_id < 0'); |
822
|
|
|
$stmt->execute(); |
823
|
|
|
$stmt = $conn->prepare('UPDATE user set owner_id = NULL WHERE id < 0'); |
824
|
|
|
$stmt->execute(); |
825
|
|
|
$stmt = $conn->prepare('DELETE FROM user WHERE id < 0'); |
826
|
|
|
$stmt->execute(); |
827
|
|
|
} |
828
|
|
|
|
829
|
|
|
/** |
830
|
|
|
* Generate password string which evaluate to "medium" security level by digitalspagethi mix |
831
|
|
|
* |
832
|
|
|
* @param int $length length of password |
833
|
|
|
* |
834
|
|
|
* @return string |
835
|
|
|
*/ |
836
|
|
|
private function generatePassword(int $length = 10): string |
837
|
|
|
{ |
838
|
|
|
$numberLength = 2; |
839
|
|
|
$vowels = 'aeuyAEUY'; |
840
|
|
|
$consonants = 'bdghjmnpqrstvzBDGHJLMNPQRSTVWXZ'; |
841
|
|
|
$numbers = '23456789'; |
842
|
|
|
|
843
|
|
|
$password = ''; |
844
|
|
|
$alt = time() % 2; |
845
|
|
|
for ($i = 0; $i < $length - $numberLength; ++$i) { |
846
|
|
|
if ($alt === 1) { |
847
|
|
|
$password .= $consonants[(mt_rand() % mb_strlen($consonants))]; |
848
|
|
|
$alt = 0; |
849
|
|
|
} else { |
850
|
|
|
$password .= $vowels[(mt_rand() % mb_strlen($vowels))]; |
851
|
|
|
$alt = 1; |
852
|
|
|
} |
853
|
|
|
} |
854
|
|
|
|
855
|
|
|
for ($i = 0; $i < $numberLength; ++$i) { |
856
|
|
|
$password .= $numbers[(mt_rand() % mb_strlen($numbers))]; |
857
|
|
|
} |
858
|
|
|
|
859
|
|
|
return $password; |
860
|
|
|
} |
861
|
|
|
} |
862
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: