#!/bin/perl

sub usage{
	println("usage: $0 <check1|check2|house|botnang|aldingen|bhw|dachswald|compare|rot>");
	exit 1;
}

$arg = lc($ARGV[0]);
$Arg = uc(substr($arg, 0, 1)) . substr($arg, 1);
usage() if($arg!~/\S/ or $arg eq "-h" or $arg eq "-help" or $arg eq "--help");
$tmpfile = "/tmp/credit.tmp";

# defaults (for compare)
$credit = 0;
$paystartmonth = 12;
$paystartyear = 2024;
$payendmonth = 12;
$payendyear = 2034;
$repay1after = 5; # repayment month (0=January...)
$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
$nrmonths = 120; # 10 years
$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
$iban = "DE48670400310308017302";
$contract = "3017559654016";

if($#ARGV>7){
	$arg = "LJASGLKJASLKDJGLKAJSDG";
	($credit, $interest, $rate, @payments) = @ARGV;
}

if($arg eq "rot"){
	$credit = 14837; # 5% = 5500
	$interest = 1.0; # yearly interest
	$rate = 300;   # monthly rates
	$paystartmonth = 11; # start month (0=January...)
	$paystartyear = 2025; # start year
	$payendmonth = 4; # end month (0=January...)
	$payendyear = 2030; # end year
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 52; # 10 years
	@payments = (0, 0, 0, 0, 0, 0, 0, 0, 0); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE75670400310308017301";
	$seedcapital = 0; # only meaning is, when the start of the credit is unknown
	$contract = "3017153024011";
}

if($arg eq "check1"){
	$credit = 110000; # 5% = 5500
	$interest = 2.5; # yearly interest
	$rate = 800;   # monthly rates
	$paystartmonth = 12; # start month (0=January...)
	$paystartyear = 2024; # start year
	$payendmonth = 12; # end month (0=January...)
	$payendyear = 2034; # end year
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (5500, 5500, 5500, 5500, 5500, 5500, 5500, 5500, 5500); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE75670400310308017301";
	$seedcapital = 0; # only meaning is, when the start of the credit is unknown
	$contract = "3017153024011";
}

if($arg eq "check2"){
	$credit = 301500; # 5% = 15000
	$interest = 2.5; # yearly interest
	$rate = 1000;   # monthly rates
	$paystartmonth = 12; # start month (0=January...)
	$paystartyear = 2024; # start year
	$payendmonth = 12; # end month (0=January...)
	$payendyear = 2034; # end year
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (15000, 15000, 15000, 15000, 15000, 15000, 15000, 15000, 15000); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE75670400310308017301";
	$seedcapital = 0; # only meaning is, when the start of the credit is unknown
	$contract = "3017153024011";
}

if($arg eq "house"){
	$credit = 415000;
	$interest = 1.11; # yearly interest
	$rate = 1507.83;   # monthly rates
	$paystartmonth = 4; # start month (0=January...)
	$paystartyear = 2015; # start year
	$payendmonth = 2; # end month (0=January...)
	$payendyear = 2025; # end year
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (17700, 17700, 17700, 17870, 17800, 17800, 17800, 18100, 17500); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE75670400310308017301";
	$seedcapital = 0; # only meaning is, when the start of the credit is unknown
	$contract = "3017153024011";
}

if($arg eq "botnang"){
	$credit = 70000;
	$interest = 1.04;
	$rate = 614.83;
	$paystartmonth = 10;
	$paystartyear = 2018;
	$payendmonth = 10;
	$payendyear = 2028;
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (0, 0, 0, 0, 0, 0, 0, 0, 0); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE48670400310308017302";
	$contract = "3017559654016";
}

if($arg eq "aldingen"){
	$credit = 185000;
	$interest = 1.01;
	$rate = 661.38;
	$paystartmonth = 5;
	$paystartyear = 2021;
	$payendmonth = 5;
	$payendyear = 2031;
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 1; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (0, 47, 0, 0, 9276, 9250, 9250, 9250, 9250, 9250); # extra payments in month $repay1after: needed to correct by real bills, therefore we have 47 EUR etc...
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE94664900003416195497";
	$contract = "Interhyp VoBa 10%";
}

