From facaf98cf00e6f15bab91e451ed957c1530eef8a Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Sun, 29 May 2016 18:31:46 +0200 Subject: [PATCH] Added support for saving receipts. --- core/core.pro | 2 ++ core/service.h | 4 ++-- core/transaction.cpp | 3 +++ shop/data/voucher.cpp | 7 ++++--- shop/data/voucher.h | 1 + shop/data/voucheritem.cpp | 12 +++++++++++- shop/data/voucheritem.h | 8 ++++++++ shop/shop.cpp | 7 +++++++ shop/shop.h | 4 ++++ shop/shop.json | 26 ++++++------------------- shop/shopform.cpp | 40 +++++++++++++++++++++++++++++++++------ shop/shopform.h | 1 + shop/shopservice.cpp | 39 ++++++++++++++++++++++++++++++++++++++ shop/shopservice.h | 6 ++++++ 14 files changed, 128 insertions(+), 32 deletions(-) diff --git a/core/core.pro b/core/core.pro index c9a1656..5b64143 100644 --- a/core/core.pro +++ b/core/core.pro @@ -144,6 +144,8 @@ OTHER_FILES += \ users/metaData.json \ roles/metaData.json +CONFIG(debug, release|debug):DEFINES += _DEBUG + win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber else:unix: LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber diff --git a/core/service.h b/core/service.h index e3267db..3d5e06c 100644 --- a/core/service.h +++ b/core/service.h @@ -67,7 +67,7 @@ public: return ret; } - void save(QSharedPointer entity) { + virtual void save(QSharedPointer entity) { if (!checkPermission(PERM_ADD)) { return; } @@ -93,7 +93,7 @@ public: emit dataChanged(); } - void update(QSharedPointer entity) { + virtual void update(QSharedPointer entity) { if (!checkPermission(PERM_EDIT)) { return; } diff --git a/core/transaction.cpp b/core/transaction.cpp index 412d450..2b29836 100644 --- a/core/transaction.cpp +++ b/core/transaction.cpp @@ -9,6 +9,9 @@ Transaction::Transaction() if (!Transaction::m_inTransaction) { m_tr = new odb::transaction(Context::instance().db()->begin()); +#ifdef _DEBUG + m_tr->tracer(odb::stderr_tracer); +#endif Transaction::m_inTransaction = true; } else diff --git a/shop/data/voucher.cpp b/shop/data/voucher.cpp index 0b8bb27..55b330c 100644 --- a/shop/data/voucher.cpp +++ b/shop/data/voucher.cpp @@ -3,6 +3,7 @@ Voucher::Voucher(QObject *parent) : QObject(parent) { + m_id = 0; m_vatRateHigh = 0; m_vatRateFirstLower = 0; m_vatRateSecondLower = 0; @@ -196,17 +197,17 @@ void Voucher::setTotalPriceVatSecondLower(QDecDouble totalPriceVatSecondLower) QDecDouble Voucher::vatAmountHigh() { - return m_totalPriceVatHigh - m_priceVatHigh; + return TO_DEC(m_totalPriceVatHigh) - TO_DEC(m_priceVatHigh); } QDecDouble Voucher::vatAmountFirstLower() { - return m_totalPriceVatFirstLower - m_priceVatFirstLower; + return TO_DEC(m_totalPriceVatFirstLower) - TO_DEC(m_priceVatFirstLower); } QDecDouble Voucher::VatAmountSecondLower() { - return m_totalPriceVatSecondLower - m_priceVatSecondLower; + return TO_DEC(m_totalPriceVatSecondLower) - TO_DEC(m_priceVatSecondLower); } int Voucher::id() const diff --git a/shop/data/voucher.h b/shop/data/voucher.h index d549ff9..c5c3782 100644 --- a/shop/data/voucher.h +++ b/shop/data/voucher.h @@ -121,6 +121,7 @@ private: int m_totalPriceVatFirstLower; int m_totalPriceVatSecondLower; int m_totalPrice; + #pragma db value_not_null inverse(m_voucher) QOdbList > m_items; VoucherStatus m_status; }; diff --git a/shop/data/voucheritem.cpp b/shop/data/voucheritem.cpp index 133d96e..3d25f3f 100644 --- a/shop/data/voucheritem.cpp +++ b/shop/data/voucheritem.cpp @@ -113,7 +113,17 @@ void VoucherItem::setPriceWitouthVat(QDecDouble priceWitouthVat) QDecDouble VoucherItem::vatAmount() const { - return m_price - m_priceWitouthVat; + return TO_DEC(m_price) - TO_DEC(m_priceWitouthVat); +} + +QWeakPointer VoucherItem::voucher() const +{ + return m_voucher; +} + +void VoucherItem::setVoucher(const QWeakPointer &voucher) +{ + m_voucher = voucher; } diff --git a/shop/data/voucheritem.h b/shop/data/voucheritem.h index a99cb56..2b412f9 100644 --- a/shop/data/voucheritem.h +++ b/shop/data/voucheritem.h @@ -5,9 +5,12 @@ #include #include #include +#include #include +class Voucher; + #pragma db object class VoucherItem : public QObject { @@ -56,6 +59,9 @@ public: QDecDouble vatAmount() const; + QWeakPointer voucher() const; + void setVoucher(const QWeakPointer &voucher); + signals: void countChanged(); @@ -72,6 +78,8 @@ private: int m_refId; QString m_itemPlugin; Enums::VatType m_vatType; + #pragma db not_null + QWeakPointer m_voucher; }; #endif // VOUCHERITEM_H diff --git a/shop/shop.cpp b/shop/shop.cpp index 50516d9..eab1499 100644 --- a/shop/shop.cpp +++ b/shop/shop.cpp @@ -18,3 +18,10 @@ QIcon Shop::pluginIcon() { return QIcon(":/icons/shop.svg"); } + +QWidget *Shop::ui() +{ + QWidget *uiWidget = IPlugin::ui(); + qobject_cast(uiWidget)->loadLast(); + return uiWidget; +} diff --git a/shop/shop.h b/shop/shop.h index bfd1efc..4bcbadb 100644 --- a/shop/shop.h +++ b/shop/shop.h @@ -22,6 +22,10 @@ protected: // IPlugin interface public: virtual QIcon pluginIcon(); + + // IPlugin interface +public: + virtual QWidget *ui() override; }; #endif // SHOP_H diff --git a/shop/shop.json b/shop/shop.json index 6bab111..80e3278 100644 --- a/shop/shop.json +++ b/shop/shop.json @@ -20,7 +20,12 @@ \"price\" INTEGER NOT NULL, \"refId\" INTEGER NOT NULL, \"itemPlugin\" TEXT NULL, - \"vatType\" INTEGER NOT NULL); + \"vatType\" INTEGER NOT NULL, + \"voucher\" INTEGER NOT NULL, + CONSTRAINT \"voucher_fk\" + FOREIGN KEY (\"voucher\") + REFERENCES \"Voucher\" (\"id\") + DEFERRABLE INITIALLY DEFERRED); CREATE TABLE \"Voucher\" ( \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, @@ -44,25 +49,6 @@ CREATE TABLE \"Voucher\" ( FOREIGN KEY (\"contact\") REFERENCES \"AddressbookData\" (\"id\") DEFERRABLE INITIALLY DEFERRED); - -CREATE TABLE \"Voucher_items\" ( - \"object_id\" INTEGER NOT NULL, - \"index\" INTEGER NOT NULL, - \"value\" INTEGER NULL, - CONSTRAINT \"object_id_fk\" - FOREIGN KEY (\"object_id\") - REFERENCES \"Voucher\" (\"id\") - ON DELETE CASCADE, - CONSTRAINT \"value_fk\" - FOREIGN KEY (\"value\") - REFERENCES \"VoucherItem\" (\"id\") - DEFERRABLE INITIALLY DEFERRED); - -CREATE INDEX \"Voucher_items_object_id_i\" - ON \"Voucher_items\" (\"object_id\"); - -CREATE INDEX \"Voucher_items_index_i\" - ON \"Voucher_items\" (\"index\"); " ], "dependencies" : [ "ADDRESSBOOK" ] diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 7628149..e2648b9 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -5,6 +5,9 @@ #include "receiptsaveform.h" #include "receiptloadform.h" #include "shopservice.h" +#include +#include +#include "shop-odb.hxx" ShopForm::ShopForm(QWidget *parent) : QWidget(parent), @@ -18,6 +21,28 @@ ShopForm::~ShopForm() delete ui; } +void ShopForm::loadLast() +{ + m_itemsModel = new AutoTableModel(this); + m_itemsModel->setEditableCols(QList() << 1); + ui->actualReceipt->setModel(m_itemsModel); + ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + + ShopService srv; + QList > receipt = srv.all(QString("status = %1").arg(QString::number(Voucher::NEW))); + if (!receipt.isEmpty()) + { + m_voucher = receipt[0]; + srv.loadItems(m_voucher); + m_itemsModel->setData(m_voucher->items()); + + foreach (QSharedPointer item, m_voucher->items()) { + connect(item.data(), SIGNAL(countChanged()), this, SLOT(onCountChanged())); + } + ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); + } +} + void ShopForm::on_directSale_clicked() { if (m_voucher.isNull()) @@ -73,17 +98,20 @@ void ShopForm::onCountChanged() ShopService srv; srv.calculate(m_voucher); - ui->total->setText(m_voucher->totalPrice().toString()); + ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); - //if (m_voucher->status()) + if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0) + { + srv.save(m_voucher); + } + else + { + srv.update(m_voucher); + } } void ShopForm::createVoucher() { ShopService srv; m_voucher = srv.createVoucher(); - m_itemsModel = new AutoTableModel(this); - m_itemsModel->setEditableCols(QList() << 1); - ui->actualReceipt->setModel(m_itemsModel); - ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); } diff --git a/shop/shopform.h b/shop/shopform.h index 4fd013d..d0aba32 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -17,6 +17,7 @@ class ShopForm : public QWidget public: explicit ShopForm(QWidget *parent = 0); ~ShopForm(); + void loadLast(); private slots: void on_directSale_clicked(); diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 4c7a42a..126cd66 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -1,4 +1,5 @@ #include "shopservice.h" +#include "shop-odb.hxx" ShopService::ShopService() { @@ -78,6 +79,12 @@ void ShopService::calculateItem(QSharedPointer item) } } +void ShopService::loadItems(QSharedPointer voucher) +{ + Service srv; + voucher->setItems(srv.all(QString("voucher = %1").arg(voucher->id()))); +} + QDecDouble ShopService::includeVat(QDecDouble price, Enums::VatType vatType) { return price * ((vatRate(vatType) / 100) + QDecDouble(1)); @@ -116,3 +123,35 @@ QDecDouble ShopService::vatRate(Enums::VatType vatType) return vatRate; } + +void ShopService::save(QSharedPointer entity) +{ + Transaction tr; + odb::database *db = Context::instance().db(); + + db->persist(entity); + + foreach (QSharedPointer item, entity->items()) { + item->setVoucher(entity.toWeakRef()); + db->persist(item); + } + + tr.commit(); +} + +void ShopService::update(QSharedPointer entity) +{ + Transaction tr; + odb::database *db = Context::instance().db(); + + db->execute(QString("DELETE FROM VoucherItem WHERE voucher = %1").arg(entity->id()).toStdString()); + + foreach (QSharedPointer item, entity->items()) { + item->setVoucher(entity.toWeakRef()); + db->persist(item); + } + + db->update(entity); + + tr.commit(); +} diff --git a/shop/shopservice.h b/shop/shopservice.h index 1360fca..82d8941 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -17,6 +17,7 @@ public: void addShopItem(QSharedPointer voucher, QSharedPointer item, int count); void calculate(QSharedPointer voucher); void calculateItem(QSharedPointer item); + void loadItems(QSharedPointer voucher); private: QDecDouble includeVat(QDecDouble price, Enums::VatType vatType); @@ -24,6 +25,11 @@ private: QSharedPointer m_gs; QDecDouble vatRate(Enums::VatType vatType); + + // Service interface +public: + virtual void save(QSharedPointer entity) override; + virtual void update(QSharedPointer entity) override; }; #endif // SHOPSERVICE_H