4 require_once __DIR__
. '/../_inc.php';
6 if ($user['droits']['compta'] < Membres
::DROIT_ADMIN
)
8 throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
11 $journal = new Compta_Journal
;
12 $cats = new Compta_Categories
;
13 $banques = new Compta_Comptes_Bancaires
;
15 $operation = $journal->get(utils
::get('id'));
19 throw new UserException("L'opération demandée n'existe pas.");
22 $debug = "_POST=".print_r($_POST, true);
24 if ($operation['id_categorie'])
26 $categorie = $cats->get($operation['id_categorie']);
33 if ($categorie && $categorie['type'] != Compta_Categories
::AUTRES
)
35 $type = $categorie['type'];
42 foreach ($operation['fluxs'] as $key => &$flux)
44 $compte = $comptes->get($flux['compte']);
45 $flux['compte_libelle'] = $compte['libelle'];
47 $tpl->assign('fluxs', $operation['fluxs']);
51 if (!empty($_POST['save']))
53 if (!utils
::CSRF_check('compta_modifier_'.$operation['id']))
55 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
65 for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++
)
67 $compte = $_POST['compte_'.$n];
68 if (array_key_exists('montant_'.$n, $_POST)) {
69 $montant = $_POST['montant_'.$n];
77 $fluxs[] = ['compte' => $compte, 'montant' => $montant];
80 if ($solde != 0.0 ||
$n < 1) {
81 $fluxs[] = ['compte' => $compte, 'montant' => - $solde];
86 $journal->edit($operation['id'], [
87 'libelle' => utils
::post('libelle'),
89 'date' => utils
::post('date'),
90 'numero_piece' => utils
::post('numero_piece'),
91 'remarques' => utils
::post('remarques'),
96 $cat = $cats->get(utils
::post('id_categorie'));
100 throw new UserException('Il faut choisir une catégorie.');
103 if (!array_key_exists(utils
::post('moyen_paiement'), $cats->listMoyensPaiement()))
105 throw new UserException('Moyen de paiement invalide.');
108 if (utils
::post('moyen_paiement') == 'ES')
110 $a = Compta_Comptes
::CAISSE
;
115 if (!trim(utils
::post('banque')))
117 throw new UserException('Le compte bancaire choisi est invalide.');
120 if (!array_key_exists(utils
::post('banque'), $banques->getList()))
122 throw new UserException('Le compte bancaire choisi n\'existe pas.');
125 $a = utils
::post('banque');
129 if ($type == Compta_Categories
::DEPENSES
)
134 elseif ($type == Compta_Categories
::RECETTES
)
140 $journal->edit($operation['id'], [
141 'libelle' => utils
::post('libelle'),
142 'date' => utils
::post('date'),
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'],
154 utils
::redirect('/admin/compta/operations/voir.php?id='.(int)$operation['id']);
156 catch (UserException
$e)
158 $error = $e->getMessage();
163 $tpl->assign('error', $error);
165 $tpl->assign('type', $type);
169 $tpl->assign('comptes', $comptes->listTree());
173 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
174 $tpl->assign('categories', $cats->getList($type));
175 $tpl->assign('comptes_bancaires', $banques->getList());
178 $tpl->assign('debug', $debug);
179 $tpl->assign('operation', $operation);
181 $tpl->display('admin/compta/operations/modifier.tpl');