init
[garradin.git] / www / admin / membres / cotisations / ajout.php
1 <?php
2 namespace Garradin;
3
4 require_once __DIR__ . '/../../_inc.php';
5
6 if ($user['droits']['membres'] < Membres::DROIT_ECRITURE)
7 {
8 throw new UserException("Vous n'avez pas le droit d'accéder à cette page.");
9 }
10
11 $membre = false;
12
13 if (!empty($_GET['id']) && is_numeric($_GET['id']))
14 {
15 $membre = $membres->get((int) $_GET['id']);
16
17 if (!$membre)
18 {
19 throw new UserException("Ce membre n'existe pas.");
20 }
21
22 $cats = new Membres_Categories;
23 $categorie = $cats->get($membre['id_categorie']);
24 }
25 else
26 {
27 $categorie = ['id_cotisation_obligatoire' => false];
28 }
29
30 $cotisations = new Cotisations;
31 $m_cotisations = new Cotisations_Membres;
32
33 $cats = new Compta_Categories;
34 $banques = new Compta_Comptes_Bancaires;
35
36 $error = false;
37
38 if (!empty($_POST['add']))
39 {
40 if (!utils::CSRF_check('add_cotisation'))
41 {
42 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
43 }
44 else
45 {
46 try {
47 $data = [
48 'date' => utils::post('date'),
49 'id_cotisation' => utils::post('id_cotisation'),
50 'id_membre' => utils::post('id_membre'),
51 'id_auteur' => $user['id'],
52 'montant' => utils::post('montant'),
53 'moyen_paiement' => utils::post('moyen_paiement'),
54 'numero_cheque' => utils::post('numero_cheque'),
55 'banque' => utils::post('banque'),
56 ];
57
58 $m_cotisations->add($data);
59
60 utils::redirect('/admin/membres/cotisations.php?id=' . (int)utils::post('id_membre'));
61 }
62 catch (UserException $e)
63 {
64 $error = $e->getMessage();
65 }
66 }
67 }
68
69 $tpl->assign('error', $error);
70 $tpl->assign('membre', $membre);
71
72 $tpl->assign('cotisations', $cotisations->listCurrent());
73
74 $tpl->assign('default_co', null);
75 $tpl->assign('default_amount', 0.00);
76 $tpl->assign('default_date', date('Y-m-d'));
77 $tpl->assign('default_compta', null);
78
79 $tpl->assign('moyens_paiement', $cats->listMoyensPaiement());
80 $tpl->assign('moyen_paiement', utils::post('moyen_paiement') ?: 'ES');
81 $tpl->assign('comptes_bancaires', $banques->getList());
82 $tpl->assign('banque', utils::post('banque'));
83
84
85 if (utils::get('cotisation'))
86 {
87 $co = $cotisations->get(utils::get('cotisation'));
88
89 if (!$co)
90 {
91 throw new UserException("La cotisation indiquée en paramètre n'existe pas.");
92 }
93
94 $tpl->assign('default_co', $co['id']);
95 $tpl->assign('default_compta', $co['id_categorie_compta']);
96 $tpl->assign('default_amount', $co['montant']);
97 }
98 elseif ($membre)
99 {
100 if (!empty($categorie['id_cotisation_obligatoire']))
101 {
102 $co = $cotisations->get($categorie['id_cotisation_obligatoire']);
103
104 $tpl->assign('default_co', $co['id']);
105 $tpl->assign('default_amount', $co['montant']);
106 }
107 }
108
109
110 $tpl->display('admin/membres/cotisations/ajout.tpl');
111
112 ?>