Ajout du support des écritures ventilées.
[garradin.git] / www / admin / compta / graph.php
1 <?php
2 namespace Garradin;
3
4 require_once __DIR__ . '/_inc.php';
5
6 if (!in_array(utils::get('g'), ['recettes_depenses', 'banques_caisses']))
7 {
8 throw new UserException('Graphique inconnu.');
9 }
10
11 $graph = utils::get('g');
12
13 if (Static_Cache::expired('graph_' . $graph))
14 {
15 $stats = new Compta_Stats;
16
17 require_once ROOT . '/include/libs/svgplot/lib.svgplot.php';
18
19 $plot = new \SVGPlot(400, 300);
20
21 if ($graph == 'recettes_depenses')
22 {
23 $r = new \SVGPlot_Data($stats->recettes());
24 $r->title = 'Recettes';
25
26 $d = new \SVGPlot_Data($stats->depenses());
27 $d->title = 'Dépenses';
28
29 $data = [$d, $r];
30
31 $plot->setTitle('Recettes et dépenses de l\'exercice courant');
32 }
33 elseif ($graph == 'banques_caisses')
34 {
35 $banques = new Compta_Comptes_Bancaires;
36
37 $data = [];
38
39 $r = new \SVGPlot_Data($stats->soldeCompte(Compta_Comptes::CAISSE));
40 $r->title = 'Caisse';
41
42 $data[] = $r;
43
44 foreach ($banques->getList() as $banque)
45 {
46 $r = new \SVGPlot_Data($stats->soldeCompte($banque['id']));
47 $r->title = $banque['libelle'];
48 $data[] = $r;
49 }
50
51 $plot->setTitle('Solde des comptes et caisses');
52 }
53
54 if (!empty($data))
55 {
56 $labels = [];
57
58 foreach ($data[0]->get() as $k=>$v)
59 {
60 $labels[] = utils::date_fr('M y', strtotime(substr($k, 0, 4) . '-' . substr($k, 4, 2) .'-01'));
61 }
62
63 $plot->setLabels($labels);
64
65 $i = 0;
66 $colors = ['#c71', '#941', '#fa4', '#fd9', '#ffc', '#cc9'];
67
68 foreach ($data as $line)
69 {
70 $line->color = $colors[$i++];
71 $line->width = 2;
72 $plot->add($line);
73
74 if ($i > count($colors))
75 $i = 0;
76 }
77 }
78
79 Static_Cache::store('graph_' . $graph, $plot->output());
80 }
81
82 header('Content-Type: image/svg+xml');
83 Static_Cache::display('graph_' . $graph);