~ubuntu-branches/ubuntu/hoary/gnucash/hoary

« back to all changes in this revision

Viewing changes to src/calculation/amort_prt.c

  • Committer: Bazaar Package Importer
  • Author(s): James A. Treacy
  • Date: 2002-03-16 14:14:59 UTC
  • Revision ID: james.westby@ubuntu.com-20020316141459-wtkyyrpfovryhl1s
Tags: upstream-1.6.6
ImportĀ upstreamĀ versionĀ 1.6.6

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
                          amort_prt.c  -  description
 
3
                             -------------------
 
4
    begin                : Thursday June 15 2000
 
5
    email                : tboldt@attglobal.net
 
6
    Author               : Terry D. Boldt
 
7
 ***************************************************************************/
 
8
 
 
9
/***************************************************************************
 
10
 *                                                                         *
 
11
 *   This program is free software; you can redistribute it and/or modify  *
 
12
 *   it under the terms of the GNU General Public License as published by  *
 
13
 *   the Free Software Foundation; either version 2 of the License, or     *
 
14
 *   (at your option) any later version.                                   *
 
15
 *                                                                         *
 
16
 ***************************************************************************/
 
17
 
 
18
/*
 
19
 *  Functions to print amortization schedules
 
20
 *  6-15-2000
 
21
 *
 
22
 */
 
23
 
 
24
#include <stdio.h>
 
25
#include <stdlib.h>
 
26
#include <string.h>
 
27
#include <time.h>
 
28
#include <mcheck.h>
 
29
 
 
30
#include "finvar.h"
 
31
#include "finproto.h"
 
32
#include "fin_spl_protos.h"
 
33
 
 
34
 
 
35
void             prt_amortization_schedule(
 
36
amort_sched_ptr  amortsched,  /* amortization schedule to print           */
 
37
FILE            *ofile)      /* output file                               */
 
