Added sell commodity feature.

print
Josef Rokos 8 years ago
parent fb6b4fe027
commit 44c74a5cfe

@ -17,3 +17,15 @@ QList<QSharedPointer<ShopItem> > CommodityService::shopItems()
return ret; return ret;
} }
void CommodityService::addedToVoucher(int itemId, int countAdded)
{
QSharedPointer<CommodityData> commodity = loadById(itemId);
if (!commodity.isNull())
{
commodity->setCount(commodity->count() - countAdded);
}
update(commodity);
}

@ -13,6 +13,7 @@ public:
// ISellableService interface // ISellableService interface
public: public:
QList<QSharedPointer<ShopItem> > shopItems() override; QList<QSharedPointer<ShopItem> > shopItems() override;
void addedToVoucher(int itemId, int countAdded) override;
}; };
#endif // COMMODITYSERVICE_H #endif // COMMODITYSERVICE_H

@ -8,7 +8,7 @@ CommodityData::CommodityData(QObject *parent)
m_price = 0; m_price = 0;
m_vat = Enums::NONE; m_vat = Enums::NONE;
} }
int CommodityData::id() const int CommodityData::id()
{ {
return m_id; return m_id;
} }
@ -17,7 +17,7 @@ void CommodityData::setId(int id)
{ {
m_id = id; m_id = id;
} }
QString CommodityData::name() const QString CommodityData::name()
{ {
return m_name; return m_name;
} }
@ -83,6 +83,21 @@ void CommodityData::setCount(int count)
m_count = count; m_count = count;
} }
QDecDouble CommodityData::unitPrice()
{
return price();
}
Enums::VatType CommodityData::vatType()
{
return vat();
}
QString CommodityData::pluginId()
{
return "COMMODITY";
}

@ -14,9 +14,9 @@
class CommodityData : public ShopItem class CommodityData : public ShopItem
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString code READ code WRITE setCode)
Q_PROPERTY(QString name READ name WRITE setName) Q_PROPERTY(QString name READ name WRITE setName)
Q_PROPERTY(QString shortName READ shortName WRITE setShortName) Q_PROPERTY(QString shortName READ shortName WRITE setShortName)
Q_PROPERTY(QString code READ code WRITE setCode)
Q_PROPERTY(QSharedPointer<QObject> type READ type WRITE setType) Q_PROPERTY(QSharedPointer<QObject> type READ type WRITE setType)
Q_PROPERTY(QDecDouble price READ price WRITE setPrice) Q_PROPERTY(QDecDouble price READ price WRITE setPrice)
Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat) Q_PROPERTY(Enums::VatType vat READ vat WRITE setVat)
@ -25,10 +25,10 @@ class CommodityData : public ShopItem
public: public:
CommodityData(QObject *parent = 0); CommodityData(QObject *parent = 0);
int id() const; int id() override;
void setId(int id); void setId(int id);
QString name() const; QString name() override;
void setName(const QString &name); void setName(const QString &name);
QString shortName() const; QString shortName() const;
@ -60,6 +60,12 @@ private:
int m_price; int m_price;
Enums::VatType m_vat; Enums::VatType m_vat;
int m_count; int m_count;
// IShopItem interface
public:
QDecDouble unitPrice() override;
Enums::VatType vatType() override;
QString pluginId() override;
}; };
#endif // COMMODITYDATA_H #endif // COMMODITYDATA_H

@ -39,8 +39,9 @@ int VoucherItem::count() const
void VoucherItem::setCount(int count) void VoucherItem::setCount(int count)
{ {
int oldCount = m_count;
m_count = count; m_count = count;
emit countChanged(); emit countChanged(oldCount);
} }
QDecDouble VoucherItem::unitPrice() const QDecDouble VoucherItem::unitPrice() const

@ -63,7 +63,7 @@ public:
void setVoucher(const QWeakPointer<Voucher> &voucher); void setVoucher(const QWeakPointer<Voucher> &voucher);
signals: signals:
void countChanged(); void countChanged(int oldCount);
private: private:
friend class odb::access; friend class odb::access;

@ -12,6 +12,7 @@ public:
ISellableService(); ISellableService();
virtual QList<QSharedPointer<ShopItem> > shopItems() = 0; virtual QList<QSharedPointer<ShopItem> > shopItems() = 0;
virtual void addedToVoucher(int itemId, int countAdded) = 0;
}; };
#endif // ISELLABLESERVICE_H #endif // ISELLABLESERVICE_H

