Ajout du support des écritures ventilées.
[garradin.git] / www / admin / compta / pie.php
1 <?php
2 namespace Garradin;
3
4 require_once __DIR__ . '/_inc.php';
5
6 if (!in_array(utils::get('g'), ['recettes', 'depenses']))
7 {
8 throw new UserException('Graphique inconnu.');
9 }
10
11 $graph = utils::get('g');
12
13 if (Static_Cache::expired('pie_' . $graph))
14 {
15 $stats = new Compta_Stats;
16 $categories = new Compta_Categories;
17
18 require_once ROOT . '/include/libs/svgplot/lib.svgpie.php';
19
20 $pie = new \SVGPie(400, 250);
21
22 if ($graph == 'recettes')
23 {
24 $data = $stats->repartitionRecettes();
25 $categories = $categories->getList(Compta_Categories::RECETTES);
26 $pie->setTitle('Répartition des recettes');
27 }
28 else
29 {
30 $data = $stats->repartitionDepenses();
31 $categories = $categories->getList(Compta_Categories::DEPENSES);
32 $pie->setTitle('Répartition des dépenses');
33 }
34
35 $others = 0;
36 $colors = ['#c71', '#941', '#fa4', '#fd9', '#ffc', '#cc9'];
37 $max = count($colors);
38 $i = 0;
39
40 foreach ($data as $row)
41 {
42 if ($i++ >= $max)
43 {
44 $others += $row['nb'];
45 }
46 else
47 {
48 $cat = $categories[$row['id_categorie']];
49 $pie->add(new \SVGPie_Data($row['nb'], substr($cat['intitule'], 0, 50), $colors[$i-1]));
50 }
51 }
52
53 if ($others > 0)
54 {
55 $pie->add(new \SVGPie_Data($others, 'Autres', '#ccc'));
56 }
57
58 Static_Cache::store('pie_' . $graph, $pie->output());
59 }
60
61 header('Content-Type: image/svg+xml');
62 Static_Cache::display('pie_' . $graph);