1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
|
-
+
+
-
-
-
-
-
-
-
-
-
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
+
-
+
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
<?php
namespace Garradin\Compta;
namespace Garradin\Entities\Compta;
use Garradin\Entity;
use Garradin\ValidationException;
use Garradin\DB;
use Garradin\Config;
class Mouvement extends Entity
{
const TABLE = 'compta_mouvements';
protected $id;
protected $libelle;
protected $remarques;
protected $numero_piece;
protected $date;
protected $moyen_paiement;
protected $reference_paiement;
protected $validation;
protected $hash;
protected $prev_hash;
protected $id_exercice;
protected $id_auteur;
protected $id_categorie;
protected $id_projet;
protected $_types = [
'libelle' => 'string',
'remarques' => '?string',
'numero_piece' => '?string',
'date' => 'date',
'moyen_paiement' => '?string',
'validation' => 'bool',
'hash' => '?string',
'prev_hash' => '?string',
'id_exercice' => '?int',
'id_auteur' => '?int',
'id_categorie' => '?int',
'id_projet' => '?int',
'libelle' => 'string',
'remarques' => '?string',
'numero_piece' => '?string',
'date' => 'date',
'moyen_paiement' => '?string',
'reference_paiement' => '?string',
'validation' => 'bool',
'hash' => '?string',
'prev_hash' => '?string',
'id_exercice' => '?int',
'id_auteur' => '?int',
'id_categorie' => '?int',
'id_projet' => '?int',
];
protected $_validation_rules = [
'libelle' => 'required|string',
'libelle' => 'required|string|max:200',
'remarques' => 'string|max:20000',
'numero_piece' => 'string|max:200',
'date' => 'required|date',
'moyen_paiement' => 'string|in_table:compta_moyens_paiement,code|required_with:id_categorie',
'moyen_paiement' => 'string|min:2|max:2|in_table:compta_moyens_paiement,code|required_with:id_categorie',
'reference_paiement' => 'string|max:200',
'validation' => 'bool',
'id_exercice' => 'integer|in_table:compta_exercices,id',
'id_auteur' => 'integer|in_table:membres,id',
'id_categorie' => 'integer|in_table:compta_categories,id',
'id_projet' => 'integer|in_table:compta_projets,id'
];
protected $lignes = [];
protected $lignes;
public function getLignes()
{
if (null === $this->lignes && $this->exists()) {
$db = DB::getInstance();
return $db->toObject($db->get('SELECT * FROM compta_mouvements_lignes WHERE id_mouvement = ? ORDER BY id;', $this->id), Ligne::class);
}
$db = DB::getInstance();
$this->lignes = $db->toObject($db->get('SELECT * FROM compta_mouvements_lignes WHERE id_mouvement = ? ORDER BY id;', $this->id), Ligne::class);
}
else {
$this->lignes = [];
}
return $this->lignes;
}
/*
public function getHash()
{
if (!$this->id_exercice) {
throw new \LogicException('Il n\'est pas possible de hasher un mouvement qui n\'est pas associé à un exercice');
}
static $keep_keys = [
'libelle',
'remarques',
'numero_piece',
'date',
'moyen_paiement',
'reference_paiement',
'validation',
'prev_hash',
];
$hash = hash_init('sha256');
$values = $this->asArray();
$values = array_intersect_key($values, $keep_keys);
hash_update($hash, implode(',', array_keys($values)));
hash_update($hash, implode(',', $values));
foreach ($this->getLignes() as $ligne) {
hash_update($hash, implode(',', [$ligne->compte, $ligne->debit, $ligne->credit]));
}
return hash_final($hash, false);
}
public function checkHash()
{
return hash_equals($this->getHash(), $this->hash);
}
*/
public function add(Ligne $ligne)
{
$this->lignes[] = $ligne;
}
public function simple($montant, $moyen, $compte)
|
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
|
-
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
-
+
-
-
-
-
+
+
-
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
$from = $compte;
$to = $categorie->compte;
}
return $this->transfer($montant, $from, $to);
}
public function transfer($amount, $from, $to)
public function transfer(int $amount, int $from, int $to)
{
$ligne1 = new Ligne;
$ligne1->compte = $from;
$ligne1->debit = $amount;
$ligne1->credit = 0;
$ligne2 = new Ligne;
$ligne1->compte = $to;
$ligne1->debit = 0;
$ligne1->credit = $amount;
return $this->add($ligne1) && $this->add($ligne2);
}
public function save()
{
if (!parent::save())
{
if ($this->validation && !isset($this->_modified['validation'])) {
throw new ValidationException('Il n\'est pas possible de modifier un mouvement qui a été validé');
}
if (!parent::save()) {
return false;
}
foreach ($this->lignes as $ligne)
{
$ligne->id_mouvement = $this->id;
$ligne->save();
}
}
public function delete()
{
if ($this->validation) {
throw new ValidationException('Il n\'est pas possible de supprimer un mouvement qui a été validé');
}
parent::delete();
}
public function filterUserEntry($key, $value)
public function filterUserValue($key, $value)
{
$value = parent::filterUserEntry($key, $value);
$value = parent::filterUserValue($key, $value);
if ($key == 'moyen_paiement')
{
$value = strtoupper($value);
}
elseif ($key == 'date' && !is_object($value))
{
$value = new \DateTimeImmutable($value);
}
return $value;
}
public function selfCheck()
{
parent::selfCheck();
$db = DB::getInstance();
$config = Config::getInstance();
// ID d'exercice obligatoire s'il existe déjà des exercices
if (null === $this->id_exercice && $db->firstColumn('SELECT 1 FROM compta_exercices LIMIT 1;'))
{
throw new ValidationException('Aucun exercice spécifié.');
}
if (null !== $this->id_exercice
&& !$db->test('compta_exercices', 'id = ? AND debut <= ? AND fin >= ?;', $this->id_exercice, $this->date, $this->date))
{
throw new ValidationException('La date ne correspond pas à l\'exercice sélectionné.');
}
}
}
$total = 0;
$lignes = $this->getLignes();
foreach ($lignes as $ligne) {
$total += $ligne->credit;
$total -= $ligne->debit;
}
if (0 !== $total) {
throw new ValidationException('Mouvement non équilibré : déséquilibre entre débits et crédits');
}
}
}
|