From b8ed333c3a711892f6e9d97fa057d95d54a686f8 Mon Sep 17 00:00:00 2001 From: Josef Rokos Date: Fri, 13 Oct 2017 12:58:26 +0200 Subject: [PATCH] Added support for Value Added Tax. --- core/settings/globalsettings.cpp | 12 ++ core/settings/globalsettings.h | 5 + core/settings/globalsettingsform.cpp | 3 + core/settings/globalsettingsform.ui | 9 +- shop/receiptgenerator.cpp | 103 ++++++++++++- shop/settings/shopsettings.cpp | 33 ++++ shop/settings/shopsettings.h | 18 +++ shop/settings/shopsettingsform.cpp | 10 ++ shop/settings/shopsettingsform.ui | 49 ++++++ shop/shopform.cpp | 49 +++++- shop/shopform.h | 1 + shop/shopform.ui | 223 ++++++++++++++++++++++++++- shop/shopservice.cpp | 179 ++++++++++++++++++--- shop/shopservice.h | 5 + 14 files changed, 665 insertions(+), 34 deletions(-) diff --git a/core/settings/globalsettings.cpp b/core/settings/globalsettings.cpp index 6bd5397..755c7c2 100644 --- a/core/settings/globalsettings.cpp +++ b/core/settings/globalsettings.cpp @@ -7,6 +7,8 @@ GlobalSettings::GlobalSettings(QObject *parent) : QObject(parent) m_vatHigh = 0; m_vatFirstLower = 0; m_vatSecondLower = 0; + m_vatPayer = false; + m_pricesWithVAT = true; } QString GlobalSettings::firmName() const @@ -156,5 +158,15 @@ void GlobalSettings::setPhone(const QString &phone) m_phone = phone; } +bool GlobalSettings::pricesWithVAT() const +{ + return m_pricesWithVAT; +} + +void GlobalSettings::setPricesWithVAT(bool pricesWithVAT) +{ + m_pricesWithVAT = pricesWithVAT; +} + diff --git a/core/settings/globalsettings.h b/core/settings/globalsettings.h index 558bab0..a7c2d9b 100644 --- a/core/settings/globalsettings.h +++ b/core/settings/globalsettings.h @@ -18,6 +18,7 @@ class CORESHARED_EXPORT GlobalSettings : public QObject Q_PROPERTY(int ic READ ic WRITE setIc) Q_PROPERTY(QString dic READ dic WRITE setDic) Q_PROPERTY(bool vatPayer READ vatPayer WRITE setVatPayer) + Q_PROPERTY(bool pricesWithVAT READ pricesWithVAT WRITE setPricesWithVAT) Q_PROPERTY(QString logoPath READ logoPath WRITE setLogoPath) Q_PROPERTY(QDecDouble vatHigh READ vatHigh WRITE setVatHigh) Q_PROPERTY(QDecDouble vatFirstLower READ vatFirstLower WRITE setVatFirstLower) @@ -74,6 +75,9 @@ public: QString phone() const; void setPhone(const QString &phone); + bool pricesWithVAT() const; + void setPricesWithVAT(bool pricesWithVAT); + private: QString m_firmName; QString m_street; @@ -83,6 +87,7 @@ private: int m_ic; QString m_dic; bool m_vatPayer; + bool m_pricesWithVAT; QString m_logoPath; int m_vatHigh; int m_vatFirstLower; diff --git a/core/settings/globalsettingsform.cpp b/core/settings/globalsettingsform.cpp index 78aa3f4..1d538d4 100644 --- a/core/settings/globalsettingsform.cpp +++ b/core/settings/globalsettingsform.cpp @@ -24,6 +24,7 @@ GlobalSettingsForm::GlobalSettingsForm(QWidget *parent) : registerBinding(ui->city); registerBinding(ui->ic); registerBinding(ui->vatPayer); + registerBinding(ui->pricesWithVAT); registerBinding(ui->dic); registerBinding(ui->vatHigh); registerBinding(ui->vatFirstLower); @@ -110,6 +111,7 @@ void GlobalSettingsForm::loadEntity() QSharedPointer settings = srv.loadSettings(); setEntity(settings); ui->grpVat->setEnabled(settings->vatPayer()); + ui->pricesWithVAT->setEnabled(settings->vatPayer()); if (!settings->logoPath().isEmpty()) { @@ -123,6 +125,7 @@ void GlobalSettingsForm::loadEntity() void GlobalSettingsForm::on_vatPayer_toggled(bool checked) { ui->grpVat->setEnabled(checked); + ui->pricesWithVAT->setEnabled(checked); } void GlobalSettingsForm::on_season_currentIndexChanged(int) diff --git a/core/settings/globalsettingsform.ui b/core/settings/globalsettingsform.ui index fa04652..81f4276 100644 --- a/core/settings/globalsettingsform.ui +++ b/core/settings/globalsettingsform.ui @@ -7,7 +7,7 @@ 0 0 759 - 557 + 630 @@ -253,6 +253,13 @@ + + + + Prices with VAT + + + diff --git a/shop/receiptgenerator.cpp b/shop/receiptgenerator.cpp index abb6153..a2f9481 100644 --- a/shop/receiptgenerator.cpp +++ b/shop/receiptgenerator.cpp @@ -148,9 +148,16 @@ QByteArray ReceiptGenerator::generate() QString price = QString::number(item->price().toDouble(), 'f', 2); int numSpaces = 0; + name = count + "x " + name; + + if (gs->vatPayer()) + { + out.append(prepareString("DPH " + QString::number(item->vatRate().toDouble(), 'f', 0)) + "%"); + out.append("\x0a"); + } + if ((name.length() + price.length()) < shopSettings->lettersPerLine()) { - name = count + "x " + name; numSpaces = shopSettings->lettersPerLine() - (name.length() + price.length()); out.append(prepareString(name)); } @@ -179,6 +186,100 @@ QByteArray ReceiptGenerator::generate() out.append((char)0); out.append("\x0a"); + if (gs->vatPayer()) + { + QString zaklad = "Zaklad DPH "; + QString dph = "DPH "; + QString output; + QDecDouble zero(0); + + if (m_voucher->vatAmountHigh() > zero) + { + output = zaklad + QString::number(m_voucher->vatRateHigh().toDouble()) + "%"; + out.append(prepareString(output)); + QString amount = QString::number(m_voucher->priceVatHigh().toDouble(), 'f', 2); + int numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + + output = dph + QString::number(m_voucher->vatRateHigh().toDouble()) + "%"; + out.append(prepareString(output)); + amount = QString::number(m_voucher->vatAmountHigh().toDouble(), 'f', 2); + numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + } + + if (m_voucher->vatAmountFirstLower() > zero) + { + output = zaklad + QString::number(m_voucher->vatRateFirstLower().toDouble()) + "%"; + out.append(prepareString(output)); + QString amount = QString::number(m_voucher->priceVatFirstLower().toDouble(), 'f', 2); + int numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + + output = dph + QString::number(m_voucher->vatRateFirstLower().toDouble()) + "%"; + out.append(prepareString(output)); + amount = QString::number(m_voucher->vatAmountFirstLower().toDouble(), 'f', 2); + numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + } + + if (m_voucher->VatAmountSecondLower() > zero) + { + output = zaklad + QString::number(m_voucher->vatRateSecondLower().toDouble()) + "%"; + out.append(prepareString(output)); + QString amount = QString::number(m_voucher->priceVatSecondLower().toDouble(), 'f', 2); + int numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + + output = dph + QString::number(m_voucher->vatRateSecondLower().toDouble()) + "%"; + out.append(prepareString(output)); + amount = QString::number(m_voucher->VatAmountSecondLower().toDouble(), 'f', 2); + numSpaces = shopSettings->lettersPerLine() - (output.length() + amount.length()); + for (int i = 0; i < numSpaces; i++) + { + out.append(" "); + } + out.append(prepareString(amount)); + out.append("\x0a"); + } + + out.append("\x1b\x21"); + out.append(printMode); + for (int i = 0; i < shopSettings->lettersPerLine(); i++ ) + { + out.append("-"); + } + out.append("\x1b\x21"); + out.append((char)0); + out.append("\x0a"); + + } + out.append("\x1b\x21"); out.append(printMode); out.append("Celkem:"); diff --git a/shop/settings/shopsettings.cpp b/shop/settings/shopsettings.cpp index f28d39b..167f5bf 100644 --- a/shop/settings/shopsettings.cpp +++ b/shop/settings/shopsettings.cpp @@ -13,6 +13,9 @@ ShopSettings::ShopSettings(QObject *parent) : QObject(parent) m_favBtnCols = 0; m_favBtnRows = 0; m_favBtnSize = 0; + + m_rounding = Enums::R_NONE; + m_decimalPlaces = 0; } QString ShopSettings::output() const @@ -164,3 +167,33 @@ void ShopSettings::setFavBtnSize(int favBtnSize) { m_favBtnSize = favBtnSize; } + +Enums::Rounding ShopSettings::rounding() const +{ + return m_rounding; +} + +void ShopSettings::setRounding(const Enums::Rounding &rounding) +{ + m_rounding = rounding; +} + +int ShopSettings::decimalPlaces() const +{ + return m_decimalPlaces; +} + +void ShopSettings::setDecimalPlaces(int decimalPlaces) +{ + m_decimalPlaces = decimalPlaces; +} + +QString ShopSettings::roundingItem() const +{ + return m_roundingItem; +} + +void ShopSettings::setRoundingItem(const QString &roundingItem) +{ + m_roundingItem = roundingItem; +} diff --git a/shop/settings/shopsettings.h b/shop/settings/shopsettings.h index ce68603..8e5fbb6 100644 --- a/shop/settings/shopsettings.h +++ b/shop/settings/shopsettings.h @@ -2,6 +2,7 @@ #define RECEIPTSETTINGS_H #include +#include class ShopSettings : public QObject { @@ -22,6 +23,10 @@ class ShopSettings : public QObject Q_PROPERTY(int favBtnRows READ favBtnRows WRITE setFavBtnRows) Q_PROPERTY(int favBtnSize READ favBtnSize WRITE setFavBtnSize) + Q_PROPERTY(Enums::Rounding rounding READ rounding WRITE setRounding) + Q_PROPERTY(int decimalPlaces READ decimalPlaces WRITE setDecimalPlaces) + Q_PROPERTY(QString roundingItem READ roundingItem WRITE setRoundingItem) + Q_OBJECT public: @@ -77,6 +82,15 @@ public: int favBtnSize() const; void setFavBtnSize(int favBtnSize); + Enums::Rounding rounding() const; + void setRounding(const Enums::Rounding &rounding); + + int decimalPlaces() const; + void setDecimalPlaces(int decimalPlaces); + + QString roundingItem() const; + void setRoundingItem(const QString &roundingItem); + private: QString m_output; CODEPAGE m_codepage; @@ -95,6 +109,10 @@ private: int m_favBtnCols; int m_favBtnRows; int m_favBtnSize; + + Enums::Rounding m_rounding; + int m_decimalPlaces; + QString m_roundingItem; }; typedef QSharedPointer ShopSettingsPtr; diff --git a/shop/settings/shopsettingsform.cpp b/shop/settings/shopsettingsform.cpp index 890aefc..5bf9b4a 100644 --- a/shop/settings/shopsettingsform.cpp +++ b/shop/settings/shopsettingsform.cpp @@ -35,6 +35,16 @@ ShopSettingsForm::ShopSettingsForm(QWidget *parent) : registerBinding(ui->favBtnRows); registerBinding(ui->favBtnSize); + QList listRounding; + listRounding + << ComboData(Enums::R_NONE, tr("None")) + << ComboData(Enums::R_UP, tr("Up")) + << ComboData(Enums::R_DOWN, tr("Down")) + << ComboData(Enums::R_MATH, tr("Mathematic")); + registerBinding(ui->rounding, listRounding); + registerBinding(ui->decimalPlaces); + registerBinding(ui->roundingItem); + m_itemModel = new AutoTableModel(); } diff --git a/shop/settings/shopsettingsform.ui b/shop/settings/shopsettingsform.ui index 5bdc9f5..b0222fd 100644 --- a/shop/settings/shopsettingsform.ui +++ b/shop/settings/shopsettingsform.ui @@ -273,6 +273,55 @@ + + + Other + + + + + + Rounding + + + + + + Direction + + + + + + + + + + Decimal places + + + Qt::PlainText + + + + + + + + + + Rounding item text + + + + + + + + + + + diff --git a/shop/shopform.cpp b/shop/shopform.cpp index 944ba34..e7844d5 100644 --- a/shop/shopform.cpp +++ b/shop/shopform.cpp @@ -89,8 +89,18 @@ void ShopForm::loadLast() ui->actualReceipt->setColumnHidden(0, true); ui->actualReceipt->setColumnHidden(3, true); ui->actualReceipt->setColumnHidden(4, true); - ui->actualReceipt->setColumnHidden(5, true); + + SettingsService srv("CORE"); + GlobalSettingsPtr settings = srv.loadSettings(); + + if (!settings->vatPayer()) + { + ui->actualReceipt->setColumnHidden(5, true); + ui->widgetVAT->setVisible(false); + } + ui->actualReceipt->setColumnHidden(6, true); + ui->actualReceipt->setColumnWidth(2, 50); ui->actualReceipt->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); } @@ -103,7 +113,8 @@ void ShopForm::loadLast() m_itemsModel->setData(m_voucher->items()); connectItemSignals(); - ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); + //ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); + setTotalText(); if (!m_voucher->items().isEmpty()) { @@ -319,7 +330,9 @@ void ShopForm::onCountChanged(int oldCount/* = 0*/) ShopService srv; srv.calculate(m_voucher); - ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); + this->m_itemsModel->setData(m_voucher->items()); + + setTotalText(); ui->temporarySaveButton->setEnabled(!m_voucher->items().isEmpty()); ui->saveButton->setEnabled(!m_voucher->items().isEmpty()); ui->payButton->setEnabled(!m_voucher->items().isEmpty()); @@ -388,7 +401,8 @@ void ShopForm::changeReceipt() srv.updateVoucher(m_voucher); m_itemsModel->setData(m_voucher->items()); - ui->total->setText(m_voucher->totalPrice().toString()); + //ui->total->setText(m_voucher->totalPrice().toString()); + setTotalText(); ui->temporarySaveButton->setEnabled(true); ui->saveButton->setEnabled(true); @@ -408,7 +422,8 @@ void ShopForm::createEmptyVoucher() { ShopService srv; m_voucher = srv.createVoucher(); - ui->total->setText("0"); + //ui->total->setText("0"); + setTotalText(); ui->temporarySaveButton->setEnabled(false); ui->saveButton->setEnabled(false); ui->payButton->setEnabled(false); @@ -428,7 +443,7 @@ void ShopForm::addItem(QSharedPointer item, int count) auto addFunc = [this](QSharedPointer shopItem, int itemCount){ ShopService srv; srv.addShopItem(m_voucher, shopItem, itemCount); - this->m_itemsModel->addRow(m_voucher->items()[m_voucher->items().count() - 1]); + //this->m_itemsModel->addRow(m_voucher->items()[m_voucher->items().count() - 1]); connect(m_voucher->items()[m_voucher->items().count() - 1].data(), SIGNAL(countChanged(int)), this, SLOT(onCountChanged(int))); onCountChanged(); }; @@ -452,6 +467,28 @@ void ShopForm::addItem(QSharedPointer item, int count) ui->commoditySearch->setText(""); } +void ShopForm::setTotalText() +{ + ui->total->setText(QString::number(m_voucher->totalPrice().toDouble(), 'f', 2)); + + ui->vatRateHigh->setText(QString::number(m_voucher->vatRateHigh().toDouble(), 'f', 0) + "%"); + ui->vatRateFirstLow->setText(QString::number(m_voucher->vatRateFirstLower().toDouble(), 'f', 0) + "%"); + ui->vatRateSecondLow->setText(QString::number(m_voucher->vatRateSecondLower().toDouble(), 'f', 0) + "%"); + + ui->priceVatHigh->setText(QString::number(m_voucher->priceVatHigh().toDouble(), 'f', 2)); + ui->priceVatFirstLower->setText(QString::number(m_voucher->priceVatFirstLower().toDouble(), 'f', 2)); + ui->priceVatSecondLower->setText(QString::number(m_voucher->priceVatSecondLower().toDouble(), 'f', 2)); + + ui->vatHigh->setText(QString::number((m_voucher->totalPriceVatHigh() - m_voucher->priceVatHigh()).toDouble(), 'f', 2)); + ui->vatFirstLower->setText(QString::number((m_voucher->totalPriceVatFirstLower() - m_voucher->priceVatFirstLower()).toDouble(), 'f', 2)); + ui->vatSecondLower->setText(QString::number((m_voucher->totalPriceVatSecondLower() - m_voucher->priceVatSecondLower()).toDouble(), 'f', 2)); + + ui->totalPriceVatHigh->setText(QString::number(m_voucher->totalPriceVatHigh().toDouble(), 'f', 2)); + ui->totalPriceFirstLower->setText(QString::number(m_voucher->totalPriceVatFirstLower().toDouble(), 'f', 2)); + ui->totalPriceSecondLower->setText(QString::number(m_voucher->totalPriceVatSecondLower().toDouble(), 'f', 2)); + ui->priceNoVat->setText(QString::number(m_voucher->priceNoVat().toDouble(), 'f', 2)); +} + void ShopForm::on_receiptCombo_currentIndexChanged(int) { if (!m_voucher.isNull() && m_voucher->items().isEmpty()) diff --git a/shop/shopform.h b/shop/shopform.h index ef903d8..bb2e3a5 100644 --- a/shop/shopform.h +++ b/shop/shopform.h @@ -63,6 +63,7 @@ private: void connectItemSignals(); void createEmptyVoucher(); void addItem(QSharedPointer item, int count); + void setTotalText(); }; #endif // SHOPFORM_H diff --git a/shop/shopform.ui b/shop/shopform.ui index 9c02e50..d3d2047 100644 --- a/shop/shopform.ui +++ b/shop/shopform.ui @@ -167,7 +167,226 @@ - + + + + 400 + 0 + + + + + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + 10% + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 75 + true + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + 0% + + + + + + + + 0 + 0 + + + + 21% + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 75 + true + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 0 + 0 + + + + 15% + + + + + + + + 75 + true + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + + 75 + true + + + + 0 + + + Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter + + + + + + + Witouth VAT + + + Qt::AlignCenter + + + + + + + VAT + + + Qt::AlignCenter + + + + + + + Total + + + Qt::AlignCenter + + + + + @@ -382,8 +601,8 @@ - + diff --git a/shop/shopservice.cpp b/shop/shopservice.cpp index 68e675c..3ff5b4b 100644 --- a/shop/shopservice.cpp +++ b/shop/shopservice.cpp @@ -8,6 +8,7 @@ #include #include #include +#include ShopService::ShopService() { @@ -16,6 +17,12 @@ ShopService::ShopService() VoucherPtr ShopService::createVoucher() { QSharedPointer voucher(new Voucher); + + loadSettings(); + voucher->setVatRateHigh(m_gs->vatHigh()); + voucher->setVatRateFirstLower(m_gs->vatFirstLower()); + voucher->setVatRateSecondLower(m_gs->vatSecondLower()); + voucher->setStatus(Voucher::NEW); return voucher; } @@ -40,39 +47,87 @@ void ShopService::calculate(VoucherPtr voucher) { QDecDouble total; + voucher->setPriceNoVat(0); + voucher->setPriceVatHigh(0); + voucher->setPriceVatFirstLower(0); + voucher->setPriceVatSecondLower(0); + + voucher->setTotalPriceVatHigh(0); + voucher->setTotalPriceVatFirstLower(0); + voucher->setTotalPriceVatSecondLower(0); + loadSettings(); - voucher->setVatRateHigh(m_gs->vatHigh()); - voucher->setVatRateFirstLower(m_gs->vatFirstLower()); - voucher->setVatRateSecondLower(m_gs->vatSecondLower()); foreach (QSharedPointer item, voucher->items()) { + + if (item->refId() == ROUNDING_ITEM) + { + voucher->removeItem(item); + continue; + } + calculateItem(item); - QDecDouble priceWitouthWat = item->priceWitouthVat(); - switch (item->vatType()) { - case Enums::NONE: - voucher->setPriceNoVat(voucher->priceNoVat() + priceWitouthWat); - break; - case Enums::HIGH: - voucher->setPriceVatHigh(voucher->priceVatHigh() + priceWitouthWat); - break; - case Enums::FIRST_LOWER: - voucher->setPriceVatFirstLower(voucher->priceVatFirstLower() + priceWitouthWat); - break; - case Enums::SECOND_LOWER: - voucher->setPriceVatSecondLower(voucher->priceVatSecondLower() + priceWitouthWat); - break; - default: - break; + QDecDouble itemPrice; + + if (item->vatType() == Enums::NONE) + { + voucher->setPriceNoVat(voucher->priceNoVat() + item->priceWitouthVat()); + } + + if (m_gs->pricesWithVAT()) + { + itemPrice = item->price(); + switch (item->vatType()) { + case Enums::HIGH: + voucher->setTotalPriceVatHigh(voucher->totalPriceVatHigh() + itemPrice); + break; + case Enums::FIRST_LOWER: + voucher->setTotalPriceVatFirstLower(voucher->totalPriceVatFirstLower() + itemPrice); + break; + case Enums::SECOND_LOWER: + voucher->setTotalPriceVatSecondLower(voucher->totalPriceVatSecondLower() + itemPrice); + break; + default: + break; + } + } + else + { + itemPrice = item->priceWitouthVat(); + switch (item->vatType()) { + case Enums::HIGH: + voucher->setPriceVatHigh(voucher->priceVatHigh() + itemPrice); + break; + case Enums::FIRST_LOWER: + voucher->setPriceVatFirstLower(voucher->priceVatFirstLower() + itemPrice); + break; + case Enums::SECOND_LOWER: + voucher->setPriceVatSecondLower(voucher->priceVatSecondLower() + itemPrice); + break; + default: + break; + } } total += item->price(); } - voucher->setTotalPriceVatHigh(includeVat(voucher->priceVatHigh(), Enums::HIGH)); - voucher->setTotalPriceVatFirstLower(includeVat(voucher->priceVatFirstLower(), Enums::FIRST_LOWER)); - voucher->setTotalPriceVatSecondLower(includeVat(voucher->priceVatSecondLower(), Enums::SECOND_LOWER)); + if (m_gs->pricesWithVAT()) + { + voucher->setPriceVatHigh(excludeVat(voucher->totalPriceVatHigh(), Enums::HIGH)); + voucher->setPriceVatFirstLower(excludeVat(voucher->totalPriceVatFirstLower(), Enums::FIRST_LOWER)); + voucher->setPriceVatSecondLower(excludeVat(voucher->totalPriceVatSecondLower(), Enums::SECOND_LOWER)); + } + else + { + voucher->setTotalPriceVatHigh(includeVat(voucher->priceVatHigh(), Enums::HIGH)); + voucher->setTotalPriceVatFirstLower(includeVat(voucher->priceVatFirstLower(), Enums::FIRST_LOWER)); + voucher->setTotalPriceVatSecondLower(includeVat(voucher->priceVatSecondLower(), Enums::SECOND_LOWER)); + } + voucher->setTotalPrice(total); + roundVoucher(voucher); } void ShopService::calculateItem(VoucherItemPtr item) @@ -81,8 +136,17 @@ void ShopService::calculateItem(VoucherItemPtr item) if (m_gs->vatPayer()) { item->setVatRate(vatRate(item->vatType())); - item->setPriceWitouthVat(item->unitPrice() * item->count()); - item->setPrice(includeVat(item->priceWitouthVat(), item->vatType())); + + if (m_gs->pricesWithVAT()) + { + item->setPrice(item->unitPrice() * item->count()); + item->setPriceWitouthVat(excludeVat(item->price(), item->vatType())); + } + else + { + item->setPriceWitouthVat(item->unitPrice() * item->count()); + item->setPrice(includeVat(item->priceWitouthVat(), item->vatType())); + } } else { @@ -147,6 +211,18 @@ bool ShopService::processEet(VoucherPtr voucher, QString &message) request.setDatOdesl(QDateTime::currentDateTime()); request.setRezim((EetRequest::EetRezim)settings->eetMode()); + if (m_gs->vatPayer()) + { + request.setDan1(voucher->vatAmountHigh().toDouble()); + request.setDan2(voucher->vatAmountFirstLower().toDouble()); + request.setDan3(voucher->VatAmountSecondLower().toDouble()); + + request.setZaklDan1(voucher->priceVatHigh().toDouble()); + request.setZaklDan2(voucher->priceVatFirstLower().toDouble()); + request.setZaklDan3(voucher->priceVatSecondLower().toDouble()); + request.setZaklNepodlDph(voucher->priceNoVat().toDouble()); + } + EetSender *sender = new EetSender(this); sender->setupSigner(settings->eetCertificate(), settings->eetKeyPassword()); sender->setPlayground(settings->eetPlayground()); @@ -232,6 +308,56 @@ bool ShopService::isEetEnabled() return settings->eetActive(); } +void ShopService::roundVoucher(VoucherPtr voucher) +{ + SettingsService setSrv("SHOP"); + ShopSettingsPtr settings = setSrv.loadSettings(); + QDecDouble totalPrice(voucher->totalPrice()); + + switch (settings->rounding()) { + case Enums::R_UP: + totalPrice = QDecDouble(ceil(voucher->totalPrice().toDouble() * pow(10, settings->decimalPlaces())) / pow(10, settings->decimalPlaces())); + break; + case Enums::R_DOWN: + totalPrice = QDecDouble(floor(voucher->totalPrice().toDouble() * pow(10, settings->decimalPlaces())) / pow(10, settings->decimalPlaces())); + break; + case Enums::R_MATH: + totalPrice = QDecDouble(round(voucher->totalPrice().toDouble() * pow(10, settings->decimalPlaces())) / pow(10, settings->decimalPlaces())); + break; + case Enums::R_NONE: + break; + } + + VoucherItemPtr roundingItem(new VoucherItem); + roundingItem->setCount(1); + roundingItem->setUnitPrice(totalPrice - voucher->totalPrice()); + roundingItem->setVatType(Enums::NONE); + roundingItem->setName(settings->roundingItem()); + roundingItem->setRefId(ROUNDING_ITEM); + roundingItem->setVoucher(voucher); + + calculateItem(roundingItem); + + if (roundingItem->price() != QDecDouble(0)) + { + voucher->addItem(roundingItem); + voucher->setPriceNoVat(voucher->priceNoVat() + roundingItem->price()); + voucher->setTotalPrice(totalPrice); + } +} + +VoucherItemPtr ShopService::roundingItem(VoucherPtr voucher) +{ + foreach (VoucherItemPtr item, voucher->items()) { + if (item->refId() == ROUNDING_ITEM) + { + return item; + } + } + + return VoucherItemPtr(); +} + void ShopService::moveItems(QList items, VoucherPtr source, VoucherPtr target) { Transaction tx; @@ -306,6 +432,11 @@ QDecDouble ShopService::includeVat(QDecDouble price, Enums::VatType vatType) return price * ((vatRate(vatType) / 100) + QDecDouble(1)); } +QDecDouble ShopService::excludeVat(QDecDouble price, Enums::VatType vatType) +{ + return price / ((vatRate(vatType) / 100) + QDecDouble(1)); +} + void ShopService::loadSettings() { if (m_gs.isNull()) diff --git a/shop/shopservice.h b/shop/shopservice.h index 6ecdc29..d4dc3a1 100644 --- a/shop/shopservice.h +++ b/shop/shopservice.h @@ -11,6 +11,8 @@ #include "shop_global.h" +#define ROUNDING_ITEM -9999 + class PayDialog; class ShopForm; @@ -32,6 +34,8 @@ public: void setEetOnline(bool online); bool isEetOnline(); bool isEetEnabled(); + void roundVoucher(VoucherPtr voucher); + VoucherItemPtr roundingItem(VoucherPtr voucher); QList savedVouchers(); QList tempVouchers(); QList paiedVouchers(); @@ -40,6 +44,7 @@ public: private: QDecDouble includeVat(QDecDouble price, Enums::VatType vatType); + QDecDouble excludeVat(QDecDouble price, Enums::VatType vatType); void loadSettings(); QSharedPointer m_gs;