Added edit and pay functionality to camp module.

master
Josef Rokos 8 years ago
parent d163a92293
commit 8b99eea25a

@ -8,7 +8,7 @@
"default" : "",
"CZ" : ""
},
"schemaVersion" : 5,
"schemaVersion" : 6,
"sql" : [
"CREATE TABLE \"CampData\" (
\"id\" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
@ -90,6 +90,9 @@ ALTER TABLE ServiceItem ADD \"description\" TEXT NULL;
ALTER TABLE ServiceItem ADD \"fullPrice\" INTEGER NULL;
ALTER TABLE CampData ADD \"fullPrice\" INTEGER NULL;
ALTER TABLE CampData ADD \"totalSale\" INTEGER NULL;
",
"ALTER TABLE CampData ADD \"onVoucher\" INTEGER NULL;
"
],
"dependencies" : [ "ADDRESSBOOK", "SHOP", "SERVICES" ],

@ -1,19 +1,51 @@
#include "campgrid.h"
#include "campwizard.h"
#include "campservice.h"
#include "campshopitem.h"
#include <data/shop-data.h>
#include <shopservice.h>
#include <paydialog.h>
CampGrid::CampGrid(QWidget *parent) : GridForm<CampData>(parent)
{
setTableModel(new AutoTableModel<CampData>);
QHBoxLayout *tbLayout = qobject_cast<QHBoxLayout*>(this->toolbar()->layout());
if (tbLayout != NULL)
{
QToolButton *btnImport = new QToolButton(this->toolbar());
btnImport->setIcon(QIcon(":/icons/pay.svg"));
btnImport->setAutoRaise(true);
btnImport->setIconSize(QSize(24, 24));
btnImport->setToolTip(tr("Pay"));
tbLayout->insertWidget(tbLayout->count() - 1, btnImport);
connect(btnImport, &QToolButton::clicked, [this](){
CampDataPtr data = currentEntity();
if (!data.isNull())
{
addToVoucher(data);
}
});
}
}
void CampGrid::handleNewRecord()
{
if (!checkPermAdd())
{
return;
}
CampService srv;
CampDataPtr data = srv.create();
CampWizard *wizard = new CampWizard();
wizard->setAttribute(Qt::WA_DeleteOnClose);
wizard->setData(data);
wizard->setNewRecord(true);
connect(wizard, &QDialog::accepted, [this, data](){
addRow(data);
@ -24,5 +56,81 @@ void CampGrid::handleNewRecord()
void CampGrid::handleEditRecord()
{
if (!checkPermEdit())
{
return;
}
CampDataPtr data = currentEntity();
if (data.isNull())
{
return;
}
if (data->onVoucher())
{
QMessageBox::critical(this, tr("Can not edit"), tr("This record is asociated with voucher. Can not edit paid items"));
return;
}
CampService srv;
srv.loadItems(data);
CampWizard *wizard = new CampWizard();
wizard->setAttribute(Qt::WA_DeleteOnClose);
wizard->setData(data);
wizard->show();
}
void CampGrid::doDelete(CampDataPtr entity)
{
if (!checkPermDelete())
{
return;
}
if (entity->onVoucher())
{
QMessageBox::critical(this, tr("Can not delete"), tr("This record is asociated with voucher. Can not delete paid items"));
return;
}
CampService srv;
srv.eraseCamp(entity);
}
void CampGrid::addToVoucher(CampDataPtr data)
{
CampShopItemPtr campItem(new CampShopItem);
campItem->setId(data->id());
campItem->setUnitPrice(data->totalPrice());
ShopService shopSrv;
VoucherPtr voucher = shopSrv.createVoucher();
shopSrv.addShopItem(voucher, campItem, 1);
shopSrv.calculate(voucher);
shopSrv.saveVoucher(voucher);
data->setOnVoucher(true);
CampService srvCamp;
srvCamp.update(data);
PayDialog *payDlg = new PayDialog(voucher->totalPrice(), this);
payDlg->setAttribute(Qt::WA_DeleteOnClose);
payDlg->show();
connect(payDlg, &QDialog::accepted, [payDlg, voucher](){
payVoucherFromUI(voucher, payDlg);
});
connect(payDlg, &QDialog::rejected, [voucher, &shopSrv, data](){
CampService srvCamp;
data->setOnVoucher(false);
srvCamp.update(data);
voucher->clearItems();
shopSrv.eraseVoucher(voucher);
});
}

@ -14,6 +14,14 @@ public:
protected:
void handleNewRecord();
void handleEditRecord();
// GridForm interface
protected:
void doDelete(CampDataPtr entity);
private:
void addToVoucher(CampDataPtr data);
};
#endif // CAMPGRID_H

@ -18,11 +18,13 @@ void CampSeller::prepareItem()
CampService srv;
CampDataPtr data = srv.create();
wizard->setData(data);
wizard->setNewRecord(true);
wizard->show();
connect(wizard, &QDialog::accepted, [this, data](){
CampShopItemPtr item(new CampShopItem);
item->setUnitPrice(data->totalPrice());
item->setId(data->id());
emit itemPrepared(item, 1);
});
}

@ -120,6 +120,43 @@ void CampService::saveCamp(CampDataPtr data)
tr.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
emit dbErrorInsert(ex.what());
return;
}
}
void CampService::updateCamp(CampDataPtr data)
{
if (!checkPermission(PERM_EDIT))
{
return;
}
Transaction tr;
try
{
odb::database *db = Context::instance().db();
db->execute(QString("DELETE FROM ServiceItem WHERE campData = %1").arg(data->id()).toStdString());
db->execute(QString("DELETE FROM AddressItem WHERE campData = %1").arg(data->id()).toStdString());
foreach (ServiceItemPtr item, data->services()) {
item->setCampData(data.toWeakRef());
db->persist(item);
}
foreach (AddressItemPtr item, data->people()) {
item->setCampData(data.toWeakRef());
db->persist(item);
}
db->update(data);
tr.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
emit dbErrorUpdate(ex.what());
@ -127,6 +164,42 @@ void CampService::saveCamp(CampDataPtr data)
}
}
void CampService::eraseCamp(CampDataPtr data)
{
if (!checkPermission(PERM_DELETE))
{
return;
}
Transaction tr;
try
{
odb::database *db = Context::instance().db();
db->execute(QString("DELETE FROM ServiceItem WHERE campData = %1").arg(data->id()).toStdString());
db->execute(QString("DELETE FROM AddressItem WHERE campData = %1").arg(data->id()).toStdString());
db->erase(data);
tr.commit();
}
catch (const odb::exception &ex)
{
emit dbError(ex.what());
emit dbErrorDelete(ex.what());
return;
}
}
void CampService::loadItems(CampDataPtr data)
{
Service<AddressItem> srv;
data->setPeople(srv.all(QString("campData = %1").arg(data->id())));
Service<ServiceItem> srvService;
data->setServices(srvService.all(QString("campData = %1").arg(data->id())));
}
void CampService::calcPeople(CampDataPtr data)
{
foreach (ServiceItemPtr service, data->services()) {
@ -291,9 +364,22 @@ ShopItemPtr CampService::shopItem(int )
return CampShopItemPtr(new CampShopItem);
}
void CampService::addedToVoucher(int , int )
void CampService::addedToVoucher(int itemId, int countAdded)
{
Transaction tx;
CampDataPtr data = loadById(itemId);
if (countAdded > 0)
{
data->setOnVoucher(true);
}
else
{
data->setOnVoucher(false);
}
this->update(data);
tx.commit();
}
ISeller *CampService::seller()

@ -21,6 +21,9 @@ public:
CampDataPtr create();
void calculate(CampDataPtr data);
void saveCamp(CampDataPtr data);
void updateCamp(CampDataPtr data);
void eraseCamp(CampDataPtr data);
void loadItems(CampDataPtr data);
private:
ServiceItemPtr addServiceInt(CampDataPtr data, AccServicePtr service);

@ -46,3 +46,13 @@ void CampShopItem::setVatType(const Enums::VatType &vatType)
{
m_vatType = vatType;
}
int CampShopItem::id()
{
return m_id;
}
void CampShopItem::setId(int id)
{
m_id = id;
}

@ -10,6 +10,7 @@ public:
// IShopItem interface
public:
int id();
QString name();
QString shortName();
QDecDouble unitPrice();
@ -21,10 +22,11 @@ public:
QString code();
void setUnitPrice(const QDecDouble &unitPrice);
void setVatType(const Enums::VatType &vatType);
void setId(int id);
private:
int m_id;
QDecDouble m_unitPrice;
Enums::VatType m_vatType;
};

