diff --git a/src/core/sale.cpp b/src/core/sale.cpp index 30f53b3..075df3c 100644 --- a/src/core/sale.cpp +++ b/src/core/sale.cpp @@ -34,6 +34,6 @@ int Sale::sumInCents() std::string Sale::sumAsString() { return formatCentAsEuroString(sumInCents()); } -std::string Sale::getTimestamp() { return timestamp_; } +std::string Sale::getTimestamp() const { return timestamp_; } void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; } \ No newline at end of file diff --git a/src/core/sale.h b/src/core/sale.h index ff69b9f..b44ff92 100644 --- a/src/core/sale.h +++ b/src/core/sale.h @@ -21,7 +21,7 @@ class Sale : public Entity void setTimestamp(const std::string& timestamp); ArticlesVec& getArticles(); - std::string getTimestamp(); + std::string getTimestamp() const; int sumInCents(); std::string sumAsString(); @@ -30,7 +30,7 @@ class Sale : public Entity private: std::string timestamp_{ boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time())}; - ArticlesVec articles_{}; + mutable ArticlesVec articles_{}; }; #endif \ No newline at end of file diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 1a7c4e4..2efe39a 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -10,6 +10,7 @@ #include "settingsdialog.h" #include +#include #include #include @@ -48,6 +49,8 @@ MainWindow::MainWindow() &MainWindow::onCancelArticleButtonClicked); connect(ui_.cancelSaleButton, &QPushButton::clicked, this, &MainWindow::onCancelSaleButtonClicked); + connect(ui_.printSaleReceiptButton, &QPushButton::clicked, this, + &MainWindow::onPrintSaleReceiptButtonClicked); connect(ui_.cancelAllArticlesButton, &QPushButton::clicked, this, &MainWindow::onCancelAllArticlesButtonClicked); connect(ui_.aboutQtAction, &QAction::triggered, this, &MainWindow::onAboutQt); @@ -169,8 +172,14 @@ void MainWindow::onSalesViewSelectionChanged(const QItemSelection& selected, { if (selected.size() > 0) { ui_.cancelSaleButton->setEnabled(true); + if (!selected.indexes()[0].parent().isValid()) + ui_.printSaleReceiptButton->setEnabled(true); + else + ui_.printSaleReceiptButton->setEnabled(false); + } else { ui_.cancelSaleButton->setEnabled(false); + ui_.printSaleReceiptButton->setEnabled(false); } } @@ -220,6 +229,18 @@ void MainWindow::onCancelSaleButtonClicked([[maybe_unused]] bool checked) } } +void MainWindow::onPrintSaleReceiptButtonClicked([[maybe_unused]] bool checked) +{ + auto selModel = ui_.salesView->selectionModel(); + if (selModel->hasSelection() == false) + return; + + auto indexes = selModel->selectedRows(); + auto& sale = marketplace_->getSales().at(indexes[0].row()); + PosPrinter printer; + printer.printReceipt(sale.get()); +} + void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked) { if (ui_.basketView->model()->rowCount() == 0) diff --git a/src/gui/mainwindow.h b/src/gui/mainwindow.h index f928e6b..685d57c 100644 --- a/src/gui/mainwindow.h +++ b/src/gui/mainwindow.h @@ -24,6 +24,7 @@ class MainWindow : public QMainWindow const QItemSelection& deselected); void onCancelArticleButtonClicked(bool checked); void onCancelSaleButtonClicked(bool checked); + void onPrintSaleReceiptButtonClicked(bool checked); void onCancelAllArticlesButtonClicked(bool checked); void onAboutQt(); void onAbout(); diff --git a/src/gui/mainwindow.ui b/src/gui/mainwindow.ui index 2e7d874..ed01175 100644 --- a/src/gui/mainwindow.ui +++ b/src/gui/mainwindow.ui @@ -246,7 +246,7 @@ stornieren - + false diff --git a/src/printer/CMakeLists.txt b/src/printer/CMakeLists.txt index 35865e2..20d65fd 100644 --- a/src/printer/CMakeLists.txt +++ b/src/printer/CMakeLists.txt @@ -9,5 +9,5 @@ set(PRINTER_SOURCES ) add_library(printer STATIC ${PRINTER_SOURCES}) -target_link_libraries(printer ${LIBUSB_1_LIBRARIES}) +target_link_libraries(printer core ${LIBUSB_1_LIBRARIES}) target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) \ No newline at end of file diff --git a/src/printer/posprinter.cpp b/src/printer/posprinter.cpp index 1e8aacb..359bd2a 100644 --- a/src/printer/posprinter.cpp +++ b/src/printer/posprinter.cpp @@ -1,18 +1,19 @@ #include "posprinter.h" #include +#include #include #include #include #include -#include const std::string PosPrinter::Command::RESET = {0x1b, 0x40}; const std::string PosPrinter::Command::ENCODING = {'\x1b', '\x74', 16}; const std::string PosPrinter::Command::CENTERING = {'\x1b', '\x61', '\x01'}; +const std::string PosPrinter::Command::LEFT_ALIGN = {0x1b, 0x61, 0x00}; +const std::string PosPrinter::Command::RIGHT_ALIGN = {0x1b, 0x61, 0x02}; const std::string PosPrinter::Command::FONT_SIZE_BIG = {'\x1b', '\x21', '\x10'}; const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {'\x1b', '\x21', '\x00'}; -const std::string PosPrinter::Command::LEFT_ALIGN = {'\x1b', '\x61', '\x00'}; const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03}; PosPrinter::PosPrinter() @@ -25,7 +26,7 @@ PosPrinter::PosPrinter() } #if defined(_WIN32) - libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK); + // libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK); #endif libusb_device** devList; @@ -126,7 +127,6 @@ void PosPrinter::printHeader() void PosPrinter::printTest() { - using namespace std::string_literals; std::stringstream commandStream; commandStream << Command::ENCODING; commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n" @@ -134,4 +134,21 @@ void PosPrinter::printTest() commandStream << Command::FEED; printHeader(); write(commandStream.str()); +} + +void PosPrinter::printReceipt(Sale* sale) +{ + std::stringstream commandStream; + printHeader(); + commandStream << Command::RESET << Command::ENCODING << Command::RIGHT_ALIGN; + commandStream << sale->getTimestamp() << "\n\n"; + commandStream << Command::LEFT_ALIGN; + for(const auto& article : sale->getArticles()) { + commandStream << "Art. " << article->getCompleteArticleNo() << "........... " << article->getPriceAsString() << "\n"; + } + commandStream << "\nGesamt................. " << sale->sumAsString() << "\n"; + commandStream << Command::CENTERING; + commandStream << "\n\nVielen Dank für Ihren Einkauf!"; + commandStream << Command::LEFT_ALIGN << Command::FEED; + write(commandStream.str()); } \ No newline at end of file diff --git a/src/printer/posprinter.h b/src/printer/posprinter.h index ffdf166..f9573cd 100644 --- a/src/printer/posprinter.h +++ b/src/printer/posprinter.h @@ -1,6 +1,8 @@ #ifndef POS_PRINTER_H #define POS_PRINTER_H +#include + #include #include @@ -21,14 +23,16 @@ class PosPrinter void write(const std::string& text); void printHeader(); void printTest(); + void printReceipt(Sale* sale); struct Command { static const std::string RESET; static const std::string ENCODING; static const std::string CENTERING; + static const std::string LEFT_ALIGN; + static const std::string RIGHT_ALIGN; static const std::string FONT_SIZE_BIG; static const std::string FONT_SIZE_NORMAL; - static const std::string LEFT_ALIGN; static const std::string FEED; };