dolibarr  16.0.5
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  $listofqualifiedelement = array('facture', 'don', 'payment', 'payment_donation', 'subscription', 'payment_various', 'cashcontrol');
68  if (!in_array($object->element, $listofqualifiedelement)) {
69  return 1;
70  }
71 
72  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
73 
74  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
75  $b = new BlockedLog($this->db);
76  $b->loadTrackedEvents();
77 
78  // Tracked events
79  if (!in_array($action, array_keys($b->trackedevents))) {
80  return 0;
81  }
82 
83  // Event/record is qualified
84  $qualified = 0;
85  $amounts = 0;
86  if ($action === 'BILL_VALIDATE' || (($action === 'BILL_DELETE' || $action === 'BILL_SENTBYMAIL') && $object->statut != 0)
87  || $action === 'BILL_SUPPLIER_VALIDATE' || (($action === 'BILL_SUPPLIER_DELETE' || $action === 'BILL_SUPPLIER_SENTBYMAIL') && $object->statut != 0)
88  || $action === 'MEMBER_SUBSCRIPTION_CREATE' || $action === 'MEMBER_SUBSCRIPTION_MODIFY' || $action === 'MEMBER_SUBSCRIPTION_DELETE'
89  || $action === 'DON_VALIDATE' || (($action === 'DON_MODIFY' || $action === 'DON_DELETE') && $object->statut != 0)
90  || $action === 'CASHCONTROL_VALIDATE'
91  || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_DOWNLOAD' && $object->statut != 0)
92  || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_PREVIEW' && $object->statut != 0)
93  || (!empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED) && in_array($action, explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED)))
94  ) {
95  $qualified++;
96 
97  if (in_array($action, array(
98  'MEMBER_SUBSCRIPTION_CREATE', 'MEMBER_SUBSCRIPTION_MODIFY', 'MEMBER_SUBSCRIPTION_DELETE',
99  'DON_VALIDATE', 'DON_MODIFY', 'DON_DELETE'))) {
100  $amounts = (double) $object->amount;
101  } elseif ($action == 'CASHCONTROL_VALIDATE') {
102  $amounts = (double) $object->cash + (double) $object->cheque + (double) $object->card;
103  } else {
104  $amounts = (double) $object->total_ttc;
105  }
106  }
107  /*if ($action === 'BILL_PAYED' || $action==='BILL_UNPAYED'
108  || $action === 'BILL_SUPPLIER_PAYED' || $action === 'BILL_SUPPLIER_UNPAYED')
109  {
110  $qualified++;
111  $amounts= (double) $object->total_ttc;
112  }*/
113  if ($action === 'PAYMENT_CUSTOMER_CREATE' || $action === 'PAYMENT_SUPPLIER_CREATE' || $action === 'DONATION_PAYMENT_CREATE'
114  || $action === 'PAYMENT_CUSTOMER_DELETE' || $action === 'PAYMENT_SUPPLIER_DELETE' || $action === 'DONATION_PAYMENT_DELETE') {
115  $qualified++;
116  $amounts = 0;
117  if (!empty($object->amounts)) {
118  foreach ($object->amounts as $amount) {
119  $amounts += price2num($amount);
120  }
121  } elseif (!empty($object->amount)) {
122  $amounts = $object->amount;
123  }
124  } elseif (strpos($action, 'PAYMENT') !== false && !in_array($action, array('PAYMENT_ADD_TO_BANK'))) {
125  $qualified++;
126  $amounts = (double) $object->amount;
127  }
128 
129  // Another protection.
130  // May be used when event is DOC_DOWNLOAD or DOC_PREVIEW and element is not an invoice
131  if (!$qualified) {
132  return 0; // not implemented action log
133  }
134 
135  $result = $b->setObjectData($object, $action, $amounts, $user); // Set field date_object, ref_object, fk_object, element, object_data
136 
137  if ($result < 0) {
138  $this->error = $b->error;
139  $this->errors = $b->errors;
140  return -1;
141  }
142 
143  $res = $b->create($user);
144 
145  if ($res < 0) {
146  $this->error = $b->error;
147  $this->errors = $b->errors;
148  return -1;
149  } else {
150  return 1;
151  }
152  }
153 }
db
$conf db
API class for accounts.
Definition: inc.php:41
description
print *****$script_file(".$version.") pid cd cd cd description as description
Definition: email_expire_services_to_customers.php:83
Translate
Class to manage translations.
Definition: translate.class.php:30
name
$conf db name
Definition: repair.php:122
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
Conf
Class to stock current configuration.
Definition: conf.class.php:33
BlockedLog
Class to manage Blocked Log.
Definition: blockedlog.class.php:26
InterfaceActionsBlockedLog\runTrigger
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called on Dolibarr payment or invoice event.
Definition: interface_50_modBlockedlog_ActionsBlockedLog.class.php:60
InterfaceActionsBlockedLog\__construct
__construct($db)
Constructor.
Definition: interface_50_modBlockedlog_ActionsBlockedLog.class.php:38
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
User
Class to manage Dolibarr users.
Definition: user.class.php:44
InterfaceActionsBlockedLog
Class of triggered functions for agenda module.
Definition: interface_50_modBlockedlog_ActionsBlockedLog.class.php:31
DolibarrTriggers
Class that all the triggers must extend.
Definition: dolibarrtriggers.class.php:21