1 #ident "Kalopa: $Id: nabal.c,v 1.9 2008/01/15 18:10:43 dtynan Exp $"
2
3 /*
4 * $Id: nabal.c,v 1.9 2008/01/15 18:10:43 dtynan Exp $
5 *
6 * Copyright (c) 2004, Kalopa Media Limited. All rights reserved.
7 * Copyright (c) 2005, Kalopa Research Limited. All rights reserved.
8 * This is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published
10 * by the Free Software Foundation; either version 2, or (at your
11 * option) any later version.
12 *
13 * It is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16 * License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this product; see the file COPYING. If not, write to
20 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
21 * USA.
22 *
23 * THIS SOFTWARE IS PROVIDED BY KALOPA RESEARCH LIMITED "AS IS" AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
26 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KALOPA
27 * RESEARCH LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
30 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
31 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
32 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
34 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 * ABSTRACT
37 *
38 * $Log: nabal.c,v $
39 * Revision 1.9 2008/01/15 18:10:43 dtynan
40 * Changed to use new company name and copyright, also fixed a few old
41 * files with the wrong license.
42 *
43 * Revision 1.8 2008/01/15 15:25:36 dtynan
44 * Changed table name.
45 *
46 * Revision 1.7 2006/07/31 10:17:51 dtynan
47 * Massive changeover to a more Ruby/Rails primary key naming convention.
48 *
49 * Revision 1.6 2005/04/11 13:52:41 dtynan
50 * Changed the nominal account code to only load the list of nominal
51 * accounts once.
52 *
53 * Revision 1.5 2005/03/22 09:41:18 dtynan
54 * Extensive upgrade to make sure the copyright block was
55 * consistent and GPL'ed.
56 *
57 * Revision 1.4 2005/02/17 17:25:26 dtynan
58 * Fixed some compiler warnings.
59 *
60 * Revision 1.3 2004/12/09 20:21:06 dtynan
61 * Fixed wrong include files.
62 *
63 * Revision 1.2 2004/12/09 19:19:30 dtynan
64 * Added a function to free nominal account balances.
65 *
66 * Revision 1.1 2004/12/08 17:42:29 dtynan
67 * Added functions to manage and manipulate journal entry balances.
68 */
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <unistd.h>
72 #include <time.h>
73
74 #include "beanie.h"
75
76 struct db_nomacct *findnomaccts(dbow_conn *);
77 struct db_journal *findjournalentries(dbow_conn *);
78 struct db_journal_item *findjournalitems(dbow_conn *, int);
79
80 /*
81 *
82 */
83 struct nabal *
loadnabal(dbow_conn * conn)84 loadnabal(dbow_conn *conn)
85 {
86 struct nabal *np;
87 static struct nabal *head = NULL;
88
89 /*
90 * Only load the nominal account data once, but clear the
91 * balances every time.
92 */
93 if (head == NULL) {
94 struct db_nomacct *ap;
95 struct nabal *tail = NULL;
96
97 for (ap = findnomaccts(conn); ap != NULL;
98 ap = db_findnomacctnext(conn, ap)) {
99 if ((np = (struct nabal *)malloc(sizeof(*np))) == NULL)
100 return(NULL);
101 np->next = NULL;
102 np->nomacct_id = ap->id;
103 np->atype = ap->atype;
104 np->name = ap->aname;
105 ap->aname = NULL;
106 np->bal = 0.0;
107 if (tail == NULL)
108 head = np;
109 else
110 tail->next = np;
111 tail = np;
112 }
113 } else
114 for (np = head; np != NULL; np = np->next)
115 np->bal = 0.0;
116 return(head);
117 }
118
119 /*
120 *
121 */
122 struct nabal *
findnabal(struct nabal * nlist,int acctid)123 findnabal(struct nabal *nlist, int acctid)
124 {
125 while (nlist != NULL && nlist->nomacct_id != acctid)
126 nlist = nlist->next;
127 return(nlist);
128 }
129
130 /*
131 *
132 */
133 void
loadbalances(dbow_conn * conn,struct nabal * nlist,int year)134 loadbalances(dbow_conn *conn, struct nabal *nlist, int year)
135 {
136 struct nabal *np;
137 struct db_journal *jp;
138 struct db_journal_item *jip;
139 struct tm *tmp;
140
141 for (jp = findjournalentries(conn); jp != NULL; jp =
142 db_findjournalnext(conn, jp)) {
143 if ((tmp = localtime((time_t *)&jp->jdate)) == NULL ||
144 (tmp->tm_year != year))
145 continue;
146 for (jip = findjournalitems(conn, jp->id); jip != NULL;
147 jip = db_findjournal_itemnext(conn, jip)) {
148 if ((np = findnabal(nlist, jip->nomacct_id)) == NULL) {
149 printf("loadbalances: can't find acct %d\n",
150 jip->nomacct_id);
151 continue;
152 }
153 if (jip->drcrf)
154 np->bal += jip->amount;
155 else
156 np->bal -= jip->amount;
157 }
158 }
159 }
160