3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Contribution class for galette
10 * Copyright © 2010-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2010-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2010-03-11
38 namespace Galette\Entity
;
41 use Laminas\Db\Sql\Expression
;
43 use Galette\Core\Login
;
44 use Galette\IO\ExternalScript
;
45 use Galette\IO\PdfContribution
;
46 use Galette\Repository\PaymentTypes
;
49 * Contribution class for galette
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2010-2014 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
58 * @since Available since 0.7dev - 2010-03-11
64 const TABLE
= 'cotisations';
65 const PK
= 'id_cotis';
72 private $_payment_type;
73 private $_orig_amount;
77 private $_transaction = null;
81 //fields list and their translation
92 * @param Db $zdb Database
93 * @param Login $login Login instance
94 * @param null|int|ResultSet $args Either a ResultSet row to load
95 * a specific contribution, or an type id
96 * to just instanciate object
98 public function __construct(Db
$zdb, Login
$login, $args = null)
101 $this->login
= $login;
104 * Fields configuration. Each field is an array and must reflect:
110 * I'd prefer a static private variable for this...
111 * But call to the _T function does not seem to be allowed there :/
113 $this->_fields
= array(
115 'label' => null, //not a field in the form
118 Adherent
::PK
=> array(
119 'label' => _T("Contributor:"),
120 'propname' => 'member'
122 ContributionsTypes
::PK
=> array(
123 'label' => _T("Contribution type:"),
126 'montant_cotis' => array(
127 'label' => _T("Amount:"),
128 'propname' => 'amount'
130 'type_paiement_cotis' => array(
131 'label' => _T("Payment type:"),
132 'propname' => 'payment_type'
134 'info_cotis' => array(
135 'label' => _T("Comments:"),
138 'date_enreg' => array(
139 'label' => null, //not a field in the form
142 'date_debut_cotis' => array(
143 'label' => _T("Date of contribution:"),
144 'cotlabel' => _T("Start date of membership:"), //if contribution is a cotisation, label differs
145 'propname' => 'begin_date'
147 'date_fin_cotis' => array(
148 'label' => _T("End date of membership:"),
149 'propname' => 'end_date'
151 Transaction
::PK
=> array(
152 'label' => null, //not a field in the form
153 'propname' => 'transaction'
155 //this one is not really a field, but is required in some cases...
156 //adding it here make simplier to check required fields
157 'duree_mois_cotis' => array(
158 'label' => _T("Membership extension:"),
159 'propname' => 'extension'
164 } elseif (is_array($args)) {
165 $this->_date
= date("Y-m-d");
166 if (isset($args['adh']) && $args['adh'] != '') {
167 $this->_member
= (int)$args['adh'];
169 if (isset($args['trans'])) {
170 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$args['trans']);
171 if (!isset($this->_member
)) {
172 $this->_member
= (int)$this->_transaction
->member
;
174 $this->_amount
= $this->_transaction
->getMissingAmount();
176 $this->type
= (int)$args['type'];
177 //calculate begin date for cotisation
178 $this->_begin_date
= $this->_date
;
179 if ($this->_is_cotis
) {
180 $curend = self
::getDueDate($this->zdb
, $this->_member
);
182 $dend = new \
DateTime($curend);
183 $now = date('Y-m-d');
184 $dnow = new \
DateTime($now);
186 // Member didn't renew on time
187 $this->_begin_date
= $now;
189 $this->_begin_date
= $curend;
192 $this->retrieveEndDate();
194 if (isset($args['payment_type'])) {
195 $this->_payment_type
= $args['payment_type'];
197 } elseif (is_object($args)) {
198 $this->loadFromRS($args);
201 $this->loadDynamicFields();
205 * Sets end contribution date
209 private function retrieveEndDate()
213 $bdate = new \
DateTime($this->_begin_date
);
214 if ($preferences->pref_beg_membership
!= '') {
215 //case beginning of membership
216 list($j, $m) = explode('/', $preferences->pref_beg_membership
);
217 $edate = new \
DateTime($bdate->format('Y') . '-' . $m . '-' . $j);
218 while ($edate <= $bdate) {
219 $edate->modify('+1 year');
221 $this->_end_date
= $edate->format('Y-m-d');
222 } elseif ($preferences->pref_membership_ext
!= '') {
223 //case membership extension
224 if ($this->_extension
== null) {
225 $this->_extension
= $preferences->pref_membership_ext
;
227 $dext = new \
DateInterval('P' . $this->_extension
. 'M');
228 $edate = $bdate->add($dext);
229 $this->_end_date
= $edate->format('Y-m-d');
231 throw new \
RuntimeException(
232 'Unable to define end date; none of pref_beg_membership nor pref_membership_ext are defined!'
238 * Loads a contribution from its id
240 * @param int $id the identifiant for the contribution to load
242 * @return bool true if query succeed, false otherwise
244 public function load($id)
247 $select = $this->zdb
->select(self
::TABLE
);
248 $select->where(self
::PK
. ' = ' . $id);
249 //restrict query on current member id if he's not admin nor staff member
250 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
251 $select->where(Adherent
::PK
. ' = ' . $this->login
->id
);
254 $results = $this->zdb
->execute($select);
255 if ($results->count() > 0) {
256 $row = $results->current();
257 $this->loadFromRS($row);
260 throw new \
Exception(
261 'No contribution #' . $id . ' (user ' .$this->login
->id
. ')'
264 } catch (\Exception
$e) {
266 'An error occurred attempting to load contribution #' . $id .
275 * Populate object from a resultset row
277 * @param ResultSet $r the resultset row
281 private function loadFromRS($r)
284 $this->_id
= (int)$r->$pk;
285 $this->_date
= $r->date_enreg
;
286 $this->_amount
= $r->montant_cotis
;
287 //save original amount, we need it for transactions parts calulations
288 $this->_orig_amount
= $r->montant_cotis
;
289 $this->_payment_type
= $r->type_paiement_cotis
;
290 $this->_info
= $r->info_cotis
;
291 $this->_begin_date
= $r->date_debut_cotis
;
292 $enddate = $r->date_fin_cotis
;
293 //do not work with knows bad dates...
294 //the one with BC comes from 0.63/pgsl demo... Why the hell a so
295 //strange date? dont know :(
296 if ($enddate !== '0000-00-00'
297 && $enddate !== '1901-01-01'
298 && $enddate !== '0001-01-01 BC'
300 $this->_end_date
= $r->date_fin_cotis
;
302 $adhpk = Adherent
::PK
;
303 $this->_member
= (int)$r->$adhpk;
305 $transpk = Transaction
::PK
;
306 if ($r->$transpk != '') {
307 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$r->$transpk);
310 $this->type
= (int)$r->id_type_cotis
;
311 $this->loadDynamicFields();
315 * Check posted values validity
317 * @param array $values All values to check, basically the $_POST array
318 * after sending the form
319 * @param array $required Array of required fields
320 * @param array $disabled Array of disabled fields
324 public function check($values, $required, $disabled)
327 $this->errors
= array();
329 $fields = array_keys($this->_fields
);
330 foreach ($fields as $key) {
331 //first of all, let's sanitize values
332 $key = strtolower($key);
333 $prop = '_' . $this->_fields
[$key]['propname'];
335 if (isset($values[$key])) {
336 $value = trim($values[$key]);
341 // if the field is enabled, check it
342 if (!isset($disabled[$key])) {
343 // fill up the adherent structure
344 //$this->$prop = stripslashes($value); //not relevant here!
346 // now, check validity
350 case 'date_debut_cotis':
351 case 'date_fin_cotis':
354 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
356 throw new \
Exception('Incorrect format');
358 $this->$prop = $d->format('Y-m-d');
359 } catch (\Exception
$e) {
361 'Wrong date format. field: ' . $key .
362 ', value: ' . $value . ', expected fmt: ' .
363 __("Y-m-d") . ' | ' . $e->getMessage(),
366 $this->errors
[] = str_replace(
373 $this->_fields
[$key]['label']
375 _T("- Wrong date format (%date_format) for %field!")
382 $this->_member
= (int)$value;
385 case ContributionsTypes
::PK
:
387 $this->type
= (int)$value;
390 case 'montant_cotis':
391 $this->_amount
= $value;
392 $value = strtr($value, ',', '.');
393 if (!is_numeric($value) && $value !== '') {
394 $this->errors
[] = _T("- The amount must be an integer!");
397 case 'type_paiement_cotis':
398 $ptypes = new PaymentTypes(
403 $ptlist = $ptypes->getList();
404 if (isset($ptlist[$value])) {
405 $this->_payment_type
= $value;
407 $this->errors
[] = _T("- Unknown payment type");
411 $this->_info
= $value;
413 case Transaction
::PK
:
415 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$value);
418 case 'duree_mois_cotis':
420 if (!is_numeric($value) ||
$value <= 0) {
421 $this->errors
[] = _T("- The duration must be a positive integer!");
423 $this->$prop = $value;
424 $this->retrieveEndDate();
431 // missing required fields?
432 foreach ($required as $key => $val) {
434 $prop = '_' . $this->_fields
[$key]['propname'];
435 if (!isset($disabled[$key])
436 && (!isset($this->$prop)
437 ||
(!is_object($this->$prop) && trim($this->$prop) == '')
438 ||
(is_object($this->$prop) && trim($this->$prop->id
) == ''))
440 $this->errors
[] = str_replace(
442 '<a href="#' . $key . '">' . $this->getFieldLabel($key) .'</a>',
443 _T("- Mandatory field %field empty.")
449 if ($this->_transaction
!= null && $this->_amount
!= null) {
450 $missing = $this->_transaction
->getMissingAmount();
451 //calculate new missing amount
452 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
454 $this->errors
[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
458 if ($this->isCotis() && count($this->errors
) == 0) {
459 $overlap = $this->checkOverlap();
460 if ($overlap !== true) {
461 if ($overlap === false) {
462 $this->errors
[] = _T("An error occurred checking overlaping fees :(");
464 //method directly return error message
465 $this->errors
[] = $overlap;
470 $this->dynamicsCheck($values);
472 if (count($this->errors
) > 0) {
474 'Some errors has been throwed attempting to edit/store a contribution' .
475 print_r($this->errors
, true),
478 return $this->errors
;
481 'Contribution checked successfully.',
489 * Check that membership fees does not overlap
491 * @return boolean|string True if all is ok, false if error,
492 * error message if overlap
494 public function checkOverlap()
497 $select = $this->zdb
->select(self
::TABLE
, 'c');
499 array('date_debut_cotis', 'date_fin_cotis')
501 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
502 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
504 )->where(Adherent
::PK
. ' = ' . $this->_member
)
505 ->where(array('cotis_extension' => new Expression('true')))
507 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
508 ->lessThan('date_debut_cotis', $this->_end_date
)
511 ->greaterThan('date_fin_cotis', $this->_begin_date
)
512 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
514 if ($this->id
!= '') {
515 $select->where(self
::PK
. ' != ' . $this->id
);
518 $results = $this->zdb
->execute($select);
519 if ($results->count() > 0) {
520 $result = $results->current();
521 $d = new \
DateTime($result->date_debut_cotis
);
523 return _T("- Membership period overlaps period starting at ") .
524 $d->format(__("Y-m-d"));
527 } catch (\Exception
$e) {
529 'An error occurred checking overlaping fee. ' . $e->getMessage(),
537 * Store the contribution
541 public function store()
543 global $hist, $emitter;
545 if (count($this->errors
) > 0) {
546 throw new \
RuntimeException(
547 'Existing errors prevents storing contribution: ' .
548 print_r($this->errors
, true)
554 $this->zdb
->connection
->beginTransaction();
556 $fields = self
::getDbFields($this->zdb
);
557 foreach ($fields as $field) {
558 $prop = '_' . $this->_fields
[$field]['propname'];
560 case ContributionsTypes
::PK
:
561 case Transaction
::PK
:
562 if (isset($this->$prop)) {
563 $values[$field] = $this->$prop->id
;
567 $values[$field] = $this->$prop;
572 //no end date, let's take database defaults
573 if (!$this->isCotis() && !$this->_end_date
) {
574 unset($values['date_fin_cotis']);
578 if (!isset($this->_id
) ||
$this->_id
== '') {
579 //we're inserting a new contribution
580 unset($values[self
::PK
]);
582 $insert = $this->zdb
->insert(self
::TABLE
);
583 $insert->values($values);
584 $add = $this->zdb
->execute($insert);
586 if ($add->count() > 0) {
587 if ($this->zdb
->isPostgres()) {
588 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
589 PREFIX_DB
. 'cotisations_id_seq'
592 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
597 _T("Contribution added"),
598 Adherent
::getSName($this->zdb
, $this->_member
)
602 $emitter->emit('contribution.add', $this);
604 $hist->add(_T("Fail to add new contribution."));
605 throw new \
Exception(
606 'An error occurred inserting new contribution!'
610 //we're editing an existing contribution
611 $update = $this->zdb
->update(self
::TABLE
);
612 $update->set($values)->where(
613 self
::PK
. '=' . $this->_id
615 $edit = $this->zdb
->execute($update);
617 //edit == 0 does not mean there were an error, but that there
618 //were nothing to change
619 if ($edit->count() > 0) {
621 _T("Contribution updated"),
622 Adherent
::getSName($this->zdb
, $this->_member
)
626 if ($edit === false) {
627 throw new \
Exception(
628 'An error occurred updating contribution # ' . $this->_id
. '!'
633 $emitter->emit('contribution.edit', $this);
636 if ($this->isCotis()) {
637 $deadline = $this->updateDeadline();
638 if ($deadline !== true) {
639 //if something went wrong, we rollback transaction
640 throw new \
Exception('An error occurred updating member\'s deadline');
646 $success = $this->dynamicsStore(true);
649 $this->zdb
->connection
->commit();
650 $this->_orig_amount
= $this->_amount
;
652 } catch (\Exception
$e) {
653 $this->zdb
->connection
->rollBack();
655 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
656 $e->getTraceAsString(),
664 * Update member dead line
668 private function updateDeadline()
671 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
673 if ($due_date != '') {
674 $date_fin_update = $due_date;
676 $date_fin_update = new Expression('NULL');
679 $update = $this->zdb
->update(Adherent
::TABLE
);
681 array('date_echeance' => $date_fin_update)
683 Adherent
::PK
. '=' . $this->_member
685 $this->zdb
->execute($update);
687 } catch (\Exception
$e) {
689 'An error occurred updating member ' . $this->_member
.
699 * Remove contribution from database
701 * @param boolean $transaction Activate transaction mode (defaults to true)
705 public function remove($transaction = true)
711 $this->zdb
->connection
->beginTransaction();
714 $delete = $this->zdb
->delete(self
::TABLE
);
715 $delete->where(self
::PK
. ' = ' . $this->_id
);
716 $del = $this->zdb
->execute($delete);
717 if ($del->count() > 0) {
718 $this->updateDeadline();
719 $this->dynamicsRemove(true);
721 throw new \
RuntimeException(
722 'Contribution has not been removed!'
726 $this->zdb
->connection
->commit();
728 $emitter->emit('contribution.remove', $this);
730 } catch (\Exception
$e) {
732 $this->zdb
->connection
->rollBack();
735 'An error occurred trying to remove contribution #' .
736 $this->_id
. ' | ' . $e->getMessage(),
746 * @param string $field Field name
750 public function getFieldLabel($field)
752 $label = $this->_fields
[$field]['label'];
753 if ($this->isCotis() && $field == 'date_debut_cotis') {
754 $label = $this->_fields
[$field]['cotlabel'];
757 $label = str_replace(' ', ' ', $label);
758 //remove trailing ':' and then trim
759 $label = trim(trim($label, ':'));
764 * Retrieve fields from database
766 * @param Db $zdb Database instance
770 public static function getDbFields(Db
$zdb)
772 $columns = $zdb->getColumns(self
::TABLE
);
774 foreach ($columns as $col) {
775 $fields[] = $col->getName();
781 * Get the relevant CSS class for current contribution
783 * @return string current contribution row class
785 public function getRowClass()
787 return ( $this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
793 * Retrieve member due date
795 * @param Db $zdb Database instance
796 * @param integer $member_id Member identifier
800 public static function getDueDate(Db
$zdb, $member_id)
806 $select = $zdb->select(self
::TABLE
, 'c');
809 'max_date' => new Expression('MAX(date_fin_cotis)')
812 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
813 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
816 Adherent
::PK
. ' = ' . $member_id
818 array('cotis_extension' => new Expression('true'))
821 $results = $zdb->execute($select);
822 $result = $results->current();
823 $due_date = $result->max_date
;
825 //avoid bad dates in postgres and bad mysql return from zenddb
826 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
830 } catch (\Exception
$e) {
832 'An error occurred trying to retrieve member\'s due date',
840 * Detach a contribution from a transaction
842 * @param Db $zdb Database instance
843 * @param Login $login Login instance
844 * @param int $trans_id Transaction identifier
845 * @param int $contrib_id Contribution identifier
849 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
852 //first, we check if contribution is part of transaction
853 $c = new Contribution($zdb, $login, (int)$contrib_id);
854 if ($c->isTransactionPartOf($trans_id)) {
855 $update = $zdb->update(self
::TABLE
);
857 array(Transaction
::PK
=> null)
859 self
::PK
. ' = ' . $contrib_id
861 $zdb->execute($update);
865 'Contribution #' . $contrib_id .
866 ' is not actually part of transaction #' . $trans_id,
871 } catch (\Exception
$e) {
873 'Unable to detach contribution #' . $contrib_id .
874 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
882 * Set a contribution as a transaction part
884 * @param Db $zdb Database instance
885 * @param int $trans_id Transaction identifier
886 * @param int $contrib_id Contribution identifier
890 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
893 $update = $zdb->update(self
::TABLE
);
895 array(Transaction
::PK
=> $trans_id)
896 )->where(self
::PK
. ' = ' . $contrib_id);
898 $zdb->execute($update);
900 } catch (\Exception
$e) {
902 'Unable to attach contribution #' . $contrib_id .
903 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
911 * Is current contribution a cotisation
915 public function isCotis()
917 return $this->_is_cotis
;
921 * Is current contribution part of specified transaction
923 * @param int $id Transaction identifier
927 public function isTransactionPartOf($id)
929 if ($this->isTransactionPart()) {
930 return $id == $this->_transaction
->id
;
937 * Is current contribution part of transaction
941 public function isTransactionPart()
943 return $this->_transaction
!= null;
947 * Execute post contribution script
949 * @param ExternalScript $es External script to execute
950 * @param array $extra Extra information on contribution
952 * @param array $pextra Extra information on payment
955 * @return mixed Script return value on success, values and script output on fail
957 public function executePostScript(
965 'type' => $this->getPaymentType()
968 if ($pextra !== null && is_array($pextra)) {
969 $payment = array_merge($payment, $pextra);
972 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
973 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
976 $voucher_path = null;
977 if ($this->_id
!== null) {
978 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
979 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
980 $voucher_path = $voucher->getPath();
984 'id' => (int)$this->_id
,
985 'date' => $this->_date
,
986 'type' => $this->getRawType(),
987 'amount' => $this->amount
,
988 'voucher' => $voucher_path,
990 'id' => $this->type
->id
,
991 'label' => $this->type
->libelle
993 'payment' => $payment
996 if ($this->_member
!== null) {
997 $m = new Adherent($this->zdb
, (int)$this->_member
);
999 'id' => (int)$this->_member
,
1000 'name' => $m->sfullname
,
1001 'email' => $m->email
,
1002 'organization' => ($m->isCompany() ?
1 : 0),
1005 'label' => $m->sstatus
1007 'country' => $m->country
1010 if ($m->isCompany()) {
1011 $member['organization_name'] = $m->company_name
;
1014 $contrib['member'] = $member;
1017 if ($extra !== null && is_array($extra)) {
1018 $contrib = array_merge($contrib, $extra);
1021 $res = $es->send($contrib);
1023 if ($res !== true) {
1025 'An error occurred calling post contribution ' .
1026 "script:\n" . $es->getOutput(),
1029 $res = _T("Contribution information") . "\n";
1030 $res .= print_r($contrib, true);
1031 $res .= "\n\n" . _T("Script output") . "\n";
1032 $res .= $es->getOutput();
1038 * Get raw contribution type
1042 public function getRawType()
1044 if ($this->isCotis()) {
1045 return 'membership';
1052 * Get contribution type label
1056 public function getTypeLabel()
1058 if ($this->isCotis()) {
1059 return _T("Membership");
1061 return _T("Donation");
1066 * Get payment type label
1070 public function getPaymentType()
1072 if ($this->_payment_type
=== null) {
1076 $ptype = new PaymentType($this->zdb
, (int)$this->payment_type
);
1077 return $ptype->getName(false);
1081 * Global getter method
1083 * @param string $name name of the property we want to retrive
1085 * @return false|object the called property
1087 public function __get($name)
1090 $forbidden = array('is_cotis');
1091 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1092 'raw_begin_date', 'raw_end_date'
1095 $rname = '_' . $name;
1097 if (in_array($name, $forbidden)) {
1099 "Call to __get for '$name' is forbidden!",
1105 return $this->isCotis();
1108 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1110 } elseif (property_exists($this, $rname)
1111 ||
in_array($name, $virtuals)
1115 case 'raw_begin_date':
1116 case 'raw_end_date':
1117 $rname = '_' . substr($name, 4);
1118 if ($this->$rname != '') {
1120 $d = new \
DateTime($this->$rname);
1122 } catch (\Exception
$e) {
1123 //oops, we've got a bad date :/
1125 'Bad date (' . $this->$rname . ') | ' .
1136 if ($this->$rname != '') {
1138 $d = new \
DateTime($this->$rname);
1139 return $d->format(__("Y-m-d"));
1140 } catch (\Exception
$e) {
1141 //oops, we've got a bad date :/
1143 'Bad date (' . $this->$rname . ') | ' .
1147 return $this->$rname;
1152 if ($this->_is_cotis
) {
1153 $date_end = new \
DateTime($this->_end_date
);
1154 $date_start = new \
DateTime($this->_begin_date
);
1155 $diff = $date_end->diff($date_start);
1156 return $diff->format('%y') * 12 +
$diff->format('%m');
1161 case 'spayment_type':
1162 if ($this->_payment_type
=== null) {
1166 $ptype = new PaymentType($this->zdb
, (int)$this->payment_type
);
1167 return $ptype->getName();
1171 if ($this->_is_cotis
=== null) {
1174 return ($this->isCotis()) ?
1175 PdfModel
::INVOICE_MODEL
:
1176 PdfModel
::RECEIPT_MODEL
;
1179 return $this->$rname;
1184 "Unknown property '$rname'",
1192 * Global setter method
1194 * @param string $name name of the property we want to assign a value to
1195 * @param object $value a relevant value for the property
1199 public function __set($name, $value)
1201 global $preferences;
1203 $forbidden = array('fields', 'is_cotis', 'end_date');
1205 if (!in_array($name, $forbidden)) {
1206 $rname = '_' . $name;
1209 if (is_int($value)) {
1210 $this->$rname = new Transaction($this->zdb
, $this->login
, $value);
1213 'Trying to set a transaction from an id that is not an integer.',
1219 if (is_int($value)) {
1221 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1222 //set is_cotis according to type
1223 if ($this->$rname->extension
== 1) {
1224 $this->_is_cotis
= true;
1226 $this->_is_cotis
= false;
1230 'Trying to set a type from an id that is not an integer.',
1237 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1239 throw new \
Exception('Incorrect format');
1241 $this->_begin_date
= $d->format('Y-m-d');
1242 } catch (\Exception
$e) {
1244 'Wrong date format. field: ' . $name .
1245 ', value: ' . $value . ', expected fmt: ' .
1246 __("Y-m-d") . ' | ' . $e->getMessage(),
1249 $this->errors
[] = str_replace(
1256 $this->_fields
['date_debut_cotis']['label']
1258 _T("- Wrong date format (%date_format) for %field!")
1263 if (is_numeric($value) && $value > 0) {
1264 $this->$rname = $value;
1267 'Trying to set an amount with a non numeric value, ' .
1268 'or with a zero value',
1274 if (is_int($value)) {
1276 $this->$rname = $value;
1279 case 'payment_type':
1280 $ptypes = new PaymentTypes(
1285 $list = $ptypes->getList();
1286 if (isset($list[$value])) {
1287 $this->_payment_type
= $value;
1290 'Unknown payment type ' . $value,
1297 '[' . __CLASS__
. ']: Trying to set an unknown property (' .