aboutsummaryrefslogtreecommitdiffstats
path: root/mktreasurer
diff options
context:
space:
mode:
authorNicolas Vigier <boklm@mageia.org>2013-05-13 19:55:02 +0000
committerNicolas Vigier <boklm@mageia.org>2013-05-13 19:55:02 +0000
commit18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124 (patch)
treeb553e5e6c2d5ba000df636f08f85c0db2cc47b2d /mktreasurer
parentbb091a5986bfcb2477bd6b0a45990d02f681585e (diff)
downloadmgatres-18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124.tar
mgatres-18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124.tar.gz
mgatres-18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124.tar.bz2
mgatres-18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124.tar.xz
mgatres-18dc74fb078b0e7cf6fcb5d87dddba0bfc44c124.zip
Rename mga-treasurer to mktreasurer
git-svn-id: svn+ssh://svn.mageia.org/svn/soft/mga-treasurer/trunk@8226 5b7c3c08-40e5-403b-9995-ace06908e4af
Diffstat (limited to 'mktreasurer')
-rwxr-xr-xmktreasurer138
1 files changed, 138 insertions, 0 deletions
diff --git a/mktreasurer b/mktreasurer
new file mode 100755
index 0000000..786246b
--- /dev/null
+++ b/mktreasurer
@@ -0,0 +1,138 @@
+#!/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 donations_list {
+ my $r = shift;
+ my @res;
+ my $don_cat = donation_category($r);
+ my @donations = grep { $_->{Ca} == $don_cat } @{$r->{Transaction}};
+ foreach my $don (@donations) {
+ my %d = (
+ amount => $don->{Am},
+ date => epoch_date($don->{Dt}),
+ who => party_name($r, $don->{Pa}),
+ type => subcat_name($r, $don->{Sca}, $don_cat),
+ $don->{No} ne '(null)' ? (notes => $don->{No}) : (),
+ );
+ push @res, \%d;
+ }
+ return \@res;
+}
+
+sub donations_infos {
+ my $r = shift;
+ my %res = (
+ donations => donations_list($r),
+ by_month => {},
+ by_year => {},
+ 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;
+ if (time - $don->{date} < '2592000') { # last 30 days
+ $res{total_30} += $don->{amount};
+ }
+ }
+ foreach my $d ((values $res{by_month}), (values $res{by_year})) {
+ 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 ($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_$month");
+ }
+
+ foreach my $year (keys $donations->{by_year}) {
+ my $vars = {
+ config => $config,
+ year => $year,
+ donations => $donations,
+ };
+ process_template($template, 'donations_by_year', $vars, "donations_$year");
+ }
+
+ 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($donations);
+