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 if ($results->count() > 0) {
262 $row = $results->current();
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) && $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
[] = str_replace(
446 '<a href="#' . $key . '">' . $this->getFieldLabel($key) .'</a>',
447 _T("- Mandatory field %field empty.")
453 if ($this->_transaction
!= null && $this->_amount
!= null) {
454 $missing = $this->_transaction
->getMissingAmount();
455 //calculate new missing amount
456 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
458 $this->errors
[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
462 if ($this->isCotis() && count($this->errors
) == 0) {
463 $overlap = $this->checkOverlap();
464 if ($overlap !== true) {
465 if ($overlap === false) {
466 $this->errors
[] = _T("An error occured checking overlaping fees :(");
468 //method directly return error message
469 $this->errors
[] = $overlap;
474 if (count($this->errors
) > 0) {
476 'Some errors has been throwed attempting to edit/store a contribution' .
477 print_r($this->errors
, true),
480 return $this->errors
;
483 'Contribution checked successfully.',
491 * Check that membership fees does not overlap
493 * @return boolean|string True if all is ok, false if error,
494 * error message if overlap
496 public function checkOverlap()
499 $select = $this->zdb
->select(self
::TABLE
, 'c');
501 array('date_debut_cotis', 'date_fin_cotis')
503 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
504 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
506 )->where(Adherent
::PK
. ' = ' . $this->_member
)
507 ->where(array('cotis_extension' => new Expression('true')))
509 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
510 ->lessThan('date_debut_cotis', $this->_end_date
)
513 ->greaterThan('date_fin_cotis', $this->_begin_date
)
514 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
516 if ($this->id
!= '') {
517 $select->where(self
::PK
. ' != ' . $this->id
);
520 $results = $this->zdb
->execute($select);
521 if ($results->count() > 0) {
522 $result = $results->current();
523 $d = new \
DateTime($result->date_debut_cotis
);
525 return _T("- Membership period overlaps period starting at ") .
526 $d->format(__("Y-m-d"));
529 } catch (\Exception
$e) {
531 'An error occured checking overlaping fee. ' . $e->getMessage(),
539 * Store the contribution
543 public function store()
547 if (count($this->errors
) > 0) {
548 throw new \
RuntimeException(
549 'Existing errors prevents storing contribution: ' .
550 print_r($this->errors
, true)
556 $this->zdb
->connection
->beginTransaction();
558 $fields = self
::getDbFields($this->zdb
);
559 foreach ($fields as $field) {
560 $prop = '_' . $this->_fields
[$field]['propname'];
562 case ContributionsTypes
::PK
:
563 case Transaction
::PK
:
564 if (isset($this->$prop)) {
565 $values[$field] = $this->$prop->id
;
569 $values[$field] = $this->$prop;
574 //no end date, let's take database defaults
575 if (!$this->isCotis() && !$this->_end_date
) {
576 unset($values['date_fin_cotis']);
579 if (!isset($this->_id
) ||
$this->_id
== '') {
580 //we're inserting a new contribution
581 unset($values[self
::PK
]);
583 $insert = $this->zdb
->insert(self
::TABLE
);
584 $insert->values($values);
585 $add = $this->zdb
->execute($insert);
587 if ($add->count() > 0) {
588 if ($this->zdb
->isPostgres()) {
589 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
590 PREFIX_DB
. 'cotisations_id_seq'
593 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
598 _T("Contribution added"),
599 Adherent
::getSName($this->zdb
, $this->_member
)
602 $hist->add(_T("Fail to add new contribution."));
603 throw new \
Exception(
604 'An error occured inserting new contribution!'
608 //we're editing an existing contribution
609 $update = $this->zdb
->update(self
::TABLE
);
610 $update->set($values)->where(
611 self
::PK
. '=' . $this->_id
613 $edit = $this->zdb
->execute($update);
615 //edit == 0 does not mean there were an error, but that there
616 //were nothing to change
617 if ($edit->count() > 0) {
619 _T("Contribution updated"),
620 Adherent
::getSName($this->zdb
, $this->_member
)
622 } elseif ($edit === false) {
623 throw new \
Exception(
624 'An error occured updating contribution # ' . $this->_id
. '!'
629 if ($this->isCotis()) {
630 $deadline = $this->updateDeadline();
631 if ($deadline !== true) {
632 //if something went wrong, we rollback transaction
633 throw new \
Exception('An error occured updating member\'s deadline');
636 $this->zdb
->connection
->commit();
637 $this->_orig_amount
= $this->_amount
;
639 } catch (\Exception
$e) {
640 $this->zdb
->connection
->rollBack();
642 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
643 $e->getTraceAsString(),
651 * Update member dead line
655 private function updateDeadline()
658 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
660 if ($due_date != '') {
661 $date_fin_update = $due_date;
663 $date_fin_update = new Expression('NULL');
666 $update = $this->zdb
->update(Adherent
::TABLE
);
668 array('date_echeance' => $date_fin_update)
670 Adherent
::PK
. '=' . $this->_member
672 $this->zdb
->execute($update);
674 } catch (\Exception
$e) {
676 'An error occured updating member ' . $this->_member
.
686 * Remove contribution from database
688 * @param boolean $transaction Activate transaction mode (defaults to true)
692 public function remove($transaction = true)
696 $this->zdb
->connection
->beginTransaction();
699 $delete = $this->zdb
->delete(self
::TABLE
);
700 $delete->where(self
::PK
. ' = ' . $this->_id
);
701 $del = $this->zdb
->execute($delete);
702 if ($del->count() > 0) {
703 $this->updateDeadline();
705 throw new \
RuntimeException(
706 'Contribution has not been removed!'
710 $this->zdb
->connection
->commit();
713 } catch (\Exception
$e) {
715 $this->zdb
->connection
->rollBack();
718 'An error occured trying to remove contribution #' .
719 $this->_id
. ' | ' . $e->getMessage(),
729 * @param string $field Field name
733 public function getFieldLabel($field)
735 $label = $this->_fields
[$field]['label'];
736 if ($this->isCotis() && $field == 'date_debut_cotis') {
737 $label = $this->_fields
[$field]['cotlabel'];
740 $label = str_replace(' ', ' ', $label);
741 //remove trailing ':' and then trim
742 $label = trim(trim($label, ':'));
747 * Retrieve fields from database
749 * @param Db $zdb Database instance
753 public static function getDbFields(Db
$zdb)
755 $columns = $zdb->getColumns(self
::TABLE
);
757 foreach ($columns as $col) {
758 $fields[] = $col->getName();
764 * Get the relevant CSS class for current contribution
766 * @return string current contribution row class
768 public function getRowClass()
770 return ( $this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
776 * Retrieve member due date
778 * @param Db $zdb Database instance
779 * @param integer $member_id Member identifier
783 public static function getDueDate(Db
$zdb, $member_id)
786 $select = $zdb->select(self
::TABLE
, 'c');
789 'max_date' => new Expression('MAX(date_fin_cotis)')
792 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
793 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
796 Adherent
::PK
. ' = ' . $member_id
798 array('cotis_extension' => new Expression('true'))
801 $results = $zdb->execute($select);
802 $result = $results->current();
803 $due_date = $result->max_date
;
805 //avoid bad dates in postgres and bad mysql return from zenddb
806 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
810 } catch (\Exception
$e) {
812 'An error occured trying to retrieve member\'s due date',
820 * Detach a contribution from a transaction
822 * @param Db $zdb Database instance
823 * @param Login $login Login instance
824 * @param int $trans_id Transaction identifier
825 * @param int $contrib_id Contribution identifier
829 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
832 //first, we check if contribution is part of transaction
833 $c = new Contribution($zdb, $login, (int)$contrib_id);
834 if ($c->isTransactionPartOf($trans_id)) {
835 $update = $zdb->update(self
::TABLE
);
837 array(Transaction
::PK
=> null)
839 self
::PK
. ' = ' . $contrib_id
841 $zdb->execute($update);
845 'Contribution #' . $contrib_id .
846 ' is not actually part of transaction #' . $trans_id,
851 } catch (\Exception
$e) {
853 'Unable to detach contribution #' . $contrib_id .
854 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
862 * Set a contribution as a transaction part
864 * @param Db $zdb Database instance
865 * @param int $trans_id Transaction identifier
866 * @param int $contrib_id Contribution identifier
870 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
873 $update = $zdb->update(self
::TABLE
);
875 array(Transaction
::PK
=> $trans_id)
876 )->where(self
::PK
. ' = ' . $contrib_id);
878 $zdb->execute($update);
880 } catch (\Exception
$e) {
882 'Unable to attach contribution #' . $contrib_id .
883 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
891 * Is current contribution a cotisation
895 public function isCotis()
897 return $this->_is_cotis
;
901 * Is current contribution part of specified transaction
903 * @param int $id Transaction identifier
907 public function isTransactionPartOf($id)
909 if ($this->isTransactionPart()) {
910 return $id == $this->_transaction
->id
;
917 * Is current contribution part of transaction
921 public function isTransactionPart()
923 return $this->_transaction
!= null;
927 * Execute post contribution script
929 * @param ExternalScript $es External script to execute
930 * @param array $extra Extra informations on contribution
932 * @param array $pextra Extra information on payment
935 * @return mixed Script return value on success, values and script output on fail
937 public function executePostScript(
945 'type' => $this->getPaymentType()
948 if ($pextra !== null && is_array($pextra)) {
949 $payment = array_merge($payment, $pextra);
952 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
953 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
956 $voucher_path = null;
957 if ($this->_id
!== null) {
958 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
959 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
960 $voucher_path = $voucher->getPath();
964 'date' => $this->_date
,
965 'type' => $this->getRawType(),
966 'amount' => $this->amount
,
967 'voucher' => $voucher_path,
969 'id' => $this->type
->id
,
970 'label' => $this->type
->libelle
972 'payment' => $payment
975 if ($this->_member
!== null) {
976 $m = new Adherent($this->zdb
, (int)$this->_member
);
978 'name' => $m->sfullname
,
979 'email' => $m->email
,
980 'organization' => ($m->isCompany() ?
1 : 0),
983 'label' => $m->sstatus
985 'country' => $m->country
988 if ($m->isCompany()) {
989 $member['organization_name'] = $m->company_name
;
992 $contrib['member'] = $member;
995 if ($extra !== null && is_array($extra)) {
996 $contrib = array_merge($contrib, $extra);
999 $res = $es->send($contrib);
1001 if ($res !== true) {
1003 'An error occured calling post contribution ' .
1004 "script:\n" . $es->getOutput(),
1007 $res = _T("Contribution informations") . "\n";
1008 $res .= print_r($contrib, true);
1009 $res .= "\n\n" . _T("Script output") . "\n";
1010 $res .= $es->getOutput();
1016 * Get raw contribution type
1020 public function getRawType()
1022 if ($this->isCotis()) {
1023 return 'membership';
1030 * Get contribution type label
1034 public function getTypeLabel()
1036 if ($this->isCotis()) {
1037 return _T("Membership");
1039 return _T("Donation");
1044 * Get payent type label
1048 public function getPaymentType()
1050 if ($this->_payment_type
=== null) {
1054 switch ($this->payment_type
) {
1055 case Contribution
::PAYMENT_CASH
:
1058 case Contribution
::PAYMENT_CREDITCARD
:
1059 return 'credit_card';
1061 case Contribution
::PAYMENT_CHECK
:
1064 case Contribution
::PAYMENT_TRANSFER
:
1067 case Contribution
::PAYMENT_PAYPAL
:
1070 case Contribution
::PAYMENT_OTHER
:
1075 __METHOD__
. ' Unknonw payment type ' . $this->payment_type
,
1078 throw new \
RuntimeException(
1079 'Unknonw payment type ' . $this->payment_type
1085 * Global getter method
1087 * @param string $name name of the property we want to retrive
1089 * @return false|object the called property
1091 public function __get($name)
1094 $forbidden = array('is_cotis');
1095 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1096 'raw_begin_date', 'raw_end_date'
1099 $rname = '_' . $name;
1101 if (in_array($name, $forbidden)) {
1103 "Call to __get for '$name' is forbidden!",
1109 return $this->isCotis();
1112 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1114 } elseif (property_exists($this, $rname)
1115 ||
in_array($name, $virtuals)
1119 case 'raw_begin_date':
1120 case 'raw_end_date':
1121 $rname = '_' . substr($name, 4);
1122 if ($this->$rname != '') {
1124 $d = new \
DateTime($this->$rname);
1126 } catch (\Exception
$e) {
1127 //oops, we've got a bad date :/
1129 'Bad date (' . $this->$rname . ') | ' .
1140 if ($this->$rname != '') {
1142 $d = new \
DateTime($this->$rname);
1143 return $d->format(__("Y-m-d"));
1144 } catch (\Exception
$e) {
1145 //oops, we've got a bad date :/
1147 'Bad date (' . $this->$rname . ') | ' .
1151 return $this->$rname;
1156 if ($this->_is_cotis
) {
1157 $date_end = new \
DateTime($this->_end_date
);
1158 $date_start = new \
DateTime($this->_begin_date
);
1159 $diff = $date_end->diff($date_start);
1160 return $diff->format('%y') * 12 +
$diff->format('%m');
1165 case 'spayment_type':
1166 if ($this->_payment_type
=== null) {
1169 switch ($this->_payment_type
) {
1170 case self
::PAYMENT_OTHER
:
1173 case self
::PAYMENT_CASH
:
1176 case self
::PAYMENT_CREDITCARD
:
1177 return _T("Credit card");
1179 case self
::PAYMENT_CHECK
:
1182 case self
::PAYMENT_TRANSFER
:
1183 return _T("Transfer");
1185 case self
::PAYMENT_PAYPAL
:
1186 return _T("Paypal");
1190 'Unknown payment type ' . $this->_payment_type
,
1198 if ($this->_is_cotis
=== null) {
1201 return ($this->isCotis()) ?
1202 PdfModel
::INVOICE_MODEL
:
1203 PdfModel
::RECEIPT_MODEL
;
1206 return $this->$rname;
1211 "Unknown property '$rname'",
1219 * Global setter method
1221 * @param string $name name of the property we want to assign a value to
1222 * @param object $value a relevant value for the property
1226 public function __set($name, $value)
1228 $forbidden = array('fields', 'is_cotis', 'end_date');
1230 if (!in_array($name, $forbidden)) {
1231 $rname = '_' . $name;
1234 if (is_int($value)) {
1235 $this->$rname = new Transaction($this->zdb
, $this->login
, $value);
1238 'Trying to set a transaction from an id that is not an integer.',
1244 if (is_int($value)) {
1246 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1247 //set is_cotis according to type
1248 if ($this->$rname->extension
== 1) {
1249 $this->_is_cotis
= true;
1251 $this->_is_cotis
= false;
1255 'Trying to set a type from an id that is not an integer.',
1262 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1264 throw new \
Exception('Incorrect format');
1266 $this->_begin_date
= $d->format('Y-m-d');
1267 } catch (\Exception
$e) {
1269 'Wrong date format. field: ' . $name .
1270 ', value: ' . $value . ', expected fmt: ' .
1271 __("Y-m-d") . ' | ' . $e->getMessage(),
1274 $this->errors
[] = str_replace(
1281 $this->_fields
['date_debut_cotis']['label']
1283 _T("- Wrong date format (%date_format) for %field!")
1288 if (is_numeric($value) && $value > 0) {
1289 $this->$rname = $value;
1292 'Trying to set an amount with a non numeric value, ' .
1293 'or with a zero value',
1299 if (is_int($value)) {
1301 $this->$rname = $value;
1304 case 'payment_type':
1305 if ($value == self
::PAYMENT_OTHER
1306 ||
$value == self
::PAYMENT_CASH
1307 ||
$value == self
::PAYMENT_CREDITCARD
1308 ||
$value == self
::PAYMENT_CHECK
1309 ||
$value == self
::PAYMENT_TRANSFER
1310 ||
$value == self
::PAYMENT_PAYPAL
1312 $this->_payment_type
= $value;
1315 'Unknown payment type ' . $value,
1322 '[' . __CLASS__
. ']: Trying to set an unknown property (' .