]> git.agnieray.net Git - galette.git/blobdiff - galette/lib/Galette/Entity/Contribution.php
Disable events from mass changes; closes #1733
[galette.git] / galette / lib / Galette / Entity / Contribution.php
index 1de9acaf5bf66b61163bd14c61e504da674799e5..3457da44e01dc8437c3eeeb0d82943f3376dc18a 100644 (file)
@@ -40,6 +40,7 @@ namespace Galette\Entity;
 use ArrayObject;
 use DateTime;
 use Galette\Events\GaletteEvent;
+use Galette\Features\HasEvent;
 use Throwable;
 use Analog\Analog;
 use Laminas\Db\Sql\Expression;
@@ -85,6 +86,7 @@ use Galette\Features\Dynamics;
 class Contribution
 {
     use Dynamics;
+    use HasEvent;
 
     public const TABLE = 'cotisations';
     public const PK = 'id_cotis';
@@ -135,6 +137,12 @@ class Contribution
         global $preferences;
         $this->_payment_type = (int)$preferences->pref_default_paymenttype;
 
+        $this
+            ->withAddEvent()
+            ->withEditEvent()
+            ->withoutDeleteEvent()
+            ->activateEvents();
+
         /*
          * Fields configuration. Each field is an array and must reflect:
          * array(
@@ -303,7 +311,7 @@ class Contribution
      */
     public function load($id)
     {
-        if (!$this->login->isLogged()) {
+        if (!$this->login->isLogged() && $this->login->id == '') {
             return false;
         }
 
@@ -434,7 +442,11 @@ class Contribution
                             try {
                                 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
                                 if ($d === false) {
-                                    throw new \Exception('Incorrect format');
+                                    //try with non localized date
+                                    $d = \DateTime::createFromFormat("Y-m-d", $value);
+                                    if ($d === false) {
+                                        throw new \Exception('Incorrect format');
+                                    }
                                 }
                                 $this->$prop = $d->format('Y-m-d');
                             } catch (Throwable $e) {
@@ -470,8 +482,8 @@ class Contribution
                         break;
                     case 'montant_cotis':
                         $value = strtr($value, ',', '.');
-                        if (!empty($value)) {
-                            $this->_amount = $value;
+                        if (!empty($value) || $value === '0') {
+                            $this->_amount = (double)$value;
                         }
                         if (!is_numeric($value) && $value !== '') {
                             $this->errors[] = _T("- The amount must be an integer!");
@@ -599,10 +611,17 @@ class Contribution
             $results = $this->zdb->execute($select);
             if ($results->count() > 0) {
                 $result = $results->current();
-                $d = new \DateTime($result->date_debut_cotis);
+
+                $d_begin = new \DateTime($result->date_debut_cotis);
+                $d_end = new \DateTime($result->date_fin_cotis);
+
+                if ($d_begin->format('m-d') == $d_end->format('m-d') && $result->date_fin_cotis == $this->_begin_date) {
+                    //see https://bugs.galette.eu/issues/1762
+                    return true;
+                }
 
                 return _T("- Membership period overlaps period starting at ") .
-                    $d->format(__("Y-m-d"));
+                    $d_begin->format(__("Y-m-d"));
             }
             return true;
         } catch (Throwable $e) {
@@ -656,7 +675,6 @@ class Contribution
                 unset($values['date_fin_cotis']);
             }
 
-            $success = false;
             if (!isset($this->_id) || $this->_id == '') {
                 //we're inserting a new contribution
                 unset($values[self::PK]);
@@ -673,8 +691,7 @@ class Contribution
                         _T("Contribution added"),
                         Adherent::getSName($this->zdb, $this->_member)
                     );
-                    $success = true;
-                    $event = 'contribution.add';
+                    $event = $this->getAddEventName();
                 } else {
                     $hist->add(_T("Fail to add new contribution."));
                     throw new \Exception(
@@ -696,13 +713,7 @@ class Contribution
                     );
                 }
 
-                if ($edit === false) {
-                    throw new \Exception(
-                        'An error occurred updating contribution # ' . $this->_id . '!'
-                    );
-                }
-                $success = true;
-                $event = 'contribution.edit';
+                $event = $this->getEditEventName();
             }
             //update deadline
             if ($this->isFee()) {
@@ -710,15 +721,13 @@ class Contribution
             }
 
             //dynamic fields
-            if ($success) {
-                $success = $this->dynamicsStore(true);
-            }
+            $this->dynamicsStore(true);
 
             $this->zdb->connection->commit();
             $this->_orig_amount = $this->_amount;
 
             //send event at the end of process, once all has been stored
-            if ($event !== null) {
+            if ($event !== null && $this->areEventsEnabled()) {
                 $emitter->dispatch(new GaletteEvent($event, $this));
             }
 
@@ -1483,7 +1492,7 @@ class Contribution
         }
 
         //admin and staff users can edit, as well as member itself
-        if (!$this->id || $this->id && $login->id == $this->_member || $login->isAdmin() || $login->isStaff()) {
+        if (!$this->id || $login->id == $this->_member || $login->isAdmin() || $login->isStaff()) {
             return true;
         }
 
@@ -1504,4 +1513,14 @@ class Contribution
 
         return false;
     }
+
+    /**
+     * Get prefix for events
+     *
+     * @return string
+     */
+    protected function getEventsPrefix(): string
+    {
+        return 'contribution';
+    }
 }