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