38
{
 
39
        unsigned            j,
 
40
                            jj,
 
41
                            prec = amortsched->prec,
 
42
                            option = amortsched->option,
 
43
                            fv_case = amortsched->fv_case;
 
44
    unsigned char       datel[100],
 
45
                        summary = amortsched->summary;
 
46
    struct tm          *times_E,
 
47
                                   *times_I;
 
48
        amort_sched_yr_ptr  amortyr,
 
49
                            prst_yr;
 
50
        sched_pmt_ptr       pmtsched = NULL;
 
51
        yearly_summary_ptr  annual_summary;
 
52
 
 
53
    times_E = (struct tm *)calloc(1,sizeof(struct tm));
 
54
    times_E->tm_mday = amortsched->day_E;
 
55
    times_E->tm_mon  = amortsched->month_E - 1;
 
56
    times_E->tm_year = amortsched->year_E - 1900;
 
57
    times_E->tm_wday = (amortsched->Eff_Date_jdn + 1) % 7;
 
58
    times_E->tm_yday = amortsched->yday_E;
 
59
 
 
60
    times_I = (struct tm *)calloc(1,sizeof(struct tm));
 
61
    times_I->tm_mday = amortsched->day_I;
 
62
    times_I->tm_mon  = amortsched->month_I - 1;
 
63
    times_I->tm_year = amortsched->year_I - 1900;
 
64
    times_I->tm_wday = (amortsched->Init_Date_jdn + 1) % 7;
 
65
    times_I->tm_yday = amortsched->yday_I;
 
66
 
 
67
    fprintf(ofile,"Amortization Table\n");
 
68
    strftime(datel,(size_t)100,"%c",times_E);
 
69
    fprintf(ofile,"Effective       Date: %s\n",datel);
 
70
    strftime(datel,(size_t)100,"%c",times_I);
 
71
    fprintf(ofile,"Initial Payment Date: %s\n",datel);
 
72
    fprintf(ofile,"Compounding Frequency per year: %u\n",amortsched->CF);
 
73
    fprintf(ofile,"Payment     Frequency per year: %u\n",amortsched->PF);
 
74
    fprintf(ofile,"Compounding: %s\n",(amortsched->disc ? "Discrete" : "Continuous"));
 
75
    fprintf(ofile,"Payments: %s\n",(amortsched->bep ? "Beginning of Period" : "End of Period"));
 
76
    fprintf(ofile,"Payments (%u): %.*f\n",amortsched->n - 1,(int)prec,(option < 3) ? amortsched->cpmt : (option == 5) ? amortsched->new_pmt : amortsched->pmt);
 
77
    fprintf(ofile,"Final payment (%u): %.*f\n",amortsched->n,(int)prec,amortsched->final_pmt);
 
78
    if ( (amortsched->CF == 1) && (amortsched->PF == 1) ) fprintf(ofile,"Nominal Interest per Payment Period: %g\t(Annualized: %g)\n",amortsched->nint,amortsched->nint * 12);
 
79
      else fprintf(ofile,"Nominal Annual Interest Rate: %g\n",amortsched->nint);
 
80
    fprintf(ofile,"  Effective Interest Rate Per Payment Period: %g\n",amortsched->eint);
 
81
    fprintf(ofile,"Present Value: %.*f\n",(int)prec,amortsched->pv);
 
82
    if ( (amortsched->option == 2) || (amortsched->option > 3) ) {
 
83
        fprintf(ofile,"Interest due to Delayed Intial Payment: %.*f\n",(int)prec,amortsched->delayed_int);
 
84
    } /* endif */
 
85
 
 
86
    free(times_E);
 
87
    free(times_I);
 
88
 
 
89
    if ( amortsched->option < 3 ) {
 
90
        summary = (summary == 'y') ? 'x' : 'o';
 
91
    } /* endif */
 
92
 
 
93
    switch ( summary ) {
 
94
        case 'a':
 
95
                /* variable prepayment schedule
 
96
                 */
 
97
            fprintf(ofile,"Advanced Prepayment Amortization - Variable Prepayment\n");
 
98
                        amortyr = amortsched->schedule.first_yr;
 
99
                        for ( j = amortsched->total_periods , jj = 0 ; j && amortyr ; j-- ) {
 
100
                                if ( !jj ) {
 
101
                    fprintf(ofile,"Pmt *     Interest    Principal       Prepay    Total Pmt      Balance\n");
 
102
                                        pmtsched = amortyr->payments;
 
103
                                        jj = amortyr->num_periods;
 
104
                                } /* endif */
 
105
 
 
106
                fprintf(ofile,"%4u  %12.*f %12.*f %12.*f %12.*f %12.*f\n",
 
107
                               pmtsched->period_num,
 
108
                               (int)prec,pmtsched->interest,
 
109
                               (int)prec,pmtsched->principal,
 
110
                               (int)prec,pmtsched->advanced_pmt,
 
111
                               (int)prec,pmtsched->total_pmt,
 
112
                               (int)prec,pmtsched->balance);
 
113
 
 
114
                if ( !--jj ) {
 
115
                    fprintf(ofile,"Summary for: %u:\n",amortyr->year);
 
116
                    fprintf(ofile,"  Interest  Paid: %.*f\n",(int)prec,amortyr->interest_pd);
 
117
                    fprintf(ofile,"  Principal Paid: %.*f\n",(int)prec,amortyr->principal_pd);
 
118
                    fprintf(ofile,"  Year Ending Balance: %.*f\n",(int)prec,amortyr->yr_end_balance);
 
119
                    fprintf(ofile,"  Sum of Interest Paid: %.*f\n",(int)prec,amortyr->total_interest_pd);
 
120
                    prst_yr = amortyr;
 
121
                    amortyr = amortyr->next_yr;
 
122
                  } else {
 
123
                        pmtsched++;
 
124
                } /* endif */
 
125
                } /* endfor */
 
126
                        break;
 
127
                case 'f':
 
128
                        /* fixed prepayment schedule
 
129
                         */
 
130
            fprintf(ofile,"Advanced Prepayment Amortization - Fixed Prepayment: %.*f\n",(int)prec,amortsched->fixed_pmt);
 
131
                        amortyr = amortsched->schedule.first_yr;
 
132
                        for ( j = amortsched->total_periods , jj = 0 ; j && amortyr ; j-- ) {
 
133
                                if ( !jj ) {
 
134
                    fprintf(ofile,"Pmt *     Interest    Principal       Prepay    Total Pmt      Balance\n");
 
135
                                        pmtsched = amortyr->payments;
 
136
                                        jj = amortyr->num_periods;
 
137
                                } /* endif */
 
138
 
 
139
                fprintf(ofile,"%4u  %12.*f %12.*f %12.*f %12.*f %12.*f\n",
 
140
                               pmtsched->period_num,
 
141
                               (int)prec,pmtsched->interest,
 
142
                               (int)prec,pmtsched->principal,
 
143
                               (int)prec,pmtsched->advanced_pmt,
 
144
                               (int)prec,pmtsched->total_pmt,
 
145
                               (int)prec,pmtsched->balance);
 
146
 
 
147
                if ( !--jj ) {
 
148
                    fprintf(ofile,"Summary for: %u:\n",amortyr->year);
 
149
                    fprintf(ofile,"  Interest  Paid: %.*f\n",(int)prec,amortyr->interest_pd);
 
150
                    fprintf(ofile,"  Principal Paid: %.*f\n",(int)prec,amortyr->principal_pd);
 
151
                    fprintf(ofile,"  Year Ending Balance: %.*f\n",(int)prec,amortyr->yr_end_balance);
 
152
                    fprintf(ofile,"  Sum of Interest Paid: %.*f\n",(int)prec,amortyr->total_interest_pd);
 
153
                    prst_yr = amortyr;
 
154
                    amortyr = amortyr->next_yr;
 
155
                  } else {
 
156
                        pmtsched++;
 
157
                } /* endif */
 
158
                } /* endfor */
 
159
                        break;
 
160
                case 'o':
 
161
                        /* constant payment to principal
 
162
                         */
 
163
            fprintf(ofile,"Constant Payment to Principal: %.*f\n",(int)prec,amortsched->cpmt);
 
164
                        amortyr = amortsched->schedule.first_yr;
 
165
                        for ( j = amortsched->total_periods , jj = 0 ; j && amortyr ; j-- ) {
 
166
                                if ( !jj ) {
 
167
                    fprintf(ofile,"Pmt#       Interest  Total Payment        Balance\n");
 
168
                                        pmtsched = amortyr->payments;
 
169
                                        jj = amortyr->num_periods;
 
170
                                } /* endif */
 
171
 
 
172
                fprintf(ofile,"%4u   %12.*f   %12.*f   %12.*f\n",
 
173
                               pmtsched->period_num,
 
174
                               (int)prec,pmtsched->interest,
 
175
                               (int)prec,pmtsched->total_pmt,
 
176
                               (int)prec,pmtsched->balance);
 
177
 
 
178
                if ( !--jj ) {
 
179
                    fprintf(ofile,"Summary for: %u:\n",amortyr->year);
 
180
                    fprintf(ofile,"  Interest  Paid: %.*f\n",(int)prec,amortyr->interest_pd);
 
181
                    fprintf(ofile,"  Principal Paid: %.*f\n",(int)prec,amortyr->principal_pd);
 
182
                    fprintf(ofile,"  Year Ending Balance: %.*f\n",(int)prec,amortyr->yr_end_balance);
 
183
                    fprintf(ofile,"  Sum of Interest Paid: %.*f\n",(int)prec,amortyr->total_interest_pd);
 
184
                    prst_yr = amortyr;
 
185
                    amortyr = amortyr->next_yr;
 
186
                  } else {
 
187
                        pmtsched++;
 
188
                } /* endif */
 
189
                } /* endfor */                  
 
190
                        break;
 
191
                case 'p':
 
192
                        /* normal payment schedule
 
193
                         */
 
194
                        fprintf(ofile,"Normal Amortization Schedule\n");
 
195
                        amortyr = amortsched->schedule.first_yr;
 
196
                        for ( j = amortsched->total_periods - 1 , jj = 0 ; j && amortyr ; j-- ) {
 
197
                                if ( !jj ) {
 
198
                    fprintf(ofile,amortsched->fv_case ? "Pmt *       Interest        Balance\n" : "Pmt *       Interest      Principal        Balance\n");
 
199
                                        pmtsched = amortyr->payments;
 
200
                                        jj = amortyr->num_periods;
 
201
                                } /* endif */
 
202
 
 
203
                                if ( fv_case ) {
 
204
                                        fprintf(ofile,"%4u   %12.*f   %12.*f\n",
 
205
                                  pmtsched->period_num,
 
206
                                  (int)prec,pmtsched->interest,
 
207
                                  (int)prec,pmtsched->balance);
 
208
                                  } else {
 
209
                    fprintf(ofile,"%4u    %12.*f   %12.*f   %12.*f\n",
 
210
                                  pmtsched->period_num,
 
211
                                  (int)prec,pmtsched->interest,
 
212
                                  (int)prec,pmtsched->principal,
 
213
                                  (int)prec,pmtsched->balance);                                 
 
214
                                } /* endif */
 
215
 
 
216
                if ( !--jj ) {
 
217
                    fprintf(ofile,"Summary for: %u:\n",amortyr->year);
 
218
                    fprintf(ofile,"  Interest  Paid: %.*f\n",(int)prec,amortyr->interest_pd);
 
219
                    if ( !fv_case ) fprintf(ofile,"  Principal Paid: %.*f\n",(int)prec,amortyr->principal_pd);
 
220
                    fprintf(ofile,"  Year Ending Balance: %.*f\n",(int)prec,amortyr->yr_end_balance);
 
221
                    fprintf(ofile,"  Sum of Interest Paid: %.*f\n",(int)prec,amortyr->total_interest_pd);
 
222
                    prst_yr = amortyr;
 
223
                    amortyr = amortyr->next_yr;
 
224
                  } else {
 
225
                        pmtsched++;
 
226
                } /* endif */
 
227
                } /* endfor */
 
228
                
 
229
                if ( !jj ) {
 
230
                        fprintf(ofile,amortsched->fv_case ? "Pmt *       Interest        Balance\n" : "Pmt *       Interest      Principal        Balance\n");
 
231
                                pmtsched = amortyr->payments;
 
232
                        } /* endif */
 
233
                        
 
234
                        fprintf(ofile,"Final Payment: %.*f\n",(int)prec,amortyr->final_pmt);
 
235
                        
 
236
                        if ( fv_case ) {
 
237
                                fprintf(ofile,"%4u   %12.*f   %12.*f\n",
 
238
                                           pmtsched->period_num,
 
239
                                           (int)prec,pmtsched->interest,
 
240
                                        (int)prec,pmtsched->balance);
 
241
                          } else {
 
242
                fprintf(ofile,"%4u    %12.*f   %12.*f   %12.*f\n",
 
243
                               pmtsched->period_num,
 
244
                               (int)prec,pmtsched->interest,
 
245
                                   (int)prec,pmtsched->principal,
 
246
                               (int)prec,pmtsched->balance);
 
247
            } /* endif */
 
248
                        
 
249
                        fprintf(ofile,"Summary for: %u:\n",amortyr->year);
 
250
            fprintf(ofile,"  Interest  Paid: %.*f\n",(int)prec,amortyr->interest_pd);
 
251
            if ( !fv_case ) fprintf(ofile,"  Principal Paid: %.*f\n",(int)prec,amortyr->principal_pd);
 
252
            fprintf(ofile,"  Year Ending Balance: %.*f\n",(int)prec,amortyr->yr_end_balance);
 
253
            fprintf(ofile,"  Sum of Interest Paid: %.*f\n",(int)prec,amortyr->total_interest_pd);
 
254
                        break;
 
255
                case 'x':
 
256
                        /* constant payment to principal - annual summary
 
257
                         */
 
258
                case 'y':
 
259
                        /* normal payment - annual summary
 
260
                         */
 
261
                        if ( summary == 'x' ) fprintf(ofile,"Annual Summary - Constant Payment to Principal: %.*f\n",(int)prec,amortsched->cpmt);
 
262
                          else fprintf(ofile,"Annual Summary - Normal Amortization\n");
 
263
                        fprintf(ofile,"Year      Interest   Ending Balance\n");
 
264
                        annual_summary = amortsched->schedule.summary;
 
265
                        for ( j = amortsched->total_periods , jj = 0 ; j ; j-- , jj++ ) {
 
266
                                fprintf(ofile,"%4u  %12.*f   %12.*f\n",
 
267
                               annual_summary[jj].year,
 
268
                               (int)prec,annual_summary[jj].interest,
 
269
                               (int)prec,annual_summary[jj].end_balance);
 
270
                        } /* endfor */
 
271
                        break;
 
272
        } /* endswitch */
 
273
        
 
274
    fprintf(ofile,"\nTotal Interest: %.*f\n",(int)prec,amortsched->total_interest);
 
275
 
 
276
} /* prt_amortization_schedule */