File indexing completed on 2024-05-26 05:10:37
0001 /*************************************************************************** 0002 * SPDX-FileCopyrightText: 2022 S. MANKOWSKI stephane@mankowski.fr 0003 * SPDX-FileCopyrightText: 2022 G. DE BURE support@mankowski.fr 0004 * SPDX-License-Identifier: GPL-3.0-or-later 0005 ***************************************************************************/ 0006 /** @file 0007 * A skrooge plugin to calculate 0008 * 0009 * @author Stephane MANKOWSKI 0010 */ 0011 #include "skgcalculatorplugin.h" 0012 0013 #include <kaboutdata.h> 0014 #include <kactioncollection.h> 0015 #include <kpluginfactory.h> 0016 #include <kstandardaction.h> 0017 0018 #include "skgaccountobject.h" 0019 #include "skgcalculatorpluginwidget.h" 0020 #include "skgdocumentbank.h" 0021 #include "skghtmlboardwidget.h" 0022 #include "skgtraces.h" 0023 0024 /** 0025 * This plugin factory. 0026 */ 0027 K_PLUGIN_CLASS_WITH_JSON(SKGCalculatorPlugin, "metadata.json") 0028 0029 SKGCalculatorPlugin::SKGCalculatorPlugin(QWidget* iWidget, QObject* iParent, const QVariantList& /*iArg*/) : SKGInterfacePlugin(iParent), m_currentBankDocument(nullptr) 0030 { 0031 Q_UNUSED(iWidget) 0032 SKGTRACEINFUNC(10) 0033 } 0034 0035 SKGCalculatorPlugin::~SKGCalculatorPlugin() 0036 { 0037 SKGTRACEINFUNC(10) 0038 m_currentBankDocument = nullptr; 0039 } 0040 0041 bool SKGCalculatorPlugin::setupActions(SKGDocument* iDocument) 0042 { 0043 SKGTRACEINFUNC(10) 0044 0045 m_currentBankDocument = qobject_cast<SKGDocumentBank*>(iDocument); 0046 if (m_currentBankDocument == nullptr) { 0047 return false; 0048 } 0049 0050 setComponentName(QStringLiteral("skrooge_calculator"), title()); 0051 setXMLFile(QStringLiteral("skrooge_calculator.rc")); 0052 0053 // Create yours actions here 0054 return true; 0055 } 0056 0057 int SKGCalculatorPlugin::getNbDashboardWidgets() 0058 { 0059 return 1; 0060 } 0061 0062 QString SKGCalculatorPlugin::getDashboardWidgetTitle(int iIndex) 0063 { 0064 Q_UNUSED(iIndex) 0065 return i18nc("The estimated amount of money earned through interests on a remunerated account", "Estimated interest"); 0066 } 0067 0068 SKGBoardWidget* SKGCalculatorPlugin::getDashboardWidget(int iIndex) 0069 { 0070 Q_UNUSED(iIndex) 0071 auto listForFilter = QStringList() 0072 << QStringLiteral("t_name") 0073 << QStringLiteral("t_number") 0074 << QStringLiteral("t_agency_number") 0075 << QStringLiteral("t_agency_address") 0076 << QStringLiteral("t_comment") 0077 << QStringLiteral("t_bookmarked") 0078 << QStringLiteral("t_TYPENLS") 0079 << QStringLiteral("t_BANK") 0080 << QStringLiteral("t_BANK_NUMBER"); 0081 0082 return new SKGHtmlBoardWidget(SKGMainPanel::getMainPanel(), m_currentBankDocument, 0083 getDashboardWidgetTitle(iIndex), 0084 QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("skrooge/html/default/interests.qml")), 0085 QStringList() << QStringLiteral("v_account_display") << QStringLiteral("interest"), 0086 SKGSimplePeriodEdit::PREVIOUS_AND_CURRENT_YEARS, 0087 listForFilter); 0088 } 0089 0090 SKGTabPage* SKGCalculatorPlugin::getWidget() 0091 { 0092 SKGTRACEINFUNC(10) 0093 return new SKGCalculatorPluginWidget(SKGMainPanel::getMainPanel(), m_currentBankDocument); 0094 } 0095 0096 QString SKGCalculatorPlugin::title() const 0097 { 0098 return toolTip(); 0099 } 0100 0101 QString SKGCalculatorPlugin::icon() const 0102 { 0103 return QStringLiteral("accessories-calculator"); 0104 } 0105 0106 QString SKGCalculatorPlugin::toolTip() const 0107 { 0108 return i18nc("Compute financial simulations for various situations (interests…)", "Simulations"); 0109 } 0110 0111 int SKGCalculatorPlugin::getOrder() const 0112 { 0113 return 100; 0114 } 0115 0116 QStringList SKGCalculatorPlugin::tips() const 0117 { 0118 QStringList output; 0119 output.push_back(i18nc("Description of a tips", "<p>… you can use the <a href=\"skg://skrooge_calculator_plugin\">calculator</a> for many things</p>")); 0120 return output; 0121 } 0122 0123 bool SKGCalculatorPlugin::isInPagesChooser() const 0124 { 0125 return true; 0126 } 0127 SKGAdviceList SKGCalculatorPlugin::advice(const QStringList& iIgnoredAdvice) 0128 { 0129 SKGTRACEINFUNC(10) 0130 SKGAdviceList output; 0131 0132 // Search investment accounts without interest rate 0133 if (!iIgnoredAdvice.contains(QStringLiteral("skgcalculatorplugin_nointerest"))) { 0134 SKGObjectBase::SKGListSKGObjectBase accounts; 0135 m_currentBankDocument->getObjects(QStringLiteral("account"), QStringLiteral("t_type='I' AND t_close='N' AND NOT EXISTS (SELECT 1 FROM interest WHERE interest.rd_account_id=account.id)"), accounts); 0136 int nb = accounts.count(); 0137 SKGAdvice::SKGAdviceActionList autoCorrections; 0138 for (int i = 0; i < nb; ++i) { 0139 SKGAccountObject account(accounts.at(i)); 0140 SKGAdvice ad; 0141 ad.setUUID("skgcalculatorplugin_nointerest|" % account.getName()); 0142 ad.setPriority(3); 0143 ad.setShortMessage(i18nc("User did not define an interest rate on an investment account", "No interest rate defined for account '%1'", account.getName())); 0144 ad.setLongMessage(i18nc("User did not define an interest rate on an investment account", "Your investment account '%1' doesn't have interest rate defined", account.getName())); 0145 autoCorrections.resize(0); 0146 { 0147 SKGAdvice::SKGAdviceAction a; 0148 a.Title = i18nc("Link allowing user to open a new tab for defining interests parameters", "Open interest page"); 0149 a.IconName = QStringLiteral("quickopen"); 0150 a.IsRecommended = false; 0151 autoCorrections.push_back(a); 0152 } 0153 ad.setAutoCorrections(autoCorrections); 0154 output.push_back(ad); 0155 } 0156 } 0157 return output; 0158 } 0159 0160 SKGError SKGCalculatorPlugin::executeAdviceCorrection(const QString& iAdviceIdentifier, int iSolution) 0161 { 0162 if ((m_currentBankDocument != nullptr) && iAdviceIdentifier.startsWith(QLatin1String("skgcalculatorplugin_nointerest|"))) { 0163 // Get parameters 0164 QString account = iAdviceIdentifier.right(iAdviceIdentifier.length() - 31); 0165 SKGMainPanel::getMainPanel()->openPage("skg://skrooge_calculator_plugin/?currentPage=0&account=" % SKGServices::encodeForUrl(account)); 0166 return SKGError(); 0167 } 0168 0169 return SKGInterfacePlugin::executeAdviceCorrection(iAdviceIdentifier, iSolution); 0170 } 0171 0172 #include <skgcalculatorplugin.moc>