dolibarr  9.0.0
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 <http://www.gnu.org/licenses/>.
17  */
18 
25 require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
26 
27 
32 {
33  public $family = 'system';
34  public $description = "Triggers of this module add action for BlockedLog module.";
35 
40  public $version = self::VERSION_DOLIBARR;
41 
45  public $picto = 'technic';
46 
57  public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
58  {
59  if (empty($conf->blockedlog->enabled)) return 0; // Module not active, we do nothing
60 
61  // Test if event/record is qualified
62  $listofqualifiedelement = array('facture', 'don', 'payment', 'payment_donation', 'subscription', 'payment_various', 'cashcontrol');
63  if (! in_array($object->element, $listofqualifiedelement)) return 1;
64 
65  dol_syslog("Trigger '".$this->name."' for action '$action' launched by ".__FILE__.". id=".$object->id);
66 
67  require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
68  $b=new BlockedLog($this->db);
69 
70  // Tracked events
71  if (! in_array($action, array_keys($b->trackedevents)))
72  {
73  return 0;
74  }
75 
76  // Event/record is qualified
77  $qualified = 0;
78  $amounts = 0;
79  if ($action==='BILL_VALIDATE' || $action==='BILL_DELETE' || $action === 'BILL_SENTBYMAIL'
80  || $action==='BILL_SUPPLIER_VALIDATE' || $action==='BILL_SUPPLIER_DELETE' || $action === 'BILL_SUPPLIER_SENTBYMAIL'
81  || $action==='MEMBER_SUBSCRIPTION_CREATE' || $action==='MEMBER_SUBSCRIPTION_MODIFY' || $action==='MEMBER_SUBSCRIPTION_DELETE'
82  || $action==='DON_VALIDATE' || $action==='DON_MODIFY' || $action==='DON_DELETE'
83  || $action==='CASHCONTROL_VALIDATE'
84  || (in_array($object->element, array('facture','suplier_invoice')) && $action === 'DOC_DOWNLOAD') || (in_array($object->element, array('facture','suplier_invoice')) && $action === 'DOC_PREVIEW')
85  )
86  {
87  $qualified++;
88 
89  if (in_array($action, array(
90  'MEMBER_SUBSCRIPTION_CREATE','MEMBER_SUBSCRIPTION_MODIFY','MEMBER_SUBSCRIPTION_DELETE',
91  'DON_VALIDATE','DON_MODIFY','DON_DELETE'))) $amounts = (double) $object->amount;
92  elseif ($action == 'CASHCONTROL_VALIDATE')
93  {
94  $amounts = (double) $object->cash + (double) $object->cheque + (double) $object->card;
95  }
96  else $amounts = (double) $object->total_ttc;
97  }
98  /*if ($action === 'BILL_PAYED' || $action==='BILL_UNPAYED'
99  || $action === 'BILL_SUPPLIER_PAYED' || $action === 'BILL_SUPPLIER_UNPAYED')
100  {
101  $qualified++;
102  $amounts= (double) $object->total_ttc;
103  }*/
104  if ($action === 'PAYMENT_CUSTOMER_CREATE' || $action === 'PAYMENT_SUPPLIER_CREATE' || $action === 'DONATION_PAYMENT_CREATE'
105  || $action === 'PAYMENT_CUSTOMER_DELETE' || $action === 'PAYMENT_SUPPLIER_DELETE' || $action === 'DONATION_PAYMENT_DELETE')
106  {
107  $qualified++;
108  $amounts = 0;
109  if(!empty($object->amounts)) {
110  foreach($object->amounts as $amount) {
111  $amounts += price2num($amount);
112  }
113  }
114  }
115  elseif (strpos($action,'PAYMENT')!==false && ! in_array($action, array('PAYMENT_ADD_TO_BANK')))
116  {
117  $qualified++;
118  $amounts = (double) $object->amount;
119  }
120 
121  // Another protection.
122  // May be used when event is DOC_DOWNLOAD or DOC_PREVIEW and element is not an invoice
123  if (! $qualified)
124  {
125  return 0; // not implemented action log
126  }
127 
128  $result = $b->setObjectData($object, $action, $amounts, $user); // Set field date_object, ref_object, fk_object, element, object_data
129 
130  if ($result < 0)
131  {
132  $this->error = $b->error;
133  $this->errors = $b->errors;
134  return -1;
135  }
136 
137  $res = $b->create($user);
138 
139  if ($res < 0)
140  {
141  $this->error = $b->error;
142  $this->errors = $b->errors;
143  return -1;
144  }
145  else
146  {
147  return 1;
148  }
149  }
150 }
Class to stock current configuration.
Definition: conf.class.php:33
Class to manage Dolibarr users.
Definition: user.class.php:41
$conf db name
Only used if Module[ID]Name translation string is not found.
Definition: repair.php:103
runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
Function called on Dolibarrr payment or invoice event.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='')
Write log message into outputs.
Class to manage Blocked Log.
Class of triggered functions for agenda module.
Class to manage translations.
Class that all the triggers must extend.
price2num($amount, $rounding='', $alreadysqlnb=0)
Function that return a number with universal decimal format (decimal separator is &#39;...