use utf8;
use warnings; no warnings "redefine";
use strict;
use Loader qw(line_in log_update list_tarife_kurz);
use Dbase::Help qw(Do DoFn qquote);
use Dbase::Globals qw(content def_or_minus);

sub set_tarifacct_tarif($$;$) {
	my($tid,$id,$kn) = @_;
    my $ksel;
    if($id) {
        $ksel="kunde = $id";
    } else {
        $ksel="kunde IS NULL";
        $id=0;
    }
	my $tari;
	while(1) {
		content ( my $itari = line_in "Tarif> " ) or return undef;
		if($itari eq "?") {
			list_tarife_kurz($id);
			next;
		}
		if($itari eq "-") {
			$tari = "NULL";
			last;
		}

		$tari = DoFn("select id from tarifname where name=${\qquote $itari}");
		unless($tari) {
			print "Tarif '$itari' unbekannt...\n";
			next;
		}
		last;
	}
	log_update("tarifacct","kunde",$id,"id",$tid,undef,"tarifname",
		def_or_minus(DoFn("select tarifname.name from tarifacct,tarifname where tarifacct.id=$tid and tarifname.id=tarifacct.tarifname and $ksel")));
	Do("update tarifacct set tarifname=$tari where id=$tid and $ksel");

	Do("update kunde set geaendert=UNIX_TIMESTAMP(NOW()) where id=$id") if $id;
	$tari;
}
1;