if($arg eq "dachswald"){
	$credit = 235000; # (285*1.115)-83 ~ 235
	$interest =3.3;
	$rate = 800; # necessary savings: 800 + 980 (~11750/12) + 600 (~72000 remainder/120)
	$paystartmonth = 5;
	$paystartyear = 2025;
	$payendmonth = 5;
	$payendyear = 2035;
	$repay1after = 5; # repayment month (0=January...)
	$repay1inyear1 = 1; # whether @payments can be paid in the 1st year as well
	$nrmonths = 120; # 10 years
	@payments = (0, 11750, 11750, 11750, 11750, 11750, 11750, 11750, 11750, 11750); # extra payments in month $repay1after: needed to correct by real bills, therefore we have 47 EUR etc...
	$forcepayment = 11750; # if set to other than 0, all @payments are set to this amount
	$iban = "undefined";
	$contract = "JustAnIdea";
}

if($arg eq "bhw"){
	$credit = 105000;
	$interest = 1.25;
	$rate = 420;
	$paystartmonth = 10;
	$paystartyear = 2014;
	$payendmonth = 10;
	$payendyear = 2034;
	$repay1after = 11; # repayment month (0=January...)
	$repay1inyear1 = 0; # whether @payments can be paid in the 1st year as well
	$nrmonths = 240; # 10 years
	@payments = (-2.92, -2.92, -2.92, -2.92, -2.92, -2.92, -2.92, -2.92, -2.92); # extra payments in month $repay1after
	@payments = (27, 27, 27, 27, 27, 27, 744, 27, 27); # extra payments in month $repay1after
	$forcepayment = 0; # if set to other than 0, all @payments are set to this amount
	$iban = "DE12254102003704666102";
	$contract = "3 704 666 1 02";
	$seedcapital = 12260 - 2784.15; # only meaning is, when the start of the credit is unknown
	$resultdeductfrom = $credit; # if !=0, then the result is deducted from that number
}


