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: */
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>
25 #include "nsComponentManagerUtils.h"
27 #include "nsStringAPI.h"
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
;
53 mClosingBalance
= do_CreateInstance(AA_LOADBALANCE_CONTRACT_ID
, connection
);
54 mClosingIncome
= do_CreateInstance(AA_LOADINCOME_CONTRACT_ID
, connection
);
55 mClosing
= mClosingBalance
;
59 mOpeningFilter
= do_CreateInstance(AA_STATEFILTER_CONTRACT_ID
);
63 mClosingFilter
= do_CreateInstance(AA_STATEFILTER_CONTRACT_ID
);
67 mList
= do_CreateInstance(AA_LOADFACTLIST_CONTRACT_ID
, connection
);
71 mConnection
= connection
;
74 aaTranscript::~aaTranscript()
78 NS_IMPL_ISUPPORTS4(aaTranscript
,
88 NS_ENSURE_TRUE(mFlow
, NS_ERROR_NOT_INITIALIZED
);
90 if (mFlow
->PickId()) {
91 mOpening
= mOpeningBalance
;
92 mClosing
= mClosingBalance
;
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
);
114 NS_ENSURE_SUCCESS(rv
, rv
);
121 aaTranscript::GetFilter(aaIFilter
* * aFilter
)
123 return NS_ERROR_NOT_IMPLEMENTED
;
126 aaTranscript::SetFilter(aaIFilter
* aFilter
)
128 return NS_ERROR_NOT_IMPLEMENTED
;
131 aaTranscript::PickFilter()
138 aaTranscript::GetFlow(aaIFlow
* *aFlow
)
140 NS_ENSURE_ARG_POINTER(aFlow
);
141 NS_IF_ADDREF(*aFlow
= mFlow
);
145 aaTranscript::SetFlow(aaIFlow
* aFlow
)
151 aaTranscript::PickLoadedFlow()
157 aaTranscript::GetStart(PRTime
*aStart
)
159 NS_ENSURE_ARG_POINTER(aStart
);
164 aaTranscript::SetStart(PRTime aStart
)
171 aaTranscript::GetEnd(PRTime
*aEnd
)
173 NS_ENSURE_ARG_POINTER(aEnd
);
178 aaTranscript::SetEnd(PRTime aEnd
)
185 aaTranscript::GetOpening(aaIBalance
* *aOpen
)
188 NS_ENSURE_ARG_POINTER(aOpen
);
189 NS_ENSURE_TRUE(mOpening
, NS_ERROR_UNEXPECTED
);
192 rv
= mOpening
->GetLength(&count
);
193 NS_ENSURE_SUCCESS(rv
, rv
);
195 return mOpening
->QueryElementAt(0, NS_GET_IID(aaIBalance
), (void **) aOpen
);
203 aaTranscript::GetClosing(aaIBalance
* *aClose
)
206 NS_ENSURE_ARG_POINTER(aClose
);
207 NS_ENSURE_TRUE(mClosing
, NS_ERROR_UNEXPECTED
);
210 rv
= mClosing
->GetLength(&count
);
211 NS_ENSURE_SUCCESS(rv
, rv
);
213 return mClosing
->QueryElementAt(0, NS_GET_IID(aaIBalance
), (void **) aClose
);
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(")");
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");