3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2018-2021 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 2018-2021 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
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.9.2dev - 2018-07-23
37 namespace Galette\Entity
;
42 use Galette\Features\I18n
;
43 use Galette\Features\Translatable
;
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2018-2021 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.9.2dev - 2018-07-23
63 public const TABLE
= 'paymenttypes';
64 public const PK
= 'type_id';
69 public const OTHER
= 6;
70 public const CASH
= 1;
71 public const CREDITCARD
= 2;
72 public const CHECK
= 3;
73 public const TRANSFER
= 4;
74 public const PAYPAL
= 5;
79 * @param Db $zdb Database instance
80 * @param mixed $args Arguments
82 public function __construct(Db
$zdb, $args = null)
87 } elseif ($args !== null && is_object($args)) {
88 $this->loadFromRs($args);
93 * Load a payment type from its identifier
95 * @param integer $id Identifier
99 private function load($id)
102 $select = $this->zdb
->select(self
::TABLE
);
103 $select->limit(1)->where(self
::PK
. ' = ' . $id);
105 $results = $this->zdb
->execute($select);
106 $res = $results->current();
109 $this->name
= $res->type_name
;
110 } catch (Throwable
$e) {
112 'An error occurred loading payment type #' . $id . "Message:\n" .
121 * Load payment type from a db ResultSet
123 * @param ResultSet $rs ResultSet
127 private function loadFromRs($rs)
130 $this->id
= $rs->$pk;
131 $this->name
= $rs->type_name
;
135 * Store payment type in database
139 public function store()
142 'type_name' => $this->name
145 if ($this->id
!== null && $this->id
> 0) {
146 if ($this->old_name
!== null) {
147 $this->deleteTranslation($this->old_name
);
148 $this->addTranslation($this->name
);
151 $update = $this->zdb
->update(self
::TABLE
);
152 $update->set($data)->where(
153 self
::PK
. '=' . $this->id
155 $this->zdb
->execute($update);
157 $insert = $this->zdb
->insert(self
::TABLE
);
158 $insert->values($data);
159 $add = $this->zdb
->execute($insert);
160 if (!$add->count() > 0) {
161 Analog
::log('Not stored!', Analog
::ERROR
);
165 $this->id
= $this->zdb
->getLastGeneratedValue($this);
167 $this->addTranslation($this->name
);
170 } catch (Throwable
$e) {
172 'An error occurred storing payment type: ' . $e->getMessage() .
173 "\n" . print_r($data, true),
181 * Remove current title
185 public function remove()
187 $id = (int)$this->id
;
188 if ($this->isSystemType()) {
189 throw new \
RuntimeException(_T("You cannot delete system payment types!"));
193 $delete = $this->zdb
->delete(self
::TABLE
);
195 self
::PK
. ' = ' . $id
197 $this->zdb
->execute($delete);
198 $this->deleteTranslation($this->name
);
200 'Payment type #' . $id . ' (' . $this->name
201 . ') deleted successfully.',
205 } catch (Throwable
$e) {
207 'Unable to delete payment type ' . $id . ' | ' . $e->getMessage(),
217 * @param string $name Property name
221 public function __get($name)
232 'Unable to get Title property ' . $name,
242 * @param string $name Property name
243 * @param mixed $value Property value
247 public function __set($name, $value)
251 if (trim($value) === '') {
253 'Name cannot be empty',
257 $this->old_name
= $this->name
;
258 $this->name
= $value;
263 'Unable to set property ' . $name,
271 * Get system payment types
273 * @param boolean $translated Return translated types (default) or not
277 public function getSystemTypes($translated = true)
281 self
::OTHER
=> _T("Other"),
282 self
::CASH
=> _T("Cash"),
283 self
::CREDITCARD
=> _T("Credit card"),
284 self
::CHECK
=> _T("Check"),
285 self
::TRANSFER
=> _T("Transfer"),
286 self
::PAYPAL
=> _T("Paypal")
290 self
::OTHER
=> "Other",
291 self
::CASH
=> "Cash",
292 self
::CREDITCARD
=> "Credit card",
293 self
::CHECK
=> "Check",
294 self
::TRANSFER
=> "Transfer",
295 self
::PAYPAL
=> "Paypal"
302 * Is current payment a system one
307 public function isSystemType()
309 return isset($this->getSystemTypes()[$this->id
]);
313 * Simple text representation
317 public function __toString()
319 return $this->getName();