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 Zend\Db\Sql\Expression
;
43 use Galette\Core\Login
;
44 use Galette\IO\ExternalScript
;
45 use Galette\IO\PdfContribution
;
48 * Contribution class for galette
53 * @author Johan Cwiklinski <johan@x-tnd.be>
54 * @copyright 2010-2014 The Galette Team
55 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
56 * @link http://galette.tuxfamily.org
57 * @since Available since 0.7dev - 2010-03-11
63 const TABLE
= 'cotisations';
64 const PK
= 'id_cotis';
66 const PAYMENT_OTHER
= 0;
67 const PAYMENT_CASH
= 1;
68 const PAYMENT_CREDITCARD
= 2;
69 const PAYMENT_CHECK
= 3;
70 const PAYMENT_TRANSFER
= 4;
71 const PAYMENT_PAYPAL
= 5;
78 private $_payment_type;
79 private $_orig_amount;
83 private $_transaction = null;
87 //fields list and their translation
98 * @param Db $zdb Database
99 * @param Login $login Login instance
100 * @param null|int|ResultSet $args Either a ResultSet row to load
101 * a specific contribution, or an type id
102 * to just instanciate object
104 public function __construct(Db
$zdb, Login
$login, $args = null)
107 $this->login
= $login;
110 * Fields configuration. Each field is an array and must reflect:
116 * I'd prefer a static private variable for this...
117 * But call to the _T function does not seem to be allowed there :/
119 $this->_fields
= array(
121 'label' => null, //not a field in the form
124 Adherent
::PK
=> array(
125 'label' => _T("Contributor:"),
126 'propname' => 'member'
128 ContributionsTypes
::PK
=> array(
129 'label' => _T("Contribution type:"),
132 'montant_cotis' => array(
133 'label' => _T("Amount:"),
134 'propname' => 'amount'
136 'type_paiement_cotis' => array(
137 'label' => _T("Payment type:"),
138 'propname' => 'payment_type'
140 'info_cotis' => array(
141 'label' => _T("Comments:"),
144 'date_enreg' => array(
145 'label' => null, //not a field in the form
148 'date_debut_cotis' => array(
149 'label' => _T("Date of contribution:"),
150 'cotlabel' => _T("Start date of membership:"), //if contribution is a cotisation, label differs
151 'propname' => 'begin_date'
153 'date_fin_cotis' => array(
154 'label' => _T("End date of membership:"),
155 'propname' => 'end_date'
157 Transaction
::PK
=> array(
158 'label' => null, //not a field in the form
159 'propname' => 'transaction'
161 //this one is not really a field, but is required in some cases...
162 //adding it here make simplier to check required fields
163 'duree_mois_cotis' => array(
164 'label' => _T("Membership extension:"),
165 'propname' => 'extension'
170 } elseif (is_array($args)) {
171 $this->_date
= date("Y-m-d");
172 if (isset($args['adh']) && $args['adh'] != '') {
173 $this->_member
= (int)$args['adh'];
175 if (isset($args['trans'])) {
176 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$args['trans']);
177 if (!isset($this->_member
)) {
178 $this->_member
= (int)$this->_transaction
->member
;
180 $this->_amount
= $this->_transaction
->getMissingAmount();
182 $this->type
= (int)$args['type'];
183 //calculate begin date for cotisation
184 $this->_begin_date
= $this->_date
;
185 if ($this->_is_cotis
) {
186 $curend = self
::getDueDate($this->zdb
, $this->_member
);
188 $dend = new \
DateTime($curend);
189 $now = date('Y-m-d');
190 $dnow = new \
DateTime($now);
192 // Member didn't renew on time
193 $this->_begin_date
= $now;
195 $this->_begin_date
= $curend;
198 $this->retrieveEndDate();
200 if (isset($args['payment_type'])) {
201 $this->_payment_type
= $args['payment_type'];
203 } elseif (is_object($args)) {
204 $this->loadFromRS($args);
207 if ($this->id
!== null) {
208 $this->loadDynamicFields();
213 * Sets end contribution date
217 private function retrieveEndDate()
221 $bdate = new \
DateTime($this->_begin_date
);
222 if ($preferences->pref_beg_membership
!= '') {
223 //case beginning of membership
224 list($j, $m) = explode('/', $preferences->pref_beg_membership
);
225 $edate = new \
DateTime($bdate->format('Y') . '-' . $m . '-' . $j);
226 while ($edate <= $bdate) {
227 $edate->modify('+1 year');
229 $this->_end_date
= $edate->format('Y-m-d');
230 } elseif ($preferences->pref_membership_ext
!= '') {
231 //case membership extension
232 $this->_extension
= $preferences->pref_membership_ext
;
233 $dext = new \
DateInterval('P' . $this->_extension
. 'M');
234 $edate = $bdate->add($dext);
235 $this->_end_date
= $edate->format('Y-m-d');
237 throw new \
RuntimeException(
238 'Unable to define end date; none of pref_beg_membership nor pref_membership_ext are defined!'
244 * Loads a contribution from its id
246 * @param int $id the identifiant for the contribution to load
248 * @return bool true if query succeed, false otherwise
250 public function load($id)
253 $select = $this->zdb
->select(self
::TABLE
);
254 $select->where(self
::PK
. ' = ' . $id);
255 //restrict query on current member id if he's not admin nor staff member
256 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
257 $select->where(Adherent
::PK
. ' = ' . $this->login
->id
);
260 $results = $this->zdb
->execute($select);
261 $row = $results->current();
262 if ($row !== false) {
263 $this->loadFromRS($row);
266 throw new \
Exception(
267 'No contribution #' . $id . ' (user ' .$this->login
->id
. ')'
270 } catch (\Exception
$e) {
272 'An error occured attempting to load contribution #' . $id .
281 * Populate object from a resultset row
283 * @param ResultSet $r the resultset row
287 private function loadFromRS($r)
290 $this->_id
= (int)$r->$pk;
291 $this->_date
= $r->date_enreg
;
292 $this->_amount
= $r->montant_cotis
;
293 //save original amount, we need it for transactions parts calulations
294 $this->_orig_amount
= $r->montant_cotis
;
295 $this->_payment_type
= $r->type_paiement_cotis
;
296 $this->_info
= $r->info_cotis
;
297 $this->_begin_date
= $r->date_debut_cotis
;
298 $enddate = $r->date_fin_cotis
;
299 //do not work with knows bad dates...
300 //the one with BC comes from 0.63/pgsl demo... Why the hell a so
301 //strange date? dont know :(
302 if ($enddate !== '0000-00-00'
303 && $enddate !== '1901-01-01'
304 && $enddate !== '0001-01-01 BC'
306 $this->_end_date
= $r->date_fin_cotis
;
308 $adhpk = Adherent
::PK
;
309 $this->_member
= (int)$r->$adhpk;
311 $transpk = Transaction
::PK
;
312 if ($r->$transpk != '') {
313 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$r->$transpk);
316 $this->type
= (int)$r->id_type_cotis
;
320 * Check posted values validity
322 * @param array $values All values to check, basically the $_POST array
323 * after sending the form
324 * @param array $required Array of required fields
325 * @param array $disabled Array of disabled fields
329 public function check($values, $required, $disabled)
331 $this->errors
= array();
333 $fields = array_keys($this->_fields
);
334 foreach ($fields as $key) {
335 //first of all, let's sanitize values
336 $key = strtolower($key);
337 $prop = '_' . $this->_fields
[$key]['propname'];
339 if (isset($values[$key])) {
340 $value = trim($values[$key]);
345 // if the field is enabled, check it
346 if (!isset($disabled[$key])) {
347 // fill up the adherent structure
348 //$this->$prop = stripslashes($value); //not relevant here!
350 // now, check validity
354 case 'date_debut_cotis':
355 case 'date_fin_cotis':
358 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
360 throw new \
Exception('Incorrect format');
362 $this->$prop = $d->format('Y-m-d');
363 } catch (\Exception
$e) {
365 'Wrong date format. field: ' . $key .
366 ', value: ' . $value . ', expected fmt: ' .
367 __("Y-m-d") . ' | ' . $e->getMessage(),
370 $this->errors
[] = str_replace(
377 $this->_fields
[$key]['label']
379 _T("- Wrong date format (%date_format) for %field!")
386 $this->_member
= $value;
389 case ContributionsTypes
::PK
:
391 $this->type
= (int)$value;
394 case 'montant_cotis':
395 $this->_amount
= $value;
396 $value = strtr($value, ',', '.');
397 if (!is_numeric($value)) {
398 $this->errors
[] = _T("- The amount must be an integer!");
401 case 'type_paiement_cotis':
402 if ($value == self
::PAYMENT_OTHER
403 ||
$value == self
::PAYMENT_CASH
404 ||
$value == self
::PAYMENT_CREDITCARD
405 ||
$value == self
::PAYMENT_CHECK
406 ||
$value == self
::PAYMENT_TRANSFER
407 ||
$value == self
::PAYMENT_PAYPAL
409 $this->_payment_type
= $value;
411 $this->errors
[] = _T("- Unknown payment type");
415 $this->_info
= $value;
417 case Transaction
::PK
:
419 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$value);
422 case 'duree_mois_cotis':
424 if (!is_numeric($value) ||
$value <= 0) {
425 $this->errors
[] = _T("- The duration must be a positive integer!");
427 $this->$prop = $value;
428 $this->retrieveEndDate();
435 // missing required fields?
436 foreach ($required as $key => $val) {
438 $prop = '_' . $this->_fields
[$key]['propname'];
439 if (!isset($disabled[$key])
440 && (!isset($this->$prop)
441 ||
(!is_object($this->$prop) && trim($this->$prop) == '')
442 ||
(is_object($this->$prop) && trim($this->$prop->id
) == ''))
444 $this->errors
[] = _T("- Mandatory field empty: ") .
445 ' <a href="#' . $key . '">' . $this->getFieldLabel($key) .'</a>';
450 if ($this->_transaction
!= null && $this->_amount
!= null) {
451 $missing = $this->_transaction
->getMissingAmount();
452 //calculate new missing amount
453 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
455 $this->errors
[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
459 if ($this->isCotis() && count($this->errors
) == 0) {
460 $overlap = $this->checkOverlap();
461 if ($overlap !== true) {
462 if ($overlap === false) {
463 $this->errors
[] = _T("An error occured checking overlaping fees :(");
465 //method directly return error message
466 $this->errors
[] = $overlap;
471 if (count($this->errors
) > 0) {
473 'Some errors has been throwed attempting to edit/store a contribution' .
474 print_r($this->errors
, true),
477 return $this->errors
;
480 'Contribution checked successfully.',
488 * Check that membership fees does not overlap
490 * @return boolean|string True if all is ok, false if error,
491 * error message if overlap
493 public function checkOverlap()
496 $select = $this->zdb
->select(self
::TABLE
, 'c');
498 array('date_debut_cotis', 'date_fin_cotis')
500 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
501 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
503 )->where(Adherent
::PK
. ' = ' . $this->_member
)
504 ->where(array('cotis_extension' => new Expression('true')))
506 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
507 ->lessThan('date_debut_cotis', $this->_end_date
)
510 ->greaterThan('date_fin_cotis', $this->_begin_date
)
511 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
513 if ($this->id
!= '') {
514 $select->where(self
::PK
. ' != ' . $this->id
);
517 $results = $this->zdb
->execute($select);
518 if ($results->count() > 0) {
519 $result = $results->current();
520 $d = new \
DateTime($result->date_debut_cotis
);
522 return _T("- Membership period overlaps period starting at ") .
523 $d->format(__("Y-m-d"));
526 } catch (\Exception
$e) {
528 'An error occured checking overlaping fee. ' . $e->getMessage(),
536 * Store the contribution
540 public function store()
545 $this->zdb
->connection
->beginTransaction();
547 $fields = self
::getDbFields($this->zdb
);
548 foreach ($fields as $field) {
549 $prop = '_' . $this->_fields
[$field]['propname'];
551 case ContributionsTypes
::PK
:
552 case Transaction
::PK
:
553 if (isset($this->$prop)) {
554 $values[$field] = $this->$prop->id
;
558 $values[$field] = $this->$prop;
563 //no end date, let's take database defaults
564 if (!$this->isCotis() && !$this->_end_date
) {
565 unset($values['date_fin_cotis']);
568 if (!isset($this->_id
) ||
$this->_id
== '') {
569 //we're inserting a new contribution
570 unset($values[self
::PK
]);
572 $insert = $this->zdb
->insert(self
::TABLE
);
573 $insert->values($values);
574 $add = $this->zdb
->execute($insert);
576 if ($add->count() > 0) {
577 if ($this->zdb
->isPostgres()) {
578 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
579 PREFIX_DB
. 'cotisations_id_seq'
582 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
587 _T("Contribution added"),
588 Adherent
::getSName($this->zdb
, $this->_member
)
591 $hist->add(_T("Fail to add new contribution."));
592 throw new \
Exception(
593 'An error occured inserting new contribution!'
597 //we're editing an existing contribution
598 $update = $this->zdb
->update(self
::TABLE
);
599 $update->set($values)->where(
600 self
::PK
. '=' . $this->_id
602 $edit = $this->zdb
->execute($update);
604 //edit == 0 does not mean there were an error, but that there
605 //were nothing to change
606 if ($edit->count() > 0) {
608 _T("Contribution updated"),
609 Adherent
::getSName($this->zdb
, $this->_member
)
611 } elseif ($edit === false) {
612 throw new \
Exception(
613 'An error occured updating contribution # ' . $this->_id
. '!'
618 if ($this->isCotis()) {
619 $deadline = $this->updateDeadline();
620 if ($deadline !== true) {
621 //if something went wrong, we rollback transaction
622 throw new \
Exception('An error occured updating member\'s deadline');
625 $this->zdb
->connection
->commit();
626 $this->_orig_amount
= $this->_amount
;
628 } catch (\Exception
$e) {
629 $this->zdb
->connection
->rollBack();
631 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
632 $e->getTraceAsString(),
640 * Update member dead line
644 private function updateDeadline()
647 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
649 if ($due_date != '') {
650 $date_fin_update = $due_date;
652 $date_fin_update = new Expression('NULL');
655 $update = $this->zdb
->update(Adherent
::TABLE
);
657 array('date_echeance' => $date_fin_update)
659 Adherent
::PK
. '=' . $this->_member
661 $this->zdb
->execute($update);
663 } catch (\Exception
$e) {
665 'An error occured updating member ' . $this->_member
.
675 * Remove contribution from database
677 * @param boolean $transaction Activate transaction mode (defaults to true)
681 public function remove($transaction = true)
685 $this->zdb
->connection
->beginTransaction();
688 $delete = $this->zdb
->delete(self
::TABLE
);
689 $delete->where(self
::PK
. ' = ' . $this->_id
);
690 $del = $this->zdb
->execute($delete);
691 if ($del->count() > 0) {
692 $this->updateDeadline();
695 $this->zdb
->connection
->commit();
698 } catch (\Exception
$e) {
700 $this->zdb
->connection
->rollBack();
703 'An error occured trying to remove contribution #' .
704 $this->_id
. ' | ' . $e->getMessage(),
714 * @param string $field Field name
718 public function getFieldLabel($field)
720 $label = $this->_fields
[$field]['label'];
721 if ($this->isCotis() && $field == 'date_debut_cotis') {
722 $label = $this->_fields
[$field]['cotlabel'];
724 //remove trailing ':' and then nbsp (for french at least)
725 $label = trim(trim($label, ':'), ' ');
730 * Retrieve fields from database
732 * @param Db $zdb Database instance
736 public static function getDbFields(Db
$zdb)
738 $columns = $zdb->getColumns(self
::TABLE
);
740 foreach ($columns as $col) {
741 $fields[] = $col->getName();
747 * Get the relevant CSS class for current contribution
749 * @return string current contribution row class
751 public function getRowClass()
753 return ( $this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
759 * Retrieve member due date
761 * @param Db $zdb Database instance
762 * @param integer $member_id Member identifier
766 public static function getDueDate(Db
$zdb, $member_id)
769 $select = $zdb->select(self
::TABLE
, 'c');
772 'max_date' => new Expression('MAX(date_fin_cotis)')
775 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
776 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
779 Adherent
::PK
. ' = ' . $member_id
781 array('cotis_extension' => new Expression('true'))
784 $results = $zdb->execute($select);
785 $result = $results->current();
786 $due_date = $result->max_date
;
788 //avoid bad dates in postgres and bad mysql return from zenddb
789 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
793 } catch (\Exception
$e) {
795 'An error occured trying to retrieve member\'s due date',
803 * Detach a contribution from a transaction
805 * @param Db $zdb Database instance
806 * @param Login $login Login instance
807 * @param int $trans_id Transaction identifier
808 * @param int $contrib_id Contribution identifier
812 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
815 //first, we check if contribution is part of transaction
816 $c = new Contribution($zdb, $login, (int)$contrib_id);
817 if ($c->isTransactionPartOf($trans_id)) {
818 $update = $zdb->update(self
::TABLE
);
820 array(Transaction
::PK
=> null)
822 self
::PK
. ' = ' . $contrib_id
824 $zdb->execute($update);
828 'Contribution #' . $contrib_id .
829 ' is not actually part of transaction #' . $trans_id,
834 } catch (\Exception
$e) {
836 'Unable to detach contribution #' . $contrib_id .
837 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
845 * Set a contribution as a transaction part
847 * @param Db $zdb Database instance
848 * @param int $trans_id Transaction identifier
849 * @param int $contrib_id Contribution identifier
853 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
856 $update = $zdb->update(self
::TABLE
);
858 array(Transaction
::PK
=> $trans_id)
859 )->where(self
::PK
. ' = ' . $contrib_id);
861 $zdb->execute($update);
863 } catch (\Exception
$e) {
865 'Unable to attach contribution #' . $contrib_id .
866 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
874 * Is current contribution a cotisation
878 public function isCotis()
880 return $this->_is_cotis
;
884 * Is current contribution part of specified transaction
886 * @param int $id Transaction identifier
890 public function isTransactionPartOf($id)
892 if ($this->isTransactionPart()) {
893 return $id == $this->_transaction
->id
;
900 * Is current contribution part of transaction
904 public function isTransactionPart()
906 return $this->_transaction
!= null;
910 * Execute post contribution script
912 * @param ExternalScript $es External script to execute
913 * @param array $extra Extra informations on contribution
915 * @param array $pextra Extra information on payment
918 * @return mixed Script return value on success, values and script output on fail
920 public function executePostScript(
928 'type' => $this->getPaymentType()
931 if ($pextra !== null && is_array($pextra)) {
932 $payment = array_merge($payment, $pextra);
935 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
936 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
939 $voucher_path = null;
940 if ($this->_id
!== null) {
941 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
942 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
943 $voucher_path = $voucher->getPath();
947 'date' => $this->_date
,
948 'type' => $this->getRawType(),
949 'amount' => $this->amount
,
950 'voucher' => $voucher_path,
952 'id' => $this->type
->id
,
953 'label' => $this->type
->libelle
955 'payment' => $payment
958 if ($this->_member
!== null) {
959 $m = new Adherent($this->zdb
, (int)$this->_member
);
961 'name' => $m->sfullname
,
962 'email' => $m->email
,
963 'organization' => ($m->isCompany() ?
1 : 0),
966 'label' => $m->sstatus
968 'country' => $m->country
971 if ($m->isCompany()) {
972 $member['organization_name'] = $m->company_name
;
975 $contrib['member'] = $member;
978 if ($extra !== null && is_array($extra)) {
979 $contrib = array_merge($contrib, $extra);
982 $res = $es->send($contrib);
986 'An error occured calling post contribution ' .
987 "script:\n" . $es->getOutput(),
990 $res = _T("Contribution informations") . "\n";
991 $res .= print_r($contrib, true);
992 $res .= "\n\n" . _T("Script output") . "\n";
993 $res .= $es->getOutput();
999 * Get raw contribution type
1003 public function getRawType()
1005 if ($this->isCotis()) {
1006 return 'membership';
1013 * Get contribution type label
1017 public function getTypeLabel()
1019 if ($this->isCotis()) {
1020 return _T("Membership");
1022 return _T("Donation");
1027 * Get payent type label
1031 public function getPaymentType()
1033 if ($this->_payment_type
=== null) {
1037 switch ($this->payment_type
) {
1038 case Contribution
::PAYMENT_CASH
:
1041 case Contribution
::PAYMENT_CREDITCARD
:
1042 return 'credit_card';
1044 case Contribution
::PAYMENT_CHECK
:
1047 case Contribution
::PAYMENT_TRANSFER
:
1050 case Contribution
::PAYMENT_PAYPAL
:
1053 case Contribution
::PAYMENT_OTHER
:
1058 __METHOD__
. ' Unknonw payment type ' . $this->payment_type
,
1061 throw new \
RuntimeException(
1062 'Unknonw payment type ' . $this->payment_type
1068 * Global getter method
1070 * @param string $name name of the property we want to retrive
1072 * @return false|object the called property
1074 public function __get($name)
1077 $forbidden = array('is_cotis');
1078 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1079 'raw_begin_date', 'raw_end_date'
1082 $rname = '_' . $name;
1084 if (in_array($name, $forbidden)) {
1086 "Call to __get for '$name' is forbidden!",
1092 return $this->isCotis();
1095 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1097 } elseif (property_exists($this, $rname)
1098 ||
in_array($name, $virtuals)
1102 case 'raw_begin_date':
1103 case 'raw_end_date':
1104 $rname = '_' . substr($name, 4);
1105 if ($this->$rname != '') {
1107 $d = new \
DateTime($this->$rname);
1109 } catch (\Exception
$e) {
1110 //oops, we've got a bad date :/
1112 'Bad date (' . $this->$rname . ') | ' .
1123 if ($this->$rname != '') {
1125 $d = new \
DateTime($this->$rname);
1126 return $d->format(__("Y-m-d"));
1127 } catch (\Exception
$e) {
1128 //oops, we've got a bad date :/
1130 'Bad date (' . $this->$rname . ') | ' .
1134 return $this->$rname;
1139 if ($this->_is_cotis
) {
1140 $date_end = new \
DateTime($this->_end_date
);
1141 $date_start = new \
DateTime($this->_begin_date
);
1142 $diff = $date_end->diff($date_start);
1143 return $diff->format('%y') * 12 +
$diff->format('%m');
1148 case 'spayment_type':
1149 if ($this->_payment_type
=== null) {
1152 switch ($this->_payment_type
) {
1153 case self
::PAYMENT_OTHER
:
1156 case self
::PAYMENT_CASH
:
1159 case self
::PAYMENT_CREDITCARD
:
1160 return _T("Credit card");
1162 case self
::PAYMENT_CHECK
:
1165 case self
::PAYMENT_TRANSFER
:
1166 return _T("Transfer");
1168 case self
::PAYMENT_PAYPAL
:
1169 return _T("Paypal");
1173 'Unknown payment type ' . $this->_payment_type
,
1181 if ($this->_is_cotis
=== null) {
1184 return ($this->isCotis()) ?
1185 PdfModel
::INVOICE_MODEL
:
1186 PdfModel
::RECEIPT_MODEL
;
1189 return $this->$rname;
1194 "Unknown property '$rname'",
1202 * Global setter method
1204 * @param string $name name of the property we want to assign a value to
1205 * @param object $value a relevant value for the property
1209 public function __set($name, $value)
1211 $forbidden = array('fields', 'is_cotis', 'end_date');
1213 if (!in_array($name, $forbidden)) {
1214 $rname = '_' . $name;
1217 if (is_int($value)) {
1218 $this->$rname = new Transaction($this->zdb
, $this->login
, $value);
1221 'Trying to set a transaction from an id that is not an integer.',
1227 if (is_int($value)) {
1229 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1230 //set is_cotis according to type
1231 if ($this->$rname->extension
== 1) {
1232 $this->_is_cotis
= true;
1234 $this->_is_cotis
= false;
1238 'Trying to set a type from an id that is not an integer.',
1245 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1247 throw new \
Exception('Incorrect format');
1249 $this->_begin_date
= $d->format('Y-m-d');
1250 } catch (\Exception
$e) {
1252 'Wrong date format. field: ' . $key .
1253 ', value: ' . $value . ', expected fmt: ' .
1254 __("Y-m-d") . ' | ' . $e->getMessage(),
1257 $this->errors
[] = str_replace(
1264 $this->_fields
[$key]['label']
1266 _T("- Wrong date format (%date_format) for %field!")
1271 if (is_numeric($value) && $value > 0) {
1272 $this->$rname = $value;
1275 'Trying to set an amount with a non numeric value, ' .
1276 'or with a zero value',
1282 if (is_int($value)) {
1284 $this->$rname = $value;
1289 '[' . __CLASS__
. ']: Trying to set an unknown property (' .