4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
22 namespace Galette\Repository
;
24 use Laminas\Db\ResultSet\ResultSet
;
27 use Laminas\Db\Sql\Expression
;
28 use Galette\Entity\PaymentType
;
33 * @author Johan Cwiklinski <johan@x-tnd.be>
35 class PaymentTypes
extends Repository
40 * @param boolean $schedulable Types that can be used in schedules only
42 * @return array<int, PaymentType>
44 public static function getAll(bool $schedulable = true): array
46 global $zdb, $preferences, $login;
47 $ptypes = new self($zdb, $preferences, $login);
48 return $ptypes->getList($schedulable);
54 * @param boolean $schedulable Types that can be used in schedules only
56 * @return array<int, PaymentType>|ResultSet
58 public function getList(bool $schedulable = true): array|ResultSet
61 $select = $this->zdb
->select(PaymentType
::TABLE
, 'a');
62 $select->order(PaymentType
::PK
);
64 if ($schedulable === false) {
65 $select->where
->notEqualTo('a.' . PaymentType
::PK
, PaymentType
::SCHEDULED
);
69 $results = $this->zdb
->execute($select);
70 foreach ($results as $row) {
71 $types[$row->type_id
] = new PaymentType($this->zdb
, $row);
74 } catch (Throwable
$e) {
76 'Cannot list payment types | ' . $e->getMessage(),
84 * Add default payment types in database
86 * @param boolean $check_first Check first if it seems initialized
90 public function installInit(bool $check_first = true): bool
94 //first of all, let's check if data seem to have already
97 if ($check_first === true) {
98 $select = $this->zdb
->select(PaymentType
::TABLE
);
101 'counter' => new Expression('COUNT(' . $ent::PK
. ')')
105 $results = $this->zdb
->execute($select);
106 $result = $results->current();
107 $count = $result->counter
;
109 //if we got no values in table, let's proceed
112 if ($count < count($this->defaults
)) {
113 return $this->checkUpdate();
121 if ($proceed === true) {
122 $this->zdb
->connection
->beginTransaction();
124 //first, we drop all values
125 $delete = $this->zdb
->delete($ent::TABLE
);
126 $this->zdb
->execute($delete);
128 $this->zdb
->handleSequence(
130 count($this->defaults
)
132 $this->insert($ent::TABLE
, $this->defaults
);
134 $this->zdb
->connection
->commit();
137 } catch (Throwable
$e) {
138 if ($this->zdb
->connection
->inTransaction()) {
139 $this->zdb
->connection
->rollBack();
146 * Checks for missing payment types in the database
150 protected function checkUpdate(): bool
153 $ent = $this->entity
;
154 $select = $this->zdb
->select($ent::TABLE
);
155 $list = $this->zdb
->execute($select);
159 foreach ($this->defaults
as $key => $value) {
161 foreach ($list as $type) {
162 if ($type->type_id
== $key) {
168 if ($exists === false) {
169 //model does not exists in database, insert it.
170 $missing[$key] = $value;
174 if (count($missing) > 0) {
175 $this->zdb
->connection
->beginTransaction();
176 $this->insert($ent::TABLE
, $missing);
178 'Missing payment types were successfully stored into database.',
181 $this->zdb
->connection
->commit();
184 } catch (Throwable
$e) {
185 if ($this->zdb
->connection
->inTransaction()) {
186 $this->zdb
->connection
->rollBack();
194 * Insert values in database
196 * @param string $table Table name
197 * @param array<string,mixed> $values Values to insert
201 private function insert(string $table, array $values): void
203 $insert = $this->zdb
->insert($table);
206 'type_id' => ':type_id',
207 'type_name' => ':type_name'
210 $stmt = $this->zdb
->sql
->prepareStatementForSqlObject($insert);
212 foreach ($values as $k => $v) {
217 $stmt->execute($value);
222 * Get defaults values
224 * @return array<string, mixed>
226 protected function loadDefaults(): array
228 if (!count($this->defaults
)) {
229 $paytype = new PaymentType($this->zdb
);
230 $this->defaults
= $paytype->getSystemTypes(false);
232 return parent
::loadDefaults();