660b879875fbd4a4f032c73115d8eb7493c1aaa0
[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 if ($operation['id_categorie'])
23 {
24 $categorie = $cats->get($operation['id_categorie']);
25 }
26 else
27 {
28 $categorie = false;
29 }
30
31 if ($categorie && $categorie['type'] != Compta_Categories::AUTRES)
32 {
33 $type = $categorie['type'];
34 }
35 else
36 {
37 $type = null;
38 }
39
40 $error = false;
41
42 if (!empty($_POST['save']))
43 {
44 if (!utils::CSRF_check('compta_modifier_'.$operation['id']))
45 {
46 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
47 }
48 else
49 {
50 try
51 {
52 if (is_null($type))
53 {
54 $journal->edit($operation['id'], [
55 'libelle' => utils::post('libelle'),
56 'montant' => utils::post('montant'),
57 'date' => utils::post('date'),
58 'compte_credit' => utils::post('compte_credit'),
59 'compte_debit' => utils::post('compte_debit'),
60 'numero_piece' => utils::post('numero_piece'),
61 'remarques' => utils::post('remarques'),
62 ]);
63 }
64 else
65 {
66 $cat = $cats->get(utils::post('id_categorie'));
67
68 if (!$cat)
69 {
70 throw new UserException('Il faut choisir une catégorie.');
71 }
72
73 if (!array_key_exists(utils::post('moyen_paiement'), $cats->listMoyensPaiement()))
74 {
75 throw new UserException('Moyen de paiement invalide.');
76 }
77
78 if (utils::post('moyen_paiement') == 'ES')
79 {
80 $a = Compta_Comptes::CAISSE;
81 $b = $cat['compte'];
82 }
83 else
84 {
85 if (!trim(utils::post('banque')))
86 {
87 throw new UserException('Le compte bancaire choisi est invalide.');
88 }
89
90 if (!array_key_exists(utils::post('banque'), $banques->getList()))
91 {
92 throw new UserException('Le compte bancaire choisi n\'existe pas.');
93 }
94
95 $a = utils::post('banque');
96 $b = $cat['compte'];
97 }
98
99 if ($type == Compta_Categories::DEPENSES)
100 {
101 $debit = $b;
102 $credit = $a;
103 }
104 elseif ($type == Compta_Categories::RECETTES)
105 {
106 $debit = $a;
107 $credit = $b;
108 }
109
110 $journal->edit($operation['id'], [
111 'libelle' => utils::post('libelle'),
112 'montant' => utils::post('montant'),
113 'date' => utils::post('date'),
114 'moyen_paiement'=> utils::post('moyen_paiement'),
115 'numero_cheque' => utils::post('numero_cheque'),
116 'compte_credit' => $credit,
117 'compte_debit' => $debit,
118 'numero_piece' => utils::post('numero_piece'),
119 'remarques' => utils::post('remarques'),
120 'id_categorie' => (int)$cat['id'],
121 ]);
122 }
123
124 utils::redirect('/admin/compta/operations/voir.php?id='.(int)$operation['id']);
125 }
126 catch (UserException $e)
127 {
128 $error = $e->getMessage();
129 }
130 }
131 }
132
133 $tpl->assign('error', $error);
134
135 $tpl->assign('type', $type);
136
137 if ($type === null)
138 {
139 $tpl->assign('comptes', $comptes->listTree());
140 }
141 else
142 {
143 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
144 $tpl->assign('categories', $cats->getList($type));
145 $tpl->assign('comptes_bancaires', $banques->getList());
146 }
147
148 $tpl->assign('operation', $operation);
149
150 $tpl->display('admin/compta/operations/modifier.tpl');
151
152 ?>