Added support for saving receipts.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -121,6 +121,7 @@ private:
|
||||
int m_totalPriceVatFirstLower;
|
||||
int m_totalPriceVatSecondLower;
|
||||
int m_totalPrice;
|
||||
#pragma db value_not_null inverse(m_voucher)
|
||||
QOdbList<QSharedPointer<VoucherItem> > m_items;
|
||||
VoucherStatus m_status;
|
||||
};
|
||||
|
||||
@@ -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<Voucher> VoucherItem::voucher() const
|
||||
{
|
||||
return m_voucher;
|
||||
}
|
||||
|
||||
void VoucherItem::setVoucher(const QWeakPointer<Voucher> &voucher)
|
||||
{
|
||||
m_voucher = voucher;
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,9 +5,12 @@
|
||||
#include <QString>
|
||||
#include <QDecDouble.hh>
|
||||
#include <odb/core.hxx>
|
||||
#include <QSharedPointer>
|
||||
|
||||
#include <enums.h>
|
||||
|
||||
class Voucher;
|
||||
|
||||
#pragma db object
|
||||
class VoucherItem : public QObject
|
||||
{
|
||||
@@ -56,6 +59,9 @@ public:
|
||||
|
||||
QDecDouble vatAmount() const;
|
||||
|
||||
QWeakPointer<Voucher> voucher() const;
|
||||
void setVoucher(const QWeakPointer<Voucher> &voucher);
|
||||
|
||||
signals:
|
||||
void countChanged();
|
||||
|
||||
@@ -72,6 +78,8 @@ private:
|
||||
int m_refId;
|
||||
QString m_itemPlugin;
|
||||
Enums::VatType m_vatType;
|
||||
#pragma db not_null
|
||||
QWeakPointer<Voucher> m_voucher;
|
||||
};
|
||||
|
||||
#endif // VOUCHERITEM_H
|
||||
|
||||
@@ -18,3 +18,10 @@ QIcon Shop::pluginIcon()
|
||||
{
|
||||
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
|
||||
public:
|
||||
virtual QIcon pluginIcon();
|
||||
|
||||
// IPlugin interface
|
||||
public:
|
||||
virtual QWidget *ui() override;
|
||||
};
|
||||
|
||||
#endif // SHOP_H
|
||||
|
||||
+6
-20
@@ -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" ]
|
||||
|
||||
+34
-6
@@ -5,6 +5,9 @@
|
||||
#include "receiptsaveform.h"
|
||||
#include "receiptloadform.h"
|
||||
#include "shopservice.h"
|
||||
#include <QList>
|
||||
#include <QSharedPointer>
|
||||
#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<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()
|
||||
{
|
||||
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<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:
|
||||
explicit ShopForm(QWidget *parent = 0);
|
||||
~ShopForm();
|
||||
void loadLast();
|
||||
|
||||
private slots:
|
||||
void on_directSale_clicked();
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "shopservice.h"
|
||||
#include "shop-odb.hxx"
|
||||
|
||||
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)
|
||||
{
|
||||
return price * ((vatRate(vatType) / 100) + QDecDouble(1));
|
||||
@@ -116,3 +123,35 @@ QDecDouble ShopService::vatRate(Enums::VatType vatType)
|
||||
|
||||
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 calculate(QSharedPointer<Voucher> voucher);
|
||||
void calculateItem(QSharedPointer<VoucherItem> item);
|
||||
void loadItems(QSharedPointer<Voucher> voucher);
|
||||
|
||||
private:
|
||||
QDecDouble includeVat(QDecDouble price, Enums::VatType vatType);
|
||||
@@ -24,6 +25,11 @@ private:
|
||||
|
||||
QSharedPointer<GlobalSettings> m_gs;
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user