Passed
Push — master ( 65bdac...4e88da )
by Alxarafe
32:38
created

...ce_50_modBlockedlog_ActionsBlockedLog.class.php (2 issues)

Severity
1
<?php
2
/* Copyright (C) 2017       ATM Consulting      <[email protected]>
3
 * Copyright (C) 2017-2018  Laurent Destailleur	<[email protected]>
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
19
/**
20
 *	\file       htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php
21
 *  \ingroup    system
22
 *  \brief      Trigger file for blockedlog module
23
 */
24
25
require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
26
27
28
/**
29
 *  Class of triggered functions for agenda module
30
 */
31
class InterfaceActionsBlockedLog extends DolibarrTriggers
32
{
33
	public $family = 'system';
34
	public $description = "Triggers of this module add action for BlockedLog module.";
35
36
	/**
37
	 * Version of the trigger
38
	 * @var string
39
	 */
40
	public $version = self::VERSION_DOLIBARR;
41
42
	/**
43
	 * @var string Image of the trigger
44
	 */
45
	public $picto = 'technic';
46
47
	/**
48
	 * Function called on Dolibarrr payment or invoice event.
49
	 *
50
	 * @param string		$action		Event action code
51
	 * @param Object		$object     Object
52
	 * @param User		    $user       Object user
53
	 * @param Translate 	$langs      Object langs
54
	 * @param conf		    $conf       Object conf
55
	 * @return int         				<0 if KO, 0 if no triggered ran, >0 if OK
56
	 */
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;
0 ignored issues
show
Deprecated Code introduced by
The property DolibarrTriggers::$error has been deprecated: Use $this->errors ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

132
			/** @scrutinizer ignore-deprecated */ $this->error = $b->error;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
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;
0 ignored issues
show
Deprecated Code introduced by
The property DolibarrTriggers::$error has been deprecated: Use $this->errors ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

141
			/** @scrutinizer ignore-deprecated */ $this->error = $b->error;

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
142
			$this->errors = $b->errors;
143
			return -1;
144
		}
145
		else
146
		{
147
			return 1;
148
		}
149
    }
150
}
151