#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 != nullptr) { 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); } }); } m_detail = new DetailWidget(this); mainLayout()->addWidget(m_detail); } 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); }); wizard->show(); } 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; } if (data->people().isEmpty() || data->services().isEmpty()) { CampService srv; srv.load(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.erase(entity); } void CampGrid::addToVoucher(const CampDataPtr& data) { if (data->onVoucher()) { QMessageBox::information(this, tr("Can not pay"), tr("This record is already paid")); return; } SettingsService settSrv("CAMP"); CampSettingsPtr settings = settSrv.loadSettings(); CampShopItemPtr campItem(new CampShopItem); campItem->setId(data->id()); campItem->setUnitPrice(data->totalPrice()); campItem->setVatType(settings->vatType()); ShopService shopSrv; VoucherPtr voucher = shopSrv.createVoucher(); shopSrv.addShopItem(voucher, campItem, 1); shopSrv.calculate(voucher); shopSrv.save(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.erase(voucher); }); } void CampGrid::currentIndexChanged(const QModelIndex ¤t) { if (current.isValid()) { auto current = currentEntity(); m_detail->setData(current); } } QList CampGrid::listForGrid() { CampService srv; return srv.allForSeason(); }