[app] Display 'transaction.value' after completion (bug #25)
[abstract.git] / report / aaTranscript.cpp
blobc3410417e7a37ff28899d514e41068535ed184ac
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */
3 /*
4 * Copyright (C) 2007 Sergey Yanovich <ynvich@gmail.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
16 * You should have received a copy of the GNU General Public
17 * License along with this program; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
22 #include <abstract/aacore.h>
24 #include "nsCOMPtr.h"
25 #include "nsComponentManagerUtils.h"
26 #include "nsIArray.h"
27 #include "nsStringAPI.h"
29 /* Unfrozen API */
30 #include "unstable/mozIStorageConnection.h"
32 /* Project includes */
33 #include <abstract/base/aaIFlow.h>
34 #include <abstract/base/aaIBalance.h>
35 #include <abstract/storage/aaIFilter.h>
36 #include <abstract/storage/aaIStateFilter.h>
37 #include <abstract/storage/aaBaseLoaders.h>
38 #include <abstract/storage/aaAccountLoaders.h>
39 #include <abstract/report/aaITranscript.h>
40 #include <abstract/storage/aaStorageUtils.h>
41 #include "aaTranscript.h"
43 aaTranscript::aaTranscript(nsISupports *aOuter)
45 nsCOMPtr<mozIStorageConnection> connection = do_QueryInterface(aOuter);
47 mOpeningBalance = do_CreateInstance(AA_LOADBALANCE_CONTRACT_ID, connection);
48 mOpeningIncome = do_CreateInstance(AA_LOADINCOME_CONTRACT_ID, connection);
49 mOpening = mOpeningBalance;
50 if (!mOpening)
51 return;
53 mClosingBalance = do_CreateInstance(AA_LOADBALANCE_CONTRACT_ID, connection);
54 mClosingIncome = do_CreateInstance(AA_LOADINCOME_CONTRACT_ID, connection);
55 mClosing = mClosingBalance;
56 if (!mClosing)
57 return;
59 mOpeningFilter = do_CreateInstance(AA_STATEFILTER_CONTRACT_ID);
60 if (!mOpeningFilter)
61 return;
63 mClosingFilter = do_CreateInstance(AA_STATEFILTER_CONTRACT_ID);
64 if (!mClosingFilter)
65 return;
67 mList = do_CreateInstance(AA_LOADFACTLIST_CONTRACT_ID, connection);
68 if (!mList)
69 return;
71 mConnection = connection;
74 aaTranscript::~aaTranscript()
78 NS_IMPL_ISUPPORTS4(aaTranscript,
79 nsIArray,
80 aaILoadQuery,
81 aaIFilter,
82 aaITranscript)
84 /* aaILoadQuery */
85 NS_IMETHODIMP
86 aaTranscript::Load()
88 NS_ENSURE_TRUE(mFlow, NS_ERROR_NOT_INITIALIZED);
89 nsresult rv;
90 if (mFlow->PickId()) {
91 mOpening = mOpeningBalance;
92 mClosing = mClosingBalance;
93 } else {
94 mOpening = mOpeningIncome;
95 mClosing = mClosingIncome;
97 mOpeningFilter->SetFlow( mFlow );
98 mOpeningFilter->SetDate( mStart );
99 mOpening->SetFilter( mOpeningFilter );
101 mClosingFilter->SetFlow( mFlow );
102 mClosingFilter->SetDate( mEnd );
103 mClosing->SetFilter( mClosingFilter );
105 mList->SetFilter(this);
107 rv = mOpening->Load();
108 NS_ENSURE_SUCCESS(rv, rv);
110 rv = mClosing->Load();
111 NS_ENSURE_SUCCESS(rv, rv);
113 rv = mList->Load();
114 NS_ENSURE_SUCCESS(rv, rv);
116 mLoadedFlow = mFlow;
117 return NS_OK;
120 NS_IMETHODIMP
121 aaTranscript::GetFilter(aaIFilter * * aFilter)
123 return NS_ERROR_NOT_IMPLEMENTED;
125 NS_IMETHODIMP
126 aaTranscript::SetFilter(aaIFilter * aFilter)
128 return NS_ERROR_NOT_IMPLEMENTED;
130 aaIFilter *
131 aaTranscript::PickFilter()
133 return nsnull;
136 /* aaITranscript */
137 NS_IMETHODIMP
138 aaTranscript::GetFlow(aaIFlow * *aFlow)
140 NS_ENSURE_ARG_POINTER(aFlow);
141 NS_IF_ADDREF(*aFlow = mFlow);
142 return NS_OK;
144 NS_IMETHODIMP
145 aaTranscript::SetFlow(aaIFlow * aFlow)
147 mFlow = aFlow;
148 return NS_OK;
150 aaIFlow *
151 aaTranscript::PickLoadedFlow()
153 return mLoadedFlow;
156 NS_IMETHODIMP
157 aaTranscript::GetStart(PRTime *aStart)
159 NS_ENSURE_ARG_POINTER(aStart);
160 *aStart = mStart;
161 return NS_OK;
163 NS_IMETHODIMP
164 aaTranscript::SetStart(PRTime aStart)
166 mStart = aStart;
167 return NS_OK;
170 NS_IMETHODIMP
171 aaTranscript::GetEnd(PRTime *aEnd)
173 NS_ENSURE_ARG_POINTER(aEnd);
174 *aEnd = mEnd;
175 return NS_OK;
177 NS_IMETHODIMP
178 aaTranscript::SetEnd(PRTime aEnd)
180 mEnd = aEnd;
181 return NS_OK;
184 NS_IMETHODIMP
185 aaTranscript::GetOpening(aaIBalance * *aOpen)
187 nsresult rv;
188 NS_ENSURE_ARG_POINTER(aOpen);
189 NS_ENSURE_TRUE(mOpening, NS_ERROR_UNEXPECTED);
191 PRUint32 count;
192 rv = mOpening->GetLength(&count);
193 NS_ENSURE_SUCCESS(rv, rv);
194 if (count) {
195 return mOpening->QueryElementAt(0, NS_GET_IID(aaIBalance), (void **) aOpen);
198 *aOpen = nsnull;
199 return NS_OK;
202 NS_IMETHODIMP
203 aaTranscript::GetClosing(aaIBalance * *aClose)
205 nsresult rv;
206 NS_ENSURE_ARG_POINTER(aClose);
207 NS_ENSURE_TRUE(mClosing, NS_ERROR_UNEXPECTED);
209 PRUint32 count;
210 rv = mClosing->GetLength(&count);
211 NS_ENSURE_SUCCESS(rv, rv);
212 if (count) {
213 return mClosing->QueryElementAt(0, NS_GET_IID(aaIBalance), (void **) aClose);
216 *aClose = nsnull;
217 return NS_OK;
220 /* aaIFilter */
221 NS_IMETHODIMP
222 aaTranscript::GetExpression(nsACString & aExpression)
224 NS_ENSURE_TRUE(mFlow, NS_ERROR_NOT_INITIALIZED);
226 if (mFlow->PickId()) {
227 aExpression.Assign(" WHERE txn.day>");
228 aExpression.AppendInt(aa_julian_day(mOpeningFilter->PickDate()));
229 aExpression.Append(" AND txn.day<=");
230 aExpression.AppendInt(aa_julian_day(mClosingFilter->PickDate()));
231 aExpression.Append(" AND (toF.flow_id=");
232 aExpression.AppendInt(mOpeningFilter->PickFlow()->PickId());
233 aExpression.Append(" OR fromF.flow_id=");
234 aExpression.AppendInt(mOpeningFilter->PickFlow()->PickId());
235 aExpression.Append(")");
236 } else {
237 aExpression.Assign(" WHERE txn.day>");
238 aExpression.AppendInt(aa_julian_day(mOpeningFilter->PickDate()));
239 aExpression.Append(" AND txn.day<=");
240 aExpression.AppendInt(aa_julian_day(mClosingFilter->PickDate()));
241 aExpression.Append(" AND txn.status=1");
243 return NS_OK;