#! /usr/bin/perl require "cgi-lib.pl"; require "cl-lib.pl"; sub getPMIRate{ if (@_[0] == "fixed"){ if (@_[1] > 0.9){ return 0.005; } elsif (@_[1] > 0.85){ return 0.0035; } else { return 0.003; } } else { if (@_[1] > 0.9) { return 0.0055; } elsif (@_[1] > 0.85) { return 0.0045; } else { return 0.0035; } } } sub RentvsBuy { local( $intratevar, $loantermvar, $numholdingyrsvar, $monthsheldvar, $downpaymentvar, $housepricevar, $loanamountvar, $rentalincomevar, $condoduesvar, $count, $renterscostvar); local( $proptaxvar, $insurancevar, $repairsvar, $closcostsvar, $taxsvgsvar, $oppcostvar, $remtincvar, $bcondoduesvar, $amortvar, $appvar, $pivar, $yearprinintvar); if ( $input{purchaseprice} == 0) { print "

You must enter a purchase price in order to perform an analysis.

\n"; return 0; } else { $housepricevar = $input{purchaseprice}; } if ( $input{loanamount} > $housepricevar) { print "

Loan amount cannot exceed purchase price.

\n"; return 0; } else { $loanamountvar = $input{loanamount}; } $rentalincomevar = $input{rentalincome}; $condoduesvar = $input{condodues}; if ( $input{interestrate} > 0.20 ) { print "

Interest rate should not be over 20%.

\n"; return 0; } else { $intratevar = $input{interestrate}; } if ( $input{loanterm} > 30 ) { print "

A loan term greater than 30 years is unrealistic.

\n"; return 0; } else { $loantermvar = $input{loanterm} * 12; } $downpaymentvar = $housepricevar - $loanamountvar; $pifactor = (($intratevar / 12) / (1 - (1 / (1 + ($intratevar / 12)) ** $loantermvar))); $pivar = $loanamountvar * $pifactor; if ($input{rent} < 1) { print "

Rent should be greater than 1$.

