Ajout : ./garradin
[garradin.git] / www / admin / compta / operations / saisir.php
1 <?php
2 namespace Garradin;
3
4 require_once __DIR__ . '/../_inc.php';
5
6 if ($user['droits']['compta'] < Membres::DROIT_ECRITURE)
7 {
8 throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
9 }
10
11 $journal = new Compta_Journal;
12
13 $journal->checkExercice();
14
15 $cats = new Compta_Categories;
16 $banques = new Compta_Comptes_Bancaires;
17
18 if (isset($_GET['depense']))
19 $type = Compta_Categories::DEPENSES;
20 elseif (isset($_GET['virement']))
21 $type = 'virement';
22 elseif (isset($_GET['dette']))
23 $type = 'dette';
24 elseif (isset($_GET['avance']))
25 $type = null;
26 else
27 $type = Compta_Categories::RECETTES;
28
29 $error = false;
30 $debug = "_POST=".print_r($_POST, true);
31
32 if (!empty($_POST['save']))
33 {
34 if (!utils::CSRF_check('compta_saisie'))
35 {
36 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
37 }
38 else
39 {
40 try
41 {
42 if (is_null($type))
43 {
44 $fluxs = [];
45 $solde = 0.0;
46 for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++)
47 {
48 $compte = $_POST['compte_'.$n];
49 if (array_key_exists('montant_'.$n, $_POST)) {
50 $montant = $_POST['montant_'.$n];
51 if ($montant == 0.0)
52 {
53 continue;
54 }
55 else
56 {
57 $solde += $montant;
58 $fluxs[] = ['compte' => $compte, 'montant' => $montant];
59 }
60 } else {
61 if ($solde != 0.0) {
62 $fluxs[] = ['compte' => $compte, 'montant' => - $solde];
63 }
64 break;
65 }
66 }
67 $debug .= "<br/>fluxs = ".print_r($fluxs, true);
68 $id = $journal->add([
69 'libelle' => utils::post('libelle'),
70 'date' => utils::post('date'),
71 'fluxs' => $fluxs,
72 'numero_piece' => utils::post('numero_piece'),
73 'remarques' => utils::post('remarques'),
74 'id_auteur' => $user['id'],
75 ]);
76 }
77 elseif ($type === 'virement')
78 {
79 $id = $journal->add([
80 'libelle' => utils::post('libelle'),
81 'date' => utils::post('date'),
82 'fluxs' =>
83 [ ['compte'=>utils::post('compte1'), 'montant' => - utils::post('montant')]
84 , ['compte'=>utils::post('compte2'), 'montant' => utils::post('montant')] ],
85 'numero_piece' => utils::post('numero_piece'),
86 'remarques' => utils::post('remarques'),
87 'id_auteur' => $user['id'],
88 ]);
89 }
90 else
91 {
92 $cat = $cats->get(utils::post('categorie'));
93
94 if (!$cat)
95 {
96 throw new UserException('Il faut choisir une catégorie.');
97 }
98
99 if ($type == 'dette')
100 {
101 if (!trim(utils::post('compte')) ||
102 (utils::post('compte') != 4010 && utils::post('compte') != 4110))
103 {
104 throw new UserException('Type de dette invalide.');
105 }
106 }
107 else
108 {
109 if (utils::post('moyen_paiement') == 'ES')
110 {
111 $a = Compta_Comptes::CAISSE;
112 $b = $cat['compte'];
113 }
114 else
115 {
116 if (!trim(utils::post('banque')))
117 {
118 throw new UserException('Le compte bancaire choisi est invalide.');
119 }
120
121 if (!array_key_exists(utils::post('banque'), $banques->getList()))
122 {
123 throw new UserException('Le compte bancaire choisi n\'existe pas.');
124 }
125
126 $a = utils::post('banque');
127 $b = $cat['compte'];
128 }
129 }
130
131 if ($type === Compta_Categories::DEPENSES)
132 {
133 $debit = $b;
134 $credit = $a;
135 }
136 elseif ($type === Compta_Categories::RECETTES)
137 {
138 $debit = $a;
139 $credit = $b;
140 }
141 elseif ($type === 'dette')
142 {
143 $debit = $cat['compte'];
144 $credit = utils::post('compte');
145 }
146
147 $id = $journal->add([
148 'libelle' => utils::post('libelle'),
149 'date' => utils::post('date'),
150 'fluxs' =>
151 [ ['compte'=>$credit, 'montant' => - utils::post('montant')]
152 , ['compte'=>$debit, 'montant' => utils::post('montant')] ],
153 'moyen_paiement'=> ($type === 'dette') ? null : utils::post('moyen_paiement'),
154 'numero_cheque' => ($type === 'dette') ? null : utils::post('numero_cheque'),
155 'numero_piece' => utils::post('numero_piece'),
156 'remarques' => utils::post('remarques'),
157 'id_categorie' => ($type === 'dette') ? null : (int)$cat['id'],
158 'id_auteur' => $user['id'],
159 ]);
160 }
161
162 $membres->sessionStore('compta_date', utils::post('date'));
163
164 if ($type == Compta_Categories::DEPENSES)
165 $type = 'depense';
166 elseif (is_null($type))
167 $type = 'avance';
168 elseif ($type == Compta_Categories::RECETTES)
169 $type = 'recette';
170
171 utils::redirect('/admin/compta/operations/saisir.php?'.$type.'&ok='.(int)$id);
172 }
173 catch (UserException $e)
174 {
175 $error = $e->getMessage();
176 }
177 }
178 }
179
180 $tpl->assign('error', $error);
181 $tpl->assign('debug', $debug."END");
182 $tpl->assign('type', $type);
183
184 if (is_null($type))
185 {
186 $tpl->assign('comptes', $comptes->listTree());
187 $tpl->assign('fluxs',
188 [ 0 => ['compte' => '', 'montant' => 0.0]
189 , 1 => ['compte' => '', 'montant' => 0.0]
190 ]
191 );
192 }
193 else
194 {
195 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
196 $tpl->assign('moyen_paiement', utils::post('moyen_paiement') ?: 'ES');
197 $tpl->assign('categories', $cats->getList($type === 'dette' ? Compta_Categories::DEPENSES : $type));
198 $tpl->assign('comptes_bancaires', $banques->getList());
199 $tpl->assign('banque', utils::post('banque'));
200 }
201
202 if (!$membres->sessionGet('compta_date'))
203 {
204 $exercices = new Compta_Exercices;
205 $exercice = $exercices->getCurrent();
206
207 if ($exercice['debut'] > time() || $exercice['fin'] < time())
208 {
209 $membres->sessionStore('compta_date', date('Y-m-d', $exercice['debut']));
210 }
211 else
212 {
213 $membres->sessionStore('compta_date', date('Y-m-d'));
214 }
215 }
216
217 $tpl->assign('date', $membres->sessionGet('compta_date') ?: false);
218 $tpl->assign('ok', (int) utils::get('ok'));
219
220 $tpl->display('admin/compta/operations/saisir.tpl');
221
222 ?>