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 $this->loadDynamicFields();
211 * Sets end contribution date
215 private function retrieveEndDate()
219 $bdate = new \
DateTime($this->_begin_date
);
220 if ($preferences->pref_beg_membership
!= '') {
221 //case beginning of membership
222 list($j, $m) = explode('/', $preferences->pref_beg_membership
);
223 $edate = new \
DateTime($bdate->format('Y') . '-' . $m . '-' . $j);
224 while ($edate <= $bdate) {
225 $edate->modify('+1 year');
227 $this->_end_date
= $edate->format('Y-m-d');
228 } elseif ($preferences->pref_membership_ext
!= '') {
229 //case membership extension
230 $this->_extension
= $preferences->pref_membership_ext
;
231 $dext = new \
DateInterval('P' . $this->_extension
. 'M');
232 $edate = $bdate->add($dext);
233 $this->_end_date
= $edate->format('Y-m-d');
235 throw new \
RuntimeException(
236 'Unable to define end date; none of pref_beg_membership nor pref_membership_ext are defined!'
242 * Loads a contribution from its id
244 * @param int $id the identifiant for the contribution to load
246 * @return bool true if query succeed, false otherwise
248 public function load($id)
251 $select = $this->zdb
->select(self
::TABLE
);
252 $select->where(self
::PK
. ' = ' . $id);
253 //restrict query on current member id if he's not admin nor staff member
254 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
255 $select->where(Adherent
::PK
. ' = ' . $this->login
->id
);
258 $results = $this->zdb
->execute($select);
259 if ($results->count() > 0) {
260 $row = $results->current();
261 $this->loadFromRS($row);
264 throw new \
Exception(
265 'No contribution #' . $id . ' (user ' .$this->login
->id
. ')'
268 } catch (\Exception
$e) {
270 'An error occured attempting to load contribution #' . $id .
279 * Populate object from a resultset row
281 * @param ResultSet $r the resultset row
285 private function loadFromRS($r)
288 $this->_id
= (int)$r->$pk;
289 $this->_date
= $r->date_enreg
;
290 $this->_amount
= $r->montant_cotis
;
291 //save original amount, we need it for transactions parts calulations
292 $this->_orig_amount
= $r->montant_cotis
;
293 $this->_payment_type
= $r->type_paiement_cotis
;
294 $this->_info
= $r->info_cotis
;
295 $this->_begin_date
= $r->date_debut_cotis
;
296 $enddate = $r->date_fin_cotis
;
297 //do not work with knows bad dates...
298 //the one with BC comes from 0.63/pgsl demo... Why the hell a so
299 //strange date? dont know :(
300 if ($enddate !== '0000-00-00'
301 && $enddate !== '1901-01-01'
302 && $enddate !== '0001-01-01 BC'
304 $this->_end_date
= $r->date_fin_cotis
;
306 $adhpk = Adherent
::PK
;
307 $this->_member
= (int)$r->$adhpk;
309 $transpk = Transaction
::PK
;
310 if ($r->$transpk != '') {
311 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$r->$transpk);
314 $this->type
= (int)$r->id_type_cotis
;
315 $this->loadDynamicFields();
319 * Check posted values validity
321 * @param array $values All values to check, basically the $_POST array
322 * after sending the form
323 * @param array $required Array of required fields
324 * @param array $disabled Array of disabled fields
328 public function check($values, $required, $disabled)
330 $this->errors
= array();
332 $fields = array_keys($this->_fields
);
333 foreach ($fields as $key) {
334 //first of all, let's sanitize values
335 $key = strtolower($key);
336 $prop = '_' . $this->_fields
[$key]['propname'];
338 if (isset($values[$key])) {
339 $value = trim($values[$key]);
344 // if the field is enabled, check it
345 if (!isset($disabled[$key])) {
346 // fill up the adherent structure
347 //$this->$prop = stripslashes($value); //not relevant here!
349 // now, check validity
353 case 'date_debut_cotis':
354 case 'date_fin_cotis':
357 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
359 throw new \
Exception('Incorrect format');
361 $this->$prop = $d->format('Y-m-d');
362 } catch (\Exception
$e) {
364 'Wrong date format. field: ' . $key .
365 ', value: ' . $value . ', expected fmt: ' .
366 __("Y-m-d") . ' | ' . $e->getMessage(),
369 $this->errors
[] = str_replace(
376 $this->_fields
[$key]['label']
378 _T("- Wrong date format (%date_format) for %field!")
385 $this->_member
= $value;
388 case ContributionsTypes
::PK
:
390 $this->type
= (int)$value;
393 case 'montant_cotis':
394 $this->_amount
= $value;
395 $value = strtr($value, ',', '.');
396 if (!is_numeric($value) && $value !== '') {
397 $this->errors
[] = _T("- The amount must be an integer!");
400 case 'type_paiement_cotis':
401 if ($value == self
::PAYMENT_OTHER
402 ||
$value == self
::PAYMENT_CASH
403 ||
$value == self
::PAYMENT_CREDITCARD
404 ||
$value == self
::PAYMENT_CHECK
405 ||
$value == self
::PAYMENT_TRANSFER
406 ||
$value == self
::PAYMENT_PAYPAL
408 $this->_payment_type
= $value;
410 $this->errors
[] = _T("- Unknown payment type");
414 $this->_info
= $value;
416 case Transaction
::PK
:
418 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$value);
421 case 'duree_mois_cotis':
423 if (!is_numeric($value) ||
$value <= 0) {
424 $this->errors
[] = _T("- The duration must be a positive integer!");
426 $this->$prop = $value;
427 $this->retrieveEndDate();
434 // missing required fields?
435 foreach ($required as $key => $val) {
437 $prop = '_' . $this->_fields
[$key]['propname'];
438 if (!isset($disabled[$key])
439 && (!isset($this->$prop)
440 ||
(!is_object($this->$prop) && trim($this->$prop) == '')
441 ||
(is_object($this->$prop) && trim($this->$prop->id
) == ''))
443 $this->errors
[] = str_replace(
445 '<a href="#' . $key . '">' . $this->getFieldLabel($key) .'</a>',
446 _T("- Mandatory field %field empty.")
452 if ($this->_transaction
!= null && $this->_amount
!= null) {
453 $missing = $this->_transaction
->getMissingAmount();
454 //calculate new missing amount
455 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
457 $this->errors
[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
461 if ($this->isCotis() && count($this->errors
) == 0) {
462 $overlap = $this->checkOverlap();
463 if ($overlap !== true) {
464 if ($overlap === false) {
465 $this->errors
[] = _T("An error occured checking overlaping fees :(");
467 //method directly return error message
468 $this->errors
[] = $overlap;
473 $this->dynamicsCheck($values);
475 if (count($this->errors
) > 0) {
477 'Some errors has been throwed attempting to edit/store a contribution' .
478 print_r($this->errors
, true),
481 return $this->errors
;
484 'Contribution checked successfully.',
492 * Check that membership fees does not overlap
494 * @return boolean|string True if all is ok, false if error,
495 * error message if overlap
497 public function checkOverlap()
500 $select = $this->zdb
->select(self
::TABLE
, 'c');
502 array('date_debut_cotis', 'date_fin_cotis')
504 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
505 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
507 )->where(Adherent
::PK
. ' = ' . $this->_member
)
508 ->where(array('cotis_extension' => new Expression('true')))
510 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
511 ->lessThan('date_debut_cotis', $this->_end_date
)
514 ->greaterThan('date_fin_cotis', $this->_begin_date
)
515 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
517 if ($this->id
!= '') {
518 $select->where(self
::PK
. ' != ' . $this->id
);
521 $results = $this->zdb
->execute($select);
522 if ($results->count() > 0) {
523 $result = $results->current();
524 $d = new \
DateTime($result->date_debut_cotis
);
526 return _T("- Membership period overlaps period starting at ") .
527 $d->format(__("Y-m-d"));
530 } catch (\Exception
$e) {
532 'An error occured checking overlaping fee. ' . $e->getMessage(),
540 * Store the contribution
544 public function store()
548 if (count($this->errors
) > 0) {
549 throw new \
RuntimeException(
550 'Existing errors prevents storing contribution: ' .
551 print_r($this->errors
, true)
557 $this->zdb
->connection
->beginTransaction();
559 $fields = self
::getDbFields($this->zdb
);
560 foreach ($fields as $field) {
561 $prop = '_' . $this->_fields
[$field]['propname'];
563 case ContributionsTypes
::PK
:
564 case Transaction
::PK
:
565 if (isset($this->$prop)) {
566 $values[$field] = $this->$prop->id
;
570 $values[$field] = $this->$prop;
575 //no end date, let's take database defaults
576 if (!$this->isCotis() && !$this->_end_date
) {
577 unset($values['date_fin_cotis']);
581 if (!isset($this->_id
) ||
$this->_id
== '') {
582 //we're inserting a new contribution
583 unset($values[self
::PK
]);
585 $insert = $this->zdb
->insert(self
::TABLE
);
586 $insert->values($values);
587 $add = $this->zdb
->execute($insert);
589 if ($add->count() > 0) {
590 if ($this->zdb
->isPostgres()) {
591 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
592 PREFIX_DB
. 'cotisations_id_seq'
595 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
600 _T("Contribution added"),
601 Adherent
::getSName($this->zdb
, $this->_member
)
605 $hist->add(_T("Fail to add new contribution."));
606 throw new \
Exception(
607 'An error occured inserting new contribution!'
611 //we're editing an existing contribution
612 $update = $this->zdb
->update(self
::TABLE
);
613 $update->set($values)->where(
614 self
::PK
. '=' . $this->_id
616 $edit = $this->zdb
->execute($update);
618 //edit == 0 does not mean there were an error, but that there
619 //were nothing to change
620 if ($edit->count() > 0) {
622 _T("Contribution updated"),
623 Adherent
::getSName($this->zdb
, $this->_member
)
627 if ($edit === false) {
628 throw new \
Exception(
629 'An error occured updating contribution # ' . $this->_id
. '!'
635 if ($this->isCotis()) {
636 $deadline = $this->updateDeadline();
637 if ($deadline !== true) {
638 //if something went wrong, we rollback transaction
639 throw new \
Exception('An error occured updating member\'s deadline');
645 $success = $this->dynamicsStore(true);
648 $this->zdb
->connection
->commit();
649 $this->_orig_amount
= $this->_amount
;
651 } catch (\Exception
$e) {
652 $this->zdb
->connection
->rollBack();
654 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
655 $e->getTraceAsString(),
663 * Update member dead line
667 private function updateDeadline()
670 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
672 if ($due_date != '') {
673 $date_fin_update = $due_date;
675 $date_fin_update = new Expression('NULL');
678 $update = $this->zdb
->update(Adherent
::TABLE
);
680 array('date_echeance' => $date_fin_update)
682 Adherent
::PK
. '=' . $this->_member
684 $this->zdb
->execute($update);
686 } catch (\Exception
$e) {
688 'An error occured updating member ' . $this->_member
.
698 * Remove contribution from database
700 * @param boolean $transaction Activate transaction mode (defaults to true)
704 public function remove($transaction = true)
708 $this->zdb
->connection
->beginTransaction();
711 $delete = $this->zdb
->delete(self
::TABLE
);
712 $delete->where(self
::PK
. ' = ' . $this->_id
);
713 $del = $this->zdb
->execute($delete);
714 if ($del->count() > 0) {
715 $this->updateDeadline();
717 throw new \
RuntimeException(
718 'Contribution has not been removed!'
722 $this->zdb
->connection
->commit();
725 } catch (\Exception
$e) {
727 $this->zdb
->connection
->rollBack();
730 'An error occured trying to remove contribution #' .
731 $this->_id
. ' | ' . $e->getMessage(),
741 * @param string $field Field name
745 public function getFieldLabel($field)
747 $label = $this->_fields
[$field]['label'];
748 if ($this->isCotis() && $field == 'date_debut_cotis') {
749 $label = $this->_fields
[$field]['cotlabel'];
752 $label = str_replace(' ', ' ', $label);
753 //remove trailing ':' and then trim
754 $label = trim(trim($label, ':'));
759 * Retrieve fields from database
761 * @param Db $zdb Database instance
765 public static function getDbFields(Db
$zdb)
767 $columns = $zdb->getColumns(self
::TABLE
);
769 foreach ($columns as $col) {
770 $fields[] = $col->getName();
776 * Get the relevant CSS class for current contribution
778 * @return string current contribution row class
780 public function getRowClass()
782 return ( $this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
788 * Retrieve member due date
790 * @param Db $zdb Database instance
791 * @param integer $member_id Member identifier
795 public static function getDueDate(Db
$zdb, $member_id)
798 $select = $zdb->select(self
::TABLE
, 'c');
801 'max_date' => new Expression('MAX(date_fin_cotis)')
804 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
805 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
808 Adherent
::PK
. ' = ' . $member_id
810 array('cotis_extension' => new Expression('true'))
813 $results = $zdb->execute($select);
814 $result = $results->current();
815 $due_date = $result->max_date
;
817 //avoid bad dates in postgres and bad mysql return from zenddb
818 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
822 } catch (\Exception
$e) {
824 'An error occured trying to retrieve member\'s due date',
832 * Detach a contribution from a transaction
834 * @param Db $zdb Database instance
835 * @param Login $login Login instance
836 * @param int $trans_id Transaction identifier
837 * @param int $contrib_id Contribution identifier
841 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
844 //first, we check if contribution is part of transaction
845 $c = new Contribution($zdb, $login, (int)$contrib_id);
846 if ($c->isTransactionPartOf($trans_id)) {
847 $update = $zdb->update(self
::TABLE
);
849 array(Transaction
::PK
=> null)
851 self
::PK
. ' = ' . $contrib_id
853 $zdb->execute($update);
857 'Contribution #' . $contrib_id .
858 ' is not actually part of transaction #' . $trans_id,
863 } catch (\Exception
$e) {
865 'Unable to detach contribution #' . $contrib_id .
866 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
874 * Set a contribution as a transaction part
876 * @param Db $zdb Database instance
877 * @param int $trans_id Transaction identifier
878 * @param int $contrib_id Contribution identifier
882 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
885 $update = $zdb->update(self
::TABLE
);
887 array(Transaction
::PK
=> $trans_id)
888 )->where(self
::PK
. ' = ' . $contrib_id);
890 $zdb->execute($update);
892 } catch (\Exception
$e) {
894 'Unable to attach contribution #' . $contrib_id .
895 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
903 * Is current contribution a cotisation
907 public function isCotis()
909 return $this->_is_cotis
;
913 * Is current contribution part of specified transaction
915 * @param int $id Transaction identifier
919 public function isTransactionPartOf($id)
921 if ($this->isTransactionPart()) {
922 return $id == $this->_transaction
->id
;
929 * Is current contribution part of transaction
933 public function isTransactionPart()
935 return $this->_transaction
!= null;
939 * Execute post contribution script
941 * @param ExternalScript $es External script to execute
942 * @param array $extra Extra informations on contribution
944 * @param array $pextra Extra information on payment
947 * @return mixed Script return value on success, values and script output on fail
949 public function executePostScript(
957 'type' => $this->getPaymentType()
960 if ($pextra !== null && is_array($pextra)) {
961 $payment = array_merge($payment, $pextra);
964 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
965 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
968 $voucher_path = null;
969 if ($this->_id
!== null) {
970 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
971 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
972 $voucher_path = $voucher->getPath();
976 'date' => $this->_date
,
977 'type' => $this->getRawType(),
978 'amount' => $this->amount
,
979 'voucher' => $voucher_path,
981 'id' => $this->type
->id
,
982 'label' => $this->type
->libelle
984 'payment' => $payment
987 if ($this->_member
!== null) {
988 $m = new Adherent($this->zdb
, (int)$this->_member
);
990 'name' => $m->sfullname
,
991 'email' => $m->email
,
992 'organization' => ($m->isCompany() ?
1 : 0),
995 'label' => $m->sstatus
997 'country' => $m->country
1000 if ($m->isCompany()) {
1001 $member['organization_name'] = $m->company_name
;
1004 $contrib['member'] = $member;
1007 if ($extra !== null && is_array($extra)) {
1008 $contrib = array_merge($contrib, $extra);
1011 $res = $es->send($contrib);
1013 if ($res !== true) {
1015 'An error occured calling post contribution ' .
1016 "script:\n" . $es->getOutput(),
1019 $res = _T("Contribution informations") . "\n";
1020 $res .= print_r($contrib, true);
1021 $res .= "\n\n" . _T("Script output") . "\n";
1022 $res .= $es->getOutput();
1028 * Get raw contribution type
1032 public function getRawType()
1034 if ($this->isCotis()) {
1035 return 'membership';
1042 * Get contribution type label
1046 public function getTypeLabel()
1048 if ($this->isCotis()) {
1049 return _T("Membership");
1051 return _T("Donation");
1056 * Get payent type label
1060 public function getPaymentType()
1062 if ($this->_payment_type
=== null) {
1066 switch ($this->payment_type
) {
1067 case Contribution
::PAYMENT_CASH
:
1070 case Contribution
::PAYMENT_CREDITCARD
:
1071 return 'credit_card';
1073 case Contribution
::PAYMENT_CHECK
:
1076 case Contribution
::PAYMENT_TRANSFER
:
1079 case Contribution
::PAYMENT_PAYPAL
:
1082 case Contribution
::PAYMENT_OTHER
:
1087 __METHOD__
. ' Unknonw payment type ' . $this->payment_type
,
1090 throw new \
RuntimeException(
1091 'Unknonw payment type ' . $this->payment_type
1097 * Global getter method
1099 * @param string $name name of the property we want to retrive
1101 * @return false|object the called property
1103 public function __get($name)
1106 $forbidden = array('is_cotis');
1107 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1108 'raw_begin_date', 'raw_end_date'
1111 $rname = '_' . $name;
1113 if (in_array($name, $forbidden)) {
1115 "Call to __get for '$name' is forbidden!",
1121 return $this->isCotis();
1124 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1126 } elseif (property_exists($this, $rname)
1127 ||
in_array($name, $virtuals)
1131 case 'raw_begin_date':
1132 case 'raw_end_date':
1133 $rname = '_' . substr($name, 4);
1134 if ($this->$rname != '') {
1136 $d = new \
DateTime($this->$rname);
1138 } catch (\Exception
$e) {
1139 //oops, we've got a bad date :/
1141 'Bad date (' . $this->$rname . ') | ' .
1152 if ($this->$rname != '') {
1154 $d = new \
DateTime($this->$rname);
1155 return $d->format(__("Y-m-d"));
1156 } catch (\Exception
$e) {
1157 //oops, we've got a bad date :/
1159 'Bad date (' . $this->$rname . ') | ' .
1163 return $this->$rname;
1168 if ($this->_is_cotis
) {
1169 $date_end = new \
DateTime($this->_end_date
);
1170 $date_start = new \
DateTime($this->_begin_date
);
1171 $diff = $date_end->diff($date_start);
1172 return $diff->format('%y') * 12 +
$diff->format('%m');
1177 case 'spayment_type':
1178 if ($this->_payment_type
=== null) {
1181 switch ($this->_payment_type
) {
1182 case self
::PAYMENT_OTHER
:
1185 case self
::PAYMENT_CASH
:
1188 case self
::PAYMENT_CREDITCARD
:
1189 return _T("Credit card");
1191 case self
::PAYMENT_CHECK
:
1194 case self
::PAYMENT_TRANSFER
:
1195 return _T("Transfer");
1197 case self
::PAYMENT_PAYPAL
:
1198 return _T("Paypal");
1202 'Unknown payment type ' . $this->_payment_type
,
1210 if ($this->_is_cotis
=== null) {
1213 return ($this->isCotis()) ?
1214 PdfModel
::INVOICE_MODEL
:
1215 PdfModel
::RECEIPT_MODEL
;
1218 return $this->$rname;
1223 "Unknown property '$rname'",
1231 * Global setter method
1233 * @param string $name name of the property we want to assign a value to
1234 * @param object $value a relevant value for the property
1238 public function __set($name, $value)
1240 $forbidden = array('fields', 'is_cotis', 'end_date');
1242 if (!in_array($name, $forbidden)) {
1243 $rname = '_' . $name;
1246 if (is_int($value)) {
1247 $this->$rname = new Transaction($this->zdb
, $this->login
, $value);
1250 'Trying to set a transaction from an id that is not an integer.',
1256 if (is_int($value)) {
1258 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1259 //set is_cotis according to type
1260 if ($this->$rname->extension
== 1) {
1261 $this->_is_cotis
= true;
1263 $this->_is_cotis
= false;
1267 'Trying to set a type from an id that is not an integer.',
1274 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1276 throw new \
Exception('Incorrect format');
1278 $this->_begin_date
= $d->format('Y-m-d');
1279 } catch (\Exception
$e) {
1281 'Wrong date format. field: ' . $name .
1282 ', value: ' . $value . ', expected fmt: ' .
1283 __("Y-m-d") . ' | ' . $e->getMessage(),
1286 $this->errors
[] = str_replace(
1293 $this->_fields
['date_debut_cotis']['label']
1295 _T("- Wrong date format (%date_format) for %field!")
1300 if (is_numeric($value) && $value > 0) {
1301 $this->$rname = $value;
1304 'Trying to set an amount with a non numeric value, ' .
1305 'or with a zero value',
1311 if (is_int($value)) {
1313 $this->$rname = $value;
1316 case 'payment_type':
1317 if ($value == self
::PAYMENT_OTHER
1318 ||
$value == self
::PAYMENT_CASH
1319 ||
$value == self
::PAYMENT_CREDITCARD
1320 ||
$value == self
::PAYMENT_CHECK
1321 ||
$value == self
::PAYMENT_TRANSFER
1322 ||
$value == self
::PAYMENT_PAYPAL
1324 $this->_payment_type
= $value;
1327 'Unknown payment type ' . $value,
1334 '[' . __CLASS__
. ']: Trying to set an unknown property (' .