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
61 const TABLE
= 'cotisations';
62 const PK
= 'id_cotis';
64 const PAYMENT_OTHER
= 0;
65 const PAYMENT_CASH
= 1;
66 const PAYMENT_CREDITCARD
= 2;
67 const PAYMENT_CHECK
= 3;
68 const PAYMENT_TRANSFER
= 4;
69 const PAYMENT_PAYPAL
= 5;
76 private $_payment_type;
77 private $_orig_amount;
81 private $_transaction = null;
85 //fields list and their translation
94 * @param Db $zdb Database
95 * @param Login $login Login instance
96 * @param null|int|ResultSet $args Either a ResultSet row to load
97 * a specific contribution, or an type id
98 * to just instanciate object
100 public function __construct(Db
$zdb, Login
$login, $args = null)
103 $this->login
= $login;
106 * Fields configuration. Each field is an array and must reflect:
112 * I'd prefer a static private variable for this...
113 * But call to the _T function does not seem to be allowed there :/
115 $this->_fields
= array(
117 'label' => null, //not a field in the form
120 Adherent
::PK
=> array(
121 'label' => _T("Contributor:"),
122 'propname' => 'member'
124 ContributionsTypes
::PK
=> array(
125 'label' => _T("Contribution type:"),
128 'montant_cotis' => array(
129 'label' => _T("Amount:"),
130 'propname' => 'amount'
132 'type_paiement_cotis' => array(
133 'label' => _T("Payment type:"),
134 'propname' => 'payment_type'
136 'info_cotis' => array(
137 'label' => _T("Comments:"),
140 'date_enreg' => array(
141 'label' => null, //not a field in the form
144 'date_debut_cotis' => array(
145 'label' => _T("Date of contribution:"),
146 'cotlabel' => _T("Start date of membership:"), //if contribution is a cotisation, label differs
147 'propname' => 'begin_date'
149 'date_fin_cotis' => array(
150 'label' => _T("End date of membership:"),
151 'propname' => 'end_date'
153 Transaction
::PK
=> array(
154 'label' => null, //not a field in the form
155 'propname' => 'transaction'
157 //this one is not really a field, but is required in some cases...
158 //adding it here make simplier to check required fields
159 'duree_mois_cotis' => array(
160 'label' => _T("Membership extension:"),
161 'propname' => 'extension'
166 } elseif (is_array($args)) {
167 $this->_date
= date("Y-m-d");
168 if (isset($args['adh']) && $args['adh'] != '') {
169 $this->_member
= (int)$args['adh'];
171 if (isset($args['trans'])) {
172 $this->_transaction
= new Transaction($this->zdb
, (int)$args['trans']);
173 if (!isset($this->_member
)) {
174 $this->_member
= (int)$this->_transaction
->member
;
176 $this->_amount
= $this->_transaction
->getMissingAmount();
178 $this->_type
= new ContributionsTypes($this->zdb
, (int)$args['type']);
179 $this->_is_cotis
= (bool)$this->_type
->extension
;
180 //calculate begin date for cotisation
181 $this->_begin_date
= $this->_date
;
182 if ($this->_is_cotis
) {
183 $curend = self
::getDueDate($this->zdb
, $this->_member
);
185 $dend = new \
DateTime($curend);
186 $now = date('Y-m-d');
187 $dnow = new \
DateTime($now);
189 // Member didn't renew on time
190 $this->_begin_date
= $now;
192 $this->_begin_date
= $curend;
195 $this->retrieveEndDate();
197 if (isset($args['payment_type'])) {
198 $this->_payment_type
= $args['payment_type'];
200 } elseif (is_object($args)) {
201 $this->loadFromRS($args);
206 * Sets end contribution date
210 private function retrieveEndDate()
214 $bdate = new \
DateTime($this->_begin_date
);
215 if ($preferences->pref_beg_membership
!= '') {
216 //case beginning of membership
217 list($j, $m) = explode('/', $preferences->pref_beg_membership
);
218 $edate = new \
DateTime($bdate->format('Y') . '-' . $m . '-' . $j);
219 while ($edate <= $bdate) {
220 $edate->modify('+1 year');
222 $this->_end_date
= $edate->format('Y-m-d');
223 } elseif ($preferences->pref_membership_ext
!= '') {
224 //case membership extension
225 $this->_extension
= $preferences->pref_membership_ext
;
226 $dext = new \
DateInterval('P' . $this->_extension
. 'M');
227 $edate = $bdate->add($dext);
228 $this->_end_date
= $edate->format('Y-m-d');
233 * Loads a contribution from its id
235 * @param int $id the identifiant for the contribution to load
237 * @return bool true if query succeed, false otherwise
239 public function load($id)
242 $select = $this->zdb
->select(self
::TABLE
);
243 $select->where(self
::PK
. ' = ' . $id);
244 //restrict query on current member id if he's not admin nor staff member
245 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
246 $select->where(Adherent
::PK
. ' = ' . $this->login
->id
);
249 $results = $this->zdb
->execute($select);
250 $row = $results->current();
251 if ($row !== false) {
252 $this->loadFromRS($row);
255 throw new \
Exception(
256 'No contribution #' . $id . ' (user ' .$this->login
->id
. ')'
259 } catch (\Exception
$e) {
261 'An error occured attempting to load contribution #' . $id .
270 * Populate object from a resultset row
272 * @param ResultSet $r the resultset row
276 private function loadFromRS($r)
279 $this->_id
= (int)$r->$pk;
280 $this->_date
= $r->date_enreg
;
281 $this->_amount
= $r->montant_cotis
;
282 //save original amount, we need it for transactions parts calulations
283 $this->_orig_amount
= $r->montant_cotis
;
284 $this->_payment_type
= $r->type_paiement_cotis
;
285 $this->_info
= $r->info_cotis
;
286 $this->_begin_date
= $r->date_debut_cotis
;
287 $enddate = $r->date_fin_cotis
;
288 //do not work with knows bad dates...
289 //the one with BC comes from 0.63/pgsl demo... Why the hell a so
290 //strange date? dont know :(
291 if ($enddate !== '0000-00-00'
292 && $enddate !== '1901-01-01'
293 && $enddate !== '0001-01-01 BC'
295 $this->_end_date
= $r->date_fin_cotis
;
297 $adhpk = Adherent
::PK
;
298 $this->_member
= (int)$r->$adhpk;
300 $transpk = Transaction
::PK
;
301 if ($r->$transpk != '') {
302 $this->_transaction
= new Transaction($this->zdb
, (int)$r->$transpk);
305 $this->_type
= new ContributionsTypes($this->zdb
, (int)$r->id_type_cotis
);
306 if ($this->_type
->extension
== 1) {
307 $this->_is_cotis
= true;
309 $this->_is_cotis
= false;
314 * Check posted values validity
316 * @param array $values All values to check, basically the $_POST array
317 * after sending the form
318 * @param array $required Array of required fields
319 * @param array $disabled Array of disabled fields
323 public function check($values, $required, $disabled)
327 $fields = array_keys($this->_fields
);
328 foreach ($fields as $key) {
329 //first of all, let's sanitize values
330 $key = strtolower($key);
331 $prop = '_' . $this->_fields
[$key]['propname'];
333 if (isset($values[$key])) {
334 $value = trim($values[$key]);
339 // if the field is enabled, check it
340 if (!isset($disabled[$key])) {
341 // fill up the adherent structure
342 //$this->$prop = stripslashes($value); //not relevant here!
344 // now, check validity
348 case 'date_debut_cotis':
349 case 'date_fin_cotis':
352 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
354 throw new \
Exception('Incorrect format');
356 $this->$prop = $d->format('Y-m-d');
357 } catch (\Exception
$e) {
359 'Wrong date format. field: ' . $key .
360 ', value: ' . $value . ', expected fmt: ' .
361 __("Y-m-d") . ' | ' . $e->getMessage(),
364 $errors[] = str_replace(
371 $this->_fields
[$key]['label']
373 _T("- Wrong date format (%date_format) for %field!")
380 $this->_member
= $value;
383 case ContributionsTypes
::PK
:
385 $this->type
= (int)$value;
388 case 'montant_cotis':
389 $this->_amount
= $value;
390 $value = strtr($value, ',', '.');
391 if (!is_numeric($value)) {
392 $errors[] = _T("- The amount must be an integer!");
395 case 'type_paiement_cotis':
396 if ($value == self
::PAYMENT_OTHER
397 ||
$value == self
::PAYMENT_CASH
398 ||
$value == self
::PAYMENT_CREDITCARD
399 ||
$value == self
::PAYMENT_CHECK
400 ||
$value == self
::PAYMENT_TRANSFER
401 ||
$value == self
::PAYMENT_PAYPAL
403 $this->_payment_type
= $value;
405 $errors[] = _T("- Unknown payment type");
409 $this->_info
= $value;
411 case Transaction
::PK
:
413 $this->_transaction
= new Transaction($this->zdb
, (int)$value);
416 case 'duree_mois_cotis':
418 if (!is_numeric($value) ||
$value <= 0) {
419 $errors[] = _T("- The duration must be a positive integer!");
421 $this->$prop = $value;
422 $this->retrieveEndDate();
429 // missing required fields?
430 while (list($key, $val) = each($required)) {
432 $prop = '_' . $this->_fields
[$key]['propname'];
433 if (!isset($disabled[$key])
434 && (!isset($this->$prop)
435 ||
(!is_object($this->$prop) && trim($this->$prop) == '')
436 ||
(is_object($this->$prop) && trim($this->$prop->id
) == ''))
438 $errors[] = _T("- Mandatory field empty: ") .
439 ' <a href="#' . $key . '">' . $this->getFieldName($key) .'</a>';
444 if ($this->_transaction
!= null && $this->_amount
!= null) {
445 $missing = $this->_transaction
->getMissingAmount();
446 //calculate new missing amount
447 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
449 $errors[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
453 if (count($errors) > 0) {
455 'Some errors has been throwed attempting to edit/store a contribution' .
456 print_r($errors, true),
462 'Contribution checked successfully.',
470 * Check that membership fees does not overlap
472 * @return boolean|string True if all is ok, false if error,
473 * error message if overlap
475 public function checkOverlap()
478 $select = $this->zdb
->select(self
::TABLE
, 'c');
480 array('date_debut_cotis', 'date_fin_cotis')
482 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
483 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
485 )->where(Adherent
::PK
. ' = ' . $this->_member
)
486 ->where(array('cotis_extension' => new Expression('true')))
488 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
489 ->lessThan('date_debut_cotis', $this->_end_date
)
492 ->greaterThan('date_fin_cotis', $this->_begin_date
)
493 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
495 if ($this->id
!= '') {
496 $select->where(self
::PK
. ' != ' . $this->id
);
499 $results = $this->zdb
->execute($select);
500 if ($results->count() > 0) {
501 $result = $results->current();
502 $d = new \
DateTime($result->date_debut_cotis
);
504 return _T("- Membership period overlaps period starting at ") .
505 $d->format(__("Y-m-d"));
508 } catch (\Exception
$e) {
510 'An error occured checking overlaping fee. ' . $e->getMessage(),
518 * Store the contribution
522 public function store()
527 $this->zdb
->connection
->beginTransaction();
529 $fields = self
::getDbFields($this->zdb
);
530 foreach ($fields as $field) {
531 $prop = '_' . $this->_fields
[$field]['propname'];
533 case ContributionsTypes
::PK
:
534 case Transaction
::PK
:
535 if (isset($this->$prop)) {
536 $values[$field] = $this->$prop->id
;
540 $values[$field] = $this->$prop;
545 //no end date, let's take database defaults
546 if (!$this->isCotis() && !$this->_end_date
) {
547 unset($values['date_fin_cotis']);
550 if (!isset($this->_id
) ||
$this->_id
== '') {
551 //we're inserting a new contribution
552 unset($values[self
::PK
]);
554 $insert = $this->zdb
->insert(self
::TABLE
);
555 $insert->values($values);
556 $add = $this->zdb
->execute($insert);
558 if ($add->count() > 0) {
559 if ($this->zdb
->isPostgres()) {
560 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
561 PREFIX_DB
. 'cotisations_id_seq'
564 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
569 _T("Contribution added"),
570 Adherent
::getSName($this->zdb
, $this->_member
)
573 $hist->add(_T("Fail to add new contribution."));
574 throw new \
Exception(
575 'An error occured inserting new contribution!'
579 //we're editing an existing contribution
580 $update = $this->zdb
->update(self
::TABLE
);
581 $update->set($values)->where(
582 self
::PK
. '=' . $this->_id
584 $edit = $this->zdb
->execute($update);
586 //edit == 0 does not mean there were an error, but that there
587 //were nothing to change
588 if ($edit->count() > 0) {
590 _T("Contribution updated"),
591 Adherent
::getSName($this->zdb
, $this->_member
)
593 } elseif ($edit === false) {
594 throw new \
Exception(
595 'An error occured updating contribution # ' . $this->_id
. '!'
600 if ($this->isCotis()) {
601 $deadline = $this->updateDeadline();
602 if ($deadline !== true) {
603 //if something went wrong, we rollback transaction
604 throw new \
Exception('An error occured updating member\'s deadline');
607 $this->zdb
->connection
->commit();
608 $this->_orig_amount
= $this->_amount
;
610 } catch (\Exception
$e) {
611 $this->zdb
->connection
->rollBack();
613 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
614 $e->getTraceAsString(),
622 * Update member dead line
626 private function updateDeadline()
629 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
631 if ($due_date != '') {
632 $date_fin_update = $due_date;
634 $date_fin_update = new Expression('NULL');
637 $update = $this->zdb
->update(Adherent
::TABLE
);
639 array('date_echeance' => $date_fin_update)
641 Adherent
::PK
. '=' . $this->_member
643 $this->zdb
->execute($update);
645 } catch (\Exception
$e) {
647 'An error occured updating member ' . $this->_member
.
657 * Remove contribution from database
659 * @param boolean $transaction Activate transaction mode (defaults to true)
663 public function remove($transaction = true)
667 $this->zdb
->connection
->beginTransaction();
670 $delete = $this->zdb
->delete(self
::TABLE
);
671 $delete->where(self
::PK
. ' = ' . $this->_id
);
672 $del = $this->zdb
->execute($delete);
673 if ($del->count() > 0) {
674 $this->updateDeadline();
677 $this->zdb
->connection
->commit();
680 } catch (\Exception
$e) {
682 $this->zdb
->connection
->rollBack();
685 'An error occured trying to remove contribution #' .
686 $this->_id
. ' | ' . $e->getMessage(),
696 * @param string $field Field name
700 public function getFieldName($field)
702 $label = $this->_fields
[$field]['label'];
703 if ($this->isCotis() && $field == 'date_debut_cotis') {
704 $label = $this->_fields
[$field]['cotlabel'];
706 //remove trailing ':' and then nbsp (for french at least)
707 $label = trim(trim($label, ':'), ' ');
712 * Retrieve fields from database
714 * @param Db $zdb Database instance
718 public static function getDbFields(Db
$zdb)
720 $columns = $zdb->getColumns(self
::TABLE
);
722 foreach ($columns as $col) {
723 $fields[] = $col->getName();
729 * Get the relevant CSS class for current contribution
731 * @return string current contribution row class
733 public function getRowClass()
735 return ( $this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
741 * Retrieve member due date
743 * @param Db $zdb Database instance
744 * @param integer $member_id Member identifier
748 public static function getDueDate(Db
$zdb, $member_id)
751 $select = $zdb->select(self
::TABLE
, 'c');
754 'max_date' => new Expression('MAX(date_fin_cotis)')
757 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
758 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
761 Adherent
::PK
. ' = ' . $member_id
763 array('cotis_extension' => new Expression('true'))
766 $results = $zdb->execute($select);
767 $result = $results->current();
768 $due_date = $result->max_date
;
770 //avoid bad dates in postgres and bad mysql return from zenddb
771 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
775 } catch (\Exception
$e) {
777 'An error occured trying to retrieve member\'s due date',
785 * Detach a contribution from a transaction
787 * @param Db $zdb Database instance
788 * @param Login $login Login instance
789 * @param int $trans_id Transaction identifier
790 * @param int $contrib_id Contribution identifier
794 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
797 //first, we check if contribution is part of transaction
798 $c = new Contribution($zdb, $login, (int)$contrib_id);
799 if ($c->isTransactionPartOf($trans_id)) {
800 $update = $zdb->update(self
::TABLE
);
802 array(Transaction
::PK
=> null)
804 self
::PK
. ' = ' . $contrib_id
806 $zdb->execute($update);
810 'Contribution #' . $contrib_id .
811 ' is not actually part of transaction #' . $trans_id,
816 } catch (\Exception
$e) {
818 'Unable to detach contribution #' . $contrib_id .
819 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
827 * Set a contribution as a transaction part
829 * @param Db $zdb Database instance
830 * @param int $trans_id Transaction identifier
831 * @param int $contrib_id Contribution identifier
835 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
838 $update = $zdb->update(self
::TABLE
);
840 array(Transaction
::PK
=> $trans_id)
841 )->where(self
::PK
. ' = ' . $contrib_id);
843 $zdb->execute($update);
845 } catch (\Exception
$e) {
847 'Unable to attach contribution #' . $contrib_id .
848 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
856 * Is current contribution a cotisation
860 public function isCotis()
862 return $this->_is_cotis
;
866 * Is current contribution part of specified transaction
868 * @param int $id Transaction identifier
872 public function isTransactionPartOf($id)
874 if ($this->isTransactionPart()) {
875 return $id == $this->_transaction
->id
;
882 * Is current contribution part of transaction
886 public function isTransactionPart()
888 return $this->_transaction
!= null;
892 * Execute post contribution script
894 * @param ExternalScript $es External script to execute
895 * @param array $extra Extra informations on contribution
897 * @param array $pextra Extra information on payment
900 * @return mixed Script return value on success, values and script output on fail
902 public function executePostScript(
910 'type' => $this->getPaymentType()
913 if ($pextra !== null && is_array($pextra)) {
914 $payment = array_merge($payment, $pextra);
917 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
918 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
921 $voucher_path = null;
922 if ($this->_id
!== null) {
923 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
924 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
925 $voucher_path = $voucher->getPath();
929 'date' => $this->_date
,
930 'type' => $this->getRawType(),
931 'amount' => $this->amount
,
932 'voucher' => $voucher_path,
934 'id' => $this->type
->id
,
935 'label' => $this->type
->libelle
937 'payment' => $payment
940 if ($this->_member
!== null) {
941 $m = new Adherent($this->zdb
, (int)$this->_member
);
943 'name' => $m->sfullname
,
944 'email' => $m->email
,
945 'organization' => ($m->isCompany() ?
1 : 0),
948 'label' => $m->sstatus
950 'country' => $m->country
953 if ($m->isCompany()) {
954 $member['organization_name'] = $m->company_name
;
957 $contrib['member'] = $member;
960 if ($extra !== null && is_array($extra)) {
961 $contrib = array_merge($contrib, $extra);
964 $res = $es->send($contrib);
968 'An error occured calling post contribution ' .
969 "script:\n" . $es->getOutput(),
972 $res = _T("Contribution informations") . "\n";
973 $res .= print_r($contrib, true);
974 $res .= "\n\n" . _T("Script output") . "\n";
975 $res .= $es->getOutput();
981 * Get raw contribution type
985 public function getRawType()
987 if ($this->isCotis()) {
995 * Get contribution type label
999 public function getTypeLabel()
1001 if ($this->isCotis()) {
1002 return _T("Membership");
1004 return _T("Donation");
1009 * Get payent type label
1013 public function getPaymentType()
1015 switch ($this->payment_type
) {
1016 case Contribution
::PAYMENT_CASH
:
1019 case Contribution
::PAYMENT_CREDITCARD
:
1020 return 'credit_card';
1022 case Contribution
::PAYMENT_CHECK
:
1025 case Contribution
::PAYMENT_TRANSFER
:
1028 case Contribution
::PAYMENT_PAYPAL
:
1031 case Contribution
::PAYMENT_OTHER
:
1036 __METHOD__
. ' Unknonw payment type ' . $this->payment_type
,
1039 throw new \
RuntimeException(
1040 'Unknonw payment type ' . $this->payment_type
1046 * Global getter method
1048 * @param string $name name of the property we want to retrive
1050 * @return false|object the called property
1052 public function __get($name)
1055 $forbidden = array('is_cotis');
1056 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1057 'raw_begin_date', 'raw_end_date'
1060 $rname = '_' . $name;
1061 if (!in_array($name, $forbidden)
1062 && isset($this->$rname)
1063 ||
in_array($name, $virtuals)
1067 case 'raw_begin_date':
1068 case 'raw_end_date':
1069 $rname = '_' . substr($name, 4);
1070 if ($this->$rname != '') {
1072 $d = new \
DateTime($this->$rname);
1074 } catch (\Exception
$e) {
1075 //oops, we've got a bad date :/
1077 'Bad date (' . $this->$rname . ') | ' .
1088 if ($this->$rname != '') {
1090 $d = new \
DateTime($this->$rname);
1091 return $d->format(__("Y-m-d"));
1092 } catch (\Exception
$e) {
1093 //oops, we've got a bad date :/
1095 'Bad date (' . $this->$rname . ') | ' .
1099 return $this->$rname;
1104 if ($this->_is_cotis
) {
1105 $date_end = new \
DateTime($this->_end_date
);
1106 $date_start = new \
DateTime($this->_begin_date
);
1107 $diff = $date_end->diff($date_start);
1108 return $diff->format('%y') * 12 +
$diff->format('%m');
1113 case 'spayment_type':
1114 switch ($this->_payment_type
) {
1115 case self
::PAYMENT_OTHER
:
1118 case self
::PAYMENT_CASH
:
1121 case self
::PAYMENT_CREDITCARD
:
1122 return _T("Credit card");
1124 case self
::PAYMENT_CHECK
:
1127 case self
::PAYMENT_TRANSFER
:
1128 return _T("Transfer");
1130 case self
::PAYMENT_PAYPAL
:
1131 return _T("Paypal");
1135 'Unknown payment type ' . $this->_payment_type
,
1143 return ($this->isCotis()) ?
1144 PdfModel
::INVOICE_MODEL
:
1145 PdfModel
::RECEIPT_MODEL
;
1148 return $this->$rname;
1157 * Global setter method
1159 * @param string $name name of the property we want to assign a value to
1160 * @param object $value a relevant value for the property
1164 public function __set($name, $value)
1166 $forbidden = array('fields', 'is_cotis', 'end_date');
1168 if (!in_array($name, $forbidden)) {
1169 $rname = '_' . $name;
1172 if (is_int($value)) {
1173 $this->$rname = new Transaction($this->zdb
, $value);
1176 'Trying to set a transaction from an id that is not an integer.',
1182 if (is_int($value)) {
1184 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1185 //set is_cotis according to type
1186 if ($this->$rname->extension
== 1) {
1187 $this->_is_cotis
= true;
1189 $this->_is_cotis
= false;
1193 'Trying to set a type from an id that is not an integer.',
1200 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1202 throw new \
Exception('Incorrect format');
1204 $this->_begin_date
= $d->format('Y-m-d');
1205 } catch (\Exception
$e) {
1207 'Wrong date format. field: ' . $key .
1208 ', value: ' . $value . ', expected fmt: ' .
1209 __("Y-m-d") . ' | ' . $e->getMessage(),
1212 $errors[] = str_replace(
1219 $this->_fields
[$key]['label']
1221 _T("- Wrong date format (%date_format) for %field!")
1226 if (is_numeric($value) && $value > 0) {
1227 $this->$rname = $value;
1230 'Trying to set an amount with a non numeric value, ' .
1231 'or with a zero value',
1237 if (is_int($value)) {
1239 $this->$rname = $value;
1244 '[' . __CLASS__
. ']: Trying to set an unknown property (' .