diff --git a/commodity/commodityservice.cpp b/commodity/commodityservice.cpp index adbfb82..ecb1d3f 100644 --- a/commodity/commodityservice.cpp +++ b/commodity/commodityservice.cpp @@ -17,3 +17,15 @@ QList > CommodityService::shopItems() return ret; } + +void CommodityService::addedToVoucher(int itemId, int countAdded) +{ + QSharedPointer commodity = loadById(itemId); + + if (!commodity.isNull()) + { + commodity->setCount(commodity->count() - countAdded); + } + + update(commodity); +} diff --git a/commodity/commodityservice.h b/commodity/commodityservice.h index 8527aef..4f67690 100644 --- a/commodity/commodityservice.h +++ b/commodity/commodityservice.h @@ -13,6 +13,7 @@ public: // ISellableService interface public: QList > shopItems() override; + void addedToVoucher(int itemId, int countAdded) override; }; #endif // COMMODITYSERVICE_H diff --git a/commodity/data/commoditydata.cpp b/commodity/data/commoditydata.cpp index 5a12089..2463564 100644 --- a/commodity/data/commoditydata.cpp +++ b/commodity/data/commoditydata.cpp @@ -8,7 +8,7 @@ CommodityData::CommodityData(QObject *parent) m_price = 0; m_vat = Enums::NONE; } -int CommodityData::id() const +int CommodityData::id() { return m_id; } @@ -17,7 +17,7 @@ void CommodityData::setId(int id) { m_id = id; } -QString CommodityData::name() const +QString CommodityData::name() { return m_name; } @@ -83,6 +83,21 @@ void CommodityData::setCount(int count) m_count = count; } +QDecDouble CommodityData::unitPrice() +{ + return price(); +} + +Enums::VatType CommodityData::vatType() +{ + return vat(); +} + +QString CommodityData::pluginId() +{ + return "COMMODITY"; +} + diff --git a/commodity/data/commoditydata.h b/commodity/data/commoditydata.h index 3fb5c42..462b4f1 100644 --- a/commodity/data/commoditydata.h +++ b/commodity/data/commoditydata.h @@ -14,9 +14,9 @@ class CommodityData : public ShopItem { Q_OBJECT + Q_PROPERTY(QString code READ code WRITE setCode) Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QString shortName READ shortName WRITE setShortName) - Q_PROPERTY(QString code READ code WRITE setCode) Q_PROPERTY(QSharedPointer type READ type WRITE setType) Q_PROPERTY(QDecDouble price READ price WRITE setPrice) Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat) @@ -25,10 +25,10 @@ class CommodityData : public ShopItem public: CommodityData(QObject *parent = 0); - int id() const; + int id() override; void setId(int id); - QString name() const; + QString name() override; void setName(const QString &name); QString shortName() const; @@ -60,6 +60,12 @@ private: int m_price; Enums::VatType m_vat; int m_count; + + // IShopItem interface +public: + QDecDouble unitPrice() override; + Enums::VatType vatType() override; + QString pluginId() override; }; #endif // COMMODITYDATA_H diff --git a/shop/data/voucheritem.cpp b/shop/data/voucheritem.cpp index 3d25f3f..cd7132f 100644 --- a/shop/data/voucheritem.cpp +++ b/shop/data/voucheritem.cpp @@ -39,8 +39,9 @@ int VoucherItem::count() const void VoucherItem::setCount(int count) { + int oldCount = m_count; m_count = count; - emit countChanged(); + emit countChanged(oldCount); } QDecDouble VoucherItem::unitPrice() const diff --git a/shop/data/voucheritem.h b/shop/data/voucheritem.h index 02ee8de..d41e77f 100644 --- a/shop/data/voucheritem.h +++ b/shop/data/voucheritem.h @@ -63,7 +63,7 @@ public: void setVoucher(const QWeakPointer &voucher); signals: - void countChanged(); + void countChanged(int oldCount); private: friend class odb::access; diff --git a/shop/isellableservice.h b/shop/isellableservice.h index 41ba709..dc778c0 100644 --- a/shop/isellableservice.h +++ b/shop/isellableservice.h @@ -11,8 +11,8 @@ class SHOPSHARED_EXPORT ISellableService public: ISellableService(); - virtual QList shopItems() = 0; - //virtual void addToReceipt() + virtual QList > shopItems() = 0; + virtual void addedToVoucher(int itemId, int countAdded) = 0; }; #endif // ISELLABLESERVICE_H diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 8995846..fd3a9eb 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -8,6 +8,7 @@ #include "receiptgenerator.h" #include "paydialog.h" #include "paydvouchersdialog.h" +#include "isellableservice.h" #include #include #include "shop-odb.hxx" @@ -60,6 +61,10 @@ void ShopForm::loadLast() { m_commodityModel = new AutoTableModel(this); ui->commodityTable->setModel(m_commodityModel); + + connect(ui->commodityTable->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex ¤t, const QModelIndex &){ + ui->btnAddItem->setEnabled(current.isValid()); + }); } m_commodityModel->setData(srv.allSellableItems()); @@ -93,11 +98,7 @@ void ShopForm::on_directSale_clicked() form->setAttribute(Qt::WA_DeleteOnClose); connect(form, &QDialog::accepted, [this, form](){ - 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(); + addItem(form->shopItem(), ((DirectSaleItem*)form->shopItem().data())->count()); }); form->show(); @@ -149,7 +150,7 @@ void ShopForm::on_loadButton_clicked() form->show(); } -void ShopForm::onCountChanged() +void ShopForm::onCountChanged(int oldCount) { VoucherItem *item = qobject_cast(sender()); if (item != NULL && item->count() == 0) @@ -179,6 +180,12 @@ void ShopForm::onCountChanged() { srv.updateVoucher(m_voucher); } + + if (item != NULL) + { + int countAdded = item->count() - oldCount; + srv.updateRelatedItem(item, countAdded); + } } void ShopForm::createVoucher() @@ -241,7 +248,7 @@ void ShopForm::changeReceipt() void ShopForm::connectItemSignals() { foreach (QSharedPointer item, m_voucher->items()) { - connect(item.data(), SIGNAL(countChanged()), this, SLOT(onCountChanged())); + connect(item.data(), SIGNAL(countChanged(int)), this, SLOT(onCountChanged(int))); } } @@ -255,6 +262,15 @@ void ShopForm::createEmptyVoucher() ui->payButton->setEnabled(false); } +void ShopForm::addItem(QSharedPointer item, int count) +{ + ShopService srv; + srv.addShopItem(m_voucher, item, 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(int)), this, SLOT(onCountChanged(int))); + onCountChanged(0); +} + void ShopForm::on_receiptCombo_currentIndexChanged(int) { if (!m_voucher.isNull() && m_voucher->items().isEmpty()) @@ -303,3 +319,14 @@ void ShopForm::on_showPaiedButton_clicked() dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->show(); } + +void ShopForm::on_btnAddItem_clicked() +{ + if (m_voucher.isNull()) + { + createVoucher(); + } + + ShopItemPtr item = m_commodityModel->itemFromIndex(ui->commodityTable->currentIndex()); + addItem(item, ui->spnCount->value()); +} diff --git a/shop/shopform.h b/shop/shopform.h index bb26645..301823b 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -7,6 +7,7 @@ #include class ShopItem; +class IShopItem; namespace Ui { class ShopForm; @@ -31,7 +32,7 @@ private slots: void on_loadButton_clicked(); - void onCountChanged(); + void onCountChanged(int oldCount); void on_receiptCombo_currentIndexChanged(int index); @@ -39,6 +40,8 @@ private slots: void on_showPaiedButton_clicked(); + void on_btnAddItem_clicked(); + private: Ui::ShopForm *ui; QSharedPointer m_voucher; @@ -50,6 +53,7 @@ private: void changeReceipt(); void connectItemSignals(); void createEmptyVoucher(); + void addItem(QSharedPointer item, int count); }; #endif // SHOPFORM_H diff --git a/shop/shopform.ui b/shop/shopform.ui index 56dceaf..93f4c36 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -55,7 +55,14 @@ - + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + @@ -68,7 +75,7 @@ - + false @@ -78,13 +85,23 @@ false + + 1 + - + + + false + Add Item + + + :/icons/new.svg:/icons/new.svg + @@ -386,8 +403,8 @@ - + diff --git a/shop/shopitem.h b/shop/shopitem.h index e9c9f26..0f03f44 100644 --- a/shop/shopitem.h +++ b/shop/shopitem.h @@ -10,6 +10,7 @@ class SHOPSHARED_EXPORT ShopItem : public QObject, public IShopItem { Q_OBJECT + Q_PROPERTY(QString code READ code) Q_PROPERTY(QString name READ name) Q_PROPERTY(QDecDouble unitPrice READ unitPrice) Q_PROPERTY(Enums::VatType vatType READ vatType) @@ -24,6 +25,7 @@ public slots: // IShopItem interface public: virtual int id() override { return 0; } + virtual QString code() { return ""; } virtual QString name() override { return ""; } virtual QDecDouble unitPrice() override { return QDecDouble(); } virtual Enums::VatType vatType() override { return Enums::NONE; } diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 242dd4a..9aa6895 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -25,6 +25,8 @@ void ShopService::addShopItem(VoucherPtr voucher, QSharedPointer item vItem->setVatType(item->vatType()); voucher->addItem(vItem); + + updateRelatedItem(vItem.data(), count); } void ShopService::calculate(VoucherPtr voucher) @@ -106,6 +108,17 @@ void ShopService::pay(VoucherPtr voucher) tx.commit(); } +void ShopService::updateRelatedItem(VoucherItem* item, int countAdded) +{ + IPlugin *plugin = Context::instance().plugin(item->itemPlugin()); + IService *srv = (plugin != NULL ? plugin->service() : NULL); + ISellableService *selSrv = dynamic_cast(srv); + + if (selSrv != NULL) + { + selSrv->addedToVoucher(item->refId(), countAdded); + } +} void ShopService::moveItems(QList items, VoucherPtr source, VoucherPtr target) { Transaction tx; diff --git a/shop/shopservice.h b/shop/shopservice.h index 4646e55..e4b5bf6 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -20,6 +20,7 @@ public: void loadItems(VoucherPtr voucher); void pay(VoucherPtr voucher); void moveItems(QList items, VoucherPtr source, VoucherPtr target); + void updateRelatedItem(VoucherItem* item, int countAdded); QList savedVouchers(); QList tempVouchers(); QList paiedVouchers();