@ -8,6 +8,7 @@
#include "receiptgenerator.h" #include "receiptgenerator.h"
#include "paydialog.h" #include "paydialog.h"
#include "paydvouchersdialog.h" #include "paydvouchersdialog.h"
#include "isellableservice.h"
#include <QList> #include <QList>
#include <QSharedPointer> #include <QSharedPointer>
#include "shop-odb.hxx" #include "shop-odb.hxx"
@ -60,6 +61,10 @@ void ShopForm::loadLast()
{ {
m_commodityModel = new AutoTableModel<ShopItem>(this); m_commodityModel = new AutoTableModel<ShopItem>(this);
ui->commodityTable->setModel(m_commodityModel); ui->commodityTable->setModel(m_commodityModel);
connect(ui->commodityTable->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex &current, const QModelIndex &){
ui->btnAddItem->setEnabled(current.isValid());
});
} }
m_commodityModel->setData(srv.allSellableItems()); m_commodityModel->setData(srv.allSellableItems());
@ -93,11 +98,7 @@ void ShopForm::on_directSale_clicked()
form->setAttribute(Qt::WA_DeleteOnClose); form->setAttribute(Qt::WA_DeleteOnClose);
connect(form, &QDialog::accepted, [this, form](){ connect(form, &QDialog::accepted, [this, form](){
ShopService srv; addItem(form->shopItem(), ((DirectSaleItem*)form->shopItem().data())->count());
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(); form->show();
@ -132,7 +133,7 @@ void ShopForm::on_loadButton_clicked()
form->show(); form->show();
} }
void ShopForm::onCountChanged() void ShopForm::onCountChanged(int oldCount)
{ {
VoucherItem *item = qobject_cast<VoucherItem*>(sender()); VoucherItem *item = qobject_cast<VoucherItem*>(sender());
if (item != NULL && item->count() == 0) if (item != NULL && item->count() == 0)
@ -162,6 +163,12 @@ void ShopForm::onCountChanged()
{ {
srv.updateVoucher(m_voucher); srv.updateVoucher(m_voucher);
} }
if (item != NULL)
{
int countAdded = item->count() - oldCount;
srv.updateRelatedItem(item, countAdded);
}
} }
void ShopForm::createVoucher() void ShopForm::createVoucher()
@ -224,7 +231,7 @@ void ShopForm::changeReceipt()
void ShopForm::connectItemSignals() void ShopForm::connectItemSignals()
{ {
foreach (QSharedPointer<VoucherItem> item, m_voucher->items()) { foreach (QSharedPointer<VoucherItem> item, m_voucher->items()) {
connect(item.data(), SIGNAL(countChanged()), this, SLOT(onCountChanged())); connect(item.data(), SIGNAL(countChanged(int)), this, SLOT(onCountChanged(int)));
} }
} }
@ -238,6 +245,15 @@ void ShopForm::createEmptyVoucher()
ui->payButton->setEnabled(false); ui->payButton->setEnabled(false);
} }
void ShopForm::addItem(QSharedPointer<IShopItem> 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) void ShopForm::on_receiptCombo_currentIndexChanged(int)
{ {
if (!m_voucher.isNull() && m_voucher->items().isEmpty()) if (!m_voucher.isNull() && m_voucher->items().isEmpty())
@ -286,3 +302,14 @@ void ShopForm::on_showPaiedButton_clicked()
dialog->setAttribute(Qt::WA_DeleteOnClose); dialog->setAttribute(Qt::WA_DeleteOnClose);
dialog->show(); 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());
}

@ -7,6 +7,7 @@
#include <autotablemodel.h> #include <autotablemodel.h>
class ShopItem; class ShopItem;
class IShopItem;
namespace Ui { namespace Ui {
class ShopForm; class ShopForm;
@ -31,7 +32,7 @@ private slots:
void on_loadButton_clicked(); void on_loadButton_clicked();
void onCountChanged(); void onCountChanged(int oldCount);
void on_receiptCombo_currentIndexChanged(int index); void on_receiptCombo_currentIndexChanged(int index);
@ -39,6 +40,8 @@ private slots:
void on_showPaiedButton_clicked(); void on_showPaiedButton_clicked();
void on_btnAddItem_clicked();
private: private:
Ui::ShopForm *ui; Ui::ShopForm *ui;
QSharedPointer<Voucher> m_voucher; QSharedPointer<Voucher> m_voucher;
@ -50,6 +53,7 @@ private:
void changeReceipt(); void changeReceipt();
void connectItemSignals(); void connectItemSignals();
void createEmptyVoucher(); void createEmptyVoucher();
void addItem(QSharedPointer<IShopItem> item, int count);
}; };
#endif // SHOPFORM_H #endif // SHOPFORM_H

