~ubuntu-branches/ubuntu/lucid/skrooge/lucid

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/***************************************************************************
 *   Copyright (C) 2008 by S. MANKOWSKI / G. DE BURE skrooge@miraks.com    *
 *                                                                         *
 *   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, see <http://www.gnu.org/licenses/>  *
 ***************************************************************************/
/** @file
 * This file is Skrooge plugin to generate report.
 *
 * @author Stephane MANKOWSKI / Guillaume DE BURE
 */
#include "skgreportplugin.h"
#include "skgreportpluginwidget.h"
#include "skgtraces.h"
#include "skgmainpanel.h"
#include "skgservices.h"
#include "skgaccountobject.h"
#include "skgcategoryobject.h"
#include "skgrefundtrackerobject.h"
#include "skgunitobject.h"

#include <kaction.h>
#include <kactioncollection.h>

#include <QDomDocument>

K_PLUGIN_FACTORY ( SKGReportPluginFactory, registerPlugin<SKGReportPlugin>(); )
K_EXPORT_PLUGIN ( SKGReportPluginFactory ( "skrooge_report", "skrooge_report" ) )

SKGReportPlugin::SKGReportPlugin ( QObject* iParent, const QVariantList& /*iArg*/ ) : SKGInterfacePlugin ( iParent )
{
        SKGTRACEIN ( 10, "SKGReportPlugin::SKGReportPlugin" );
}

SKGReportPlugin::~SKGReportPlugin()
{
        SKGTRACEIN ( 10, "SKGReportPlugin::~SKGReportPlugin" );
        parent=NULL;
        currentBankDocument=NULL;
        openReportAction=NULL;
}

void SKGReportPlugin::setupActions ( SKGMainPanel* iParent, SKGDocument* iDocument, const QStringList& iArgument )
{
        SKGTRACEIN ( 10, "SKGReportPlugin::setupActions" );
        Q_UNUSED ( iArgument );

        currentBankDocument=iDocument;
        parent=iParent;

        setComponentData ( SKGReportPluginFactory::componentData() );
        setXMLFile ( "skrooge_report.rc" );

        //Menu
        openReportAction = new KAction ( KIcon ( "skrooge_open_report" ), i18n ( "Open &report" ), this );
        connect ( openReportAction, SIGNAL ( triggered ( bool ) ), this, SLOT ( actionOpenReport() ) );
        actionCollection()->addAction ( QLatin1String ( "open_report" ), openReportAction );
        openReportAction->setShortcut ( Qt::CTRL+Qt::Key_G );

        if ( parent ) parent->registedGlobalAction ( "open_report", openReportAction );
}

void SKGReportPlugin::refresh()
{
        SKGTRACEIN ( 10, "SKGReportPlugin::refresh" );
        if ( parent ) {
                SKGObjectBase::SKGListSKGObjectBase selection=parent->getSelectedObjects();

                if ( selection.count() >0 ) {
                        QString table=selection.at ( 0 ).getRealTable();
                        bool onOperation= ( table=="operation" || table=="account" || table=="unit" || table=="category" || table=="refund" );
                        if ( openReportAction ) openReportAction->setEnabled ( onOperation );
                } else {
                        if ( openReportAction ) openReportAction->setEnabled ( false );
                }
        }
}

void SKGReportPlugin::close()
{
        SKGTRACEIN ( 10, "SKGReportPlugin::close" );
}

SKGTabWidget* SKGReportPlugin::getWidget()
{
        SKGTRACEIN ( 10, "SKGReportPlugin::getWidget" );
        return new SKGReportPluginWidget ( parent, ( SKGDocumentBank* ) currentBankDocument );
}

QString SKGReportPlugin::title() const
{
        return i18n ( "Report" );
}

QString SKGReportPlugin::icon() const
{
        return "view-statistics";
}

QString SKGReportPlugin::statusTip () const
{
        return i18n ( "Generate report" );
}

QString SKGReportPlugin::toolTip () const
{
        return i18n ( "Generate report" );
}

int SKGReportPlugin::getOrder() const
{
        return 40;
}

