From cecbe53e1563d809b1f5a752cdc92d54e84c2184 Mon Sep 17 00:00:00 2001 From: Sergey Yanovich Date: Fri, 4 Apr 2008 18:10:11 +0300 Subject: [PATCH] [report] Load total debits in 'transcript' (bug 151) --- abstract/report/aaITranscript.idl | 2 + report/Makefile.in | 1 + report/aaTranscript.cpp | 18 +++++- report/aaTranscript.h | 3 + report/aaTranscriptCounter.cpp | 69 ++++++++++++++++++++++ .../aaTranscriptCounter.h | 44 +++++++------- report/test/aaReportTest.cpp | 7 ++- 7 files changed, 122 insertions(+), 22 deletions(-) create mode 100644 report/aaTranscriptCounter.cpp copy abstract/report/aaITranscript.idl => report/aaTranscriptCounter.h (51%) diff --git a/abstract/report/aaITranscript.idl b/abstract/report/aaITranscript.idl index c789602..9ebfaba 100644 --- a/abstract/report/aaITranscript.idl +++ b/abstract/report/aaITranscript.idl @@ -41,5 +41,7 @@ interface aaITranscript : aaILoadQuery readonly attribute aaIBalance opening; readonly attribute aaIBalance closing; + + readonly attribute double totalDebits; }; diff --git a/report/Makefile.in b/report/Makefile.in index 629afbb..fb8b51c 100644 --- a/report/Makefile.in +++ b/report/Makefile.in @@ -64,6 +64,7 @@ CPPSRCS = \ aaTranscript.cpp \ aaBalanceCounter.cpp \ aaBalanceSheet.cpp \ + aaTranscriptCounter.cpp \ aaReportModule.cpp \ $(NULL) diff --git a/report/aaTranscript.cpp b/report/aaTranscript.cpp index ffdf365..90b458a 100644 --- a/report/aaTranscript.cpp +++ b/report/aaTranscript.cpp @@ -38,6 +38,8 @@ #include "aaBaseLoaders.h" #include "aaAccountLoaders.h" #include "aaStorageUtils.h" +#include "aaTranscriptCounter.h" + #include "aaTranscript.h" aaTranscript::aaTranscript(nsISupports *aOuter) @@ -65,7 +67,8 @@ aaTranscript::aaTranscript(nsISupports *aOuter) return; mList = do_CreateInstance(AA_LOADFACTLIST_CONTRACT_ID, connection); - if (!mList) + mListInternal = do_QueryInterface(mList); + if (!mListInternal) return; mConnection = connection; @@ -102,6 +105,7 @@ aaTranscript::Load() mClosingFilter->SetDate( mEnd ); mClosing->SetFilter( mClosingFilter ); + mTotalDebits = 0.0; rv = mOpening->Load(); NS_ENSURE_SUCCESS(rv, rv); @@ -109,10 +113,13 @@ aaTranscript::Load() rv = mClosing->Load(); NS_ENSURE_SUCCESS(rv, rv); + aaTranscriptCounter counter(mFlow->PickId()); + mListInternal->SetLoadObserver(&counter); mList->SetFilter(this); rv = mList->Load(); mList->SetFilter(nsnull); NS_ENSURE_SUCCESS(rv, rv); + mTotalDebits = counter.GetDebits(); mLoadedFlow = mFlow; return NS_OK; @@ -218,6 +225,15 @@ aaTranscript::GetClosing(aaIBalance * *aClose) return NS_OK; } +NS_IMETHODIMP +aaTranscript::GetTotalDebits(double *aTotalDebits) +{ + NS_ENSURE_ARG_POINTER(aTotalDebits); + + *aTotalDebits = mTotalDebits; + return NS_OK; +} + /* aaIFilter */ NS_IMETHODIMP aaTranscript::GetExpression(nsACString & aExpression) diff --git a/report/aaTranscript.h b/report/aaTranscript.h index 6fbda3f..4f4c571 100644 --- a/report/aaTranscript.h +++ b/report/aaTranscript.h @@ -59,12 +59,15 @@ private: nsCOMPtr mOpening; nsCOMPtr mClosing; nsCOMPtr mList; + nsCOMPtr mListInternal; nsCOMPtr mOpeningBalance; nsCOMPtr mClosingBalance; nsCOMPtr mOpeningIncome; nsCOMPtr mClosingIncome; + + double mTotalDebits; }; #endif /* AATRANSCRIPT_H */ diff --git a/report/aaTranscriptCounter.cpp b/report/aaTranscriptCounter.cpp new file mode 100644 index 0000000..9e04c10 --- /dev/null +++ b/report/aaTranscriptCounter.cpp @@ -0,0 +1,69 @@ +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ +/* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */ +/* + * Copyright (C) 2007 Sergey Yanovich + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "xpcom-config.h" + +#include "nsCOMPtr.h" +#include "nsWeakReference.h" + +/* Unfrozen API */ + +/* Project includes */ +#include "aaIFlow.h" +#include "aaIFact.h" +#include "aaITransaction.h" +#include "aaTranscriptCounter.h" + +NS_IMPL_QUERY_INTERFACE2(aaTranscriptCounter, + nsISupportsWeakReference, + aaILoadObserver) + +NS_IMETHODIMP_(nsrefcnt) +aaTranscriptCounter::AddRef(void) +{ + return 1; +} + +NS_IMETHODIMP_(nsrefcnt) +aaTranscriptCounter::Release(void) +{ + return 1; +} + +NS_IMETHODIMP +aaTranscriptCounter::ObserveLoad(aaIDataNode *aNode) +{ + nsresult rv; + nsCOMPtr txn = do_QueryInterface(aNode, &rv); + NS_ENSURE_SUCCESS(rv, rv); + NS_ENSURE_TRUE(txn->PickFact(), NS_ERROR_UNEXPECTED); + + if (txn->PickFact()->PickGiveTo() + && txn->PickFact()->PickGiveTo()->PickId() == mFlowId) + mDebits += txn->PickFact()->PickAmount(); + else if (txn->PickFact()->PickTakeFrom() + && txn->PickFact()->PickTakeFrom()->PickId() == mFlowId) + ; + else + return NS_ERROR_UNEXPECTED; + + return NS_OK; +} diff --git a/abstract/report/aaITranscript.idl b/report/aaTranscriptCounter.h similarity index 51% copy from abstract/report/aaITranscript.idl copy to report/aaTranscriptCounter.h index c789602..d9e7d04 100644 --- a/abstract/report/aaITranscript.idl +++ b/report/aaTranscriptCounter.h @@ -1,7 +1,7 @@ /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ -/* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=idl: */ +/* vim:set ts=2 sw=2 sts=2 et cindent tw=79 ft=cpp: */ /* - * Copyright (C) 2007 Sergey Yanovich + * Copyright (C) 2008 Sergey Yanovich * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -19,27 +19,31 @@ * Boston, MA 02111-1307, USA. */ -#include "aaILoadQuery.idl" +#ifndef AATRANSCRIPTCOUNTER_H +#define AATRANSCRIPTCOUNTER_H 1 -/* - * The aaITranscript interface - * @status UNSTABLE - */ - -interface nsIArray; -interface aaIFlow; -interface aaIBalance; +#include "aaILoadQuery.h" +#include "nsWeakReference.h" -[scriptable, uuid(b5d51ead-cc96-4e5a-a3b7-bde49d3221e8)] -interface aaITranscript : aaILoadQuery -{ - attribute aaIFlow flow; - [noscript, notxpcom] aaIFlow pickLoadedFlow(); +class mozIStorageStatement; +class mozIStorageConnection; - attribute PRTime start; - attribute PRTime end; +class aaTranscriptCounter : public nsSupportsWeakReference, + public aaILoadObserver +{ +public: + aaTranscriptCounter(PRInt64 aFlowId) :mFlowId(aFlowId), mDebits(0.0) {;} + virtual ~aaTranscriptCounter() {;} + NS_DECL_ISUPPORTS + NS_DECL_AAILOADOBSERVER + + double GetDebits() const { return mDebits; } + void Reset() { mDebits = 0.0; } +private: + friend class aaAccountTest; - readonly attribute aaIBalance opening; - readonly attribute aaIBalance closing; + PRInt64 mFlowId; + double mDebits; }; +#endif /* AATRANSCRIPTCOUNTER_H */ diff --git a/report/test/aaReportTest.cpp b/report/test/aaReportTest.cpp index 03c9e49..028b3e6 100644 --- a/report/test/aaReportTest.cpp +++ b/report/test/aaReportTest.cpp @@ -200,8 +200,13 @@ aaReportTest::testTranscript(nsITestRunner *aTestRunner) txn = do_QueryElementAt(transcript, 2); NS_TEST_ASSERT_MSG(txn && testFact(aTestRunner, txn->PickFact(), 3, 4, - AA_EVENT_AMOUNT_4), "[update txn] pending fact is wrong"); + AA_EVENT_AMOUNT_4), "[transcript] pending fact is wrong"); + double val = 0.0; + rv = transcript->GetTotalDebits(&val); + NS_TEST_ASSERT_OK(rv); + NS_TEST_ASSERT_MSG(val == AA_EVENT_AMOUNT_2 + AA_EVENT_AMOUNT_3, + "[transcript] [3] wrong debit sum"); /* *** Transcript [3] *** */ /* Set start time to 2007-08-29 */ -- 2.11.4.GIT