if($arg eq "compare"){ # compare
	push(@offers, [("CommerzBank 5%", 185000, 1.23, 649, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("BauFi 5%", 185000, 0.89, 650, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("DrKlein ING 5%", 185000, 0.90, 639.79, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("Interhyp ING 5%", 185000, 0.90, 639.79, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("Interhyp SK PF 5%", 185000, 0.78, 649.04, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("Sparkasse PF 5%", 185000, 0.79, 650.0, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("Sparkasse PF 5%", 185000, 0.82, 650.0, 0, 0, 0, 9250, 9250, 9250, 9250, 9250, 9250)]);
	push(@offers, [("CommerzBank 10%", 185000, 1.25, 655, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("DrKlein VoBa 10%", 185000, 1.03, 650.58, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("Sparkasse PF 10%", 185000, 0.94, 650.0, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("Sparkasse PF 10%", 185000, 0.92, 650.0, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("Check24 SK PF 10%", 185000, 0.89, 650.58, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("Interhyp SK PF 10%", 185000, 0.88, 664.46, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);
	push(@offers, [("Interhyp VoBa 10%", 185000, 1.01, 661.37, 0, 0, 0, 18500, 18500, 18500, 18500, 18500, 18500)]);

	println("=========================================================================================================");
	printf(" %-20s | %10s | %5s | %10s | %8s | %15s | %15s\n", "Name", "Darlehen", "Zins", "Rate", "Rest", "Kosten Total", "Kosten Prozent");
	println("=========================================================================================================");
	for($o=0;$o<$#offers+1;$o++){
		($caption, $credit, $interest, $rate, @payments) = @{$offers[$o]};
		system("perl $0 $credit $interest $rate ". join(" ", @payments) ." >$tmpfile");
		open(F, "<$tmpfile") or die("ERROR: cannot read $tmpfile: $!");
		while(<F>){
			chomp($_);
			$payment = $1 if($_=~/payment in .+ = ([\.\d]+)$/);
			$totalint = $1 if($_=~/total interest was: ([\.\d]+)$/);
			$totalper = $1 if($_=~/in percent: ([\.\d]+)$/);
		}
		close(F);
		printf(" %-20s | %10s | %5.2f | %10.2f | %08.2f | %15.2f | %15.2f \n", $caption, $credit, $interest, $rate, $payment, $totalint, $totalper);
	}
	exit;
}

usage() if($credit==0);

println("=====================================");
println("   $Arg credit, $contract");
println("=====================================");
println("- credit sum: $credit");
println("- seed capital: $seedcapital") if($seedcapital);
println("- credit iban: $iban");
println("- yearly interest: $interest");
println("- monthly rate: $rate");
println("- duration: $paystartmonth/$paystartyear - $payendmonth/$payendyear");
println("- extra payments: ". join(", ", @payments));


################ START ###################
init();
execPayments();


sub init{
	$credit -= $seedcapital;
	$creditStart = $credit;
	$interest /= 12;
	if($forcepayment){
		for($p=0;$p<$#payments+1;$p++){
			$payments[$p] = $forcepayment;
		}
	}
	$paymentId = 0;
	$totalInterest = 0;
	$endmonthname = "$payendyear/". d2($payendmonth+1);
}

sub execPayments{
	my $yearnr = 0;
	println("- start doing payments ...");
	for(my $m=$paystartmonth;$m<$nrmonths+$paystartmonth;$m++){
		abortPayments() if($credit<=0);
		my $monthnr = $m%12;
		my $monthname = "$paystartyear/". d2($monthnr+1);
		doPaymentNormal($monthname);
		if($monthnr==$repay1after){
			$yearnr++;
			if($yearnr==1 and !$repay1inyear1){}
			else{ doPaymentExtra($monthname, $payments[$paymentId++]); }
		}
		$paystartyear++	if($monthnr>=11);
		abortPayments() if($monthname eq $endmonthname);
	}
	abortPayments();
}

sub doPaymentNormal{
	my $mname = shift;
	my $intamount = $credit / 100 * $interest; # interest amount
	$totalInterest += $intamount;
	my $repay = $rate - $intamount;
	my $before = $credit;
	$credit -= $repay;
	my $diff = $creditStart - $credit;
	my $rest = $credit;
	my $output = "-  norm payment in $mname: ". roundN($before) ." - ". roundN($repay) ." [/". roundN($intamount) ."] " . " = ". roundN($credit);
	if($resultdeductfrom){
		$rest = roundN($resultdeductfrom - $credit);
		$output .= " [ => $rest]";
	}
	println($output);
}

sub doPaymentExtra{
	my $mname = shift;
	my $repay = shift;
	my $before = $credit;
	$credit -= $repay;
	my $diff = $creditStart - $credit;
	my $rest = $credit;
	my $output = "- extra payment in $mname: ". roundN($before) ." - ". roundN($repay) ." = ". roundN($credit);
	if($resultdeductfrom){
		$rest = roundN($resultdeductfrom - $credit);
		$output .= " [ => $rest]";
	}
	println($output);
}

sub abortPayments{
	println("- credit is payed") if($credit <=0);
	println("- total interest was: ". roundN($totalInterest));
	$intperc = roundN(($totalInterest/$creditStart)*100);
	println("	- in percent: $intperc");
	println("- abort!");
	exit;
}

sub d2{
	my $nr = shift;
	return "0$nr" if($nr<10);
	return $nr;
}

sub roundN{
	my $nr = shift;
	my $d = shift;
	$d = 2 if($d!=~/\S/);
	my $f = 10 ** $d;
	$nr *= $f;
	my $newnr = int($nr);
	$newnr++ if($nr-$newnr>=0.5);
	my $res = $newnr / $f;
	$res .= ".00" if($res!~/\./);
	$res .= "0" if($res=~/\.\d$/);
	return $res;
}

sub println{
	my $str = shift;
	print $str ."\n";
}

	