@ -55,7 +55,14 @@
<widget class="QLineEdit" name="commoditySearch"/> <widget class="QLineEdit" name="commoditySearch"/>
</item> </item>
<item> <item>
<widget class="QTableView" name="commodityTable"/> <widget class="QTableView" name="commodityTable">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item> </item>
<item> <item>
<widget class="QWidget" name="widget_8" native="true"> <widget class="QWidget" name="widget_8" native="true">
@ -68,7 +75,7 @@
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QSpinBox" name="spinBox"> <widget class="QSpinBox" name="spnCount">
<property name="wrapping"> <property name="wrapping">
<bool>false</bool> <bool>false</bool>
</property> </property>
@ -78,13 +85,23 @@
<property name="showGroupSeparator" stdset="0"> <property name="showGroupSeparator" stdset="0">
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="minimum">
<number>1</number>
</property>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="pushButton"> <widget class="QPushButton" name="btnAddItem">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text"> <property name="text">
<string>Add Item</string> <string>Add Item</string>
</property> </property>
<property name="icon">
<iconset resource="../core/rc.qrc">
<normaloff>:/icons/new.svg</normaloff>:/icons/new.svg</iconset>
</property>
</widget> </widget>
</item> </item>
</layout> </layout>
@ -386,8 +403,8 @@
</layout> </layout>
</widget> </widget>
<resources> <resources>
<include location="shoprc.qrc"/>
<include location="../core/rc.qrc"/> <include location="../core/rc.qrc"/>
<include location="shoprc.qrc"/>
</resources> </resources>
<connections/> <connections/>
</ui> </ui>

@ -10,6 +10,7 @@ class SHOPSHARED_EXPORT ShopItem : public QObject, public IShopItem
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString code READ code)
Q_PROPERTY(QString name READ name) Q_PROPERTY(QString name READ name)
Q_PROPERTY(QDecDouble unitPrice READ unitPrice) Q_PROPERTY(QDecDouble unitPrice READ unitPrice)
Q_PROPERTY(Enums::VatType vatType READ vatType) Q_PROPERTY(Enums::VatType vatType READ vatType)
@ -24,10 +25,13 @@ public slots:
// IShopItem interface // IShopItem interface
public: public:
virtual int id() override { return 0; } virtual int id() override { return 0; }
virtual QString code() { return ""; }
virtual QString name() override { return ""; } virtual QString name() override { return ""; }
virtual QDecDouble unitPrice() override { return QDecDouble(); } virtual QDecDouble unitPrice() override { return QDecDouble(); }
virtual Enums::VatType vatType() override { return Enums::NONE; } virtual Enums::VatType vatType() override { return Enums::NONE; }
virtual QString pluginId() override { return ""; } virtual QString pluginId() override { return ""; }
}; };
typedef QSharedPointer<ShopItem> ShopItemPtr;
#endif // SHOPITEM_H #endif // SHOPITEM_H

@ -25,6 +25,8 @@ void ShopService::addShopItem(QSharedPointer<Voucher> voucher, QSharedPointer<IS
vItem->setVatType(item->vatType()); vItem->setVatType(item->vatType());
voucher->addItem(vItem); voucher->addItem(vItem);
updateRelatedItem(vItem.data(), count);
} }
void ShopService::calculate(QSharedPointer<Voucher> voucher) void ShopService::calculate(QSharedPointer<Voucher> voucher)
@ -106,6 +108,18 @@ void ShopService::pay(QSharedPointer<Voucher> voucher)
tx.commit(); tx.commit();
} }
void ShopService::updateRelatedItem(VoucherItem* item, int countAdded)
{
IPlugin *plugin = Context::instance().plugin(item->itemPlugin());
IService *srv = (plugin != NULL ? plugin->service<IService>() : NULL);
ISellableService *selSrv = dynamic_cast<ISellableService*>(srv);
if (selSrv != NULL)
{
selSrv->addedToVoucher(item->refId(), countAdded);
}
}
QList<QSharedPointer<Voucher> > ShopService::savedVouchers() QList<QSharedPointer<Voucher> > ShopService::savedVouchers()
{ {
return all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID))); return all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID)));

@ -19,6 +19,7 @@ public:
void calculateItem(QSharedPointer<VoucherItem> item); void calculateItem(QSharedPointer<VoucherItem> item);
void loadItems(QSharedPointer<Voucher> voucher); void loadItems(QSharedPointer<Voucher> voucher);
void pay(QSharedPointer<Voucher> voucher); void pay(QSharedPointer<Voucher> voucher);
void updateRelatedItem(VoucherItem* item, int countAdded);
QList<QSharedPointer<Voucher> > savedVouchers(); QList<QSharedPointer<Voucher> > savedVouchers();
QList<QSharedPointer<Voucher> > tempVouchers(); QList<QSharedPointer<Voucher> > tempVouchers();
QList<QSharedPointer<Voucher> > paiedVouchers(); QList<QSharedPointer<Voucher> > paiedVouchers();

Loading…
Cancel
Save