settings implemented

This commit is contained in:
Martin Brodbeck 2018-07-30 13:40:58 +02:00
parent 06c99052df
commit 9e85e2892d
9 changed files with 284 additions and 2 deletions

View File

@ -644,3 +644,73 @@ unsigned int Database::loadSales(std::vector<std::unique_ptr<Sale>>& sales,
return count;
}
void Database::updateCashPointNo(int oldCashPointNo, int newCashPointNo)
{
int retCode{};
sqlite3_stmt* stmt;
// Check if the new no ist already in use
retCode = sqlite3_prepare_v2(db_, "SELECT COUNT() FROM articles WHERE source_no = :source_no",
-1, &stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":source_no"), newCashPointNo);
retCode = sqlite3_step(stmt);
int count{};
if (retCode != SQLITE_ROW) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
if (count > 0) {
throw std::runtime_error("The desired cashpoint no is aleady in use.");
}
beginTransaction();
retCode = sqlite3_prepare_v2(
db_, "UPDATE articles SET source_no = :new_source_no WHERE source_no = :old_source_no", -1,
&stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":new_source_no"), newCashPointNo);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":old_source_no"), oldCashPointNo);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
sqlite3_finalize(stmt);
retCode = sqlite3_prepare_v2(
db_, "UPDATE sales SET source_no = :new_source_no WHERE source_no = :old_source_no", -1,
&stmt, nullptr);
if (retCode != SQLITE_OK)
throw std::string(sqlite3_errmsg(db_));
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":new_source_no"), newCashPointNo);
sqlite3_bind_int(stmt, sqlite3_bind_parameter_index(stmt, ":old_source_no"), oldCashPointNo);
retCode = sqlite3_step(stmt);
if (retCode != SQLITE_DONE) {
std::string errMsg(sqlite3_errmsg(db_));
sqlite3_finalize(stmt);
throw std::runtime_error(errMsg);
}
sqlite3_finalize(stmt);
endTransaction();
}

View File

@ -23,6 +23,7 @@ class Database
unsigned int storeSales(std::vector<std::unique_ptr<Sale>>& sales);
unsigned int loadSales(std::vector<std::unique_ptr<Sale>>& sales,
std::vector<std::unique_ptr<Seller>>& sellers);
void updateCashPointNo(int oldCashPointNo, int newCashPointNo);
private:
sqlite3* db_{nullptr};

View File

@ -139,6 +139,9 @@ void Marketplace::removeSale(boost::uuids::uuid uuid)
double marketFee(int sum, int percent, int maxFee)
{
int fee = (sum * percent) / 100.0L;
if (maxFee <= 0) {
return fee;
}
return fee > maxFee ? maxFee : fee;
}

View File

@ -22,6 +22,8 @@ set(GUI_SOURCES
reportdialog.cpp
reportdialog.ui
reportmodel.cpp
settingsdialog.cpp
settingsdialog.ui
)
add_executable(kima2 ${GUI_SOURCES})

View File

@ -1,6 +1,9 @@
#include "mainwindow.h"
#include <QApplication>
#include <QMessageBox>
#include <QSettings>
#include "settingsdialog.h"
int main(int argc, char* argv[])
{
@ -17,6 +20,15 @@ int main(int argc, char* argv[])
QCoreApplication::setOrganizationDomain("rustysoft.de");
QCoreApplication::setApplicationName("KIMA2");
QSettings settings{};
while (!settings.contains("global/cashPointNo")) {
QMessageBox(QMessageBox::Icon::Information, "Erster Start von KIMA2",
"Es wurden keine Einstellungen gefunden. Vermutlich starteten Sie KIMA2 zum "
"ersten Mal. Bitte legen Sie nun die Einstellungen fest.",
QMessageBox::StandardButton::Ok).exec();
SettingsDialog().exec();
}
auto mainWin = std::make_unique<MainWindow>();
mainWin->show();

View File

@ -5,10 +5,12 @@
#include "reportdialog.h"
#include "salemodel.h"
#include "sellerdialog.h"
#include "settingsdialog.h"
#include <regex>
#include <QMessageBox>
#include <QSettings>
constexpr int STATUSBAR_TIMEOUT = 5000;
@ -29,6 +31,8 @@ MainWindow::MainWindow()
ui_.salesView->setColumnHidden(2, true);
ui_.salesView->resizeColumnToContents(0);
setWindowTitle("KIMA2 - Kasse Nr. " + QSettings().value("global/cashPointNo").toString());
connect(ui_.actionQuit, &QAction::triggered, qApp, QApplication::quit);
connect(ui_.actionEditSeller, &QAction::triggered, this,
&MainWindow::onActionEditSellerTriggered);
@ -49,6 +53,11 @@ MainWindow::MainWindow()
static_cast<SaleModel*>(ui_.salesView->model()), &SaleModel::onBasketDataChanged);
connect(ui_.aboutQtAction, &QAction::triggered, this, &MainWindow::onAboutQt);
connect(ui_.reportAction, &QAction::triggered, this, [=]() { ReportDialog(this).exec(); });
connect(ui_.configAction, &QAction::triggered, this, [=]() {
SettingsDialog(this).exec();
this->setWindowTitle("KIMA2 - Kasse Nr. " +
QSettings().value("global/cashPointNo").toString());
});
}
void MainWindow::onActionEditSellerTriggered()

