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.

236 lines
6.0 KiB
C++

#include "shopform.h"
#include "ui_shopform.h"
#include "directsaleform.h"
#include "temporaryreceiptsaveform.h"
#include "receiptsaveform.h"
#include "receiptloadform.h"
#include "shopservice.h"
#include <QList>
#include <QSharedPointer>
#include "shop-odb.hxx"
ShopForm::ShopForm(QWidget *parent) :
QWidget(parent),
ui(new Ui::ShopForm)
{
ui->setupUi(this);
m_itemsModel = NULL;
ui->temporarySaveButton->setEnabled(false);
ui->saveButton->setEnabled(false);
}
ShopForm::~ShopForm()
{
delete ui;
}
void ShopForm::loadLast()
{
if (m_itemsModel == NULL)
{
m_itemsModel = new AutoTableModel<VoucherItem>(this);
m_itemsModel->setEditableCols(QList<int>() << 1);
m_itemsModel->setTranslations(Context::instance().plugin("SHOP")->translations());
ui->actualReceipt->setModel(m_itemsModel);
ui->actualReceipt->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Stretch);
}
ShopService srv;
QList<QSharedPointer<Voucher> > receipt = srv.all(QString("status = %1").arg(QString::number(Voucher::NEW)));
if (!receipt.isEmpty())
{
m_voucher = receipt[0];
srv.loadItems(m_voucher);
m_itemsModel->setData(m_voucher->items());
connectItemSignals();
ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2));
ui->temporarySaveButton->setEnabled(true);
ui->saveButton->setEnabled(true);
}
}
void ShopForm::fillRaceiptCombo()
{
bool oldState = ui->receiptCombo->blockSignals(true);
ShopService srv;
QList<QSharedPointer<Voucher> > receipts = srv.all(QString("status = %1").arg(QString::number(Voucher::TEMPORARY)));
ui->receiptCombo->clear();
ui->receiptCombo->addItem(tr("<< empty >>"));
foreach (QSharedPointer<Voucher> voucher, receipts) {
ui->receiptCombo->addItem(voucher->name(), voucher->id());
}
ui->receiptCombo->blockSignals(oldState);
}
void ShopForm::on_directSale_clicked()
{
if (m_voucher.isNull())
{
createVoucher();
}
DirectSaleForm *form = new DirectSaleForm(this);
form->setAttribute(Qt::WA_DeleteOnClose);
connect(form, &QDialog::accepted, [this, form](){
ShopService srv;
srv.addShopItem(m_voucher, form->shopItem(), ((DirectSaleItem*)form->shopItem().data())->count());
this->m_itemsModel->addRow(m_voucher->items()[m_voucher->items().count() - 1]);
connect(m_voucher->items()[m_voucher->items().count() - 1].data(), SIGNAL(countChanged()), this, SLOT(onCountChanged()));
onCountChanged();
});
form->show();
}
void ShopForm::on_temporarySaveButton_clicked()
{
doTempSave(false);
}
void ShopForm::on_saveButton_clicked()
{
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(this);
form->setAttribute(Qt::WA_DeleteOnClose);
form->show();
}
void ShopForm::onCountChanged()
{
VoucherItem *item = qobject_cast<VoucherItem*>(sender());
if (item != NULL && item->count() == 0)
{
for (int i = 0; i < m_voucher->items().count(); i++)
{
if (m_voucher->items()[i].data() == item)
{
m_voucher->removeItem(m_voucher->items()[i]);
m_itemsModel->setData(m_voucher->items());
}
}
}
ShopService srv;
srv.calculate(m_voucher);
ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2));
ui->temporarySaveButton->setEnabled(!m_voucher->items().isEmpty());
ui->saveButton->setEnabled(!m_voucher->items().isEmpty());
if (m_voucher->status() == Voucher::NEW && m_voucher->id() == 0)
{
srv.saveVoucher(m_voucher);
}
else
{
srv.updateVoucher(m_voucher);
}
}
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.updateVoucher(m_voucher);
}
if (comboChanged && ui->receiptCombo->currentIndex() > 0)
{
changeReceipt();
}
else
{
m_voucher = srv.createVoucher();
ui->total->setText("0");
ui->temporarySaveButton->setEnabled(false);
ui->saveButton->setEnabled(false);
}
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.updateVoucher(m_voucher);
m_itemsModel->setData(m_voucher->items());
ui->total->setText(m_voucher->totalPrice().toString());
ui->temporarySaveButton->setEnabled(true);
ui->saveButton->setEnabled(true);
fillRaceiptCombo();
}
void ShopForm::connectItemSignals()
{
foreach (QSharedPointer<VoucherItem> 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);
}
}