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