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