use strict;
use lib qw(ticketserver/tools/ . /mnt/datev/);
use ADBGUI::Tools qw(ensureLength ReadConfig $loglevel $logdst);
use ADBGUI::BasicVariables;
use Date::Calc qw(Monday_of_Week Add_Delta_Days Mktime Decode_Date_EU2 Decode_Date_US2 Add_Delta_YM);
use ticketserver::Tools qw(printHeader timetodatev);

die "No Month given!\n"
   unless my $MONTH = $ARGV[0];
die "No Year given!\n"
   unless my $YEAR = $ARGV[1];

my ($year,$month,$day) = ($YEAR, $MONTH, 1);
my $start = Mktime($YEAR,$MONTH, 1, 0, 0, 0);
($year,$month,$day) = Add_Delta_YM($YEAR, $MONTH, 1, 0, 1);
my $end = Mktime($year,$month,$day, 0, 0, 0)-1;

print "FROM ".localtime($start)." -> ".localtime($end)."\n";
my $infilename = "/mnt/datev/".$YEAR.".".ensureLength($MONTH, 2, "0")."/in.csv";
open(A, "<", $infilename) || die($infilename.": ".$!);
my $c = {};
while(<A>) {
   chomp;
   my $x = [split(",", $_)];
   next
      unless join("", @$x);
   my $loc = $x->[0];
   next
      unless $loc;
   die "/opt/".$loc."/dbm.cfg missing!"
      unless -s "/opt/".$loc."/dbm.cfg";
   $ADBGUI::Tools::loglevel = $ERROR;
   $loglevel = $ERROR;
   $ADBGUI::Tools::logdst = 2**0;
   $logdst = 2**0;
   my $config = ReadConfig("/opt/".$loc."/dbm.cfg");
   die $loc."/kundenid"
      unless $config->{kundenid};
   my $mappingip = $config->{rechnungid} || $config->{kundenid} || die "LOC:".$loc.":";
   $c->{$mappingip}->{$loc}->{config} ||= $config;
   push(@{$c->{$mappingip}->{$loc}->{money}}, $x);
}
close(A);

my $filename = "/mnt/datev/".$YEAR.".".ensureLength($MONTH, 2, "0")."/rechnungen.xml";
open(my $out, ">", $filename) || die($!);
print $out "<Belege>\n";
my $sum = 0;
my $j = 1;
my $lastrechnungid = undef;
my $currechnungid = undef;
print $out "   <Beleg>"."\n";
my $heap = {};
foreach my $mappingip (keys %$c) {
   print $mappingip."\n";
   if (defined($lastrechnungid) &&
              ($lastrechnungid ne $mappingip)) {
      print $out "      </Positionen>"."\n";
      print $out "   </Beleg>"."\n";
      print $out "   <Beleg>"."\n";
      $j = 1;
      $heap = {};
   }
   $lastrechnungid = $mappingip || 0;
   print join("", map {
      my $loc = $_;
      printHeader($heap, $out, $c->{$mappingip}->{$loc}->{config}, $end);
      my $return = "  ".$loc."/".$c->{$mappingip}->{$loc}->{config}->{kundenid}."\t".join(",\t", map {
         $sum += $_->[2];
         print $out "         <Textposition>"."\n";
         print $out "            <Positionstext>".$c->{$mappingip}->{$loc}->{config}->{ticketstreet}."</Positionstext>"."\n";
         print $out "         </Textposition>"."\n";
         print $out "         <Artikelposition>"."\n";
         print $out "            <Artikelnummer>IZV-TKTS-10</Artikelnummer>"."\n";
         print $out "            <Referenznummer>".$j++."</Referenznummer>"."\n";
         print $out "            <Menge>".(int($_->[2] / 5) / 2)."</Menge>"."\n";
         print $out "         </Artikelposition>"."\n";
         $_->[2]."(".$_->[1].")"
      } @{$c->{$mappingip}->{$loc}->{money}})."\n";
      $return
   } keys %{$c->{$mappingip}});
}
print $out "      </Positionen>"."\n";
print $out "   </Beleg>"."\n";
print $out "</Belege>\n";

print "Sum: ".$sum."\n";
