interface_50_modBlockedlog_ActionsBlockedLog.class.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. /**
  19. * \file htdocs/core/triggers/interface_50_modBlockedlog_ActionsBlockedLog.class.php
  20. * \ingroup system
  21. * \brief Trigger file for blockedlog module
  22. */
  23. require_once DOL_DOCUMENT_ROOT.'/core/triggers/dolibarrtriggers.class.php';
  24. /**
  25. * Class of triggered functions for agenda module
  26. */
  27. class InterfaceActionsBlockedLog extends DolibarrTriggers
  28. {
  29. /**
  30. * Constructor
  31. *
  32. * @param DoliDB $db Database handler
  33. */
  34. public function __construct($db)
  35. {
  36. $this->db = $db;
  37. $this->name = preg_replace('/^Interface/i', '', get_class($this));
  38. $this->family = "system";
  39. $this->description = "Triggers of this module add action for BlockedLog module (Module of unalterable logs).";
  40. // 'development', 'experimental', 'dolibarr' or version
  41. $this->version = self::VERSION_DOLIBARR;
  42. $this->picto = 'technic';
  43. }
  44. /**
  45. * Function called on Dolibarr payment or invoice event.
  46. *
  47. * @param string $action Event action code
  48. * @param Object $object Object
  49. * @param User $user Object user
  50. * @param Translate $langs Object langs
  51. * @param conf $conf Object conf
  52. * @return int <0 if KO, 0 if no triggered ran, >0 if OK
  53. */
  54. public function runTrigger($action, $object, User $user, Translate $langs, Conf $conf)
  55. {
  56. if (empty($conf->blockedlog) || empty($conf->blockedlog->enabled)) {
  57. return 0; // Module not active, we do nothing
  58. }
  59. // Test if event/record is qualified
  60. if (empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED) || !in_array($action, explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED))) {
  61. // If custom actions are not set or if action not into custom actions, we can exclude action if object->elementis not valid
  62. $listofqualifiedelement = array('facture', 'don', 'payment', 'payment_donation', 'subscription', 'payment_various', 'cashcontrol');
  63. if (!in_array($object->element, $listofqualifiedelement)) {
  64. return 1;
  65. }
  66. }
  67. dol_syslog("Trigger '".$this->name."' for action '".$action."' launched by ".__FILE__.". id=".$object->id);
  68. require_once DOL_DOCUMENT_ROOT.'/blockedlog/class/blockedlog.class.php';
  69. $b = new BlockedLog($this->db);
  70. $b->loadTrackedEvents();
  71. // Tracked events
  72. if (!in_array($action, array_keys($b->trackedevents))) {
  73. return 0;
  74. }
  75. // Event/record is qualified
  76. $qualified = 0;
  77. $amounts = 0;
  78. if ($action === 'BILL_VALIDATE' || (($action === 'BILL_DELETE' || $action === 'BILL_SENTBYMAIL') && $object->statut != 0)
  79. || $action === 'BILL_SUPPLIER_VALIDATE' || (($action === 'BILL_SUPPLIER_DELETE' || $action === 'BILL_SUPPLIER_SENTBYMAIL') && $object->statut != 0)
  80. || $action === 'MEMBER_SUBSCRIPTION_CREATE' || $action === 'MEMBER_SUBSCRIPTION_MODIFY' || $action === 'MEMBER_SUBSCRIPTION_DELETE'
  81. || $action === 'DON_VALIDATE' || (($action === 'DON_MODIFY' || $action === 'DON_DELETE') && $object->statut != 0)
  82. || $action === 'CASHCONTROL_VALIDATE'
  83. || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_DOWNLOAD' && $object->statut != 0)
  84. || (in_array($object->element, array('facture', 'supplier_invoice')) && $action === 'DOC_PREVIEW' && $object->statut != 0)
  85. || (!empty($conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED) && in_array($action, explode(',', $conf->global->BLOCKEDLOG_ADD_ACTIONS_SUPPORTED)))
  86. ) {
  87. $qualified++;
  88. if (in_array($action, array(
  89. 'MEMBER_SUBSCRIPTION_CREATE', 'MEMBER_SUBSCRIPTION_MODIFY', 'MEMBER_SUBSCRIPTION_DELETE',
  90. 'DON_VALIDATE', 'DON_MODIFY', 'DON_DELETE'))) {
  91. $amounts = (double) $object->amount;
  92. } elseif ($action == 'CASHCONTROL_VALIDATE') {
  93. $amounts = (double) $object->cash + (double) $object->cheque + (double) $object->card;
  94. } elseif (property_exists($object, 'total_ttc')) {
  95. $amounts = (double) $object->total_ttc;
  96. }
  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. $qualified++;
  107. $amounts = 0;
  108. if (!empty($object->amounts)) {
  109. foreach ($object->amounts as $amount) {
  110. $amounts += price2num($amount);
  111. }
  112. } elseif (!empty($object->amount)) {
  113. $amounts = $object->amount;
  114. }
  115. } elseif (strpos($action, 'PAYMENT') !== false && !in_array($action, array('PAYMENT_ADD_TO_BANK'))) {
  116. $qualified++;
  117. $amounts = (double) $object->amount;
  118. }
  119. // Another protection.
  120. // May be used when event is DOC_DOWNLOAD or DOC_PREVIEW and element is not an invoice
  121. if (!$qualified) {
  122. return 0; // not implemented action log
  123. }
  124. // Set field date_object, ref_object, fk_object, element, object_data
  125. $result = $b->setObjectData($object, $action, $amounts, $user);
  126. //var_dump($b); exit;
  127. if ($result < 0) {
  128. $this->error = $b->error;
  129. $this->errors = $b->errors;
  130. return -1;
  131. }
  132. $res = $b->create($user);
  133. if ($res < 0) {
  134. $this->error = $b->error;
  135. $this->errors = $b->errors;
  136. return -1;
  137. } else {
  138. return 1;
  139. }
  140. }
  141. }