From 8b132c6c51826ed4563c8ce6b4091c082d3d5ba7 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Fri, 2 Jun 2023 11:58:13 +0200 Subject: [PATCH] Bugfix - voucher items manipulation fixed. --- shop/data/voucher.cpp | 8 +++++++- shop/shopform.cpp | 8 ++++++-- shop/shopservice.cpp | 11 ++++++++--- 3 files changed, 21 insertions(+), 6 deletions(-) diff --git a/shop/data/voucher.cpp b/shop/data/voucher.cpp index 3588e8b..807895b 100644 --- a/shop/data/voucher.cpp +++ b/shop/data/voucher.cpp @@ -131,7 +131,13 @@ void Voucher::addItem(QSharedPointer item) void Voucher::removeItem(QSharedPointer item) { - m_items.removeOne(item); + if (item->id() != 0) { + m_items.removeIf([&item](const auto& it){ + return item->id() == it->id(); + }); + } else { + m_items.removeOne(item); + } } void Voucher::clearItems() diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 1e4e934..f0b095f 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -515,7 +515,6 @@ void ShopForm::recalculate() { ShopService srv; srv.calculate(m_voucher); - this->m_itemsModel->setData(m_voucher->items()); setTotalText(); ui->temporarySaveButton->setEnabled(!m_voucher->items().isEmpty()); @@ -530,6 +529,9 @@ void ShopForm::recalculate() { srv.update(m_voucher); } + + this->m_itemsModel->setData(m_voucher->items()); + connectItemSignals(); } void ShopForm::on_receiptCombo_currentIndexChanged(int) @@ -654,7 +656,9 @@ void ShopForm::on_actionDelete_items_triggered() QList forDelete; for (const auto& row : ui->actualReceipt->selectionModel()->selectedIndexes()) { - forDelete << m_itemsModel->itemFromIndex(row); + if (!forDelete.contains(m_itemsModel->itemFromIndex(row))) { + forDelete << m_itemsModel->itemFromIndex(row); + } } for (auto& item : forDelete) { diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index c3a18b7..7216bad 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -173,6 +173,10 @@ void ShopService::pay(VoucherPtr voucher) voucher->setPayDateTime(QDateTime::currentDateTime()); this->update(voucher, &session); + + if (!session.isValid()) { + qDebug() << session.firstError().text(); + } } void ShopService::updateRelatedItem(VoucherItem* item, int countAdded) @@ -526,10 +530,11 @@ void ShopService::update(VoucherPtr entity, qx::QxSession* pSession) { Service::update(entity, pSession); auto oldItems = entity->items(); - entity->clearItems(); - load(entity); - for (auto item : entity->items()) { + Service srvItem; + auto items = srvItem.all("voucher = " + QString::number(entity->id())); + + for (auto item : items) { auto newItem = std::find_if(oldItems.begin(), oldItems.end(), [item](auto it){ return item->id() == it->id(); });