Added support for saving receipts.

print
Josef Rokos 9 years ago
parent 9707ec7c66
commit facaf98cf0

@ -144,6 +144,8 @@ OTHER_FILES += \
users/metaData.json \ users/metaData.json \
roles/metaData.json roles/metaData.json
CONFIG(debug, release|debug):DEFINES += _DEBUG
win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber 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:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber
else:unix: LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber else:unix: LIBS += -L$$OUT_PWD/../qdecimal/lib/ -lqdecimal -ldecnumber

@ -67,7 +67,7 @@ public:
return ret; return ret;
} }
void save(QSharedPointer<T> entity) { virtual void save(QSharedPointer<T> entity) {
if (!checkPermission(PERM_ADD)) { if (!checkPermission(PERM_ADD)) {
return; return;
} }
@ -93,7 +93,7 @@ public:
emit dataChanged(); emit dataChanged();
} }
void update(QSharedPointer<T> entity) { virtual void update(QSharedPointer<T> entity) {
if (!checkPermission(PERM_EDIT)) { if (!checkPermission(PERM_EDIT)) {
return; return;
} }

@ -9,6 +9,9 @@ Transaction::Transaction()
if (!Transaction::m_inTransaction) if (!Transaction::m_inTransaction)
{ {
m_tr = new odb::transaction(Context::instance().db()->begin()); m_tr = new odb::transaction(Context::instance().db()->begin());
#ifdef _DEBUG
m_tr->tracer(odb::stderr_tracer);
#endif
Transaction::m_inTransaction = true; Transaction::m_inTransaction = true;
} }
else else

@ -3,6 +3,7 @@
Voucher::Voucher(QObject *parent) : QObject(parent) Voucher::Voucher(QObject *parent) : QObject(parent)
{ {
m_id = 0;
m_vatRateHigh = 0; m_vatRateHigh = 0;
m_vatRateFirstLower = 0; m_vatRateFirstLower = 0;
m_vatRateSecondLower = 0; m_vatRateSecondLower = 0;
@ -196,17 +197,17 @@ void Voucher::setTotalPriceVatSecondLower(QDecDouble totalPriceVatSecondLower)
QDecDouble Voucher::vatAmountHigh() QDecDouble Voucher::vatAmountHigh()
{ {
return m_totalPriceVatHigh - m_priceVatHigh; return TO_DEC(m_totalPriceVatHigh) - TO_DEC(m_priceVatHigh);
} }
QDecDouble Voucher::vatAmountFirstLower() QDecDouble Voucher::vatAmountFirstLower()
{ {
return m_totalPriceVatFirstLower - m_priceVatFirstLower; return TO_DEC(m_totalPriceVatFirstLower) - TO_DEC(m_priceVatFirstLower);
} }
QDecDouble Voucher::VatAmountSecondLower() QDecDouble Voucher::VatAmountSecondLower()
{ {
return m_totalPriceVatSecondLower - m_priceVatSecondLower; return TO_DEC(m_totalPriceVatSecondLower) - TO_DEC(m_priceVatSecondLower);
} }
int Voucher::id() const int Voucher::id() const

@ -121,6 +121,7 @@ private:
int m_totalPriceVatFirstLower; int m_totalPriceVatFirstLower;
int m_totalPriceVatSecondLower; int m_totalPriceVatSecondLower;
int m_totalPrice; int m_totalPrice;
#pragma db value_not_null inverse(m_voucher)
QOdbList<QSharedPointer<VoucherItem> > m_items; QOdbList<QSharedPointer<VoucherItem> > m_items;
VoucherStatus m_status; VoucherStatus m_status;
}; };

@ -113,7 +113,17 @@ void VoucherItem::setPriceWitouthVat(QDecDouble priceWitouthVat)
QDecDouble VoucherItem::vatAmount() const QDecDouble VoucherItem::vatAmount() const
{ {
return m_price - m_priceWitouthVat; return TO_DEC(m_price) - TO_DEC(m_priceWitouthVat);
}
QWeakPointer<Voucher> VoucherItem::voucher() const
{
return m_voucher;
}
void VoucherItem::setVoucher(const QWeakPointer<Voucher> &voucher)
{
m_voucher = voucher;
} }

@ -5,9 +5,12 @@
#include <QString> #include <QString>
#include <QDecDouble.hh> #include <QDecDouble.hh>
#include <odb/core.hxx> #include <odb/core.hxx>
#include <QSharedPointer>
#include <enums.h> #include <enums.h>
class Voucher;
#pragma db object #pragma db object
class VoucherItem : public QObject class VoucherItem : public QObject
{ {
@ -56,6 +59,9 @@ public:
QDecDouble vatAmount() const; QDecDouble vatAmount() const;
QWeakPointer<Voucher> voucher() const;
void setVoucher(const QWeakPointer<Voucher> &voucher);
signals: signals:
void countChanged(); void countChanged();
@ -72,6 +78,8 @@ private:
int m_refId; int m_refId;
QString m_itemPlugin; QString m_itemPlugin;
Enums::VatType m_vatType; Enums::VatType m_vatType;
#pragma db not_null
QWeakPointer<Voucher> m_voucher;
}; };
#endif // VOUCHERITEM_H #endif // VOUCHERITEM_H

@ -18,3 +18,10 @@ QIcon Shop::pluginIcon()
{ {
return QIcon(":/icons/shop.svg"); return QIcon(":/icons/shop.svg");
} }
QWidget *Shop::ui()
{
QWidget *uiWidget = IPlugin::ui();
qobject_cast<ShopForm*>(uiWidget)->loadLast();
return uiWidget;
}

@ -22,6 +22,10 @@ protected:
// IPlugin interface // IPlugin interface
public: public:
virtual QIcon pluginIcon(); virtual QIcon pluginIcon();
// IPlugin interface
public:
virtual QWidget *ui() override;
}; };
#endif // SHOP_H #endif // SHOP_H