@ -195,6 +195,8 @@ CampWizard::CampWizard(QWidget *parent) :
ui->tabServices->hideColumn(6);
ui->tabServices->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
ui->tabServices->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Stretch);
m_newRecord = false;
}
CampWizard::~CampWizard()
@ -212,6 +214,9 @@ void CampWizard::setData(const CampDataPtr &data)
m_peopleModel->setData(data->people());
m_itemsModel->setData(data->services());
ui->sale_2->setValue(m_data->sale().toDouble());
ui->lblSale->setText(m_data->fixedSale() ? tr("Fixed amound") : tr("%"));
}
void CampWizard::on_btnAdd_clicked()
@ -357,6 +362,22 @@ void CampWizard::on_sale_currentIndexChanged(int)
applySale();
}
void CampWizard::setNewRecord(bool newRecord)
{
m_newRecord = newRecord;
if (newRecord)
{
ui->wSale->setVisible(false);
ui->checkSale->setText(tr("Apply sale"));
}
else
{
ui->wSale->setVisible(true);
ui->checkSale->setText(tr("Change"));
}
}
bool CampWizard::validateCurrentPage()
{
m_dataBinder->bindToData();
@ -385,7 +406,6 @@ bool CampWizard::validateCurrentPage()
void CampWizard::accept()
{
CampService srv;
srv.saveCamp(m_data);
bool success = true;
connect(&srv, &IService::dbError, [this, &success](QString msg){
@ -394,10 +414,19 @@ void CampWizard::accept()
});
connect(&srv, &IService::permissionDenied, [this, &success](QString msg){
QMessageBox::critical(this, "Permission denied", msg.toStdString().c_str());
QMessageBox::critical(this, tr("Permission denied"), msg.toStdString().c_str());
success = false;
});
if (m_newRecord)
{
srv.saveCamp(m_data);
}
else
{
srv.updateCamp(m_data);
}
if (success)
{
QDialog::accept();

@ -64,6 +64,7 @@ public:
~CampWizard();
void setData(const CampDataPtr &data);
void setNewRecord(bool newRecord);
private slots:
void on_btnAdd_clicked();
@ -102,12 +103,12 @@ private:
AutoTableModel<AccService> *m_servicesModel;
AutoTableModel<ServiceItem> *m_itemsModel;
bool m_bindAddrCombo;
bool m_newRecord;
// QWizard interface
public:
bool validateCurrentPage();
// QDialog interface
public slots:
void accept();
};

@ -273,6 +273,12 @@
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>300</width>
<height>0</height>
</size>
</property>
<property name="editable">
<bool>true</bool>
</property>
@ -422,7 +428,14 @@
</property>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QTableView" name="tableServices"/>
<widget class="QTableView" name="tableServices">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
<item>
<widget class="QWidget" name="widget_2" native="true">
@ -503,7 +516,14 @@
</widget>
</item>
<item>
<widget class="QTableView" name="tableItems"/>
<widget class="QTableView" name="tableItems">
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
</widget>
</item>
</layout>
</widget>
@ -514,20 +534,68 @@
<string>Sale</string>
</property>
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<item row="1" column="0">
<widget class="QCheckBox" name="checkSale">
<property name="text">
<string>Apply sale</string>
<string>Change</string>
</property>
</widget>
</item>
<item row="1" column="0">
<item row="2" column="0">
<widget class="QComboBox" name="sale">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="0">
<widget class="QWidget" name="wSale" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_10">
<item>
<widget class="QDoubleSpinBox" name="sale_2">
<property name="maximumSize">
<size>
<width>60</width>
<height>16777215</height>
</size>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
<property name="readOnly">
<bool>true</bool>
</property>
<property name="buttonSymbols">
<enum>QAbstractSpinBox::NoButtons</enum>
</property>
<property name="maximum">
<double>999.990000000000009</double>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="lblSale">
<property name="text">
<string>TextLabel</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_8">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</item>

@ -9,6 +9,7 @@ CampData::CampData(QObject *parent) : QObject(parent)
m_totalSale = 0;
m_fixedSale = false;
m_fullPrice = 0;
m_onVoucher = false;
}
int CampData::id() const
@ -180,3 +181,13 @@ void CampData::setFullPrice(QDecDouble fullPrice)
{
m_fullPrice = FROM_DEC(fullPrice);
}
bool CampData::onVoucher() const
{
return m_onVoucher;
}
void CampData::setOnVoucher(bool onVoucher)
{
m_onVoucher = onVoucher;
}

@ -24,6 +24,7 @@ class CampData : public QObject
Q_PROPERTY(QDecDouble sale READ sale WRITE setSale)
Q_PROPERTY(bool fixedSale READ fixedSale WRITE setFixedSale)
Q_PROPERTY(QDecDouble totalSale READ totalSale WRITE setTotalSale)
Q_PROPERTY(bool onVoucher READ onVoucher WRITE setOnVoucher)
public:
explicit CampData(QObject *parent = 0);
@ -77,6 +78,9 @@ public:
QDecDouble fullPrice() const;
void setFullPrice(QDecDouble fullPrice);
bool onVoucher() const;
void setOnVoucher(bool onVoucher);
private:
friend class odb::access;
#pragma db id auto
@ -97,6 +101,7 @@ private:
int m_totalSale;
bool m_fixedSale;
SeasonPtr m_season;
bool m_onVoucher;
};
#endif // CAMPDATA_H

Loading…
Cancel
Save