dolibarr  19.0.0-dev
interface_50_modBlockedlog_ActionsBlockedLog.class.php
Go to the documentation of this file.
1 <?php
2 /* Copyright (C) 2017 ATM Consulting <contact@atm-consulting.fr>
3  * Copyright (C) 2017-2018 Laurent Destailleur <eldy@users.sourceforge.net>
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 
25 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
26 
27 
32 {
38  public function __construct($db)
39  {
40  $this->db = $db;
41 
42  $this->name = preg_replace('/^Interface/i', '', get_class($this));
43  $this->family = "system";
44  $this->description = "Triggers of this module add action for BlockedLog module (Module of unalterable logs).";
45  // 'development', 'experimental', 'dolibarr' or version
46  $this->version = self::VERSION_DOLIBARR;
47  $this->picto = 'technic';
48  }
49 
60  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
61  {
62  if (empty($conf->blockedlog) || empty($conf->blockedlog->enabled)) {
63  return 0; // Module not active, we do nothing
64  }
65 
66  // Test if event/record is qualified
67  if (empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED) || !in_array($action, explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED))) {
68  // If custom actions are not set or if action not into custom actions, we can exclude action if object->elementis not valid
69  $listofqualifiedelement = array('facture', 'don', 'payment', 'payment_donation', 'subscription', 'payment_various', 'cashcontrol');
70  if (!in_array($object->element, $listofqualifiedelement)) {
71  return 1;
72  }
73  }
74 
75  dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
76 
77  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
78  $b = new BlockedLog($this->db);
79  $b->loadTrackedEvents();
80 
81  // Tracked events
82  if (!in_array($action, array_keys($b->trackedevents))) {
83  return 0;
84  }
85 
86  // Event/record is qualified
87  $qualified = 0;
88  $amounts = 0;
89  if ($action === 'BILL_VALIDATE' || (($action === 'BILL_DELETE' || $action === 'BILL_SENTBYMAIL') && $object->statut != 0)
90  || $action === 'BILL_SUPPLIER_VALIDATE' || (($action === 'BILL_SUPPLIER_DELETE' || $action === 'BILL_SUPPLIER_SENTBYMAIL') && $object->statut != 0)
91  || $action === 'MEMBER_SUBSCRIPTION_CREATE' || $action === 'MEMBER_SUBSCRIPTION_MODIFY' || $action === 'MEMBER_SUBSCRIPTION_DELETE'
92  || $action === 'DON_VALIDATE' || (($action === 'DON_MODIFY' || $action === 'DON_DELETE') && $object->statut != 0)
93  || $action === 'CASHCONTROL_VALIDATE'
94  || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_DOWNLOAD' && $object->statut != 0)
95  || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_PREVIEW' && $object->statut != 0)
96  || (!empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED) && in_array($action, explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED)))
97  ) {
98  $qualified++;
99 
100  if (in_array($action, array(
101  'MEMBER_SUBSCRIPTION_CREATE', 'MEMBER_SUBSCRIPTION_MODIFY', 'MEMBER_SUBSCRIPTION_DELETE',
102  'DON_VALIDATE', 'DON_MODIFY', 'DON_DELETE'))) {
103  $amounts = (double) $object->amount;
104  } elseif ($action == 'CASHCONTROL_VALIDATE') {
105  $amounts = (double) $object->cash + (double) $object->cheque + (double) $object->card;
106  } elseif (property_exists($object, 'total_ttc')) {
107  $amounts = (double) $object->total_ttc;
108  }
109  }
110  /*if ($action === 'BILL_PAYED' || $action==='BILL_UNPAYED'
111  || $action === 'BILL_SUPPLIER_PAYED' || $action === 'BILL_SUPPLIER_UNPAYED')
112  {
113  $qualified++;
114  $amounts= (double) $object->total_ttc;
115  }*/
116  if ($action === 'PAYMENT_CUSTOMER_CREATE' || $action === 'PAYMENT_SUPPLIER_CREATE' || $action === 'DONATION_PAYMENT_CREATE'
117  || $action === 'PAYMENT_CUSTOMER_DELETE' || $action === 'PAYMENT_SUPPLIER_DELETE' || $action === 'DONATION_PAYMENT_DELETE') {
118  $qualified++;
119  $amounts = 0;
120  if (!empty($object->amounts)) {
121  foreach ($object->amounts as $amount) {
122  $amounts += (double) $amount;
123  }
124  } elseif (!empty($object->amount)) {
125  $amounts = $object->amount;
126  }
127  } elseif (strpos($action, 'PAYMENT') !== false && !in_array($action, array('PAYMENT_ADD_TO_BANK'))) {
128  $qualified++;
129  $amounts = (double) $object->amount;
130  }
131 
132  // Another protection.
133  // May be used when event is DOC_DOWNLOAD or DOC_PREVIEW and element is not an invoice
134  if (!$qualified) {
135  return 0; // not implemented action log
136  }
137 
138  // Set field date_object, ref_object, fk_object, element, object_data
139  $result = $b->setObjectData($object, $action, $amounts, $user);
140  //var_dump($b); exit;
141 
142  if ($result < 0) {
143  $this->error = $b->error;
144  $this->errors = $b->errors;
145  return -1;
146  }
147 
148  $res = $b->create($user);
149 
150  if ($res < 0) {
151  $this->error = $b->error;
152  $this->errors = $b->errors;
153  return -1;
154  } else {
155  return 1;
156  }
157  }
158 }
Class to manage Blocked Log.
Class to stock current configuration.
Definition: conf.class.php:34
Class that all the triggers must extend.
Class of triggered functions for agenda module.
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called on Dolibarr payment or invoice event.
Class to manage translations.
Class to manage Dolibarr users.
Definition: user.class.php:48
print *****$script_file(".$version.") pid cd cd cd description as description
Only used if Module[ID]Desc translation string is not found.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:123