@ -20,7 +20,12 @@
\"price\" INTEGER NOT NULL, \"price\" INTEGER NOT NULL,
\"refId\" INTEGER NOT NULL, \"refId\" INTEGER NOT NULL,
\"itemPlugin\" TEXT 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\" ( CREATE TABLE \"Voucher\" (
\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@ -44,25 +49,6 @@ CREATE TABLE \"Voucher\" (
FOREIGN KEY (\"contact\") FOREIGN KEY (\"contact\")
REFERENCES \"AddressbookData\" (\"id\") REFERENCES \"AddressbookData\" (\"id\")
DEFERRABLE INITIALLY DEFERRED); 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" ] "dependencies" : [ "ADDRESSBOOK" ]

@ -5,6 +5,9 @@
#include "receiptsaveform.h" #include "receiptsaveform.h"
#include "receiptloadform.h" #include "receiptloadform.h"
#include "shopservice.h" #include "shopservice.h"
#include <QList>
#include <QSharedPointer>
#include "shop-odb.hxx"
ShopForm::ShopForm(QWidget *parent) : ShopForm::ShopForm(QWidget *parent) :
QWidget(parent), QWidget(parent),
@ -18,6 +21,28 @@ ShopForm::~ShopForm()
delete ui; delete ui;
} }
void ShopForm::loadLast()
{
m_itemsModel = new AutoTableModel<VoucherItem>(this);
m_itemsModel->setEditableCols(QList<int>() << 1);
ui->actualReceipt->setModel(m_itemsModel);
ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ShopService srv;
QList<QSharedPointer<Voucher> > 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<VoucherItem> 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() void ShopForm::on_directSale_clicked()
{ {
if (m_voucher.isNull()) if (m_voucher.isNull())
@ -73,17 +98,20 @@ void ShopForm::onCountChanged()
ShopService srv; ShopService srv;
srv.calculate(m_voucher); 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() void ShopForm::createVoucher()
{ {
ShopService srv; ShopService srv;
m_voucher = srv.createVoucher(); m_voucher = srv.createVoucher();
m_itemsModel = new AutoTableModel<VoucherItem>(this);
m_itemsModel->setEditableCols(QList<int>() << 1);
ui->actualReceipt->setModel(m_itemsModel);
ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
} }

@ -17,6 +17,7 @@ class ShopForm : public QWidget
public: public:
explicit ShopForm(QWidget *parent = 0); explicit ShopForm(QWidget *parent = 0);
~ShopForm(); ~ShopForm();
void loadLast();
private slots: private slots:
void on_directSale_clicked(); void on_directSale_clicked();

@ -1,4 +1,5 @@
#include "shopservice.h" #include "shopservice.h"
#include "shop-odb.hxx"
ShopService::ShopService() ShopService::ShopService()
{ {
@ -78,6 +79,12 @@ void ShopService::calculateItem(QSharedPointer<VoucherItem> item)
} }
} }
void ShopService::loadItems(QSharedPointer<Voucher> voucher)
{
Service<VoucherItem> srv;
voucher->setItems(srv.all(QString("voucher = %1").arg(voucher->id())));
}
QDecDouble ShopService::includeVat(QDecDouble price, Enums::VatType vatType) QDecDouble ShopService::includeVat(QDecDouble price, Enums::VatType vatType)
{ {
return price * ((vatRate(vatType) / 100) + QDecDouble(1)); return price * ((vatRate(vatType) / 100) + QDecDouble(1));
@ -116,3 +123,35 @@ QDecDouble ShopService::vatRate(Enums::VatType vatType)
return vatRate; return vatRate;
} }
void ShopService::save(QSharedPointer<Voucher> entity)
{
Transaction tr;
odb::database *db = Context::instance().db();
db->persist(entity);
foreach (QSharedPointer<VoucherItem> item, entity->items()) {
item->setVoucher(entity.toWeakRef());
db->persist(item);
}
tr.commit();
}
void ShopService::update(QSharedPointer<Voucher> entity)
{
Transaction tr;
odb::database *db = Context::instance().db();
db->execute(QString("DELETE FROM VoucherItem WHERE voucher = %1").arg(entity->id()).toStdString());
foreach (QSharedPointer<VoucherItem> item, entity->items()) {
item->setVoucher(entity.toWeakRef());
db->persist(item);
}
db->update(entity);
tr.commit();
}

@ -17,6 +17,7 @@ public:
void addShopItem(QSharedPointer<Voucher> voucher, QSharedPointer<IShopItem> item, int count); void addShopItem(QSharedPointer<Voucher> voucher, QSharedPointer<IShopItem> item, int count);
void calculate(QSharedPointer<Voucher> voucher); void calculate(QSharedPointer<Voucher> voucher);
void calculateItem(QSharedPointer<VoucherItem> item); void calculateItem(QSharedPointer<VoucherItem> item);
void loadItems(QSharedPointer<Voucher> voucher);
private: private:
QDecDouble includeVat(QDecDouble price, Enums::VatType vatType); QDecDouble includeVat(QDecDouble price, Enums::VatType vatType);
@ -24,6 +25,11 @@ private:
QSharedPointer<GlobalSettings> m_gs; QSharedPointer<GlobalSettings> m_gs;
QDecDouble vatRate(Enums::VatType vatType); QDecDouble vatRate(Enums::VatType vatType);
// Service interface
public:
virtual void save(QSharedPointer<Voucher> entity) override;
virtual void update(QSharedPointer<Voucher> entity) override;
}; };
#endif // SHOPSERVICE_H #endif // SHOPSERVICE_H

Loading…
Cancel
Save