//===== eAthena Script ======================================= 
//= Kafra Express - Bank Module
//===== By: ================================================== 
//= Skotlex 
//===== Current Version: ===================================== 
//= 2.8
//===== Compatible With: ===================================== 
//= eAthena SVN R3524+
//===== Description: ========================================= 
//= Part of the Kafra Express Script Package.
//= Offers banking services with daily interests, and 
//= withdrawal/deposit fees.
//===== Additional Comments: ================================= 
//= See config.txt for configuration.
//============================================================ 

-	script	keInit_bank	-1,{
OnInit:	//Load Config
	donpcevent "keConfig::OnLoadBank";
	end;
}

function	script	F_keBank	{

	function	SF_calcInterest;
	SF_calcInterest();
	
	do {
		set @feeD,callfunc("F_keCost",$@kebk_depositCost,100);
		set @maxDep,Zeny;
		if (@maxDep > $@kebk_maxTransact)
			set @maxDep,$@kebk_maxTransact;
		if (#ke_bank + @maxDep > $@kebk_capacity)
			set @maxDep,$@kebk_capacity-#ke_bank;

		set @feeW,callfunc("F_keCost",$@kebk_withdrawCost,100);
		set @maxWith,#ke_bank-(#ke_bank*@feeW/100);
		if (@maxWith > $@kebk_maxTransact)
			set @maxWith,$@kebk_maxTransact;
		
		if (#ke_bank < $@kebk_capacity) {
			set @kmenu, select(
				"- Return (balance is "+#ke_bank+"z)",
				"- Deposit (up to "+@maxDep+"z - "+@feeD+"% fee)",
				"- Withdraw (up to "+@maxWith+"z + "+@feeW+"% fee)",
				"- Transfer from 1st B.o.P. ("+#bankstorage+"z)",
				"- Transfer from 2nd B.o.P. ("+#kafrabank+"z)"
			);
		} else {
			set @kmenu, select(
				"- Return (balance is "+#ke_bank+"z)",
				"- Withdraw (up to "+@maxWith+"z + "+@feeW+"% fee)",
				"- Transfer from 1st B.o.P. ("+#bankstorage+"z)",
				"- Transfer from 2nd B.o.P. ("+#kafrabank+"z)"
			);
			if (@kmenu > 1)
				set @kmenu, @kmenu+1;
		}
		switch (@kmenu) {
		case 2:	//Deposit
			input @amount;	
			if (@amount < $@kebk_minTransact) {
				callfunc "F_keIntro", -1, "Sorry, the minimum transaction value is "+$@kebk_minTransact+"z.";
				break;
			}
			if (@amount > @maxDep) {
				callfunc "F_keIntro", e_an, "You can't deposit that quantity!";
				break;
			}
			set #ke_bank,#ke_bank+@amount-(@amount*@feeD/100);
			if ($@kebk_useGlobalBank)
				set $ke_globalbank, $ke_globalbank +@amount*@feeD/100;
			set Zeny,Zeny-@amount;
			emotion e_cash;
			break;

		case 3:	//Withdraw
			input @amount;
			if (@amount < $@kebk_minTransact) {
				callfunc "F_keIntro", -1, "Sorry, the minimum transaction value is "+$@kebk_minTransact+"z.";
				break;
			}
			if (@amount > @maxWith) {
				callfunc "F_keIntro", e_X, "You can't withdraw that quantity!";
				break;
			}
			set #ke_bank,#ke_bank-@amount-(@amount*@feeW/100);
			if ($@kebk_useGlobalBank)
				set $ke_globalbank, $ke_globalbank +@amount*@feeW/100;
			set Zeny,Zeny+@amount;
			emotion e_oh;
			break;
			
		case 4:	//1st B.o.P.
			if (#bankstorage==0) {
				callfunc "F_keIntro", -1, "Sorry, there is nothing left to transfer.";
				break;
			}
			callfunc "F_keIntro", -1, #bankstorage+"z have been transferred from your account in the First Bank of Prontera.";
			set #ke_bank,#ke_bank+#bankstorage;
			set #bankstorage,0;
			break;

		case 5:	//2nd B.o.P.
			if (#kafrabank==0) {
				callfunc "F_keIntro", -1, "Sorry, there is nothing left to transfer.";
				break;
			}
			callfunc "F_keIntro", -1, #kafrabank+"z have been transferred from your account in the Second Bank of Prontera.";
			set #ke_bank,#ke_bank+#kafrabank;
			set #kafrabank,0;
			break;
		}
	} while (@kmenu > 1);
return;

//Subfunction: SF_calcInterest
//Calculates the interests gained on the account. Uses 30-day months
function	SF_calcInterest {
	set @day,gettime(5);
	if (@day>30)
		set @day,30; //Use 30 day-months
	set @month,gettime(6);
	if (#ke_banktick == 0) { //Init bank.
		set #ke_banktick,gettime(6)*30+@day;
		if ($@kebk_monMaintenance < 1) return;
		set #ke_bankmtick,@month;
		return;
	}
	set @banktick,gettime(6)*30+@day;
	if (@banktick == #ke_banktick) return;
	if (@banktick < #ke_banktick) { //Year change.
		do {
			set #ke_banktick,#ke_banktick-360;
			if ($@kebk_monMaintenance > 0) set #ke_bankmtick,#ke_bankmtick-12;
		} while (@banktick < #ke_banktick);
	}
	if ($@kebk_monMaintenance > 0 && #ke_bankmtick < @month){	//Month change
		set @qty,0;
		do {
			if (#ke_bank < $@kebk_monMaintenance) {
				if (@qty>0)
					mes "You've been charged "+@qty+"z for account maintenance, but it was not enough.";
				if ($@kebk_useGlobalBank)
					set $ke_globalbank, $ke_globalbank + @qty;
				mes "You owe the Bank "+($@kebk_monMaintenance*(#ke_bankmtick-@month))+"z for maintenance costs. Until paid you won't earn interests...";
				return;
			}
			set @qty, @qty+$@kebk_monMaintenance;
			set #ke_bankmtick,#ke_bankmtick+1;
			set #ke_bank, #ke_bank-$@kebk_monMaintenance;
		} while (#ke_bankmtick < @month);
		mes "You've been charged "+@qty+"z for account maintenance.";
		if ($@kebk_useGlobalBank)
			set $ke_globalbank, $ke_globalbank + @qty;
	}
	set @qty,0;
	if (#ke_bank > $@kebk_monMaintenance && #ke_bank < $@kebk_capacity)
	{
		if (#ke_bank > 1000000) //Overflow protection
			set @qty,(#ke_bank/10000)*$@kebk_dayInterest*(@banktick-#ke_banktick);
		else
			set @qty,#ke_bank*$@kebk_dayInterest*(@banktick-#ke_banktick)/10000;
	}
	set #ke_banktick,@banktick;
	if (@qty == 0) return;
	if (@qty < 0) {
		callfunc "F_keIntro", -1, "Sorry, your gained interests have been cancelled for being way too much.";
		return;
	}
	if (#ke_bank + @qty > $@kebk_capacity)
		set #ke_bank, $@kebk_capacity;
	else
		set #ke_bank,#ke_bank+@qty;

	callfunc "F_keIntro", -1, "Since your last visit, you've earned an interest of "+@qty+"z!";
}	//SF end

}

