diff --git a/camp/camp.json b/camp/camp.json index cbbecf4..948c651 100644 --- a/camp/camp.json +++ b/camp/camp.json @@ -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" ], diff --git a/camp/campgrid.cpp b/camp/campgrid.cpp index 73a07a7..ced56f5 100644 --- a/camp/campgrid.cpp +++ b/camp/campgrid.cpp @@ -1,19 +1,51 @@ #include "campgrid.h" #include "campwizard.h" #include "campservice.h" +#include "campshopitem.h" + +#include +#include +#include CampGrid::CampGrid(QWidget *parent) : GridForm(parent) { setTableModel(new AutoTableModel); + + QHBoxLayout *tbLayout = qobject_cast(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); + }); } diff --git a/camp/campgrid.h b/camp/campgrid.h index ff36e96..ec33b29 100644 --- a/camp/campgrid.h +++ b/camp/campgrid.h @@ -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 diff --git a/camp/campseller.cpp b/camp/campseller.cpp index 17f9eb9..4f4011f 100644 --- a/camp/campseller.cpp +++ b/camp/campseller.cpp @@ -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); }); } diff --git a/camp/campservice.cpp b/camp/campservice.cpp index a751172..b7e4b3b 100644 --- a/camp/campservice.cpp +++ b/camp/campservice.cpp @@ -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 srv; + data->setPeople(srv.all(QString("campData = %1").arg(data->id()))); + + Service 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() diff --git a/camp/campservice.h b/camp/campservice.h index 4097836..86b83fb 100644 --- a/camp/campservice.h +++ b/camp/campservice.h @@ -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); diff --git a/camp/campshopitem.cpp b/camp/campshopitem.cpp index 8c2547d..3a53347 100644 --- a/camp/campshopitem.cpp +++ b/camp/campshopitem.cpp @@ -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; +} diff --git a/camp/campshopitem.h b/camp/campshopitem.h index 60a4767..82c9118 100644 --- a/camp/campshopitem.h +++ b/camp/campshopitem.h @@ -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; }; diff --git a/camp/campwizard.cpp b/camp/campwizard.cpp index 8bbf952..51ef15a 100644 --- a/camp/campwizard.cpp +++ b/camp/campwizard.cpp @@ -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(); diff --git a/camp/campwizard.h b/camp/campwizard.h index 4c5bcaf..41ff339 100644 --- a/camp/campwizard.h +++ b/camp/campwizard.h @@ -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 *m_servicesModel; AutoTableModel *m_itemsModel; bool m_bindAddrCombo; + bool m_newRecord; // QWizard interface public: bool validateCurrentPage(); - // QDialog interface public slots: void accept(); }; diff --git a/camp/campwizard.ui b/camp/campwizard.ui index 422df1d..e3f1c80 100644 --- a/camp/campwizard.ui +++ b/camp/campwizard.ui @@ -273,6 +273,12 @@ 0 + + + 300 + 0 + + true @@ -422,7 +428,14 @@ - + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + @@ -503,7 +516,14 @@ - + + + QAbstractItemView::SingleSelection + + + QAbstractItemView::SelectRows + + @@ -514,20 +534,68 @@ Sale - + - Apply sale + Change - + false + + + + + + + + 60 + 16777215 + + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + true + + + QAbstractSpinBox::NoButtons + + + 999.990000000000009 + + + + + + + TextLabel + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + diff --git a/camp/data/campdata.cpp b/camp/data/campdata.cpp index 1e5029a..61db83a 100644 --- a/camp/data/campdata.cpp +++ b/camp/data/campdata.cpp @@ -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; +} diff --git a/camp/data/campdata.h b/camp/data/campdata.h index 17b6536..7cb51cb 100644 --- a/camp/data/campdata.h +++ b/camp/data/campdata.h @@ -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