<?xml version="1.0" encoding="UTF-8"?>

<!--
REFERENCES:
1) http://financialplan.about.com/od/banking/ht/BalCheckbook.htm
2) http://www.essortment.com/howtobalancea_rsii.htm
-->

<taskModel about="http://tempuri.org" xmlns="http://ce.org/cea-2018" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

	<task id="ReconcileAccount">
	
		<input name="startDate" type="Date" />
		<input name="closingDate" type="Date" />
		<input name="previousBalance" type="number" />
		<input name="withdrawalsTotal" type="number" />
		<input name="depositsTotal" type="number" />
		<input name="interestEarned" type="number" />
		<input name="finalBalance" type="number" />
	
		<subtasks id="reconciling">
			<step name="markTransactions" task="MarkClearedTransactions" />
			<step name="verifyBalance" task="VerifyFinalBalance" />
			<binding slot="$markTransactions.startDate" value="$this.startDate"/>
			<binding slot="$markTransactions.closingDate" value="$this.closingDate"/>
			<binding slot="$markTransactions.previousBalance" value="$this.previousBalance" />
			<binding slot="$markTransactions.withdrawalsTotal" value="$this.withdrawalsTotal" />
			<binding slot="$markTransactions.depositsTotal" value="$this.depositsTotal" />
			<binding slot="$markTransactions.finalBalance" value="$this.finalBalance" />
			<binding slot="$verifyBalance.previousBalance" value="$this.previousBalance" />
			<binding slot="$verifyBalance.withdrawalsCleared" value="$markTransactions.withdrawalsCleared" />
			<binding slot="$verifyBalance.depositsCleared" value="$markTransactions.depositsCleared" />
			<binding slot="$verifyBalance.interestEarned" value="$this.interestEarned" />
			<binding slot="$verifyBalance.finalBalance" value="$this.finalBalance" />
		</subtasks>
		
	</task>
	
	<task id="MarkClearedTransactions">

		<input name="startDate" type="Date" />
		<input name="closingDate" type="Date" />
		<input name="previousBalance" type="number" />
		<input name="withdrawalsTotal" type="number" />
		<input name="depositsTotal" type="number" />
		<input name="finalBalance" type="number" />
		<output name="withdrawalsCleared" type="number" />
		<output name="depositsCleared" type="number" />
		
		<subtasks id="markClearedManual" ordered="false">
			<step name="mark" task="MarkClearedTransactionsManual" />
			<binding slot="$mark.startDate" value="$this.startDate" />
			<binding slot="$mark.closingDate" value="$this.closingDate" />
			<binding slot="$mark.withdrawalsTotal" value="$this.withdrawalsTotal" />
			<binding slot="$mark.depositsTotal" value="$this.depositsTotal" />
		</subtasks>
		
		<!--
			Will mark cleared every non-reconciled transaction in the date range
		 	Could be more intelligent, checking the totals, etc.
		-->
		<subtasks id="markClearedAuto">
			<applicable>!this.external</applicable>
			<step name="mark" task="MarkClearedTransactionsAuto" />
			<binding slot="$mark.startDate" value="$this.startDate" />
			<binding slot="$mark.closingDate" value="$this.closingDate" />
			<binding slot="$mark.withdrawalsTotal" value="$this.withdrawalsTotal" />
			<binding slot="$mark.depositsTotal" value="$this.depositsTotal" />
			<binding slot="$this.withdrawalsCleared" value="$mark.withdrawalsCleared" />
			<binding slot="$this.depositsCleared" value="$mark.depositsCleared" />
		</subtasks>
	
	</task>
	
	<task id="MarkClearedTransactionsManual">
		<input name="startDate" type="Date" />
		<input name="closingDate" type="Date" />
		<input name="withdrawalsTotal" type="number" />
		<input name="depositsTotal" type="number" />
	</task>
	
	<task id="MarkClearedTransactionsAuto">
		<input name="startDate" type="Date" />
		<input name="closingDate" type="Date" />
		<input name="withdrawalsTotal" type="number" />
		<input name="depositsTotal" type="number" />
		<output name="withdrawalsCleared" type="number" />
		<output name="depositsCleared" type="number" />
		<script>
			if (isDefined(model)) {
				var result = model.markTransactionsCleared($this.startDate,
					$this.closingDate, $this.withdrawalsTotal, $this.depositsTotal);
				$this.withdrawalsCleared = model.getWithdrawalsCleared($this.startDate,
					$this.closingDate);
				$this.depositsCleared = model.getDepositsCleared($this.startDate,
					$this.closingDate);
			}
		</script>
	</task>
	
	<task id="VerifyFinalBalance">
		<input name="previousBalance" type="number" />
		<input name="withdrawalsCleared" type="number" />
		<input name="depositsCleared" type="number" />
		<input name="interestEarned" type="number" />
		<input name="finalBalance" type="number" />
		<script>
			var calculatedFinalBalance = ($this.previousBalance
				+ $this.withdrawalsCleared
				+ $this.depositsCleared
				+ $this.interestEarned);
			var diff = (calculatedFinalBalance - $this.finalBalance);
			print("Calculated final balance: " + calculatedFinalBalance);
			print("Difference is " + diff);
			if (isDefined(gui)) {
				gui.println("Calculated final balance: " + roundToHundredths(calculatedFinalBalance));
				gui.println("Difference is " + roundToHundredths(diff));
			}
			$this.success = (Math.abs(diff) &lt;= .01);
		</script>
	</task>
	
	<script init="true">
		function isDefined(x) {
			return typeof(x) != "undefined";
		}
		
		function roundToHundredths(x) {
			return Math.round(x*100)/100;  
		}
		
		try {
			importClass(Packages.net.danielpalma.checkbook.AppUI);
			importClass(Packages.net.danielpalma.checkbook.AppModel);
			var gui = AppUI.getInstance();
			var model = AppModel.getInstance();
		}
		catch (err) {
		}
	</script>
	
</taskModel>
