You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
3.1 KiB
C++

#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);
});
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;
}
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);
});
}