4 require_once __DIR__
. '/../_inc.php';
6 if ($user['droits']['compta'] < Membres
::DROIT_ECRITURE
)
8 throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
11 $journal = new Compta_Journal
;
13 $journal->checkExercice();
15 $cats = new Compta_Categories
;
16 $banques = new Compta_Comptes_Bancaires
;
18 if (isset($_GET['depense']))
19 $type = Compta_Categories
::DEPENSES
;
20 elseif (isset($_GET['virement']))
22 elseif (isset($_GET['dette']))
24 elseif (isset($_GET['avance']))
27 $type = Compta_Categories
::RECETTES
;
30 $debug = "_POST=".print_r($_POST, true);
32 if (!empty($_POST['save']))
34 if (!utils
::CSRF_check('compta_saisie'))
36 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
46 for ($n=0; array_key_exists('compte_'.$n, $_POST); $n++
)
48 $compte = $_POST['compte_'.$n];
49 if (array_key_exists('montant_'.$n, $_POST)) {
50 $montant = $_POST['montant_'.$n];
58 $fluxs[] = ['compte' => $compte, 'montant' => $montant];
62 $fluxs[] = ['compte' => $compte, 'montant' => - $solde];
67 $debug .= "<br/>fluxs = ".print_r($fluxs, true);
69 'libelle' => utils
::post('libelle'),
70 'date' => utils
::post('date'),
72 'numero_piece' => utils
::post('numero_piece'),
73 'remarques' => utils
::post('remarques'),
74 'id_auteur' => $user['id'],
77 elseif ($type === 'virement')
80 'libelle' => utils
::post('libelle'),
81 'date' => utils
::post('date'),
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'],
92 $cat = $cats->get(utils
::post('categorie'));
96 throw new UserException('Il faut choisir une catégorie.');
101 if (!trim(utils
::post('compte')) ||
102 (utils
::post('compte') != 4010 && utils
::post('compte') != 4110))
104 throw new UserException('Type de dette invalide.');
109 if (utils
::post('moyen_paiement') == 'ES')
111 $a = Compta_Comptes
::CAISSE
;
116 if (!trim(utils
::post('banque')))
118 throw new UserException('Le compte bancaire choisi est invalide.');
121 if (!array_key_exists(utils
::post('banque'), $banques->getList()))
123 throw new UserException('Le compte bancaire choisi n\'existe pas.');
126 $a = utils
::post('banque');
131 if ($type === Compta_Categories
::DEPENSES
)
136 elseif ($type === Compta_Categories
::RECETTES
)
141 elseif ($type === 'dette')
143 $debit = $cat['compte'];
144 $credit = utils
::post('compte');
147 $id = $journal->add([
148 'libelle' => utils
::post('libelle'),
149 'date' => utils
::post('date'),
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'],
162 $membres->sessionStore('compta_date', utils
::post('date'));
164 if ($type == Compta_Categories
::DEPENSES
)
166 elseif (is_null($type))
168 elseif ($type == Compta_Categories
::RECETTES
)
171 utils
::redirect('/admin/compta/operations/saisir.php?'.$type.'&ok='.(int)$id);
173 catch (UserException
$e)
175 $error = $e->getMessage();
180 $tpl->assign('error', $error);
181 $tpl->assign('debug', $debug."END");
182 $tpl->assign('type', $type);
186 $tpl->assign('comptes', $comptes->listTree());
187 $tpl->assign('fluxs',
188 [ 0 => ['compte' => '', 'montant' => 0.0]
189 , 1 => ['compte' => '', 'montant' => 0.0]
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'));
202 if (!$membres->sessionGet('compta_date'))
204 $exercices = new Compta_Exercices
;
205 $exercice = $exercices->getCurrent();
207 if ($exercice['debut'] > time() ||
$exercice['fin'] < time())
209 $membres->sessionStore('compta_date', date('Y-m-d', $exercice['debut']));
213 $membres->sessionStore('compta_date', date('Y-m-d'));
217 $tpl->assign('date', $membres->sessionGet('compta_date') ?
: false);
218 $tpl->assign('ok', (int) utils
::get('ok'));
220 $tpl->display('admin/compta/operations/saisir.tpl');