diff --git a/shop/data/voucher.h b/shop/data/voucher.h index c5c3782..6d18076 100644 --- a/shop/data/voucher.h +++ b/shop/data/voucher.h @@ -126,4 +126,6 @@ private: VoucherStatus m_status; }; +typedef QSharedPointer VoucherPtr; + #endif // VOUCHER_H diff --git a/shop/data/voucheritem.h b/shop/data/voucheritem.h index 2b412f9..02ee8de 100644 --- a/shop/data/voucheritem.h +++ b/shop/data/voucheritem.h @@ -82,4 +82,6 @@ private: QWeakPointer m_voucher; }; +typedef QSharedPointer VoucherItemPtr; + #endif // VOUCHERITEM_H diff --git a/shop/receiptloadform.cpp b/shop/receiptloadform.cpp index c14a808..9622c27 100644 --- a/shop/receiptloadform.cpp +++ b/shop/receiptloadform.cpp @@ -1,14 +1,58 @@ +#include + #include "receiptloadform.h" #include "ui_receiptloadform.h" +#include "shopservice.h" +#include "shop-odb.hxx" ReceiptLoadForm::ReceiptLoadForm(QWidget *parent) : QDialog(parent), ui(new Ui::ReceiptLoadForm) { ui->setupUi(this); + + m_voucherModel = new AutoTableModel(this); + ShopService srv; + m_voucherModel->setData(srv.all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID)))); + m_voucherModel->setTranslations(Context::instance().plugin("SHOP")->translations()); + ui->tabVouchers->setModel(m_voucherModel); + ui->tabVouchers->hideColumn(3); + ui->tabVouchers->hideColumn(4); + ui->tabVouchers->hideColumn(5); + ui->tabVouchers->hideColumn(6); + ui->tabVouchers->hideColumn(7); + ui->tabVouchers->hideColumn(8); + ui->tabVouchers->hideColumn(10); + ui->tabVouchers->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); + ui->tabVouchers->setColumnWidth(0, 190); + ui->tabVouchers->setColumnWidth(2, 200); + + m_itemModel = new AutoTableModel(this); + ui->tabItems->setModel(m_itemModel); + + connect(ui->tabVouchers->selectionModel(), &QItemSelectionModel::currentRowChanged, [this](const QModelIndex ¤t, QModelIndex){ + ShopService srv; + VoucherPtr voucher = m_voucherModel->itemFromIndex(current); + srv.loadItems(voucher); + m_itemModel->setData(voucher->items()); + }); } ReceiptLoadForm::~ReceiptLoadForm() { delete ui; } + +void ReceiptLoadForm::on_lineEdit_textChanged(const QString &text) +{ + QSortFilterProxyModel proxy; + proxy.setSourceModel(m_voucherModel); + proxy.setFilterKeyColumn(0); + proxy.setFilterFixedString(text); + + QModelIndex matchingIndex = proxy.mapToSource(proxy.index(0,0)); + if(matchingIndex.isValid()){ + ui->tabVouchers->scrollTo(matchingIndex,QAbstractItemView::EnsureVisible); + ui->tabVouchers->setCurrentIndex(matchingIndex); + } +} diff --git a/shop/receiptloadform.h b/shop/receiptloadform.h index ee2b703..ec022d3 100644 --- a/shop/receiptloadform.h +++ b/shop/receiptloadform.h @@ -2,6 +2,8 @@ #define RECEIPTLOADFORM_H #include +#include "data/voucher.h" +#include namespace Ui { class ReceiptLoadForm; @@ -15,8 +17,13 @@ public: explicit ReceiptLoadForm(QWidget *parent = 0); ~ReceiptLoadForm(); +private slots: + void on_lineEdit_textChanged(const QString &text); + private: Ui::ReceiptLoadForm *ui; + AutoTableModel *m_voucherModel; + AutoTableModel *m_itemModel; }; #endif // RECEIPTLOADFORM_H diff --git a/shop/receiptloadform.ui b/shop/receiptloadform.ui index 4a29e36..4651ac7 100644 --- a/shop/receiptloadform.ui +++ b/shop/receiptloadform.ui @@ -6,8 +6,8 @@ 0 0 - 454 - 483 + 804 + 500 @@ -23,9 +23,6 @@ - - - @@ -33,6 +30,9 @@ + + + @@ -43,7 +43,7 @@ - + diff --git a/shop/receiptsaveform.cpp b/shop/receiptsaveform.cpp index f3dd4e8..1abc244 100644 --- a/shop/receiptsaveform.cpp +++ b/shop/receiptsaveform.cpp @@ -12,9 +12,10 @@ #include #include "data/voucher.h" +#include "shopservice.h" #include "shop-odb.hxx" -ReceiptSaveForm::ReceiptSaveForm(QWidget *parent) : +ReceiptSaveForm::ReceiptSaveForm(QSharedPointer voucher, QWidget *parent) : QDialog(parent), ui(new Ui::ReceiptSaveForm) { @@ -22,7 +23,7 @@ ReceiptSaveForm::ReceiptSaveForm(QWidget *parent) : m_voucherModel = new AutoTableModel(this); Service srv; - m_voucherModel->setData(srv.all()); + m_voucherModel->setData(srv.all(QString("status = %1").arg(QString::number(Voucher::NOT_PAID)))); m_voucherModel->setTranslations(Context::instance().plugin("SHOP")->translations()); ui->tabVouchers->setModel(m_voucherModel); ui->tabVouchers->hideColumn(3); @@ -50,8 +51,11 @@ ReceiptSaveForm::ReceiptSaveForm(QWidget *parent) : comboData << ComboData(adb); } - m_binder.setData(new Voucher); + m_voucher = voucher; + m_binder.setData(m_voucher.data()); m_binder.registerBinding(ui->contact, comboData); + m_binder.registerBinding(ui->name); + m_binder.registerBinding(ui->description); m_binder.bindToUi(); ui->contact->completer()->setCompletionMode(QCompleter::PopupCompletion); @@ -102,3 +106,27 @@ void ReceiptSaveForm::on_radioAdd_toggled(bool checked) ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(!ui->name->text().isEmpty()); } } + +void ReceiptSaveForm::accept() +{ + ShopService srv; + m_binder.bindToData(); + if (m_saveAsNew) + { + m_voucher->setStatus(Voucher::NOT_PAID); + srv.updateVoucher(m_voucher); + } + else + { + QSharedPointer voucher = m_voucherModel->itemFromIndex(ui->tabVouchers->currentIndex()); + foreach (QSharedPointer item, m_voucher->items()) { + voucher->addItem(item); + } + + srv.calculate(voucher); + srv.updateVoucher(voucher); + srv.erase(m_voucher); + } + + QDialog::accept(); +} diff --git a/shop/receiptsaveform.h b/shop/receiptsaveform.h index a65db07..663f3c4 100644 --- a/shop/receiptsaveform.h +++ b/shop/receiptsaveform.h @@ -15,7 +15,7 @@ class ReceiptSaveForm : public QDialog Q_OBJECT public: - explicit ReceiptSaveForm(QWidget *parent = 0); + explicit ReceiptSaveForm(QSharedPointer voucher, QWidget *parent = 0); ~ReceiptSaveForm(); private slots: @@ -28,6 +28,11 @@ private: ObjectBinder m_binder; AutoTableModel *m_voucherModel; bool m_saveAsNew; + QSharedPointer m_voucher; + + // QDialog interface +public slots: + virtual void accept() override; }; #endif // RECEIPTSAVEFORM_H diff --git a/shop/shopform.cpp b/shop/shopform.cpp index a6c74bf..df2cdf5 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -96,13 +96,23 @@ void ShopForm::on_temporarySaveButton_clicked() void ShopForm::on_saveButton_clicked() { - ReceiptSaveForm *form = new ReceiptSaveForm; + ReceiptSaveForm *form = new ReceiptSaveForm(m_voucher, this); + form->setAttribute(Qt::WA_DeleteOnClose); + + connect(form, &QDialog::accepted, [this]() { + ShopService srv; + m_voucher = srv.createVoucher(); + m_itemsModel->setData(m_voucher->items()); + ui->total->setText("0"); + }); + form->show(); } void ShopForm::on_loadButton_clicked() { - ReceiptLoadForm *form = new ReceiptLoadForm; + ReceiptLoadForm *form = new ReceiptLoadForm(this); + form->setAttribute(Qt::WA_DeleteOnClose); form->show(); }