#
# Liefert für einen Rechnungsschritt die korrekte Einheit zurück.
#
# get_recheinheit(rechstep, Typ, Flags, anzahl, ...) => (faktor, einheit)

use utf8;
use warnings; no warnings "redefine";
use strict;
use Dbase::Help qw(DoFn qquote);
use Dbase::Globals qw(test_flag rund);

sub get_recheinheit($$;$$$) {
	my($rstep,$typ,$flags,$anz,$panz) = @_;
	#print "$rstep,$typ,$flags,$anz,$panz => ";
	$flags = DoFn("select flags from rechstep where id=$rstep") unless defined $flags;

	if(not defined $typ or $typ eq "") {
		if(test_flag("acctflag","anz_pakete",$flags)) {
			$typ="p";
			$anz=$panz if defined $panz;
		} else {
			$typ="b";
		}
	}

	my $lev = "start";
	$lev="faktor" if test_flag("acctflag","via_faktor",$flags);
	my($einh,$fak) = DoFn("select name,faktor from recheinheit where rechstep=$rstep and typ=${\qquote $typ} and $lev <= ".rund($anz*1000)." order by $lev desc limit 1");

	$anz=rund($anz*1000000/$fak,1);
	if($anz % 100 or not test_flag("acctflag","ohne_komma",$flags)) {
		$anz = sprintf("%d,%02d",int($anz/100),($anz%100));
	} else {
		$anz = sprintf("%d   ",int($anz/100));
	}
	#print "$anz,$einh,$fak\n";
	return ( $anz, $einh, $fak);
}

1;
