2 * Copyright (C) 1987-2008 Sun Microsystems, Inc. All Rights Reserved.
3 * Copyright (C) 2008-2012 Robert Ancell
5 * This program is free software: you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation, either version 2 of the License, or (at your option) any later
8 * version. See http://www.gnu.org/copyleft/gpl.html the full text of the
12 public enum FinancialDialog
26 public void do_finc_expression (MathEquation equation
, FinancialDialog function
, Number arg1
, Number arg2
, Number arg3
, Number arg4
)
31 case FinancialDialog
.CTRM_DIALOG
:
32 result
= calc_ctrm (equation
, arg1
, arg2
, arg3
);
34 case FinancialDialog
.DDB_DIALOG
:
35 result
= calc_ddb (equation
, arg1
, arg2
, arg3
);
37 case FinancialDialog
.FV_DIALOG
:
38 result
= calc_fv (equation
, arg1
, arg2
, arg3
);
40 case FinancialDialog
.GPM_DIALOG
:
41 result
= calc_gpm (equation
, arg1
, arg2
);
43 case FinancialDialog
.PMT_DIALOG
:
44 result
= calc_pmt (equation
, arg1
, arg2
, arg3
);
46 case FinancialDialog
.PV_DIALOG
:
47 result
= calc_pv (equation
, arg1
, arg2
, arg3
);
49 case FinancialDialog
.RATE_DIALOG
:
50 result
= calc_rate (equation
, arg1
, arg2
, arg3
);
52 case FinancialDialog
.SLN_DIALOG
:
53 result
= calc_sln (equation
, arg1
, arg2
, arg3
);
55 case FinancialDialog
.SYD_DIALOG
:
56 result
= calc_syd (equation
, arg1
, arg2
, arg3
, arg4
);
58 case FinancialDialog
.TERM_DIALOG
:
59 result
= calc_term (equation
, arg1
, arg2
, arg3
);
62 result
= new Number
.integer (0);
66 equation
.set_number (result
);
69 private Number
calc_ctrm (MathEquation equation
, Number pint
, Number fv
, Number pv
)
71 /* Cterm - pint (periodic interest rate).
75 * RESULT = log (fv / pv) / log (1 + pint)
77 var t1
= fv
.divide (pv
);
79 var t3
= pint
.add (new Number
.integer (1));
81 return t2
.divide (t4
);
84 private Number
calc_ddb (MathEquation equation
, Number cost
, Number life
, Number period
)
86 /* Ddb - cost (amount paid for asset).
87 * life (useful life of the asset).
88 * period (time period for depreciation allowance).
91 * for (i = 0; i < life; i++)
93 * VAL = ((cost - bv) * 2) / life
100 var z
= new Number
.integer (0);
102 var tbv
= new Number
.integer (0);
103 var len
= period
.to_integer ();
104 for (var i
= 0; i
< len
; i
++)
106 var t1
= cost
.subtract (tbv
);
107 var t2
= t1
.multiply_integer (2);
108 z
= t2
.divide (life
);
110 tbv
= t1
.add (z
); /* TODO: why result is tbv, for next loop? */
114 equation
.status
= _("Error: the number of periods must be positive");
119 private Number
calc_fv (MathEquation equation
, Number pmt
, Number pint
, Number n
)
121 /* Fv - pmt (periodic payment).
122 * pint (periodic interest rate).
123 * n (number of periods).
125 * RESULT = pmt * (pow (1 + pint, n) - 1) / pint
128 var t1
= pint
.add (new Number
.integer (1));
129 var t2
= t1
.xpowy (n
);
130 var t3
= t2
.add (new Number
.integer (-1));
131 var t4
= pmt
.multiply (t3
);
132 return t4
.divide (pint
);
135 private Number
calc_gpm (MathEquation equation
, Number cost
, Number margin
)
137 /* Gpm - cost (cost of sale).
138 * margin (gross profit margin.
140 * RESULT = cost / (1 - margin)
143 var t1
= new Number
.integer (1);
144 var t2
= t1
.subtract (margin
);
145 return cost
.divide (t2
);
148 private Number
calc_pmt (MathEquation equation
, Number prin
, Number pint
, Number n
)
150 /* Pmt - prin (principal).
151 * pint (periodic interest rate).
154 * RESULT = prin * (pint / (1 - pow (pint + 1, -1 * n)))
157 var t1
= pint
.add (new Number
.integer (1));
158 var t2
= n
.multiply_integer (-1);
159 var t3
= t1
.xpowy (t2
);
160 var t4
= t3
.multiply_integer (-1);
161 t1
= t4
.add (new Number
.integer (1));
162 t2
= pint
.divide (t1
);
163 return prin
.multiply (t2
);
166 private Number
calc_pv (MathEquation equation
, Number pmt
, Number pint
, Number n
)
168 /* Pv - pmt (periodic payment).
169 * pint (periodic interest rate).
172 * RESULT = pmt * (1 - pow (1 + pint, -1 * n)) / pint
175 var t1
= pint
.add (new Number
.integer (1));
176 var t2
= n
.multiply_integer (-1);
177 var t3
= t1
.xpowy (t2
);
178 var t4
= t3
.multiply_integer (-1);
179 t1
= t4
.add (new Number
.integer (1));
180 t2
= t1
.divide (pint
);
181 return pmt
.multiply (t2
);
184 private Number
calc_rate (MathEquation equation
, Number fv
, Number pv
, Number n
)
186 /* Rate - fv (future value).
187 * pv (present value).
190 * RESULT = pow (fv / pv, 1 / n) - 1
193 var t1
= fv
.divide (pv
);
194 var t2
= new Number
.integer (1);
195 var t3
= t2
.divide (n
);
196 var t4
= t1
.xpowy (t3
);
197 return t4
.add (new Number
.integer (-1));
200 private Number
calc_sln (MathEquation equation
, Number cost
, Number salvage
, Number life
)
202 /* Sln - cost (cost of the asset).
203 * salvage (salvage value of the asset).
204 * life (useful life of the asset).
206 * RESULT = (cost - salvage) / life
209 var t1
= cost
.subtract (salvage
);
210 return t1
.divide (life
);
213 private Number
calc_syd (MathEquation equation
, Number cost
, Number salvage
, Number life
, Number period
)
215 /* Syd - cost (cost of the asset).
216 * salvage (salvage value of the asset).
217 * life (useful life of the asset).
218 * period (period for which depreciation is computed).
220 * RESULT = (cost - salvage) * (life - period + 1) /
221 * (life * (life + 1)) / 2
224 var t3
= life
.subtract (period
).add (new Number
.integer (1));
225 var t2
= life
.add (new Number
.integer (1));
226 var t4
= life
.multiply (t2
);
227 var t1
= t4
.divide (new Number
.integer (2));
229 t1
= cost
.subtract (salvage
);
230 return t1
.multiply (t2
);
233 private Number
calc_term (MathEquation equation
, Number pmt
, Number fv
, Number pint
)
235 /* Term - pmt (periodic payment).
237 * pint (periodic interest rate).
239 * RESULT = log (1 + (fv * pint / pmt)) / log (1 + pint)
242 var t1
= pint
.add (new Number
.integer (1));
244 t1
= fv
.multiply (pint
);
245 var t3
= t1
.divide (pmt
);
246 var t4
= t3
.add (new Number
.integer (1));
248 return t1
.divide (t2
);