transport: pass correct channel
[abstract.git] / storage / base / aaLoadIncome.cpp
blob3dd7630d8a2c6e039748fc475965e79853be77fb
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,2008 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 "xpcom-config.h"
24 #include "nsCOMPtr.h"
25 #include "nsIInterfaceRequestorUtils.h"
26 #include "nsStringAPI.h"
27 #include "nsComponentManagerUtils.h"
28 #include "nsXPCOMCID.h"
29 #include "nsIMutableArray.h"
30 #include "nsISupportsPrimitives.h"
32 /* Unfrozen API */
34 /* Project includes */
35 #include "aaITimeFrame.h"
36 #include "aaIFlow.h"
37 #include "aaIChartHandler.h"
38 #include "aaLoadIncome.h"
40 #define AA_LOADINCOME_SELECT_QUERY "SELECT\
41 0.0,\
42 income.side,\
43 income.value,\
44 SUM(-takeQ.diff),\
45 SUM( giveQ.diff),\
46 strftime('%s',income.start),\
47 strftime('%s',income.paid)\
48 FROM income\
49 LEFT JOIN quote AS takeQ ON\
50 takeQ.day = income.start\
51 AND takeQ.diff<0.0\
52 LEFT JOIN quote AS giveQ ON\
53 giveQ.day = income.start\
54 AND giveQ.diff>0.0\
55 WHERE IFNULL(start<=round(julianday(?,'unixepoch')),1)\
56 AND (IFNULL(paid>round(julianday(?,'unixepoch')),0)\
57 OR paid IS NULL)\
58 GROUP BY income.start"
60 static struct aaLoadRequest::ColumnMap map[] = {
61 {"amount", 0}
62 ,{"side", 1}
63 ,{"value", 2}
64 ,{"credit_diff", 3}
65 ,{"debit_diff", 4}
66 ,{"start", 5}
67 ,{"paid", 6}
68 ,{0, 0}
71 aaLoadIncome::aaLoadIncome()
73 mSql = AA_LOADINCOME_SELECT_QUERY;
74 mMap = &map[0];
77 NS_IMPL_ISUPPORTS_INHERITED0(aaLoadIncome,
78 aaLoadRequest)
80 /* aaISqlRequest */
81 NS_IMETHODIMP
82 aaLoadIncome::GetParams(nsIArray * * aParams)
84 nsresult rv;
85 nsCOMPtr<aaIFlow> flow = do_GetInterface(mFilter);
86 NS_ENSURE_TRUE(!flow || !(flow->PickId()), NS_ERROR_INVALID_ARG);
88 nsCOMPtr<aaITimeFrame> frame = do_GetInterface(mFilter);
89 nsCOMPtr<nsIMutableArray> result
90 = do_CreateInstance(NS_ARRAY_CONTRACTID, &rv);
91 NS_ENSURE_SUCCESS(rv, rv);
93 nsCOMPtr<nsISupportsPRInt64> valInt64;
95 if (frame) {
96 valInt64 = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID, &rv);
97 NS_ENSURE_SUCCESS(rv, rv);
98 rv = valInt64->SetData(frame->PickEnd() / 1000000);
99 NS_ENSURE_SUCCESS(rv, rv);
100 rv = result->InsertElementAt(valInt64, 0, PR_FALSE);
101 NS_ENSURE_SUCCESS(rv, rv);
103 valInt64 = do_CreateInstance(NS_SUPPORTS_PRINT64_CONTRACTID, &rv);
104 NS_ENSURE_SUCCESS(rv, rv);
105 rv = valInt64->SetData(frame->PickStart() / 1000000);
106 NS_ENSURE_SUCCESS(rv, rv);
107 rv = result->InsertElementAt(valInt64, 1, PR_FALSE);
108 NS_ENSURE_SUCCESS(rv, rv);
110 else {
111 rv = result->InsertElementAt(nsnull, 0, PR_FALSE);
112 NS_ENSURE_SUCCESS(rv, rv);
114 rv = result->InsertElementAt(nsnull, 1, PR_FALSE);
115 NS_ENSURE_SUCCESS(rv, rv);
118 *aParams = result;
119 result.forget();
121 return NS_OK;
124 /* aaIDataNode */
125 NS_IMETHODIMP
126 aaLoadIncome::Accept(aaIHandler *aQuery)
128 nsresult rv;
129 nsCOMPtr<aaIChartHandler> chartQuery = do_QueryInterface(aQuery, &rv);
130 NS_ENSURE_SUCCESS(rv, rv);
131 return chartQuery->HandleIncome(nsnull);