]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/PaymentType.php
5d0d728643a783bde2c5554e32b918f4bf1ee325
[galette.git] / galette / lib / Galette / Entity / PaymentType.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Payment type
7 *
8 * PHP version 5
9 *
10 * Copyright © 2018-2021 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Entity
28 * @package Galette
29 *
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
35 */
36
37 namespace Galette\Entity;
38
39 use Throwable;
40 use Galette\Core\Db;
41 use Analog\Analog;
42 use Galette\Features\I18n;
43 use Galette\Features\Translatable;
44
45 /**
46 * Payment type
47 *
48 * @category Entity
49 * @name PaymentType
50 * @package Galette
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
56 */
57
58 class PaymentType
59 {
60 use Translatable;
61 use I18n;
62
63 public const TABLE = 'paymenttypes';
64 public const PK = 'type_id';
65
66 private $zdb;
67 private $id;
68
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;
75
76 /**
77 * Main constructor
78 *
79 * @param Db $zdb Database instance
80 * @param mixed $args Arguments
81 */
82 public function __construct(Db $zdb, $args = null)
83 {
84 $this->zdb = $zdb;
85 if (is_int($args)) {
86 $this->load($args);
87 } elseif ($args !== null && is_object($args)) {
88 $this->loadFromRs($args);
89 }
90 }
91
92 /**
93 * Load a payment type from its identifier
94 *
95 * @param integer $id Identifier
96 *
97 * @return void
98 */
99 private function load($id)
100 {
101 try {
102 $select = $this->zdb->select(self::TABLE);
103 $select->limit(1)->where(self::PK . ' = ' . $id);
104
105 $results = $this->zdb->execute($select);
106 $res = $results->current();
107
108 $this->id = $id;
109 $this->name = $res->type_name;
110 } catch (Throwable $e) {
111 Analog::log(
112 'An error occurred loading payment type #' . $id . "Message:\n" .
113 $e->getMessage(),
114 Analog::ERROR
115 );
116 throw $e;
117 }
118 }
119
120 /**
121 * Load payment type from a db ResultSet
122 *
123 * @param ResultSet $rs ResultSet
124 *
125 * @return void
126 */
127 private function loadFromRs($rs)
128 {
129 $pk = self::PK;
130 $this->id = $rs->$pk;
131 $this->name = $rs->type_name;
132 }
133
134 /**
135 * Store payment type in database
136 *
137 * @return boolean
138 */
139 public function store()
140 {
141 $data = array(
142 'type_name' => $this->name
143 );
144 try {
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);
149 }
150
151 $update = $this->zdb->update(self::TABLE);
152 $update->set($data)->where(
153 self::PK . '=' . $this->id
154 );
155 $this->zdb->execute($update);
156 } else {
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);
162 return false;
163 }
164
165 $this->id = $this->zdb->getLastGeneratedValue($this);
166
167 $this->addTranslation($this->name);
168 }
169 return true;
170 } catch (Throwable $e) {
171 Analog::log(
172 'An error occurred storing payment type: ' . $e->getMessage() .
173 "\n" . print_r($data, true),
174 Analog::ERROR
175 );
176 throw $e;
177 }
178 }
179
180 /**
181 * Remove current title
182 *
183 * @return boolean
184 */
185 public function remove()
186 {
187 $id = (int)$this->id;
188 if ($this->isSystemType()) {
189 throw new \RuntimeException(_T("You cannot delete system payment types!"));
190 }
191
192 try {
193 $delete = $this->zdb->delete(self::TABLE);
194 $delete->where(
195 self::PK . ' = ' . $id
196 );
197 $this->zdb->execute($delete);
198 $this->deleteTranslation($this->name);
199 Analog::log(
200 'Payment type #' . $id . ' (' . $this->name
201 . ') deleted successfully.',
202 Analog::INFO
203 );
204 return true;
205 } catch (Throwable $e) {
206 Analog::log(
207 'Unable to delete payment type ' . $id . ' | ' . $e->getMessage(),
208 Analog::ERROR
209 );
210 throw $e;
211 }
212 }
213
214 /**
215 * Getter
216 *
217 * @param string $name Property name
218 *
219 * @return mixed
220 */
221 public function __get($name)
222 {
223 global $lang;
224
225 switch ($name) {
226 case 'id':
227 case 'name':
228 return $this->$name;
229 break;
230 default:
231 Analog::log(
232 'Unable to get Title property ' . $name,
233 Analog::WARNING
234 );
235 break;
236 }
237 }
238
239 /**
240 * Setter
241 *
242 * @param string $name Property name
243 * @param mixed $value Property value
244 *
245 * @return void
246 */
247 public function __set($name, $value)
248 {
249 switch ($name) {
250 case 'name':
251 if (trim($value) === '') {
252 Analog::log(
253 'Name cannot be empty',
254 Analog::WARNING
255 );
256 } else {
257 $this->old_name = $this->name;
258 $this->name = $value;
259 }
260 break;
261 default:
262 Analog::log(
263 'Unable to set property ' . $name,
264 Analog::WARNING
265 );
266 break;
267 }
268 }
269
270 /**
271 * Get system payment types
272 *
273 * @param boolean $translated Return translated types (default) or not
274 *
275 * @return array
276 */
277 public function getSystemTypes($translated = true)
278 {
279 if ($translated) {
280 $systypes = [
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")
287 ];
288 } else {
289 $systypes = [
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"
296 ];
297 }
298 return $systypes;
299 }
300
301 /**
302 * Is current payment a system one
303 *
304 * @return boolean
305 *
306 */
307 public function isSystemType()
308 {
309 return isset($this->getSystemTypes()[$this->id]);
310 }
311
312 /**
313 * Simple text representation
314 *
315 * @return string
316 */
317 public function __toString()
318 {
319 return $this->getName();
320 }
321 }