33bf1a65870f00e5dd6bc4674c1c59c43896f8b6
[moodle.git] / blocks / loancalc / block_loancalc.php
1 <?php
3 class block_loancalc extends block_base {
5     function init() {
6         $this->title = get_string('loancalc','block_loancalc');
7         $this->content_type = BLOCK_TYPE_TEXT;
8         $this->version = 2007101509;
9     }
11     function get_content() {
12         global $CFG, $OUTPUT;
14         $calc = $OUTPUT->pix_url('i/calc');
16         $this->content->text = '
17         <script type="text/javascript">
18        // <![CDATA[
19     function Next()
20     {
21         submitScreen("Next");
22         document.getElementById("vbankform").submit();
23     }
24     function Back()
25     {
26         submitScreen("Back");
27         document.getElementById("vbankform").submit();
28     }
30     function num_format(x) { // format numbers with two digits
31     sgn = (x < 0);
32     x = Math.abs(x);
33     x = Math.floor((x * 100) + .5);
34     i = 3;
35     y = "";
36     while(((i--) > 0) || (x > 0)) {
37         y = (x % 10) + y;
38         x = Math.floor(x / 10);
39         if(i == 1) {
40             y = "." + y;
41         }
42     }
43     if(sgn) {
44         y = "-" + y;
45     }
46     return(y);
47 }
48 function comp(v) { // general entry point for all cases
50     // convert all entry fields into variables
51     x = document.getElementById("vbankform");
52     pv = parseFloat(x.LOANAMOUNT.value);
53     lpp = parseFloat(x.LOANPAYPERIOD[x.LOANPAYPERIOD.selectedIndex].value);
54     if (isNaN(pv) && (v != "pv"))
55     {
56         x.LOANAMOUNT.select();
57         x.LOANAMOUNT.focus();
58         alert("Numbers only to be entered");
59         return;
60     }
61     fv = parseFloat("0");
62     yr = parseFloat(x.LOANTERM.value);
63     if (isNaN(yr) && (v != "np"))
64     {
65         x.LOANTERM.select();
66         x.LOANTERM.focus();
67         alert("Numbers only to be entered");
68         return;
69     }
70     np = lpp * yr;
71     pmt = -parseFloat(x.LOANREPAYMENT.value);
72     if (isNaN(pmt) && (v != "pmt"))
73     {
74         x.LOANREPAYMENT.select();
75         x.LOANREPAYMENT.focus();
76         alert("Numbers only to be entered");
77         return;
78     }
79     if(x.LOANINTRATE.value == "") {
80         alert("You must enter an interest rate (ir).");
81     }
82     else {
83         ir = parseFloat(x.LOANINTRATE.value);
84         if (isNaN(ir))
85         {
86             x.LOANINTRATE.select();
87             x.LOANINTRATE.focus();
88             alert("Numbers only to be entered");
89             return;
90         }
91         ir = ((ir / lpp) / 100);
93         // test and compute all cases
95         if (v == "pv") {
96             if(ir == 0) {
97                 pv = -(fv + (pmt * np));
98             }
99             else {
100                 q1 = Math.pow(1 + ir,-np);
101                 q2 = Math.pow(1 + ir,np);
102                 pv = -(q1 * (fv * ir - pmt + q2 * pmt))/ir;
103             }
104             x.LOANAMOUNT.value = num_format(pv);
105         }
107         if (v == "np") {
108             if(ir == 0) {
109                 if(pmt != 0) {
110                     np = - (fv + pv)/pmt;
111                 }
112                 else {
113                     alert("Divide by zero error.");
114                 }
115             }
116             else {
117                 np = Math.log((-fv * ir + pmt)/(pmt + ir * pv))/ Math.log(1 + ir);
118             }
119             if(np == 0) {
120                 alert("Can\'t compute Number of Periods for the present values.");
121             }
122             else {
123                 np = (np / lpp)
124                 if (isNaN(np)) {
125                     alert("The repayment amount is less than the interest. You must increase your repayments to pay off this loan!");
126                 } else {
127                     x.LOANTERM.value = num_format(np);
128                 }
129             }
130         }
132         if (v == "pmt") {
133             if(ir == 0.0) {
134                 if(np != 0) {
135                     pmt = (fv + pv)/np;
136                 }
137                 else {
138                     alert("Divide by zero error.");
139                 }
140             }
141             else {
142                 q = Math.pow(1 + ir,np);
143                 pmt = ((ir * (fv + q * pv))/(-1 + q));
144             }
145             x.LOANREPAYMENT.value = num_format(pmt);
146         }
149     }
150 } // function comp
151 //]]>
152 </script>
153 <form method="post" id="vbankform" action="">
154             <table>
155                 <tr>
156                     <td colspan="2">'.get_string('amountofloan','block_loancalc').'</td>
157                 </tr>
158                 <tr>
159                     <td><input name="LOANAMOUNT" id="LOANAMOUNT" size="17" /></td>
160                     <td><a href="JavaScript:comp(\'pv\');"><img src="'.$calc.'" alt="calculate" /></a></td>
161                 </tr>
162                 <tr>
163                     <td colspan="2">'.get_string('repaymentamount','block_loancalc').'</td>
164                 </tr>
165                 <tr>
166                     <td><input name="LOANREPAYMENT" id="LOANREPAYMENT" size="17" /></td>
167                     <td><a href="JavaScript:comp(\'pmt\');"><img src="'.$calc.'" alt="calculate" /></a></td>
168                 </tr>
169                 <tr>
170                     <td colspan="2">'.get_string('loanterm','block_loancalc').'</td>
171                 </tr>
172                 <tr>
173                     <td><input name="LOANTERM" id="LOANTERM" size="17" /></td>
174                     <td><a href="JavaScript:comp(\'np\');"><img src="'.$calc.'" alt="calculate" /></a></td>
175                 </tr>
176                 <tr>
177                     <td colspan="2">'.get_string('interestrate','block_loancalc').'</td>
178                 </tr>
179                 <tr>
180                     <td><input name="LOANINTRATE" id="LOANINTRATE" size="17" /></td>
181                     <td></td>
182                 </tr>
183                 <tr>
184                     <td colspan="2">'.get_string('repaymentfreq','block_loancalc').'</td>
185                 </tr>
186                 <tr>
187                     <td>';
188         $options[52] = get_string('weekly','block_loancalc');
189         $options[26] = get_string('fortnightly','block_loancalc');
190         $options[12] = get_string('monthly','block_loancalc');
191         $this->content->text .= html_writer::select($options,'LOANPAYPERIOD','12');
192         $this->content->text .= '</td>
193                     <td></td>
194                 </tr>
195             </table>
196             </form>';
197         $this->content->footer = '';
199         return $this->content;
201     }