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