\n"; return 0; } $renterscostvar = $input{rent} + $input{rentersinsurance} + $input{rentersrepairs} + $input{renterscarport} + $input{rentersofees} + $input{rentershaf}; if ( ($input{flow} eq "firstmonth")) { $principalinterest = $pivar; $buyers{propertytax} = $housepricevar * $input{propertytax} / 12; $buyers{insurance} = $housepricevar * $input{hazins} / 12; $buyers{repairs} = $housepricevar / 1200; $buyers{closingcosts} = 0; $buyers{taxsavings} = -1 * $input{incometax} * ($loanamountvar * $intratevar + $housepricevar * $input{propertytax}) / 12; $buyers{opportunitycost} = (1 - $input{incometax}) * $input{returnoninvestment} * $downpaymentvar / 12; $buyers{rentalincome} = ($input{incometax} - 1) * $rentalincomevar; $buyers{condodues} = $condoduesvar; $buyers{amortization} = 0; $buyers{appreciation} = 0; $renters{realmonthlycost} = $renterscostvar; $buyers{realmonthlycost} = $pivar + $buyers{propertytax} + $buyers{insurance} + $buyers{repairs} + $buyers{taxsavings} + $buyers{opportunitycost} + $buyers{rentalincome}; } else { $numholdingyrsvar = $input{timetohold}; $monthsheldvar = 12 * $numholdingyrsvar; $yearprinintvar = 12 * $pivar; $rvb{rentcost, 0} = 0; $rvb{pri, 0} = 0; $rvb{intr, 0} = 0; $rvb{loanamt, 0} = $loanamountvar; $closcostsvar = ($input{points} * $loanamountvar) + ($input{closcosts} * $housepricevar); $taxsvgsvar = -1 * $input{incometax} * $input{points} * $loanamountvar; $rvb{buycost, 0} = $closcostsvar; $count = 1; while ($count <= $numholdingyrsvar) { if ( ($loanamountvar > (0.8 * $housepricevar))) { $closcostsvar += ($loanamountvar * &getPMIRate("fixed", ($loanamountvar / $housepricevar))); } $housepricevar = (1 + $input{appreciation}) * $housepricevar; $proptaxvar += ($housepricevar * $input{propertytax}); $insurancevar += ($housepricevar * $input{hazins}); $repairsvar += ($housepricevar / 100); if ( $count <= ($loantermvar / 12)) { $rvb{pri, $count} = ($yearprinintvar )- (($intratevar * $loanamountvar) ); $rvb{intr, $count} = ($intratevar * $loanamountvar); $loanamountvar = (1 + $intratevar) * $loanamountvar - $yearprinintvar; $rvb{loanamt, $count} = $loanamountvar; } else { $loanamountvar = 0; $yearprinintvar = 0; $rvb{pri, $count} = 0; $rvp{intr, $count} = 0; $rvb{loanamt, $count} = 0; } $taxsvgsvar += (-1 * $input{incometax} * ($intratevar * $loanamountvar + ($housepricevar * $input{propertytax}))); $oppcostvar += ((1 - $input{incometax}) * $input{returnoninvestment} * $downpaymentvar); $remtincvar += (-12 * (1 - $input{incometax}) * $rentalincomevar); # used to be a monthly V $bcondoduesvar += $condoduesvar; if ( ($input{flow} eq "cashflow")) { $amortvar = 0; $appvar = 0; } else { $amortvar += (-1 * ($yearprinintvar - ($intratevar * $loanamountvar))); $appvar += (-1 * $housepricevar * $input{appreciation}); } $rvb{buycost, $count} = ($count <= ($loantermvar / 12))? ($count * $yearprinintvar + $proptaxvar + $insurancevar + $repairsvar + $closcostsvar + $taxsvgsvar + $oppcostvar + $remtincvar + $bcondoduesvar + $amortvar + $appvar): ($loantermvar * $pivar + $proptaxvar + $insurancevar + $repairsvar + $closcostsvar + $taxsvgsvar + $oppcostvar + $remtincvar + $bcondoduesvar + $amortvar + $appvar); $rvb{rentcost, $count} = $rvb{rentcost, $count - 1} + (12 * $renterscostvar); $count++; } if ( $count <= ($loantermvar / 12)) { $principalinterest = $pivar; } else { $principalinterest = $pivar * $loantermvar / $monthsheldvar; } $buyers{propertytax} = $proptaxvar / $monthsheldvar; $buyers{insurance} = $insurancevar / $monthsheldvar; $buyers{repairs} = $repairsvar / $monthsheldvar; $buyers{closingcosts} = $closcostsvar / $monthsheldvar; $buyers{taxsavings} = $taxsvgsvar / $monthsheldvar; $buyers{opportunitycost} = $oppcostvar / $monthsheldvar; $buyers{rentalincome} = $remtincvar / $monthsheldvar; $buyers{condodues} = $bcondoduesvar / $monthsheldvar; $buyers{amortization} = $amortvar / $monthsheldvar; $buyers{appreciation} = $appvar / $monthsheldvar; $renters{realmonthlycost} = ($rvb{rentcost, $numholdingyrsvar} / $monthsheldvar); $buyers{realmonthlycost} = ($rvb{buycost, $numholdingyrsvar} / $monthsheldvar); } } sub dumpinfo { printf( <After Tax Monthly Costs Principal Interest \$%9.2fClosing Costs \$%9.2f Condo Dues \$%9.2f Property Tax \$%9.2fTax Savings \$%9.2f Amortization \$%9.2f Insurance \$%9.2fOpportunity Cost \$%9.2f Appreciation \$%9.2f Repairs \$%9.2fRental Income \$%9.2f
 BuyingRenting
