#! /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( <| Buying | Renting | |
| Monthly Payment | \$%9.2f | \$%9.2f |
| Monthly Interest | \$%9.2f | \$0.00 |
| Total Cost if Paid Today | \$%9.2f | \$%9.2f |
| Yr. | Principal | Interest | Loan Amount | Buying Cost | Renting Cost |
| %2.0f | \$%10.2f | \$%10.2f | \$%10.2f | \$%10.2f | \$%10.2f |