Ajout du support des écritures ventilées.
[garradin.git] / www / admin / compta / import.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 $e = new Compta_Exercices;
12 $import = new Compta_Import;
13 $comptes = new Compta_Comptes;
14
15 if (isset($_GET['export']))
16 {
17 header('Content-type: application/csv');
18 header('Content-Disposition: attachment; filename="Export comptabilité - ' . $config->get('nom_asso') . ' - ' . date('Y-m-d') . '.csv"');
19 $import->toCSV($e->getCurrentId());
20 exit;
21 }
22
23 $error = false;
24
25 if (!empty($_POST['import']))
26 {
27 if (!utils::CSRF_check('compta_import'))
28 {
29 $error = 'Une erreur est survenue, merci de renvoyer le formulaire.';
30 }
31 if (empty($_FILES['upload']['tmp_name']))
32 {
33 $error = 'Aucun fichier fourni.';
34 }
35 else
36 {
37 try
38 {
39 if (isset($_FILES['upload-plan-comptable']))
40 {
41 if (!$comptes->importPlan($_FILES['upload-plan-comptable']['tmp_name']))
42 {
43 throw new UserException('Erreur lors de l’import du plan comptable');
44 }
45 }
46 if (utils::post('type') == 'citizen')
47 {
48 $import->fromCitizen($_FILES['upload']['tmp_name']);
49 }
50 elseif (utils::post('type') == 'garradin')
51 {
52 if (!$import->fromCSV($_FILES['upload']['tmp_name']))
53 {
54 throw new UserException('Erreur lors de l’import');
55 }
56 }
57 else
58 {
59 throw new UserException('Import inconnu.');
60 }
61
62 utils::redirect('/admin/compta/import.php?ok');
63 }
64 catch (UserException $e)
65 {
66 $error = $e->getMessage();
67 }
68 }
69 }
70
71 $tpl->assign('error', $error);
72 $tpl->assign('ok', isset($_GET['ok']) ? true : false);
73
74 $tpl->display('admin/compta/import.tpl');
75
76 ?>