use utf8;
use warnings; no warnings "redefine";
use strict;

use Loader qw(line_printer);
use Dbase::Help qw(DoSelect isotime);
use Dbase::Globals qw(bignum content get_descr name_kunde name_dienst
	is_dienst);

sub list_acct($$;$) {
	my($hash,$seq,$kn) = @_;
	line_printer;

	DoSelect {
		my($timestamp,$dienst,$ziel,$quelle,$kunde,$bytes,$pakete,$jjmm,$tt,$txt,$tk,$ai) = @_;

		my($dien,$zz);
		$dien = name_dienst($dienst) if defined $dienst;
		if(is_dienst($dienst,"nic")) {
			$zz = get_descr('acctinfo',$ziel) if defined $ziel;
		} else {
			$zz = get_descr($dien,$ziel) if defined $dien and defined $ziel 
				and $dien ne "" and $ziel ne "";
		}
		$zz = get_descr('ziel',$ziel) if defined $ziel and $ziel ne "" and (not defined $zz or $zz eq "");
#		if($direkt ne "") {
#			my $dir = get_descr('direkt',"'$direkt'");
#			$direkt = $dir if $dir ne "";
#		}
		if($jjmm =~ /^(\d+)(\d\d)$/) {
			$tt = "0$tt" if length $tt == 1;
			$jjmm = "$1-$2-$tt";
		}
		print $Db::pr_fh "ID       : $hash-$seq\n";
		print $Db::pr_fh "Timestamp: ".isotime($timestamp)."\n";
		print $Db::pr_fh "Kunde    : #$kunde: ".name_kunde($kunde)."\n" if $kunde;
		print $Db::pr_fh "Dienst   : $dien\n" if defined $dienst;
		print $Db::pr_fh "Ziel     : $zz\n" if $ziel ne "";
		print $Db::pr_fh "Quelle   : ".get_descr("quelle",$quelle)."\n" if $quelle > 0;
		print $Db::pr_fh "Pakete   : $pakete\n" if $pakete;
		print $Db::pr_fh "Bytes    : $bytes\n" if $bytes ne "0";
		print $Db::pr_fh "Datum    : $jjmm\n" if $jjmm ne "0";
		print $Db::pr_fh "Text     : $txt\n" if content $txt;

		if($tk or $ai) {
			print $Db::pr_fh "* Tarif  : \n";
			my $it = DoFn("select infotext from tarifkunde where id=$tk");
			$it = "?" unless defined $it;
			print $Db::pr_fh "Tarif    : #$tk $it\n" if $tk;
			print $Db::pr_fh "Info     : ".get_descr("acctinfo",$ai)."\n" if $ai;
		}
	} "select acct.timestamp,acct.dienst,acct.dest,acct.quelle,acct.kunde,acct.bytes,acct.pakete,acct.jjmm,acct.tt,acctassoc.info,acctassoc.tarifkunde,acctassoc.acctinfo from acct left join acctassoc on acct.`hash`=acctassoc.`hash` and acct.seq=acctassoc.seq where acct.`hash`=$hash and acct.seq=$seq"
		or print "(leer)\n";
}

1;
