use ArrayObject;
use DateTime;
use Galette\Events\GaletteEvent;
+use Galette\Features\HasEvent;
use Throwable;
use Analog\Analog;
use Laminas\Db\Sql\Expression;
class Contribution
{
use Dynamics;
+ use HasEvent;
public const TABLE = 'cotisations';
public const PK = 'id_cotis';
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(
*/
public function load($id)
{
- if (!$this->login->isLogged()) {
+ if (!$this->login->isLogged() && $this->login->id == '') {
return false;
}
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) {
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!");
$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) {
unset($values['date_fin_cotis']);
}
- $success = false;
if (!isset($this->_id) || $this->_id == '') {
//we're inserting a new contribution
unset($values[self::PK]);
_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(
);
}
- 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()) {
}
//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));
}
}
//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;
}
return false;
}
+
+ /**
+ * Get prefix for events
+ *
+ * @return string
+ */
+ protected function getEventsPrefix(): string
+ {
+ return 'contribution';
+ }
}