aboutsummaryrefslogtreecommitdiffstats
path: root/mktreasurer
blob: f46cc0ce6912cc1805e13455a970573760ce831d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl -w

use strict;

use YAML qw/LoadFile/;
use XML::Simple;
use Template;
use DateTime;
#use Data::Dump qw/dd/;

my $config_file = '/usr/share/mga-treasurer/config';
my $config = LoadFile($ENV{MGATRES_CONF} ? $ENV{MGATRES_CONF} : $config_file);
my $etc_config_file = '/etc/mga-treasurer.conf';
my $etc_config = LoadFile($etc_config_file);

foreach my $k (keys %{$etc_config}) {
    $config->{$k} = $etc_config->{$k};
}


sub donation_category {
    my $r = shift;
    my @c = grep {$_->{Na} eq 'Donation'} @{$r->{Category}};
    return @c ? $c[0]->{Nb} : undef;
}

sub party_name {
    my ($r, $Nb) = @_;
    (grep { $_->{Nb} == $Nb } @{$r->{Party}})[0]->{Na};
}

sub subcat_name {
    my ($r, $Nb, $Nbc) = @_;
    (grep { $_->{Nb} == $Nb && $_->{Nbc} == $Nbc }
        @{$r->{Sub_category}})[0]->{Na};
}

sub epoch_date {
    my ($m, $d, $y) = split /\//, shift;
    DateTime->new(day => $d, month => $m, year => $y)->epoch;
}

sub transactions_list {
    my ($r, $cat) = @_;
    my @res;
    my @transactions = $cat ? grep { $_->{Ca} == $cat } @{$r->{Transaction}}
                                : @{$r->{Transaction}};
    foreach my $tra (@transactions) {
        my %d = (
            nb     => $tra->{Nb},
            amount => $tra->{Am},
            date   => epoch_date($tra->{Dt}),
            pa     => $tra->{Pa},
            who    => party_name($r, $tra->{Pa}),
            type   => subcat_name($r, $tra->{Sca}, $tra->{Ca}),
            $tra->{No} ne '(null)' ? (notes  => $tra->{No}) : (),
        );
        push @res, \%d;
    }
    return \@res;
}

sub donations_infos {
    my $r = shift;
    my %res = (
        donations => transactions_list($r, donation_category($r)),
        by_month  => {},
        by_year   => {},
        by_pa     => {},
        total_30  => 0,
    );
    foreach my $don (@{$res{donations}}) {
        my $dt = DateTime->from_epoch(epoch => $don->{date});
        my $month = $dt->year . '-' . sprintf("%02d", $dt->month);
        my $year = $dt->year;
        push @{$res{by_month}->{$month}->{donations}}, $don;
        $res{by_month}->{$month}->{year} = $year;
        $res{by_month}->{$month}->{month} = $dt->month;
        push @{$res{by_year}->{$year}->{donations}}, $don;
        push @{$res{by_year}->{$year}->{months}}, $month;
        push @{$res{by_pa}->{$don->{pa}}->{donations}}, $don;
        if (time - $don->{date} < '2592000') { # last 30 days
            $res{total_30} += $don->{amount};
        }
    }
    foreach my $d ((values %{$res{by_month}}), (values %{$res{by_year}}),
                   (values %{$res{by_pa}})) {
        foreach my $don (@{$d->{donations}}) {
            $d->{total} += $don->{amount};
            $d->{type}->{$don->{type}} += $don->{amount};
        }
    }
    return \%res;
}

sub process_template {
    my ($template, $src, $vars, $dest) = @_;
    foreach my $extension (@{$config->{output_format}}) {
        next unless -f "$config->{tmpl_dir}/$src.$extension";
        $template->process("$src.$extension", $vars, "$dest.$extension");
    }
}

sub output_pages {
    my ($r, $donations) = @_;
    my $template = Template->new(
        INCLUDE_PATH => $config->{tmpl_dir},
        OUTPUT_PATH  => $config->{out_dir},
    );

    my $last_update = (stat $config->{grisbi_file})[9];
    foreach my $month (keys %{$donations->{by_month}}) {
        my $vars = {
            config    => $config,
            month     => $month,
            donations => $donations,
        };
        process_template($template, 'donations_by_month', $vars, "donations_m_$month");
    }

    foreach my $year (keys %{$donations->{by_year}}) {
        my $vars = {
            config    => $config,
            year      => $year,
            donations => $donations,
        };
        process_template($template, 'donations_by_year', $vars, "donations_y_$year");
    }

    foreach my $pa (keys %{$donations->{by_pa}}) {
        my $vars = {
            config    => $config,
            pa        => $pa,
            who       => party_name($r, $pa),
            donations => $donations,
        };
        process_template($template, 'donations_by_pa', $vars, "donations_p_$pa");
    }

    my $vars = {
        config      => $config,
        donations   => $donations,
        last_update => $last_update,
    };
    process_template($template, 'donations', $vars, 'donations');
    process_template($template, 'index', $vars, 'index');
}

my $r = XMLin($config->{grisbi_file});
my $donations = donations_infos($r);

output_pages($r, $donations);