dolibarr  13.0.2
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 Removed hardcoded values of id
58  $arrayofparameters = array(
59  'DATAPOLICIES_TIERS_CLIENT' => array(
60  'sql' => "
61  SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
62  WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
63  AND s.entity = %d
64  AND s.client = 1
65  AND s.fournisseur = 0
66  AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
67  AND s.rowid NOT IN (
68  SELECT DISTINCT a.fk_soc
69  FROM ".MAIN_DB_PREFIX."actioncomm as a
70  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
71  AND a.fk_soc IS NOT NULL
72  )
73  ",
74  "class" => "Societe",
75  "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
76  'fields_anonym' => array(
77  'name' => $langs->trans('ANONYME'),
78  'name_bis' => '',
79  'name_alias' => '',
80  'address' => '',
81  'town' => '',
82  'zip' => '',
83  'phone' => '',
84  'email' => '',
85  'url' => '',
86  'fax' => '',
87  'state' => '',
88  'country' => '',
89  'state_id' => '',
90  'skype' => '',
91  'country_id' => '',
92  )
93  ),
94  'DATAPOLICIES_TIERS_PROSPECT' => array(
95  'sql' => "
96  SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
97  WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
98  AND s.entity = %d
99  AND s.client = 2
100  AND s.fournisseur = 0
101  AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
102  AND s.rowid NOT IN (
103  SELECT DISTINCT a.fk_soc
104  FROM ".MAIN_DB_PREFIX."actioncomm as a
105  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
106  AND a.fk_soc IS NOT NULL
107  )
108  ",
109  "class" => "Societe",
110  "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
111  'fields_anonym' => array(
112  'name' => $langs->trans('ANONYME'),
113  'name_bis' => '',
114  'name_alias' => '',
115  'address' => '',
116  'town' => '',
117  'zip' => '',
118  'phone' => '',
119  'email' => '',
120  'url' => '',
121  'fax' => '',
122  'state' => '',
123  'country' => '',
124  'state_id' => '',
125  'skype' => '',
126  'country_id' => '',
127  )
128  ),
129  'DATAPOLICIES_TIERS_PROSPECT_CLIENT' => array(
130  'sql' => "
131  SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
132  WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
133  AND s.entity = %d
134  AND s.client = 3
135  AND s.fournisseur = 0
136  AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
137  AND s.rowid NOT IN (
138  SELECT DISTINCT a.fk_soc
139  FROM ".MAIN_DB_PREFIX."actioncomm as a
140  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
141  AND a.fk_soc IS NOT NULL
142  )
143  ",
144  "class" => "Societe",
145  "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
146  'fields_anonym' => array(
147  'name' => $langs->trans('ANONYME'),
148  'name_bis' => '',
149  'name_alias' => '',
150  'address' => '',
151  'town' => '',
152  'zip' => '',
153  'phone' => '',
154  'email' => '',
155  'url' => '',
156  'fax' => '',
157  'state' => '',
158  'country' => '',
159  'state_id' => '',
160  'skype' => '',
161  'country_id' => '',
162  )
163  ),
164  'DATAPOLICIES_TIERS_NIPROSPECT_NICLIENT' => array(
165  'sql' => "
166  SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
167  WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
168  AND s.entity = %d
169  AND s.client = 0
170  AND s.fournisseur = 0
171  AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
172  AND s.rowid NOT IN (
173  SELECT DISTINCT a.fk_soc
174  FROM ".MAIN_DB_PREFIX."actioncomm as a
175  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
176  AND a.fk_soc IS NOT NULL
177  )
178  ",
179  "class" => "Societe",
180  "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
181  'fields_anonym' => array(
182  'name' => $langs->trans('ANONYME'),
183  'name_bis' => '',
184  'name_alias' => '',
185  'address' => '',
186  'town' => '',
187  'zip' => '',
188  'phone' => '',
189  'email' => '',
190  'url' => '',
191  'fax' => '',
192  'state' => '',
193  'country' => '',
194  'state_id' => '',
195  'skype' => '',
196  'country_id' => '',
197  )
198  ),
199  'DATAPOLICIES_TIERS_FOURNISSEUR' => array(
200  'sql' => "
201  SELECT s.rowid FROM ".MAIN_DB_PREFIX."societe as s
202  WHERE (s.fk_forme_juridique IN (11, 12, 13, 15, 17, 18, 19, 35, 60, 312, 316, 401, 600, 700, 1005) OR s.fk_typent = 8)
203  AND s.entity = %d
204  AND s.fournisseur = 1
205  AND s.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
206  AND s.rowid NOT IN (
207  SELECT DISTINCT a.fk_soc
208  FROM ".MAIN_DB_PREFIX."actioncomm as a
209  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
210  AND a.fk_contact IS NOT NULL
211  )
212  ",
213  "class" => "Societe",
214  "file" => DOL_DOCUMENT_ROOT.'/societe/class/societe.class.php',
215  'fields_anonym' => array(
216  'name' => $langs->trans('ANONYME'),
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  'skype' => '',
230  'country_id' => '',
231  )
232  ),
233  'DATAPOLICIES_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 c.rowid NOT IN (
242  SELECT DISTINCT a.fk_contact
243  FROM ".MAIN_DB_PREFIX."actioncomm as a
244  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
245  AND a.fk_contact IS NOT NULL
246  )
247  ",
248  "class" => "Contact",
249  "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
250  'fields_anonym' => array(
251  'lastname' => $langs->trans('ANONYME'),
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  'skype' => '',
268  'jabberid' => '',
269  'country_id' => '',
270  )
271  ),
272  'DATAPOLICIES_CONTACT_PROSPECT' => array(
273  'sql' => "
274  SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
275  INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
276  WHERE c.entity = %d
277  AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
278  AND s.client = 2
279  AND s.fournisseur = 0
280  AND c.rowid NOT IN (
281  SELECT DISTINCT a.fk_contact
282  FROM ".MAIN_DB_PREFIX."actioncomm as a
283  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
284  AND a.fk_contact IS NOT NULL
285  )
286  ",
287  "class" => "Contact",
288  "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
289  'fields_anonym' => array(
290  'lastname' => $langs->trans('ANONYME'),
291  'firstname' => '',
292  'civility_id' => '',
293  'poste' => '',
294  'address' => '',
295  'town' => '',
296  'zip' => '',
297  'phone_pro' => '',
298  'phone_perso' => '',
299  'phone_mobile' => '',
300  'email' => '',
301  'url' => '',
302  'fax' => '',
303  'state' => '',
304  'country' => '',
305  'state_id' => '',
306  'skype' => '',
307  'jabberid' => '',
308  'country_id' => '',
309  )
310  ),
311  'DATAPOLICIES_CONTACT_PROSPECT_CLIENT' => array(
312  'sql' => "
313  SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
314  INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
315  WHERE c.entity = %d
316  AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
317  AND s.client = 3
318  AND s.fournisseur = 0
319  AND c.rowid NOT IN (
320  SELECT DISTINCT a.fk_contact
321  FROM ".MAIN_DB_PREFIX."actioncomm as a
322  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
323  AND a.fk_contact IS NOT NULL
324  )
325  ",
326  "class" => "Contact",
327  "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
328  'fields_anonym' => array(
329  'lastname' => $langs->trans('ANONYME'),
330  'firstname' => '',
331  'civility_id' => '',
332  'poste' => '',
333  'address' => '',
334  'town' => '',
335  'zip' => '',
336  'phone_pro' => '',
337  'phone_perso' => '',
338  'phone_mobile' => '',
339  'email' => '',
340  'url' => '',
341  'fax' => '',
342  'state' => '',
343  'country' => '',
344  'state_id' => '',
345  'skype' => '',
346  'jabberid' => '',
347  'country_id' => '',
348  )
349  ),
350  'DATAPOLICIES_CONTACT_NIPROSPECT_NICLIENT' => array(
351  'sql' => "
352  SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
353  INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
354  WHERE c.entity = %d
355  AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
356  AND s.client = 0
357  AND s.fournisseur = 0
358  AND c.rowid NOT IN (
359  SELECT DISTINCT a.fk_contact
360  FROM ".MAIN_DB_PREFIX."actioncomm as a
361  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
362  AND a.fk_contact IS NOT NULL
363  )
364  ",
365  "class" => "Contact",
366  "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
367  'fields_anonym' => array(
368  'lastname' => $langs->trans('ANONYME'),
369  'firstname' => '',
370  'civility_id' => '',
371  'poste' => '',
372  'address' => '',
373  'town' => '',
374  'zip' => '',
375  'phone_pro' => '',
376  'phone_perso' => '',
377  'phone_mobile' => '',
378  'email' => '',
379  'url' => '',
380  'fax' => '',
381  'state' => '',
382  'country' => '',
383  'state_id' => '',
384  'skype' => '',
385  'jabberid' => '',
386  'country_id' => '',
387  )
388  ),
389  'DATAPOLICIES_CONTACT_FOURNISSEUR' => array(
390  'sql' => "
391  SELECT c.rowid FROM ".MAIN_DB_PREFIX."socpeople as c
392  INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = c.fk_soc
393  WHERE c.entity = %d
394  AND c.tms < DATE_SUB(NOW(), INTERVAL %d MONTH)
395  AND s.fournisseur = 1
396  AND c.rowid NOT IN (
397  SELECT DISTINCT a.fk_contact
398  FROM ".MAIN_DB_PREFIX."actioncomm as a
399  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
400  AND a.fk_contact IS NOT NULL
401  )
402  ",
403  "class" => "Contact",
404  "file" => DOL_DOCUMENT_ROOT.'/contact/class/contact.class.php',
405  'fields_anonym' => array(
406  'lastname' => $langs->trans('ANONYME'),
407  'firstname' => '',
408  'civility_id' => '',
409  'poste' => '',
410  'address' => '',
411  'town' => '',
412  'zip' => '',
413  'phone_pro' => '',
414  'phone_perso' => '',
415  'phone_mobile' => '',
416  'email' => '',
417  'url' => '',
418  'fax' => '',
419  'state' => '',
420  'country' => '',
421  'state_id' => '',
422  'skype' => '',
423  'jabberid' => '',
424  'country_id' => '',
425  )
426  ),
427  'DATAPOLICIES_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 a.rowid NOT IN (
433  SELECT DISTINCT a.fk_element
434  FROM ".MAIN_DB_PREFIX."actioncomm as a
435  WHERE a.tms > DATE_SUB(NOW(), INTERVAL %d MONTH)
436  AND a.elementtype LIKE 'member'
437  AND a.fk_element IS NOT NULL
438  )
439  ",
440  "class" => "Adherent",
441  "file" => DOL_DOCUMENT_ROOT.'/adherents/class/adherent.class.php',
442  'fields_anonym' => array(
443  'lastname' => $langs->trans('ANONYME'),
444  'firstname' => $langs->trans('ANONYME'),
445  'civility_id' => '',
446  'societe' => '',
447  'address' => '',
448  'town' => '',
449  'zip' => '',
450  'phone' => '',
451  'phone_perso' => '',
452  'phone_mobile' => '',
453  'email' => '',
454  'url' => '',
455  'fax' => '',
456  'state' => '',
457  'country' => '',
458  'state_id' => '',
459  'skype' => '',
460  'country_id' => '',
461  )
462  ),
463  );
464 
465  $this->db->begin();
466 
467  foreach ($arrayofparameters as $key => $params)
468  {
469  if ($conf->global->$key != '' && is_numeric($conf->global->$key) && (int) $conf->global->$key > 0)
470  {
471  $sql = sprintf($params['sql'], (int) $conf->entity, (int) $conf->global->$key, (int) $conf->global->$key);
472 
473  $resql = $this->db->query($sql);
474 
475  if ($resql && $this->db->num_rows($resql) > 0)
476  {
477  $num = $this->db->num_rows($resql);
478  $i = 0;
479 
480  require_once $params['file'];
481  $object = new $params['class']($this->db);
482 
483  while ($i < $num && !$error)
484  {
485  $obj = $this->db->fetch_object($resql);
486 
487  $object->fetch($obj->rowid);
488  $object->id = $obj->rowid;
489 
490  if ($object->isObjectUsed($obj->rowid) > 0) // If object to clean is used
491  {
492  foreach ($params['fields_anonym'] as $fields => $val) {
493  $object->$fields = $val;
494  }
495  $result = $object->update($obj->rowid, $user);
496  if ($result > 0)
497  {
498  if ($params['class'] == 'Societe') {
499  // We delete contacts of thirdparty
500  $sql = "DELETE FROM ".MAIN_DB_PREFIX."socpeople WHERE fk_soc = ".$obj->rowid;
501  $result = $this->db->query($sql);
502  if ($result < 0)
503  {
504  $errormsg = $this->db->lasterror();
505  $error++;
506  }
507  }
508  } else {
509  $errormsg = $object->error;
510  $error++;
511  }
512  $nbupdated++;
513  } else { // If object to clean is not used
514  if ($object->element == 'adherent') {
515  $result = $object->delete($obj->rowid, $user);
516  } else {
517  $result = $object->delete($user);
518  }
519  if ($result < 0)
520  {
521  $errormsg = $object->error;
522  $error++;
523  }
524 
525  $nbdeleted++;
526  }
527 
528  $i++;
529  }
530  }
531  }
532  }
533 
534  $this->db->commit();
535 
536  if (!$error)
537  {
538  $this->output = $nbupdated.' record updated, '.$nbdeleted.' record deleted';
539  } else {
540  $this->error = $errormsg;
541  }
542 
543  return 0;
544  }
545 }
cleanDataForDataPolicy()
Function exec CAN BE A CRON TASK.
$conf db
API class for accounts.
Definition: inc.php:54
__construct($db)
Constructor.
Class DataPolicyCron.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232