Monthly Payment\$%9.2f\$%9.2f
Monthly Interest\$%9.2f\$0.00
Total Cost if Paid Today\$%9.2f\$%9.2f
endDump if ($input{form} ne "firstmonth") { print ""; print ""; foreach $count (0 .. $input{timetohold}) { printf("",$count,$rvb{pri, $count},$rvb{intr, $count},$rvb{loanamt, $count},$rvb{buycost, $count},$rvb{rentcost, $count}); } print "
Yr.PrincipalInterestLoan AmountBuying CostRenting Cost
%2.0f\$%10.2f\$%10.2f\$%10.2f\$%10.2f\$%10.2f
"; } } sub doform { if ($forms) { open(INPUTFORM,"rvb/rvbinpre.html") || die "Can't open your output form-pre. $!\n"; while() { print $_; } close INPUTFORM; } #hidden print qq!
!; print qq!!; print qq!!; print qq!!; print qq!!; print qq!!; print qq!!; #renter print<Buying

\$ Purchase Price
\$ Loan Amount
% Interest Rate
Loan Term in years
% Closing Costs in Pts.
\$ Yearly Homeowner's Insurance
/Condo Dues
% Property Tax
% Income Tax (Fed/State)
% Appreciation
Time expected to Occupy
endBuyerQuestions print<Renting:

\$

Monthly Rent

\$

Insurance Premium

\$

Monthly Repairs

\$

Carport Fees

\$

Association Fees

\$

Other Costs
Associated
with Renting

endRenterQuestions #control print qq! Cash Flow Avg
!; print qq! Economic Costs Avg
!; print qq! First Month Only
!; print qq!
!; print qq! !; print "
"; if ($forms) { open(INPUTFORM,"rvb/rvbinpost.html") || die "Can't open your output form-pre. $!\n"; while() { print $_; } close INPUTFORM; } print qq!


Return to Home Page

! if ($input{c}); } sub noweb { local($in,$i,$key,$val); $in = ""; # "global" vars $in .= "&banana=spiders"; $in .= "&c=abcmortgage"; #list of input vars #buyer $in .= "&purchaseprice_=100,000"; $in .= "&loanamount_=80,000"; $in .= "&interestrate%=8"; $in .= "&loanterm=30"; $in .= "&points%=0"; $in .= "&incometax%=37"; $in .= "&hazins%=0.2"; $in .= "&closcosts%=2"; $in .= "&propertytax%=1"; $in .= "&returnoninvestment%=8"; $in .= "&rentalincome_=0"; $in .= "&condodues_=0"; $in .= "&appreciation%=5"; $in .= "&timetohold=30"; #renter $in .= "&rent_=1,000"; $in .= "&rentersinsurance_=100"; $in .= "&rentersrepairs_=0"; $in .= "&renterscarport_=0"; $in .= "&rentersofees_=0"; #control (flow = cashflow, ecocost, firstmonth) $in .= "&flow=cashflow"; @input = split(/&/,$in); foreach $i (1 .. $#input) { ($key, $val) = split(/=/,$input[$i],2); if (index($key,"\%") > $[) { $val = $val * 0.01; $key =~ s/\%//; } if (index($key,"_") > $[) { $val =~ s/\,//; $key =~ s/_//; } $input{$key} .= $val; # print "$key = $val\n"; } } main: { $forms = true ; print &PrintHeader; if (&MethGet) { &ReadParse (*input); &doform; } else { &noweb if (! &ReadParse (*input)); if ($forms) { open(INPUTFORM,"rvb/rvboutpre.html") || die "Can't open your output form-pre. $!\n"; while() { print $_; } close INPUTFORM; } #guts of the calc &RentvsBuy; &dumpinfo; if ($forms) { open(INPUTFORM,"rvb/rvboutpost.html") || die "Can't open your output form-pre. $!\n"; while() { print $_; } close INPUTFORM; } print qq!


Return to Home Page

! if ($input{c}); } }