Ajout du support des écritures ventilées.
[garradin.git] / www / admin / compta / operations / modifier.php
1 <?php
2 namespace Garradin;
3
4 require_once __DIR__ . '/../_inc.php';
5
6 if ($user['droits']['compta'] < Membres::DROIT_ADMIN)
7 {
8 throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
9 }
10
11 $journal = new Compta_Journal;
12 $cats = new Compta_Categories;
13 $banques = new Compta_Comptes_Bancaires;
14
15 $operation = $journal->get(utils::get('id'));
16
17 if (!$operation)
18 {
19 throw new UserException("L'opération demandée n'existe pas.");
20 }
21
22 $debug = "_POST=".print_r($_POST, true);
23
24 if ($operation['id_categorie'])
25 {
26 $categorie = $cats->get($operation['id_categorie']);
27 }
28 else
29 {
30 $categorie = false;
31 }
32
33 if ($categorie && $categorie['type'] != Compta_Categories::AUTRES)
34 {
35 $type = $categorie['type'];
36 }
37 else
38 {
39 $type = null;
40 }
41
42 foreach ($operation['fluxs'] as $key => &$flux)
43 {
44 $compte = $comptes->get($flux['compte']);
45 $flux['compte_libelle'] = $compte['libelle'];
46 }
47 $tpl->assign('fluxs', $operation['fluxs']);
48
49 $error = false;
50
51 if (!empty($_POST['save']))
52 {
53 if (!utils::CSRF_check('compta_modifier_'.$operation['id']))
54 {
55 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
56 }
57 else
58 {
59 try
60 {
61 if (is_null($type))
62 {
63 $fluxs = [];
64 $solde = 0.0;
65 for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++)
66 {
67 $compte = $_POST['compte_'.$n];
68 if (array_key_exists('montant_'.$n, $_POST)) {
69 $montant = $_POST['montant_'.$n];
70 if ($montant == 0.0)
71 {
72 continue;
73 }
74 else
75 {
76 $solde += $montant;
77 $fluxs[] = ['compte' => $compte, 'montant' => $montant];
78 }
79 } else {
80 if ($solde != 0.0 || $n < 1) {
81 $fluxs[] = ['compte' => $compte, 'montant' => - $solde];
82 }
83 break;
84 }
85 }
86 $journal->edit($operation['id'], [
87 'libelle' => utils::post('libelle'),
88 'fluxs' => $fluxs,
89 'date' => utils::post('date'),
90 'numero_piece' => utils::post('numero_piece'),
91 'remarques' => utils::post('remarques'),
92 ]);
93 }
94 else
95 {
96 $cat = $cats->get(utils::post('id_categorie'));
97
98 if (!$cat)
99 {
100 throw new UserException('Il faut choisir une catégorie.');
101 }
102
103 if (!array_key_exists(utils::post('moyen_paiement'), $cats->listMoyensPaiement()))
104 {
105 throw new UserException('Moyen de paiement invalide.');
106 }
107
108 if (utils::post('moyen_paiement') == 'ES')
109 {
110 $a = Compta_Comptes::CAISSE;
111 $b = $cat['compte'];
112 }
113 else
114 {
115 if (!trim(utils::post('banque')))
116 {
117 throw new UserException('Le compte bancaire choisi est invalide.');
118 }
119
120 if (!array_key_exists(utils::post('banque'), $banques->getList()))
121 {
122 throw new UserException('Le compte bancaire choisi n\'existe pas.');
123 }
124
125 $a = utils::post('banque');
126 $b = $cat['compte'];
127 }
128
129 if ($type == Compta_Categories::DEPENSES)
130 {
131 $debit = $b;
132 $credit = $a;
133 }
134 elseif ($type == Compta_Categories::RECETTES)
135 {
136 $debit = $a;
137 $credit = $b;
138 }
139
140 $journal->edit($operation['id'], [
141 'libelle' => utils::post('libelle'),
142 'date' => utils::post('date'),
143 'fluxs' =>
144 [ ['compte'=>$credit, 'montant' => - utils::post('montant')]
145 , ['compte'=>$debit, 'montant' => utils::post('montant')] ],
146 'moyen_paiement'=> utils::post('moyen_paiement'),
147 'numero_cheque' => utils::post('numero_cheque'),
148 'numero_piece' => utils::post('numero_piece'),
149 'remarques' => utils::post('remarques'),
150 'id_categorie' => (int)$cat['id'],
151 ]);
152 }
153
154 utils::redirect('/admin/compta/operations/voir.php?id='.(int)$operation['id']);
155 }
156 catch (UserException $e)
157 {
158 $error = $e->getMessage();
159 }
160 }
161 }
162
163 $tpl->assign('error', $error);
164
165 $tpl->assign('type', $type);
166
167 if ($type === null)
168 {
169 $tpl->assign('comptes', $comptes->listTree());
170 }
171 else
172 {
173 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
174 $tpl->assign('categories', $cats->getList($type));
175 $tpl->assign('comptes_bancaires', $banques->getList());
176 }
177
178 $tpl->assign('debug', $debug);
179 $tpl->assign('operation', $operation);
180
181 $tpl->display('admin/compta/operations/modifier.tpl');
182
183 ?>