[base] Switch to mozbuild (#114)
[abstract.git] / storage / aaStateFilter.cpp
blob72e23f03966f3919c7c181d1d81e67a6fd6e0eb8
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 "nsStringAPI.h"
26 #include "nsEmbedString.h"
28 /* Project includes */
29 #include <abstract/base/aaIFlow.h>
30 #include <abstract/storage/aaIStateFilter.h>
31 #include <abstract/storage/aaStorageUtils.h>
32 #include "aaStateFilter.h"
34 aaStateFilter::aaStateFilter()
38 aaStateFilter::~aaStateFilter()
42 NS_IMPL_ISUPPORTS2(aaStateFilter,
43 aaIFilter,
44 aaIStateFilter)
45 /* aaIFilter */
46 NS_IMETHODIMP
47 aaStateFilter::GetExpression(nsACString & aExpression)
49 if (!mFlow)
50 balanceSheetExpression(aExpression);
51 else if (mFlow->PickId())
52 balanceExpression(aExpression);
53 else
54 incomeExpression(aExpression);
56 return NS_OK;
59 /* aaIStateFilter */
60 NS_IMETHODIMP
61 aaStateFilter::GetFlow(aaIFlow * *aFlow)
63 NS_ENSURE_ARG_POINTER(aFlow);
64 NS_IF_ADDREF( *aFlow = mFlow );
65 return NS_OK;
67 NS_IMETHODIMP
68 aaStateFilter::SetFlow(aaIFlow * aFlow)
70 mFlow = aFlow;
71 return NS_OK;
73 aaIFlow *
74 aaStateFilter::PickFlow()
76 return mFlow;
79 NS_IMETHODIMP
80 aaStateFilter::GetDate(PRTime *aDate)
82 NS_ENSURE_ARG_POINTER(aDate);
83 *aDate = mDate;
84 return NS_OK;
86 NS_IMETHODIMP
87 aaStateFilter::SetDate(PRTime aDate)
89 mDate = aDate;
90 return NS_OK;
92 PRTime
93 aaStateFilter::PickDate()
95 return mDate;
98 /* Private methods */
99 void
100 aaStateFilter::balanceSheetExpression(nsACString & aExpression)
102 aExpression.Assign(" WHERE balance.start<=");
103 aExpression.AppendInt(aa_julian_day(mDate));
104 aExpression.Append(" AND (balance.paid>");
105 aExpression.AppendInt(aa_julian_day(mDate));
106 aExpression.Append(" OR balance.paid IS NULL)");
109 void
110 aaStateFilter::balanceExpression(nsACString & aExpression)
112 aExpression.Assign(" WHERE balance.flow_id=");
113 aExpression.AppendInt(mFlow->PickId());
114 aExpression.Append(" AND balance.start=(SELECT MAX(start) FROM balance\
115 WHERE flow_id=");
116 aExpression.AppendInt(mFlow->PickId());
117 aExpression.Append(" AND start<=");
118 aExpression.AppendInt(aa_julian_day(mDate));
119 aExpression.Append(") AND (balance.paid>");
120 aExpression.AppendInt(aa_julian_day(mDate));
121 aExpression.Append(" OR balance.paid IS NULL)");
124 void
125 aaStateFilter::incomeExpression(nsACString & aExpression)
127 aExpression.Assign(" WHERE income.start<=");
128 aExpression.AppendInt(aa_julian_day(mDate));
129 aExpression.Append(" AND (income.paid>");
130 aExpression.AppendInt(aa_julian_day(mDate));
131 aExpression.Append(" OR income.paid IS NULL)");