View File

@ -0,0 +1,61 @@
#include "settingsdialog.h"
#include <database.h>
#include <QMessageBox>
#include <QSettings>
SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(parent, f)
{
ui_.setupUi(this);
QSettings settings{};
int cashPointNo = settings.value("global/cashPointNo").toInt();
QString posPrinterDevice = settings.value("global/posPrinterDevice").toString();
int feeInPercent = settings.value("global/feeInPercent").toInt();
int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt();
ui_.cashPointNoSpinBox->setValue(cashPointNo);
ui_.posPrinterDeviceEdit->setText(posPrinterDevice);
ui_.feePercentSpinBox->setValue(feeInPercent);
ui_.maxFeeSpinBox->setValue(maxFeeInEuro);
}
void SettingsDialog::accept()
{
QSettings settings;
int oldCashPointNo = settings.value("global/cashPointNo").toInt();
int newCashPointNo = ui_.cashPointNoSpinBox->value();
settings.setValue("global/posPrinterDevice", ui_.posPrinterDeviceEdit->text());
settings.setValue("global/feeInPercent", ui_.feePercentSpinBox->value());
settings.setValue("global/maxFeeInEuro", ui_.maxFeeSpinBox->value());
if (oldCashPointNo != newCashPointNo) {
int result =
QMessageBox(QMessageBox::Icon::Question, "Sind Sie sicher?",
"Möchten Sie die Kassen-Nr wirklich ändern? Diese muss über alle "
"Installationen hinweg eindeutig sein.",
QMessageBox::StandardButton::Yes | QMessageBox::StandardButton::No, this)
.exec();
if (result == QMessageBox::Yes) {
try {
Database().updateCashPointNo(oldCashPointNo, newCashPointNo);
} catch (std::exception& ex) {
std::string errMsg("Das Ändern der Kassen-Nr. ist fehlgeschlagen: ");
errMsg.append(ex.what());
QMessageBox(QMessageBox::Icon::Critical, "Fehler",
errMsg.c_str(),
QMessageBox::StandardButton::Ok,
this)
.exec();
QDialog::accept();
return;
}
settings.setValue("global/cashPointNo", ui_.cashPointNoSpinBox->value());
}
}
QDialog::accept();
}

21
src/gui/settingsdialog.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef SETTINGS_DIALOG_H
#define SETTINGS_DIALOG_H
#include "ui_settingsdialog.h"
#include <QDialog>
class SettingsDialog : public QDialog
{
public:
SettingsDialog(QWidget* parent = nullptr,
Qt::WindowFlags f = Qt::WindowTitleHint | Qt::WindowSystemMenuHint);
public slots:
void accept() override;
private:
Ui::SettingsDialog ui_;
};
#endif

103
src/gui/settingsdialog.ui Normal file
View File

@ -0,0 +1,103 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>SettingsDialog</class>
<widget class="QDialog" name="SettingsDialog">
<property name="windowTitle">
<string>Einstellungen</string>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label">
<property name="text">
<string>Kassen-Nr.:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QSpinBox" name="cashPointNoSpinBox"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_2">
<property name="text">
<string>Bondrucker Gerätedatei:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="posPrinterDeviceEdit"/>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="testPosPrinterButton">
<property name="text">
<string>Testen</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="label_3">
<property name="text">
<string>Gebühr in %:</string>
</property>
</widget>
</item>
<item row="2" column="1">
<widget class="QSpinBox" name="feePercentSpinBox"/>
</item>
<item row="3" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>max. Gebühr in €:</string>
</property>
</widget>
</item>
<item row="3" column="1">
<widget class="QSpinBox" name="maxFeeSpinBox"/>
</item>
<item row="4" column="0" colspan="3">
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</widget>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>SettingsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>248</x>
<y>254</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>SettingsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>316</x>
<y>260</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>