From 9e9e1f6dba93bf3e18b66f5c0ab18295d540ce75 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Wed, 3 Aug 2016 18:24:15 +0200 Subject: [PATCH] Added load receipt functionality. --- core/context.cpp | 4 ++++ shop/receiptloadform.cpp | 11 +++++++++++ shop/receiptloadform.h | 2 ++ shop/shopform.cpp | 17 +++++++++++++++++ shop/shopservice.cpp | 27 +++++++++++++++++++++++++++ shop/shopservice.h | 1 + 6 files changed, 62 insertions(+) diff --git a/core/context.cpp b/core/context.cpp index c658df6..7b92ef3 100644 --- a/core/context.cpp +++ b/core/context.cpp @@ -65,6 +65,10 @@ void Context::loadPlugins() m_plugins.append(plugin); } } + else + { + qDebug() << pluginLoader.errorString(); + } } QString dbPath = m_settings->value("db/path", "").toString(); diff --git a/shop/receiptloadform.cpp b/shop/receiptloadform.cpp index 9622c27..1eff0a6 100644 --- a/shop/receiptloadform.cpp +++ b/shop/receiptloadform.cpp @@ -28,6 +28,7 @@ ReceiptLoadForm::ReceiptLoadForm(QWidget *parent) : ui->tabVouchers->setColumnWidth(2, 200); m_itemModel = new AutoTableModel(this); + m_itemModel->setCheckboxSelect(true); ui->tabItems->setModel(m_itemModel); connect(ui->tabVouchers->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex ¤t, QModelIndex){ @@ -43,6 +44,16 @@ ReceiptLoadForm::~ReceiptLoadForm() delete ui; } +QList ReceiptLoadForm::selectedItems() +{ + return m_itemModel->selectedItems(); +} + +VoucherPtr ReceiptLoadForm::selectedVoucher() +{ + return m_voucherModel->itemFromIndex(ui->tabVouchers->currentIndex()); +} + void ReceiptLoadForm::on_lineEdit_textChanged(const QString &text) { QSortFilterProxyModel proxy; diff --git a/shop/receiptloadform.h b/shop/receiptloadform.h index ec022d3..f49954d 100644 --- a/shop/receiptloadform.h +++ b/shop/receiptloadform.h @@ -16,6 +16,8 @@ class ReceiptLoadForm : public QDialog public: explicit ReceiptLoadForm(QWidget *parent = 0); ~ReceiptLoadForm(); + QList selectedItems(); + VoucherPtr selectedVoucher(); private slots: void on_lineEdit_textChanged(const QString &text); diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 3b1cfc5..8995846 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -129,6 +129,23 @@ void ShopForm::on_loadButton_clicked() { ReceiptLoadForm *form = new ReceiptLoadForm(this); form->setAttribute(Qt::WA_DeleteOnClose); + + connect(form, &QDialog::accepted, [this, form](){ + ShopService srv; + + if (m_voucher.isNull()) + { + m_voucher = srv.createVoucher(); + } + + srv.moveItems(form->selectedItems(), form->selectedVoucher(), this->m_voucher); + + m_itemsModel->setData(m_voucher->items()); + + connectItemSignals(); + onCountChanged(); + }); + form->show(); } diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 23833d7..242dd4a 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -106,6 +106,33 @@ void ShopService::pay(VoucherPtr voucher) tx.commit(); } +void ShopService::moveItems(QList items, VoucherPtr source, VoucherPtr target) +{ + Transaction tx; + + if (target->status() == Voucher::NEW && target->id() == 0) + { + this->saveVoucher(target); + } + + odb::database *db = Context::instance().db(); + + foreach (VoucherItemPtr item, items) { + QString sql = QString("update VoucherItem set voucher = %1 where id = %2").arg(QString::number(target->id()), QString::number(item->id())); + db->execute(sql.toStdString()); + } + + loadItems(source); + loadItems(target); + + if (source->items().isEmpty()) + { + erase(source); + } + + tx.commit(); +} + QList ShopService::savedVouchers() { return all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID))); diff --git a/shop/shopservice.h b/shop/shopservice.h index 2b1ef82..4646e55 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -19,6 +19,7 @@ public: void calculateItem(VoucherItemPtr item); void loadItems(VoucherPtr voucher); void pay(VoucherPtr voucher); + void moveItems(QList items, VoucherPtr source, VoucherPtr target); QList savedVouchers(); QList tempVouchers(); QList paiedVouchers();