init
[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
31 if (!empty($_POST['save']))
32 {
33 if (!utils::CSRF_check('compta_saisie'))
34 {
35 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
36 }
37 else
38 {
39 try
40 {
41 if (is_null($type))
42 {
43 $id = $journal->add([
44 'libelle' => utils::post('libelle'),
45 'montant' => utils::post('montant'),
46 'date' => utils::post('date'),
47 'compte_credit' => utils::post('compte_credit'),
48 'compte_debit' => utils::post('compte_debit'),
49 'numero_piece' => utils::post('numero_piece'),
50 'remarques' => utils::post('remarques'),
51 'id_auteur' => $user['id'],
52 ]);
53 }
54 elseif ($type === 'virement')
55 {
56 $id = $journal->add([
57 'libelle' => utils::post('libelle'),
58 'montant' => utils::post('montant'),
59 'date' => utils::post('date'),
60 'compte_credit' => utils::post('compte1'),
61 'compte_debit' => utils::post('compte2'),
62 'numero_piece' => utils::post('numero_piece'),
63 'remarques' => utils::post('remarques'),
64 'id_auteur' => $user['id'],
65 ]);
66 }
67 else
68 {
69 $cat = $cats->get(utils::post('categorie'));
70
71 if (!$cat)
72 {
73 throw new UserException('Il faut choisir une catégorie.');
74 }
75
76 if ($type == 'dette')
77 {
78 if (!trim(utils::post('compte')) ||
79 (utils::post('compte') != 4010 && utils::post('compte') != 4110))
80 {
81 throw new UserException('Type de dette invalide.');
82 }
83 }
84 else
85 {
86 if (utils::post('moyen_paiement') == 'ES')
87 {
88 $a = Compta_Comptes::CAISSE;
89 $b = $cat['compte'];
90 }
91 else
92 {
93 if (!trim(utils::post('banque')))
94 {
95 throw new UserException('Le compte bancaire choisi est invalide.');
96 }
97
98 if (!array_key_exists(utils::post('banque'), $banques->getList()))
99 {
100 throw new UserException('Le compte bancaire choisi n\'existe pas.');
101 }
102
103 $a = utils::post('banque');
104 $b = $cat['compte'];
105 }
106 }
107
108 if ($type === Compta_Categories::DEPENSES)
109 {
110 $debit = $b;
111 $credit = $a;
112 }
113 elseif ($type === Compta_Categories::RECETTES)
114 {
115 $debit = $a;
116 $credit = $b;
117 }
118 elseif ($type === 'dette')
119 {
120 $debit = $cat['compte'];
121 $credit = utils::post('compte');
122 }
123
124 $id = $journal->add([
125 'libelle' => utils::post('libelle'),
126 'montant' => utils::post('montant'),
127 'date' => utils::post('date'),
128 'moyen_paiement'=> ($type === 'dette') ? null : utils::post('moyen_paiement'),
129 'numero_cheque' => ($type === 'dette') ? null : utils::post('numero_cheque'),
130 'compte_credit' => $credit,
131 'compte_debit' => $debit,
132 'numero_piece' => utils::post('numero_piece'),
133 'remarques' => utils::post('remarques'),
134 'id_categorie' => ($type === 'dette') ? null : (int)$cat['id'],
135 'id_auteur' => $user['id'],
136 ]);
137 }
138
139 $membres->sessionStore('compta_date', utils::post('date'));
140
141 if ($type == Compta_Categories::DEPENSES)
142 $type = 'depense';
143 elseif (is_null($type))
144 $type = 'avance';
145 elseif ($type == Compta_Categories::RECETTES)
146 $type = 'recette';
147
148 utils::redirect('/admin/compta/operations/saisir.php?'.$type.'&ok='.(int)$id);
149 }
150 catch (UserException $e)
151 {
152 $error = $e->getMessage();
153 }
154 }
155 }
156
157 $tpl->assign('error', $error);
158
159 $tpl->assign('type', $type);
160
161 if ($type === null)
162 {
163 $tpl->assign('comptes', $comptes->listTree());
164 }
165 else
166 {
167 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
168 $tpl->assign('moyen_paiement', utils::post('moyen_paiement') ?: 'ES');
169 $tpl->assign('categories', $cats->getList($type === 'dette' ? Compta_Categories::DEPENSES : $type));
170 $tpl->assign('comptes_bancaires', $banques->getList());
171 $tpl->assign('banque', utils::post('banque'));
172 }
173
174 if (!$membres->sessionGet('compta_date'))
175 {
176 $exercices = new Compta_Exercices;
177 $exercice = $exercices->getCurrent();
178
179 if ($exercice['debut'] > time() || $exercice['fin'] < time())
180 {
181 $membres->sessionStore('compta_date', date('Y-m-d', $exercice['debut']));
182 }
183 else
184 {
185 $membres->sessionStore('compta_date', date('Y-m-d'));
186 }
187 }
188
189 $tpl->assign('date', $membres->sessionGet('compta_date') ?: false);
190 $tpl->assign('ok', (int) utils::get('ok'));
191
192 $tpl->display('admin/compta/operations/saisir.tpl');
193
194 ?>