#!/usr/bin/perl -w
use warnings;
use strict;
use lib '.';
use POE;
use CM::POETools qw(requestData);
use JSON;
use Data::Dumper;
use FileHandle;
use ADBGUI::Tools qw(ReadConfig);
use HTTP::Date;

my $config = ReadConfig('dbm.cfg');
my $x = {};

POE::Session->create(
inline_states => {
   _start  => sub {
      my ($kernel, $heap, $session) = @_[KERNEL, HEAP, SESSION];
      requestData($session->ID(), "adbgui://127.0.0.1:".$config->{listenport}."/GETBUCHUNGEN", "getdone", undef, {
         auth => [$config->{user}, $config->{pass}],
         adbguitimeout => 2500,
      });
   },
   getdone => sub {
      my ($kernel, $session, $heap, $input) = @_[ KERNEL, SESSION, HEAP, ARG0];
      eval {
         my $json = undef;
         if ($input && ($json = from_json($input)) && (
                        $json->{result} eq "ok")) {
            foreach my $curline (sort { $a->{"ticketbuchungen.id"} <=> 
                                        $b->{"ticketbuchungen.id"} } @{$json->{data}}) {
               #print Dumper $curline;
#                print "--------\n";
#                foreach my $key (sort { $a cmp $b } keys %$curline) {
#                   next
#                      if $key =~ /^vorlage/  ||
#                         $key =~ /^customer/ ||
#                         $key =~ /^user/;
#                   next
#                      unless defined($curline->{$key}) &&
#                                     $curline->{$key};
#                   print "".$key." = ".$curline->{$key}."\n";
#                }
# tickets_id_tickets.nomultiple = 1
# ticketbuchungen.zahlungsart_id = 3

# tickets_id_tickets.id = 7406
# ticketbuchungen.tickets_id = 7406
# ticketbuchungen.brums = 1660787473
# ticketbuchungen.id = 1845
# tickets_id_tickets.used = 1

# ticketbuchungen.mydate = 2023-08-20 08:14:46
# tickets_id_tickets.start = 2023-08-20 07:06:18
# tickets_id_tickets.mydate = 2023-09-03 05:06:18

# ticketbuchungen.payment = 5
                next
                   unless defined($curline->{"ticketbuchungen.zahlungsart_id"}) &&
                                (($curline->{"ticketbuchungen.zahlungsart_id"} eq "3") ||
                                 ($curline->{"ticketbuchungen.zahlungsart_id"} eq "1"));
                my $buchdatum = str2time($curline->{"ticketbuchungen.mydate"});
                my $start     = str2time($curline->{"tickets_id_tickets.start"}) || $buchdatum;
                my $end       = str2time($curline->{"tickets_id_tickets.mydate"});
                my $dauer = undef;
                if ($start && $end) {
                   my $dauersec = $end - $start;
                   $dauer = int(($dauersec * 100) / 60 / 60 / 24)/100;
                }
                my $betrag    = $curline->{"ticketbuchungen.payment"};
                my $id        = $curline->{"ticketbuchungen.id"};
                my $tid       = $curline->{"tickets_id_tickets.id"};
                my $buchung = $buchdatum ? $curline->{"ticketbuchungen.mydate"} :
                              $start     ? $curline->{"tickets_id_tickets.start"} :
                                           $curline->{"tickets_id_tickets.mydate"};
                my $ltime = [localtime(str2time($buchung))];
                print STDERR
                   #"ID="     .($id    ||"-")."\t".
                   #"TICKET=" .($tid   ||"-")."\t".
                   "BETRAG=".($betrag||"-")."\t".
                   "BUCHUNG=".($buchung   )."\t".($ltime->[4]+1)."/".($ltime->[5]+1900)."\t".
                   "DAUER=" .($dauer ||"-")."\t".
                   #"START="  .($start ||"-")."\t".
                   #"END="    .($end   ||"-")."\t".
                   "\n";
                $x->{$ltime->[5]+1900}->{$ltime->[4]+1} += $betrag;
            }
         }
      };
   },
});

$poe_kernel->run();

foreach my $year (sort {
   $a <=> $b
} keys %$x) {
   print STDERR $year."\n";
   foreach my $month (sort {
      $a <=> $b
   } keys %{$x->{$year}}) {
      print STDERR "  ".$month." ".$x->{$year}->{$month}."\n";
   }
}

open(OUT, ">", "tickets.json") || die($!);
print OUT to_json($x)."\n";
