dolibarr 19.0.3
datapolicycron.class.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
3 * Copyright (C) 2018-2023 Frédéric France <frederic.france@netlogic.fr>
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
29{
33 public $db;
34
35 public $error;
36
37 public $output;
38
39
45 public function __construct($db)
46 {
47 $this->db = $db;
48 }
49
50
57 public function cleanDataForDataPolicy()
58 {
59 global $conf, $langs, $user;
60
61 $langs->load('datapolicy@datapolicy');
62
63 $error = 0;
64 $errormsg = '';
65 $nbupdated = $nbdeleted = 0;
66
67 // FIXME Exclude data from the selection if there is at least 1 invoice.
68 $arrayofparameters = array(
69 'DATAPOLICY_TIERS_CLIENT' => array(
70 'sql' => "
71 SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
72 WHERE s.entity = %d
73 AND s.client = 1
74 AND s.fournisseur = 0
75 AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
76 AND NOT EXISTS (
77 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_soc = s.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
78 )
79 AND NOT EXISTS (
80 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
81 )
82 ",
83 "class" => "Societe",
84 "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
85 'fields_anonym' => array(
86 'name' => 'MAKEANONYMOUS',
87 'name_bis' => '',
88 'name_alias' => '',
89 'address' => '',
90 'town' => '',
91 'zip' => '',
92 'phone' => '',
93 'email' => '',
94 'url' => '',
95 'fax' => '',
96 'state' => '',
97 'country' => '',
98 'state_id' => '',
99 'socialnetworks' => '',
100 'country_id' => '',
101 )
102 ),
103 'DATAPOLICY_TIERS_PROSPECT' => array(
104 'sql' => "
105 SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
106 WHERE s.entity = %d
107 AND s.client = 2
108 AND s.fournisseur = 0
109 AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
110 AND NOT EXISTS (
111 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_soc = s.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
112 )
113 AND NOT EXISTS (
114 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
115 )
116 ",
117 "class" => "Societe",
118 "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
119 'fields_anonym' => array(
120 'name' => 'MAKEANONYMOUS',
121 'name_bis' => '',
122 'name_alias' => '',
123 'address' => '',
124 'town' => '',
125 'zip' => '',
126 'phone' => '',
127 'email' => '',
128 'url' => '',
129 'fax' => '',
130 'state' => '',
131 'country' => '',
132 'state_id' => '',
133 'socialnetworks' => '',
134 'country_id' => '',
135 )
136 ),
137 'DATAPOLICY_TIERS_PROSPECT_CLIENT' => array(
138 'sql' => "
139 SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
140 WHERE s.entity = %d
141 AND s.client = 3
142 AND s.fournisseur = 0
143 AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
144 AND NOT EXISTS (
145 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_soc = s.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
146 )
147 AND NOT EXISTS (
148 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
149 )
150 ",
151 "class" => "Societe",
152 "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
153 'fields_anonym' => array(
154 'name' => 'MAKEANONYMOUS',
155 'name_bis' => '',
156 'name_alias' => '',
157 'address' => '',
158 'town' => '',
159 'zip' => '',
160 'phone' => '',
161 'email' => '',
162 'url' => '',
163 'fax' => '',
164 'state' => '',
165 'country' => '',
166 'state_id' => '',
167 'socialnetworks' => '',
168 'country_id' => '',
169 )
170 ),
171 'DATAPOLICY_TIERS_NIPROSPECT_NICLIENT' => array(
172 'sql' => "
173 SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
174 WHERE s.entity = %d
175 AND s.client = 0
176 AND s.fournisseur = 0
177 AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
178 AND NOT EXISTS (
179 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_soc = s.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
180 )
181 AND NOT EXISTS (
182 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
183 )
184 ",
185 "class" => "Societe",
186 "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
187 'fields_anonym' => array(
188 'name' => 'MAKEANONYMOUS',
189 'name_bis' => '',
190 'name_alias' => '',
191 'address' => '',
192 'town' => '',
193 'zip' => '',
194 'phone' => '',
195 'email' => '',
196 'url' => '',
197 'fax' => '',
198 'state' => '',
199 'country' => '',
200 'state_id' => '',
201 'socialnetworks' => '',
202 'country_id' => '',
203 )
204 ),
205 'DATAPOLICY_TIERS_FOURNISSEUR' => array(
206 'sql' => "
207 SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
208 WHERE s.entity = %d
209 AND s.fournisseur = 1
210 AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
211 AND NOT EXISTS (
212 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_soc = s.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
213 )
214 AND NOT EXISTS (
215 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
216 )
217 ",
218 "class" => "Societe",
219 "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
220 'fields_anonym' => array(
221 'name' => 'MAKEANONYMOUS',
222 'name_bis' => '',
223 'name_alias' => '',
224 'address' => '',
225 'town' => '',
226 'zip' => '',
227 'phone' => '',
228 'email' => '',
229 'url' => '',
230 'fax' => '',
231 'state' => '',
232 'country' => '',
233 'state_id' => '',
234 'socialnetworks' => '',
235 'country_id' => '',
236 )
237 ),
238 'DATAPOLICY_CONTACT_CLIENT' => array(
239 'sql' => "
240 SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
241 INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
242 WHERE c.entity = %d
243 AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
244 AND s.client = 1
245 AND s.fournisseur = 0
246 AND NOT EXISTS (
247 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_contact = c.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
248 )
249 AND NOT EXISTS (
250 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
251 )
252 ",
253 "class" => "Contact",
254 "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
255 'fields_anonym' => array(
256 'lastname' => 'MAKEANONYMOUS',
257 'firstname' => '',
258 'civility_id' => '',
259 'poste' => '',
260 'address' => '',
261 'town' => '',
262 'zip' => '',
263 'phone_pro' => '',
264 'phone_perso' => '',
265 'phone_mobile' => '',
266 'email' => '',
267 'url' => '',
268 'fax' => '',
269 'state' => '',
270 'country' => '',
271 'state_id' => '',
272 'socialnetworks' => '',
273 'country_id' => '',
274 )
275 ),
276 'DATAPOLICY_CONTACT_PROSPECT' => array(
277 'sql' => "
278 SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
279 INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
280 WHERE c.entity = %d
281 AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
282 AND s.client = 2
283 AND s.fournisseur = 0
284 AND NOT EXISTS (
285 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_contact = c.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
286 )
287 AND NOT EXISTS (
288 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
289 )
290 ",
291 "class" => "Contact",
292 "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
293 'fields_anonym' => array(
294 'lastname' => 'MAKEANONYMOUS',
295 'firstname' => '',
296 'civility_id' => '',
297 'poste' => '',
298 'address' => '',
299 'town' => '',
300 'zip' => '',
301 'phone_pro' => '',
302 'phone_perso' => '',
303 'phone_mobile' => '',
304 'email' => '',
305 'url' => '',
306 'fax' => '',
307 'state' => '',
308 'country' => '',
309 'state_id' => '',
310 'socialnetworks' => '',
311 'country_id' => '',
312 )
313 ),
314 'DATAPOLICY_CONTACT_PROSPECT_CLIENT' => array(
315 'sql' => "
316 SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
317 INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
318 WHERE c.entity = %d
319 AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
320 AND s.client = 3
321 AND s.fournisseur = 0
322 AND NOT EXISTS (
323 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_contact = c.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
324 )
325 AND NOT EXISTS (
326 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
327 )
328 ",
329 "class" => "Contact",
330 "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
331 'fields_anonym' => array(
332 'lastname' => 'MAKEANONYMOUS',
333 'firstname' => '',
334 'civility_id' => '',
335 'poste' => '',
336 'address' => '',
337 'town' => '',
338 'zip' => '',
339 'phone_pro' => '',
340 'phone_perso' => '',
341 'phone_mobile' => '',
342 'email' => '',
343 'url' => '',
344 'fax' => '',
345 'state' => '',
346 'country' => '',
347 'state_id' => '',
348 'socialnetworks' => '',
349 'country_id' => '',
350 )
351 ),
352 'DATAPOLICY_CONTACT_NIPROSPECT_NICLIENT' => array(
353 'sql' => "
354 SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
355 INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
356 WHERE c.entity = %d
357 AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
358 AND s.client = 0
359 AND s.fournisseur = 0
360 AND NOT EXISTS (
361 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_contact = c.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
362 )
363 AND NOT EXISTS (
364 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
365 )
366 ",
367 "class" => "Contact",
368 "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
369 'fields_anonym' => array(
370 'lastname' => 'MAKEANONYMOUS',
371 'firstname' => '',
372 'civility_id' => '',
373 'poste' => '',
374 'address' => '',
375 'town' => '',
376 'zip' => '',
377 'phone_pro' => '',
378 'phone_perso' => '',
379 'phone_mobile' => '',
380 'email' => '',
381 'url' => '',
382 'fax' => '',
383 'state' => '',
384 'country' => '',
385 'state_id' => '',
386 'socialnetworks' => '',
387 'country_id' => '',
388 )
389 ),
390 'DATAPOLICY_CONTACT_FOURNISSEUR' => array(
391 'sql' => "
392 SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
393 INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
394 WHERE c.entity = %d
395 AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
396 AND s.fournisseur = 1
397 AND NOT EXISTS (
398 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_contact = c.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
399 )
400 AND NOT EXISTS (
401 SELECT rowid FROM ".MAIN_DB_PREFIX."facture as f WHERE f.fk_soc = s.rowid
402 )
403 ",
404 "class" => "Contact",
405 "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
406 'fields_anonym' => array(
407 'lastname' => 'MAKEANONYMOUS',
408 'firstname' => '',
409 'civility_id' => '',
410 'poste' => '',
411 'address' => '',
412 'town' => '',
413 'zip' => '',
414 'phone_pro' => '',
415 'phone_perso' => '',
416 'phone_mobile' => '',
417 'email' => '',
418 'url' => '',
419 'fax' => '',
420 'state' => '',
421 'country' => '',
422 'state_id' => '',
423 'socialnetworks' => '',
424 'country_id' => '',
425 )
426 ),
427 'DATAPOLICY_ADHERENT' => array(
428 'sql' => "
429 SELECT a.rowid FROM ".MAIN_DB_PREFIX."adherent as a
430 WHERE a.entity = %d
431 AND a.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
432 AND NOT EXISTS (
433 SELECT id FROM ".MAIN_DB_PREFIX."actioncomm as a WHERE a.fk_element = a.rowid AND a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH) AND a.elementtype LIKE 'member'
434 )
435 ",
436 "class" => "Adherent",
437 "file" => DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php',
438 'fields_anonym' => array(
439 'lastname' => 'MAKEANONYMOUS',
440 'firstname' => 'MAKEANONYMOUS',
441 'civility_id' => '',
442 'societe' => '',
443 'address' => '',
444 'town' => '',
445 'zip' => '',
446 'phone' => '',
447 'phone_perso' => '',
448 'phone_mobile' => '',
449 'email' => '',
450 'url' => '',
451 'fax' => '',
452 'state' => '',
453 'country' => '',
454 'state_id' => '',
455 'socialnetworks' => '',
456 'country_id' => '',
457 )
458 ),
459 );
460
461 $this->db->begin();
462
463 foreach ($arrayofparameters as $key => $params) {
464 if (getDolGlobalInt($key) > 0) {
465 $sql = sprintf($params['sql'], (int) $conf->entity, (int) getDolGlobalInt($key), (int) getDolGlobalInt($key));
466
467 $resql = $this->db->query($sql);
468
469 if ($resql && $this->db->num_rows($resql) > 0) {
470 $num = $this->db->num_rows($resql);
471 $i = 0;
472
473 require_once $params['file'];
474 $object = new $params['class']($this->db);
475
476 while ($i < $num && !$error) {
477 $obj = $this->db->fetch_object($resql);
478
479 $object->fetch($obj->rowid);
480 $object->id = $obj->rowid;
481
482 $action = 'anonymize'; // TODO Offer also action "delete" in setup of module
483
484 if ($action == 'anonymize') {
485 if ($object->isObjectUsed($obj->rowid) == 0) { // If object to clean is used
486 foreach ($params['fields_anonym'] as $fields => $val) {
487 if ($val == 'MAKEANONYMOUS') {
488 $object->$fields = $fields.'-anonymous-'.$obj->rowid;
489 } else {
490 $object->$fields = $val;
491 }
492 }
493 $result = $object->update($obj->rowid, $user);
494 if ($result > 0) {
495 $errormsg = $object->error;
496 $error++;
497 }
498 $nbupdated++;
499 }
500 }
501
502 if ($action == 'delete') { // If object to clean is not used
503 if ($object->element == 'adherent') {
504 $result = $object->delete($obj->rowid, $user);
505 } else {
506 $result = $object->delete($user);
507 }
508 if ($result < 0) {
509 $errormsg = $object->error;
510 $error++;
511 }
512
513 $nbdeleted++;
514 }
515
516 $i++;
517 }
518 }
519 }
520 }
521
522 $this->db->commit();
523
524 if (!$error) {
525 $this->output = $nbupdated.' record updated, '.$nbdeleted.' record deleted';
526 } else {
527 $this->error = $errormsg;
528 }
529
530 return 0;
531 }
532}
Class DataPolicyCron.
cleanDataForDataPolicy()
Function exec CAN BE A CRON TASK.
__construct($db)
Constructor.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.