3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
6 * Contribution class for galette
10 * Copyright © 2010-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2010-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7dev - 2010-03-11
38 namespace Galette\Entity
;
41 use Laminas\Db\Sql\Expression
;
43 use Galette\Core\Login
;
44 use Galette\IO\ExternalScript
;
45 use Galette\IO\PdfContribution
;
46 use Galette\Repository\PaymentTypes
;
49 * Contribution class for galette
54 * @author Johan Cwiklinski <johan@x-tnd.be>
55 * @copyright 2010-2014 The Galette Team
56 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
57 * @link http://galette.tuxfamily.org
58 * @since Available since 0.7dev - 2010-03-11
64 const TABLE
= 'cotisations';
65 const PK
= 'id_cotis';
72 private $_payment_type;
73 private $_orig_amount;
77 private $_transaction = null;
81 //fields list and their translation
92 * @param Db $zdb Database
93 * @param Login $login Login instance
94 * @param null|int|ResultSet $args Either a ResultSet row to load
95 * a specific contribution, or an type id
96 * to just instanciate object
98 public function __construct(Db
$zdb, Login
$login, $args = null)
101 $this->login
= $login;
104 * Fields configuration. Each field is an array and must reflect:
110 * I'd prefer a static private variable for this...
111 * But call to the _T function does not seem to be allowed there :/
113 $this->_fields
= array(
115 'label' => null, //not a field in the form
118 Adherent
::PK
=> array(
119 'label' => _T("Contributor:"),
120 'propname' => 'member'
122 ContributionsTypes
::PK
=> array(
123 'label' => _T("Contribution type:"),
126 'montant_cotis' => array(
127 'label' => _T("Amount:"),
128 'propname' => 'amount'
130 'type_paiement_cotis' => array(
131 'label' => _T("Payment type:"),
132 'propname' => 'payment_type'
134 'info_cotis' => array(
135 'label' => _T("Comments:"),
138 'date_enreg' => array(
139 'label' => null, //not a field in the form
142 'date_debut_cotis' => array(
143 'label' => _T("Date of contribution:"),
144 'cotlabel' => _T("Start date of membership:"), //if contribution is a cotisation, label differs
145 'propname' => 'begin_date'
147 'date_fin_cotis' => array(
148 'label' => _T("End date of membership:"),
149 'propname' => 'end_date'
151 Transaction
::PK
=> array(
152 'label' => null, //not a field in the form
153 'propname' => 'transaction'
155 //this one is not really a field, but is required in some cases...
156 //adding it here make simplier to check required fields
157 'duree_mois_cotis' => array(
158 'label' => _T("Membership extension:"),
159 'propname' => 'extension'
164 } elseif (is_array($args)) {
165 $this->_date
= date("Y-m-d");
166 if (isset($args['adh']) && $args['adh'] != '') {
167 $this->_member
= (int)$args['adh'];
169 if (isset($args['trans'])) {
170 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$args['trans']);
171 if (!isset($this->_member
)) {
172 $this->_member
= (int)$this->_transaction
->member
;
174 $this->_amount
= $this->_transaction
->getMissingAmount();
176 $this->type
= (int)$args['type'];
177 //calculate begin date for cotisation
178 $this->_begin_date
= $this->_date
;
179 if ($this->_is_cotis
) {
180 $curend = self
::getDueDate($this->zdb
, $this->_member
);
182 $dend = new \
DateTime($curend);
183 $now = date('Y-m-d');
184 $dnow = new \
DateTime($now);
186 // Member didn't renew on time
187 $this->_begin_date
= $now;
189 $this->_begin_date
= $curend;
192 $this->retrieveEndDate();
194 if (isset($args['payment_type'])) {
195 $this->_payment_type
= $args['payment_type'];
197 } elseif (is_object($args)) {
198 $this->loadFromRS($args);
201 $this->loadDynamicFields();
205 * Sets end contribution date
209 private function retrieveEndDate()
213 $bdate = new \
DateTime($this->_begin_date
);
214 if ($preferences->pref_beg_membership
!= '') {
215 //case beginning of membership
216 list($j, $m) = explode('/', $preferences->pref_beg_membership
);
217 $edate = new \
DateTime($bdate->format('Y') . '-' . $m . '-' . $j);
218 while ($edate <= $bdate) {
219 $edate->modify('+1 year');
222 if ($preferences->pref_membership_offermonths
> 0) {
223 //count days until end of membership date
224 $diff1 = (int)$bdate->diff($edate)->format('%a');
226 //count days beetween end of membership date and offered months
227 $tdate = clone $edate;
228 $tdate->modify('-' . $preferences->pref_membership_offermonths
. ' month');
229 $diff2 = (int)$edate->diff($tdate)->format('%a');
231 //when number of days until end of membership is less than for offered months, it's free :)
232 if ($diff1 <= $diff2) {
233 $edate->modify('+1 year');
237 $this->_end_date
= $edate->format('Y-m-d');
238 } elseif ($preferences->pref_membership_ext
!= '') {
239 //case membership extension
240 if ($this->_extension
== null) {
241 $this->_extension
= $preferences->pref_membership_ext
;
243 $dext = new \
DateInterval('P' . $this->_extension
. 'M');
244 $edate = $bdate->add($dext);
245 $this->_end_date
= $edate->format('Y-m-d');
247 throw new \
RuntimeException(
248 'Unable to define end date; none of pref_beg_membership nor pref_membership_ext are defined!'
254 * Loads a contribution from its id
256 * @param int $id the identifiant for the contribution to load
258 * @return bool true if query succeed, false otherwise
260 public function load($id)
263 $select = $this->zdb
->select(self
::TABLE
);
264 $select->where(self
::PK
. ' = ' . $id);
265 //restrict query on current member id if he's not admin nor staff member
266 if (!$this->login
->isAdmin() && !$this->login
->isStaff()) {
267 $select->where(Adherent
::PK
. ' = ' . $this->login
->id
);
270 $results = $this->zdb
->execute($select);
271 if ($results->count() > 0) {
272 $row = $results->current();
273 $this->loadFromRS($row);
276 throw new \
Exception(
277 'No contribution #' . $id . ' (user ' . $this->login
->id
. ')'
280 } catch (\Exception
$e) {
282 'An error occurred attempting to load contribution #' . $id .
291 * Populate object from a resultset row
293 * @param ResultSet $r the resultset row
297 private function loadFromRS($r)
300 $this->_id
= (int)$r->$pk;
301 $this->_date
= $r->date_enreg
;
302 $this->_amount
= $r->montant_cotis
;
303 //save original amount, we need it for transactions parts calulations
304 $this->_orig_amount
= $r->montant_cotis
;
305 $this->_payment_type
= $r->type_paiement_cotis
;
306 $this->_info
= $r->info_cotis
;
307 $this->_begin_date
= $r->date_debut_cotis
;
308 $enddate = $r->date_fin_cotis
;
309 //do not work with knows bad dates...
310 //the one with BC comes from 0.63/pgsl demo... Why the hell a so
311 //strange date? dont know :(
312 if ($enddate !== '0000-00-00'
313 && $enddate !== '1901-01-01'
314 && $enddate !== '0001-01-01 BC'
316 $this->_end_date
= $r->date_fin_cotis
;
318 $adhpk = Adherent
::PK
;
319 $this->_member
= (int)$r->$adhpk;
321 $transpk = Transaction
::PK
;
322 if ($r->$transpk != '') {
323 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$r->$transpk);
326 $this->type
= (int)$r->id_type_cotis
;
327 $this->loadDynamicFields();
331 * Check posted values validity
333 * @param array $values All values to check, basically the $_POST array
334 * after sending the form
335 * @param array $required Array of required fields
336 * @param array $disabled Array of disabled fields
340 public function check($values, $required, $disabled)
343 $this->errors
= array();
345 $fields = array_keys($this->_fields
);
346 foreach ($fields as $key) {
347 //first of all, let's sanitize values
348 $key = strtolower($key);
349 $prop = '_' . $this->_fields
[$key]['propname'];
351 if (isset($values[$key])) {
352 $value = trim($values[$key]);
357 // if the field is enabled, check it
358 if (!isset($disabled[$key])) {
359 // fill up the adherent structure
360 //$this->$prop = stripslashes($value); //not relevant here!
362 // now, check validity
366 case 'date_debut_cotis':
367 case 'date_fin_cotis':
370 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
372 throw new \
Exception('Incorrect format');
374 $this->$prop = $d->format('Y-m-d');
375 } catch (\Exception
$e) {
377 'Wrong date format. field: ' . $key .
378 ', value: ' . $value . ', expected fmt: ' .
379 __("Y-m-d") . ' | ' . $e->getMessage(),
382 $this->errors
[] = str_replace(
389 $this->_fields
[$key]['label']
391 _T("- Wrong date format (%date_format) for %field!")
398 $this->_member
= (int)$value;
401 case ContributionsTypes
::PK
:
403 $this->type
= (int)$value;
406 case 'montant_cotis':
407 $this->_amount
= $value;
408 $value = strtr($value, ',', '.');
409 if (!is_numeric($value) && $value !== '') {
410 $this->errors
[] = _T("- The amount must be an integer!");
413 case 'type_paiement_cotis':
414 $ptypes = new PaymentTypes(
419 $ptlist = $ptypes->getList();
420 if (isset($ptlist[$value])) {
421 $this->_payment_type
= $value;
423 $this->errors
[] = _T("- Unknown payment type");
427 $this->_info
= $value;
429 case Transaction
::PK
:
431 $this->_transaction
= new Transaction($this->zdb
, $this->login
, (int)$value);
434 case 'duree_mois_cotis':
436 if (!is_numeric($value) ||
$value <= 0) {
437 $this->errors
[] = _T("- The duration must be a positive integer!");
439 $this->$prop = $value;
440 $this->retrieveEndDate();
447 // missing required fields?
448 foreach ($required as $key => $val) {
450 $prop = '_' . $this->_fields
[$key]['propname'];
451 if (!isset($disabled[$key])
452 && (!isset($this->$prop)
453 ||
(!is_object($this->$prop) && trim($this->$prop) == '')
454 ||
(is_object($this->$prop) && trim($this->$prop->id
) == ''))
456 $this->errors
[] = str_replace(
458 '<a href="#' . $key . '">' . $this->getFieldLabel($key) . '</a>',
459 _T("- Mandatory field %field empty.")
465 if ($this->_transaction
!= null && $this->_amount
!= null) {
466 $missing = $this->_transaction
->getMissingAmount();
467 //calculate new missing amount
468 $missing = $missing +
$this->_orig_amount
- $this->_amount
;
470 $this->errors
[] = _T("- Sum of all contributions exceed corresponding transaction amount.");
474 if ($this->isCotis() && count($this->errors
) == 0) {
475 $overlap = $this->checkOverlap();
476 if ($overlap !== true) {
477 if ($overlap === false) {
478 $this->errors
[] = _T("An error occurred checking overlaping fees :(");
480 //method directly return error message
481 $this->errors
[] = $overlap;
486 $this->dynamicsCheck($values);
488 if (count($this->errors
) > 0) {
490 'Some errors has been throwed attempting to edit/store a contribution' .
491 print_r($this->errors
, true),
494 return $this->errors
;
497 'Contribution checked successfully.',
505 * Check that membership fees does not overlap
507 * @return boolean|string True if all is ok, false if error,
508 * error message if overlap
510 public function checkOverlap()
513 $select = $this->zdb
->select(self
::TABLE
, 'c');
515 array('date_debut_cotis', 'date_fin_cotis')
517 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
518 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
520 )->where(Adherent
::PK
. ' = ' . $this->_member
)
521 ->where(array('cotis_extension' => new Expression('true')))
523 ->greaterThanOrEqualTo('date_debut_cotis', $this->_begin_date
)
524 ->lessThan('date_debut_cotis', $this->_end_date
)
527 ->greaterThan('date_fin_cotis', $this->_begin_date
)
528 ->lessThanOrEqualTo('date_fin_cotis', $this->_end_date
);
530 if ($this->id
!= '') {
531 $select->where(self
::PK
. ' != ' . $this->id
);
534 $results = $this->zdb
->execute($select);
535 if ($results->count() > 0) {
536 $result = $results->current();
537 $d = new \
DateTime($result->date_debut_cotis
);
539 return _T("- Membership period overlaps period starting at ") .
540 $d->format(__("Y-m-d"));
543 } catch (\Exception
$e) {
545 'An error occurred checking overlaping fee. ' . $e->getMessage(),
553 * Store the contribution
557 public function store()
559 global $hist, $emitter;
561 if (count($this->errors
) > 0) {
562 throw new \
RuntimeException(
563 'Existing errors prevents storing contribution: ' .
564 print_r($this->errors
, true)
570 $this->zdb
->connection
->beginTransaction();
572 $fields = self
::getDbFields($this->zdb
);
573 foreach ($fields as $field) {
574 $prop = '_' . $this->_fields
[$field]['propname'];
576 case ContributionsTypes
::PK
:
577 case Transaction
::PK
:
578 if (isset($this->$prop)) {
579 $values[$field] = $this->$prop->id
;
583 $values[$field] = $this->$prop;
588 //no end date, let's take database defaults
589 if (!$this->isCotis() && !$this->_end_date
) {
590 unset($values['date_fin_cotis']);
594 if (!isset($this->_id
) ||
$this->_id
== '') {
595 //we're inserting a new contribution
596 unset($values[self
::PK
]);
598 $insert = $this->zdb
->insert(self
::TABLE
);
599 $insert->values($values);
600 $add = $this->zdb
->execute($insert);
602 if ($add->count() > 0) {
603 if ($this->zdb
->isPostgres()) {
604 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue(
605 PREFIX_DB
. 'cotisations_id_seq'
608 $this->_id
= $this->zdb
->driver
->getLastGeneratedValue();
613 _T("Contribution added"),
614 Adherent
::getSName($this->zdb
, $this->_member
)
618 $emitter->emit('contribution.add', $this);
620 $hist->add(_T("Fail to add new contribution."));
621 throw new \
Exception(
622 'An error occurred inserting new contribution!'
626 //we're editing an existing contribution
627 $update = $this->zdb
->update(self
::TABLE
);
628 $update->set($values)->where(
629 self
::PK
. '=' . $this->_id
631 $edit = $this->zdb
->execute($update);
633 //edit == 0 does not mean there were an error, but that there
634 //were nothing to change
635 if ($edit->count() > 0) {
637 _T("Contribution updated"),
638 Adherent
::getSName($this->zdb
, $this->_member
)
642 if ($edit === false) {
643 throw new \
Exception(
644 'An error occurred updating contribution # ' . $this->_id
. '!'
649 $emitter->emit('contribution.edit', $this);
652 if ($this->isCotis()) {
653 $deadline = $this->updateDeadline();
654 if ($deadline !== true) {
655 //if something went wrong, we rollback transaction
656 throw new \
Exception('An error occurred updating member\'s deadline');
662 $success = $this->dynamicsStore(true);
665 $this->zdb
->connection
->commit();
666 $this->_orig_amount
= $this->_amount
;
668 } catch (\Exception
$e) {
669 $this->zdb
->connection
->rollBack();
671 'Something went wrong :\'( | ' . $e->getMessage() . "\n" .
672 $e->getTraceAsString(),
680 * Update member dead line
684 private function updateDeadline()
687 $due_date = self
::getDueDate($this->zdb
, $this->_member
);
689 if ($due_date != '') {
690 $date_fin_update = $due_date;
692 $date_fin_update = new Expression('NULL');
695 $update = $this->zdb
->update(Adherent
::TABLE
);
697 array('date_echeance' => $date_fin_update)
699 Adherent
::PK
. '=' . $this->_member
701 $this->zdb
->execute($update);
703 } catch (\Exception
$e) {
705 'An error occurred updating member ' . $this->_member
.
715 * Remove contribution from database
717 * @param boolean $transaction Activate transaction mode (defaults to true)
721 public function remove($transaction = true)
727 $this->zdb
->connection
->beginTransaction();
730 $delete = $this->zdb
->delete(self
::TABLE
);
731 $delete->where(self
::PK
. ' = ' . $this->_id
);
732 $del = $this->zdb
->execute($delete);
733 if ($del->count() > 0) {
734 $this->updateDeadline();
735 $this->dynamicsRemove(true);
737 throw new \
RuntimeException(
738 'Contribution has not been removed!'
742 $this->zdb
->connection
->commit();
744 $emitter->emit('contribution.remove', $this);
746 } catch (\Exception
$e) {
748 $this->zdb
->connection
->rollBack();
751 'An error occurred trying to remove contribution #' .
752 $this->_id
. ' | ' . $e->getMessage(),
762 * @param string $field Field name
766 public function getFieldLabel($field)
768 $label = $this->_fields
[$field]['label'];
769 if ($this->isCotis() && $field == 'date_debut_cotis') {
770 $label = $this->_fields
[$field]['cotlabel'];
773 $label = str_replace(' ', ' ', $label);
774 //remove trailing ':' and then trim
775 $label = trim(trim($label, ':'));
780 * Retrieve fields from database
782 * @param Db $zdb Database instance
786 public static function getDbFields(Db
$zdb)
788 $columns = $zdb->getColumns(self
::TABLE
);
790 foreach ($columns as $col) {
791 $fields[] = $col->getName();
797 * Get the relevant CSS class for current contribution
799 * @return string current contribution row class
801 public function getRowClass()
803 return ($this->_end_date
!= $this->_begin_date
&& $this->_is_cotis
) ?
804 'cotis-normal' : 'cotis-give';
808 * Retrieve member due date
810 * @param Db $zdb Database instance
811 * @param integer $member_id Member identifier
815 public static function getDueDate(Db
$zdb, $member_id)
821 $select = $zdb->select(self
::TABLE
, 'c');
824 'max_date' => new Expression('MAX(date_fin_cotis)')
827 array('ct' => PREFIX_DB
. ContributionsTypes
::TABLE
),
828 'c.' . ContributionsTypes
::PK
. '=ct.' . ContributionsTypes
::PK
,
831 Adherent
::PK
. ' = ' . $member_id
833 array('cotis_extension' => new Expression('true'))
836 $results = $zdb->execute($select);
837 $result = $results->current();
838 $due_date = $result->max_date
;
840 //avoid bad dates in postgres and bad mysql return from zenddb
841 if ($due_date == '0001-01-01 BC' ||
$due_date == '1901-01-01') {
845 } catch (\Exception
$e) {
847 'An error occurred trying to retrieve member\'s due date',
855 * Detach a contribution from a transaction
857 * @param Db $zdb Database instance
858 * @param Login $login Login instance
859 * @param int $trans_id Transaction identifier
860 * @param int $contrib_id Contribution identifier
864 public static function unsetTransactionPart(Db
$zdb, Login
$login, $trans_id, $contrib_id)
867 //first, we check if contribution is part of transaction
868 $c = new Contribution($zdb, $login, (int)$contrib_id);
869 if ($c->isTransactionPartOf($trans_id)) {
870 $update = $zdb->update(self
::TABLE
);
872 array(Transaction
::PK
=> null)
874 self
::PK
. ' = ' . $contrib_id
876 $zdb->execute($update);
880 'Contribution #' . $contrib_id .
881 ' is not actually part of transaction #' . $trans_id,
886 } catch (\Exception
$e) {
888 'Unable to detach contribution #' . $contrib_id .
889 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
897 * Set a contribution as a transaction part
899 * @param Db $zdb Database instance
900 * @param int $trans_id Transaction identifier
901 * @param int $contrib_id Contribution identifier
905 public static function setTransactionPart(Db
$zdb, $trans_id, $contrib_id)
908 $update = $zdb->update(self
::TABLE
);
910 array(Transaction
::PK
=> $trans_id)
911 )->where(self
::PK
. ' = ' . $contrib_id);
913 $zdb->execute($update);
915 } catch (\Exception
$e) {
917 'Unable to attach contribution #' . $contrib_id .
918 ' to transaction #' . $trans_id . ' | ' . $e->getMessage(),
926 * Is current contribution a cotisation
930 public function isCotis()
932 return $this->_is_cotis
;
936 * Is current contribution part of specified transaction
938 * @param int $id Transaction identifier
942 public function isTransactionPartOf($id)
944 if ($this->isTransactionPart()) {
945 return $id == $this->_transaction
->id
;
952 * Is current contribution part of transaction
956 public function isTransactionPart()
958 return $this->_transaction
!= null;
962 * Execute post contribution script
964 * @param ExternalScript $es External script to execute
965 * @param array $extra Extra information on contribution
967 * @param array $pextra Extra information on payment
970 * @return mixed Script return value on success, values and script output on fail
972 public function executePostScript(
980 'type' => $this->getPaymentType()
983 if ($pextra !== null && is_array($pextra)) {
984 $payment = array_merge($payment, $pextra);
987 if (!file_exists(GALETTE_CACHE_DIR
. '/pdf_contribs')) {
988 @mkdir
(GALETTE_CACHE_DIR
. '/pdf_contribs');
991 $voucher_path = null;
992 if ($this->_id
!== null) {
993 $voucher = new PdfContribution($this, $this->zdb
, $preferences);
994 $voucher->store(GALETTE_CACHE_DIR
. '/pdf_contribs');
995 $voucher_path = $voucher->getPath();
999 'id' => (int)$this->_id
,
1000 'date' => $this->_date
,
1001 'type' => $this->getRawType(),
1002 'amount' => $this->amount
,
1003 'voucher' => $voucher_path,
1004 'category' => array(
1005 'id' => $this->type
->id
,
1006 'label' => $this->type
->libelle
1008 'payment' => $payment
1011 if ($this->_member
!== null) {
1012 $m = new Adherent($this->zdb
, (int)$this->_member
);
1014 'id' => (int)$this->_member
,
1015 'name' => $m->sfullname
,
1016 'email' => $m->email
,
1017 'organization' => ($m->isCompany() ?
1 : 0),
1020 'label' => $m->sstatus
1022 'country' => $m->country
1025 if ($m->isCompany()) {
1026 $member['organization_name'] = $m->company_name
;
1029 $contrib['member'] = $member;
1032 if ($extra !== null && is_array($extra)) {
1033 $contrib = array_merge($contrib, $extra);
1036 $res = $es->send($contrib);
1038 if ($res !== true) {
1040 'An error occurred calling post contribution ' .
1041 "script:\n" . $es->getOutput(),
1044 $res = _T("Contribution information") . "\n";
1045 $res .= print_r($contrib, true);
1046 $res .= "\n\n" . _T("Script output") . "\n";
1047 $res .= $es->getOutput();
1053 * Get raw contribution type
1057 public function getRawType()
1059 if ($this->isCotis()) {
1060 return 'membership';
1067 * Get contribution type label
1071 public function getTypeLabel()
1073 if ($this->isCotis()) {
1074 return _T("Membership");
1076 return _T("Donation");
1081 * Get payment type label
1085 public function getPaymentType()
1087 if ($this->_payment_type
=== null) {
1091 $ptype = new PaymentType($this->zdb
, (int)$this->payment_type
);
1092 return $ptype->getName(false);
1096 * Global getter method
1098 * @param string $name name of the property we want to retrive
1100 * @return false|object the called property
1102 public function __get($name)
1105 $forbidden = array('is_cotis');
1106 $virtuals = array('duration', 'spayment_type', 'model', 'raw_date',
1107 'raw_begin_date', 'raw_end_date'
1110 $rname = '_' . $name;
1112 if (in_array($name, $forbidden)) {
1114 "Call to __get for '$name' is forbidden!",
1120 return $this->isCotis();
1123 throw new \
RuntimeException("Call to __get for '$name' is forbidden!");
1125 } elseif (property_exists($this, $rname)
1126 ||
in_array($name, $virtuals)
1130 case 'raw_begin_date':
1131 case 'raw_end_date':
1132 $rname = '_' . substr($name, 4);
1133 if ($this->$rname != '') {
1135 $d = new \
DateTime($this->$rname);
1137 } catch (\Exception
$e) {
1138 //oops, we've got a bad date :/
1140 'Bad date (' . $this->$rname . ') | ' .
1151 if ($this->$rname != '') {
1153 $d = new \
DateTime($this->$rname);
1154 return $d->format(__("Y-m-d"));
1155 } catch (\Exception
$e) {
1156 //oops, we've got a bad date :/
1158 'Bad date (' . $this->$rname . ') | ' .
1162 return $this->$rname;
1167 if ($this->_is_cotis
) {
1168 $date_end = new \
DateTime($this->_end_date
);
1169 $date_start = new \
DateTime($this->_begin_date
);
1170 $diff = $date_end->diff($date_start);
1171 return $diff->format('%y')*12 +
$diff->format('%m');
1176 case 'spayment_type':
1177 if ($this->_payment_type
=== null) {
1181 $ptype = new PaymentType($this->zdb
, (int)$this->payment_type
);
1182 return $ptype->getName();
1186 if ($this->_is_cotis
=== null) {
1189 return ($this->isCotis()) ?
1190 PdfModel
::INVOICE_MODEL
: PdfModel
::RECEIPT_MODEL
;
1193 return $this->$rname;
1198 "Unknown property '$rname'",
1206 * Global setter method
1208 * @param string $name name of the property we want to assign a value to
1209 * @param object $value a relevant value for the property
1213 public function __set($name, $value)
1215 global $preferences;
1217 $forbidden = array('fields', 'is_cotis', 'end_date');
1219 if (!in_array($name, $forbidden)) {
1220 $rname = '_' . $name;
1223 if (is_int($value)) {
1224 $this->$rname = new Transaction($this->zdb
, $this->login
, $value);
1227 'Trying to set a transaction from an id that is not an integer.',
1233 if (is_int($value)) {
1235 $this->$rname = new ContributionsTypes($this->zdb
, $value);
1236 //set is_cotis according to type
1237 if ($this->$rname->extension
== 1) {
1238 $this->_is_cotis
= true;
1240 $this->_is_cotis
= false;
1244 'Trying to set a type from an id that is not an integer.',
1251 $d = \DateTime
::createFromFormat(__("Y-m-d"), $value);
1253 throw new \
Exception('Incorrect format');
1255 $this->_begin_date
= $d->format('Y-m-d');
1256 } catch (\Exception
$e) {
1258 'Wrong date format. field: ' . $name .
1259 ', value: ' . $value . ', expected fmt: ' .
1260 __("Y-m-d") . ' | ' . $e->getMessage(),
1263 $this->errors
[] = str_replace(
1270 $this->_fields
['date_debut_cotis']['label']
1272 _T("- Wrong date format (%date_format) for %field!")
1277 if (is_numeric($value) && $value > 0) {
1278 $this->$rname = $value;
1281 'Trying to set an amount with a non numeric value, ' .
1282 'or with a zero value',
1288 if (is_int($value)) {
1290 $this->$rname = $value;
1293 case 'payment_type':
1294 $ptypes = new PaymentTypes(
1299 $list = $ptypes->getList();
1300 if (isset($list[$value])) {
1301 $this->_payment_type
= $value;
1304 'Unknown payment type ' . $value,
1311 '[' . __CLASS__
. ']: Trying to set an unknown property (' .