QStringList SKGReportPlugin::tips() const
{
        QStringList output;
        output.push_back ( i18n ( "<p>... you can double click on a value in \"%1\" to show corresponding operations.</p>", title() ) );
        output.push_back ( i18n ( "<p>... you can open reports for selections made in other pages.</p>" ) );
        output.push_back ( i18n ( "<p>... you can export reports in many formats.</p>" ) );
        return output;
}

bool SKGReportPlugin::isInContext() const
{
        return true;
}

void SKGReportPlugin::actionOpenReport()
{
        SKGError err;
        SKGTRACEINRC ( 10, "SKGReportPlugin::actionOpenReport",err );
        if ( parent ) {
                SKGObjectBase::SKGListSKGObjectBase selection=parent->getSelectedObjects();

                int nb=selection.count();
                if ( nb>0 ) {
                        QString wc;
                        QString title;
                        QString table=selection.at ( 0 ).getRealTable();
                        if ( table=="account" ) {
                                wc="rd_account_id in (";
                                title=i18n ( "Operations of account " );

                                for ( int i=0; i<nb; ++i ) {
                                        SKGAccountObject tmp=selection.at ( i );
                                        if ( i ) {
                                                wc+=',';
                                                title+=',';
                                        }
                                        wc+=SKGServices::intToString ( tmp.getID() );
                                        title+='\''+tmp.getName() +'\'';
                                }
                                wc+=')';
                        } else if ( table=="unit" ) {
                                wc="rc_unit_id in (";
                                title=i18n ( "Operations with Unit equal to " );

                                for ( int i=0; i<nb; ++i ) {
                                        SKGUnitObject tmp=selection.at ( i );
                                        if ( i ) {
                                                wc+=',';
                                                title+=',';
                                        }
                                        wc+=SKGServices::intToString ( tmp.getID() );
                                        title+='\''+tmp.getName() +'\'';
                                }
                                wc+=')';
                        } else if ( table=="category" ) {
                                wc="t_REALCATEGORY in (";
                                QString wc2;
                                title=i18n ( "Operations with Category equal to " );

                                for ( int i=0; i<nb; ++i ) {
                                        SKGCategoryObject tmp=selection.at ( i );
                                        if ( i ) {
                                                wc+=',';
                                                wc2+=" OR ";
                                                title+=',';
                                        }
                                        wc+='\''+SKGServices::stringToSqlString ( tmp.getFullName() ) +'\'';
                                        wc2+="t_REALCATEGORY like '"+SKGServices::stringToSqlString ( tmp.getFullName() ) +"%'";
                                        title+='\''+tmp.getFullName() +'\'';
                                }
                                wc+=") OR "+wc2;
                        } else if ( table=="refund" ) {
                                wc="r_refund_id in (";
                                title=i18n ( "Operations followed by " );

                                for ( int i=0; i<nb; ++i ) {
                                        SKGRefundTrackerObject tmp=selection.at ( i );
                                        if ( i ) {
                                                wc+=',';
                                                title+=',';
                                        }
                                        wc+=SKGServices::intToString ( tmp.getID() );
                                        title+='\''+tmp.getName() +'\'';
                                }
                                wc+=')';
                        } else if ( table=="operation" ) {
                                wc="id in (";
                                title=i18n ( "Selected operations" );

                                for ( int i=0; i<nb; ++i ) {
                                        if ( i ) {
                                                wc+=',';
                                        }
                                        wc+=SKGServices::intToString ( selection.at ( i ).getID() );
                                }
                                wc+=')';
                        }

                        //Call report plugin
                        QDomDocument doc("SKGML");
                        doc.setContent ( currentBankDocument->getParameter("SKGREPORT_DEFAULT_PARAMETERS") );
                        QDomElement root = doc.documentElement();
                        if (root.isNull()) {
                                root=doc.createElement("parameters");
                                doc.appendChild(root);
                        }

                        root.setAttribute ( "operationWhereClause", wc );
                        root.setAttribute ( "title", title );
                        root.setAttribute ( "title_icon", "view-statistics" );
                        parent->setNewTabContent ( parent->getPluginByName ( "Skrooge report plugin" ), -1, doc.toString() );
                }
        }
}
#include "skgreportplugin.moc"