From 9fbf216bba551234b76a836d5a00cda7fdc87b03 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Thu, 26 May 2016 15:11:55 +0200 Subject: [PATCH] Added recalculating receipt on item count changed. Some UI changes. --- addressbook/data/addressbookdata.h | 9 +- commodity/commoditysettingsform.cpp | 2 + shop/data/voucher.cpp | 5 + shop/data/voucher.h | 1 + shop/data/voucheritem.cpp | 2 + shop/data/voucheritem.h | 5 +- shop/directsaleform.ui | 12 +- shop/shopform.cpp | 25 +++ shop/shopform.h | 2 + shop/shopform.ui | 237 ++++++++++++++++++++-------- shop/shopservice.cpp | 9 +- shop/shopservice.h | 1 + 12 files changed, 239 insertions(+), 71 deletions(-) diff --git a/addressbook/data/addressbookdata.h b/addressbook/data/addressbookdata.h index cc0f2b8..bcfc735 100644 --- a/addressbook/data/addressbookdata.h +++ b/addressbook/data/addressbookdata.h @@ -5,9 +5,16 @@ #include #include #include +#include + +#if defined(ADDRESSBOOK_LIBRARY) +# define ADDRESSBOOKSHARED_EXPORT Q_DECL_EXPORT +#else +# define ADDRESSBOOKSHARED_EXPORT Q_DECL_IMPORT +#endif #pragma db object -class AddressbookData : public QObject +class ADDRESSBOOKSHARED_EXPORT AddressbookData : public QObject { Q_OBJECT Q_PROPERTY(QString title READ title WRITE setTitle) diff --git a/commodity/commoditysettingsform.cpp b/commodity/commoditysettingsform.cpp index a17e072..ea25466 100644 --- a/commodity/commoditysettingsform.cpp +++ b/commodity/commoditysettingsform.cpp @@ -44,6 +44,8 @@ bool CommoditySettingsForm::saveRecord() commodityService.erase(item); } + + return true; } void CommoditySettingsForm::on_addCommodityType_clicked() diff --git a/shop/data/voucher.cpp b/shop/data/voucher.cpp index 83f64b0..113a592 100644 --- a/shop/data/voucher.cpp +++ b/shop/data/voucher.cpp @@ -74,6 +74,11 @@ void Voucher::addItem(QSharedPointer item) m_items.append(item); } +void Voucher::removeItem(QSharedPointer item) +{ + m_items.removeOne(item); +} + void Voucher::clearItems() { m_items.clear(); diff --git a/shop/data/voucher.h b/shop/data/voucher.h index db4e380..b6b13b1 100644 --- a/shop/data/voucher.h +++ b/shop/data/voucher.h @@ -56,6 +56,7 @@ public: void setItems(const QList > &items); void addItem(QSharedPointer item); + void removeItem(QSharedPointer item); void clearItems(); private: diff --git a/shop/data/voucheritem.cpp b/shop/data/voucheritem.cpp index 3129b58..d0615e8 100644 --- a/shop/data/voucheritem.cpp +++ b/shop/data/voucheritem.cpp @@ -7,6 +7,7 @@ VoucherItem::VoucherItem(QObject *parent) : QObject(parent) m_unitPrice = 0; m_count = 0; m_refId = 0; + m_vatType = Enums::HIGH; } int VoucherItem::id() const @@ -37,6 +38,7 @@ int VoucherItem::count() const void VoucherItem::setCount(int count) { m_count = count; + emit countChanged(); } QDecDouble VoucherItem::unitPrice() const diff --git a/shop/data/voucheritem.h b/shop/data/voucheritem.h index 0457c57..29a5684 100644 --- a/shop/data/voucheritem.h +++ b/shop/data/voucheritem.h @@ -14,7 +14,7 @@ class VoucherItem : public QObject Q_OBJECT Q_PROPERTY(QString name READ name WRITE setName) - Q_PROPERTY(int count READ count WRITE setCount) + Q_PROPERTY(int count READ count WRITE setCount NOTIFY countChanged) Q_PROPERTY(QDecDouble unitPrice READ unitPrice WRITE setUnitPrice) Q_PROPERTY(QDecDouble price READ price WRITE setPrice) Q_PROPERTY(Enums::VatType vatType READ vatType WRITE setVatType) @@ -46,6 +46,9 @@ public: Enums::VatType vatType() const; void setVatType(const Enums::VatType &vatType); +signals: + void countChanged(); + private: friend class odb::access; #pragma db id auto diff --git a/shop/directsaleform.ui b/shop/directsaleform.ui index 952f906..77cf8eb 100644 --- a/shop/directsaleform.ui +++ b/shop/directsaleform.ui @@ -32,7 +32,11 @@ - + + + 9999999999.989999771118164 + + @@ -42,7 +46,11 @@ - + + + 999999 + + diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 5072dfb..497391f 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -16,6 +16,7 @@ ShopForm::ShopForm(QWidget *parent) : m_itemsModel = new AutoTableModel(this); m_itemsModel->setEditableCols(QList() << 1); ui->actualReceipt->setModel(m_itemsModel); + ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); } ShopForm::~ShopForm() @@ -31,6 +32,8 @@ void ShopForm::on_directSale_clicked() ShopService srv; srv.addShopItem(m_voucher, form->shopItem(), ((DirectSaleItem*)form->shopItem().data())->count()); this->m_itemsModel->addRow(m_voucher->items()[m_voucher->items().count() - 1]); + connect(m_voucher->items()[m_voucher->items().count() - 1].data(), SIGNAL(countChanged()), this, SLOT(onCountChanged())); + onCountChanged(); }); form->show(); @@ -53,3 +56,25 @@ void ShopForm::on_loadButton_clicked() ReceiptLoadForm *form = new ReceiptLoadForm; form->show(); } + +void ShopForm::onCountChanged() +{ + VoucherItem *item = qobject_cast(sender()); + if (item != NULL && item->count() == 0) + { + for (int i = 0; i < m_voucher->items().count(); i++) + { + if (m_voucher->items()[i].data() == item) + { + m_voucher->removeItem(m_voucher->items()[i]); + m_itemsModel->setData(m_voucher->items()); + } + } + } + + ShopService srv; + srv.calculate(m_voucher); + ui->total->setText(m_voucher->totalPrice().toString()); + + //if (m_voucher->status()) +} diff --git a/shop/shopform.h b/shop/shopform.h index e62453b..4e94ffa 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -27,6 +27,8 @@ private slots: void on_loadButton_clicked(); + void onCountChanged(); + private: Ui::ShopForm *ui; QSharedPointer m_voucher; diff --git a/shop/shopform.ui b/shop/shopform.ui index f02cb6d..2a2ebd2 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -7,95 +7,187 @@ 0 0 959 - 531 + 582 Form - + - + + + 0 + 100 + + + + + 0 + + + 0 + + + 0 + + + 0 + - - - Commodity + + + + 500 + 16777215 + - - - - - - - - - - - + - + - Count + Commodity - - - false - - - QAbstractSpinBox::UpDownArrows - - - false - - + - - - Add Item - + + + + + + + + + Count + + + + + + + false + + + QAbstractSpinBox::UpDownArrows + + + false + + + + + + + Add Item + + + + - - - - - - - - - - - - Direct Sale - + + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + Direct Sale + + + + :/icons/shop.svg:/icons/shop.svg + + + + 45 + 45 + + + + Ctrl+D + + + Qt::ToolButtonTextUnderIcon + + + true + + + + - - - - - - - - + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + - - - - - - - - + + + + + 0 + + + 0 + + + 0 + + + 0 + + + 0 + + + + + + @@ -104,7 +196,7 @@ - + @@ -122,6 +214,19 @@ + + + + Qt::Horizontal + + + + 40 + 20 + + + + @@ -196,6 +301,8 @@ - + + + diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 3441ef3..57e618f 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -29,9 +29,14 @@ void ShopService::calculate(QSharedPointer voucher) QDecDouble total; foreach (QSharedPointer item, voucher->items()) { - QDecDouble itemPrice = item->unitPrice() * item->count(); - total += itemPrice; + calculateItem(item); + total += item->price(); } voucher->setTotalPrice(total); } + +void ShopService::calculateItem(QSharedPointer item) +{ + item->setPrice(item->unitPrice() * item->count()); +} diff --git a/shop/shopservice.h b/shop/shopservice.h index 77212f7..6bcb841 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -15,6 +15,7 @@ public: QSharedPointer createVoucher(); void addShopItem(QSharedPointer voucher, QSharedPointer item, int count); void calculate(QSharedPointer voucher); + void calculateItem(QSharedPointer item); }; #endif // SHOPSERVICE_H