From 5eb73fb877448c7a8966dfcfd21729a16233d9cd Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Mon, 30 May 2016 22:12:23 +0200 Subject: [PATCH] Implemented temporary save. --- shop/shop.cpp | 1 + shop/shopform.cpp | 114 +++++++++++++++++++++++++++--- shop/shopform.h | 6 ++ shop/shopform.ui | 2 +- shop/temporaryreceiptsaveform.cpp | 11 ++- shop/temporaryreceiptsaveform.h | 12 +++- shop/temporaryreceiptsaveform.ui | 2 +- 7 files changed, 135 insertions(+), 13 deletions(-) diff --git a/shop/shop.cpp b/shop/shop.cpp index e75f969..448c811 100644 --- a/shop/shop.cpp +++ b/shop/shop.cpp @@ -23,6 +23,7 @@ QWidget *Shop::ui() { QWidget *uiWidget = IPlugin::ui(); qobject_cast(uiWidget)->loadLast(); + qobject_cast(uiWidget)->fillRaceiptCombo(); return uiWidget; } diff --git a/shop/shopform.cpp b/shop/shopform.cpp index e2648b9..cb7c69d 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -14,6 +14,7 @@ ShopForm::ShopForm(QWidget *parent) : ui(new Ui::ShopForm) { ui->setupUi(this); + m_itemsModel = NULL; } ShopForm::~ShopForm() @@ -23,10 +24,14 @@ ShopForm::~ShopForm() void ShopForm::loadLast() { - m_itemsModel = new AutoTableModel(this); - m_itemsModel->setEditableCols(QList() << 1); - ui->actualReceipt->setModel(m_itemsModel); - ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + if (m_itemsModel == NULL) + { + m_itemsModel = new AutoTableModel(this); + m_itemsModel->setEditableCols(QList() << 1); + m_itemsModel->setTranslations(Context::instance().plugin("SHOP")->translations()); + ui->actualReceipt->setModel(m_itemsModel); + ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch); + } ShopService srv; QList > receipt = srv.all(QString("status = %1").arg(QString::number(Voucher::NEW))); @@ -35,14 +40,29 @@ void ShopForm::loadLast() m_voucher = receipt[0]; srv.loadItems(m_voucher); m_itemsModel->setData(m_voucher->items()); + connectItemSignals(); - foreach (QSharedPointer 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::fillRaceiptCombo() +{ + bool oldState = ui->receiptCombo->blockSignals(true); + + ShopService srv; + QList > receipts = srv.all(QString("status = %1").arg(QString::number(Voucher::TEMPORARY))); + + ui->receiptCombo->clear(); + ui->receiptCombo->addItem(tr("<< empty >>")); + + foreach (QSharedPointer voucher, receipts) { + ui->receiptCombo->addItem(voucher->name(), voucher->id()); + } + + ui->receiptCombo->blockSignals(oldState); +} + void ShopForm::on_directSale_clicked() { if (m_voucher.isNull()) @@ -51,6 +71,7 @@ void ShopForm::on_directSale_clicked() } DirectSaleForm *form = new DirectSaleForm(this); + form->setAttribute(Qt::WA_DeleteOnClose); connect(form, &QDialog::accepted, [this, form](){ ShopService srv; @@ -65,8 +86,7 @@ void ShopForm::on_directSale_clicked() void ShopForm::on_temporarySaveButton_clicked() { - TemporaryReceiptSaveForm *form = new TemporaryReceiptSaveForm(this); - form->show(); + doTempSave(false); } void ShopForm::on_saveButton_clicked() @@ -115,3 +135,79 @@ void ShopForm::createVoucher() ShopService srv; m_voucher = srv.createVoucher(); } + +void ShopForm::doTempSave(bool comboChanged) +{ + TemporaryReceiptSaveForm *form = new TemporaryReceiptSaveForm(m_voucher, this); + form->setAttribute(Qt::WA_DeleteOnClose); + + connect(form, &QDialog::accepted, [this, form, comboChanged](){ + ShopService srv; + + if (!m_voucher->items().isEmpty()) + { + m_voucher->setStatus(Voucher::TEMPORARY); + srv.update(m_voucher); + } + + if (comboChanged && ui->receiptCombo->currentIndex() > 0) + { + changeReceipt(); + } + else + { + m_voucher = srv.createVoucher(); + ui->total->setText("0"); + } + + fillRaceiptCombo(); + + m_itemsModel->setData(m_voucher->items()); + }); + + form->show(); +} + +void ShopForm::changeReceipt() +{ + ShopService srv; + + m_voucher = srv.loadById(ui->receiptCombo->currentData().toInt()); + srv.loadItems(m_voucher); + connectItemSignals(); + m_voucher->setStatus(Voucher::NEW); + srv.update(m_voucher); + + m_itemsModel->setData(m_voucher->items()); + ui->total->setText(m_voucher->totalPrice().toString()); + + fillRaceiptCombo(); +} + +void ShopForm::connectItemSignals() +{ + foreach (QSharedPointer item, m_voucher->items()) { + connect(item.data(), SIGNAL(countChanged()), this, SLOT(onCountChanged())); + } +} + +void ShopForm::on_receiptCombo_currentIndexChanged(int) +{ + if (!m_voucher.isNull() && m_voucher->items().isEmpty()) + { + ShopService srv; + srv.erase(m_voucher); + changeReceipt(); + + return; + } + + if (m_voucher.isNull()) + { + changeReceipt(); + } + else + { + doTempSave(true); + } +} diff --git a/shop/shopform.h b/shop/shopform.h index d0aba32..df37aa8 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -18,6 +18,7 @@ public: explicit ShopForm(QWidget *parent = 0); ~ShopForm(); void loadLast(); + void fillRaceiptCombo(); private slots: void on_directSale_clicked(); @@ -30,12 +31,17 @@ private slots: void onCountChanged(); + void on_receiptCombo_currentIndexChanged(int index); + private: Ui::ShopForm *ui; QSharedPointer m_voucher; AutoTableModel *m_itemsModel; void createVoucher(); + void doTempSave(bool comboChanged); + void changeReceipt(); + void connectItemSignals(); }; #endif // SHOPFORM_H diff --git a/shop/shopform.ui b/shop/shopform.ui index c6010b3..40a0a21 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -206,7 +206,7 @@ - + diff --git a/shop/temporaryreceiptsaveform.cpp b/shop/temporaryreceiptsaveform.cpp index 73820ce..916dbe7 100644 --- a/shop/temporaryreceiptsaveform.cpp +++ b/shop/temporaryreceiptsaveform.cpp @@ -1,14 +1,23 @@ #include "temporaryreceiptsaveform.h" #include "ui_temporaryreceiptsaveform.h" -TemporaryReceiptSaveForm::TemporaryReceiptSaveForm(QWidget *parent) : +TemporaryReceiptSaveForm::TemporaryReceiptSaveForm(QSharedPointer voucher, QWidget *parent) : QDialog(parent), ui(new Ui::TemporaryReceiptSaveForm) { ui->setupUi(this); + m_voucher = voucher; + m_binder.registerBinding(ui->name); + m_binder.setData(m_voucher.data()); } TemporaryReceiptSaveForm::~TemporaryReceiptSaveForm() { delete ui; } + +void TemporaryReceiptSaveForm::accept() +{ + m_binder.bindToData(); + QDialog::accept(); +} diff --git a/shop/temporaryreceiptsaveform.h b/shop/temporaryreceiptsaveform.h index 072e265..fd3f70b 100644 --- a/shop/temporaryreceiptsaveform.h +++ b/shop/temporaryreceiptsaveform.h @@ -2,6 +2,10 @@ #define TEMPORARYRECEIPTSAVEFORM_H #include +#include +#include + +#include "data/voucher.h" namespace Ui { class TemporaryReceiptSaveForm; @@ -12,11 +16,17 @@ class TemporaryReceiptSaveForm : public QDialog Q_OBJECT public: - explicit TemporaryReceiptSaveForm(QWidget *parent = 0); + explicit TemporaryReceiptSaveForm(QSharedPointer voucher, QWidget *parent = 0); ~TemporaryReceiptSaveForm(); private: Ui::TemporaryReceiptSaveForm *ui; + QSharedPointer m_voucher; + ObjectBinder m_binder; + + // QDialog interface +public slots: + virtual void accept() override; }; #endif // TEMPORARYRECEIPTSAVEFORM_H diff --git a/shop/temporaryreceiptsaveform.ui b/shop/temporaryreceiptsaveform.ui index c36555f..358e386 100644 --- a/shop/temporaryreceiptsaveform.ui +++ b/shop/temporaryreceiptsaveform.ui @@ -22,7 +22,7 @@ - +