printing sale receipt

This commit is contained in:
Martin Brodbeck 2018-08-06 16:14:45 +02:00
parent 23659078b4
commit 6e6510ebcf
8 changed files with 53 additions and 10 deletions

View file

@ -34,6 +34,6 @@ int Sale::sumInCents()
std::string Sale::sumAsString() { return formatCentAsEuroString(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; } void Sale::setTimestamp(const std::string& timestamp) { timestamp_ = timestamp; }

View file

@ -21,7 +21,7 @@ class Sale : public Entity
void setTimestamp(const std::string& timestamp); void setTimestamp(const std::string& timestamp);
ArticlesVec& getArticles(); ArticlesVec& getArticles();
std::string getTimestamp(); std::string getTimestamp() const;
int sumInCents(); int sumInCents();
std::string sumAsString(); std::string sumAsString();
@ -30,7 +30,7 @@ class Sale : public Entity
private: private:
std::string timestamp_{ std::string timestamp_{
boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time())}; boost::posix_time::to_iso_extended_string(boost::posix_time::second_clock::local_time())};
ArticlesVec articles_{}; mutable ArticlesVec articles_{};
}; };
#endif #endif

View file

@ -10,6 +10,7 @@
#include "settingsdialog.h" #include "settingsdialog.h"
#include <excelreader.h> #include <excelreader.h>
#include <posprinter.h>
#include <exception> #include <exception>
#include <regex> #include <regex>
@ -48,6 +49,8 @@ MainWindow::MainWindow()
&MainWindow::onCancelArticleButtonClicked); &MainWindow::onCancelArticleButtonClicked);
connect(ui_.cancelSaleButton, &QPushButton::clicked, this, connect(ui_.cancelSaleButton, &QPushButton::clicked, this,
&MainWindow::onCancelSaleButtonClicked); &MainWindow::onCancelSaleButtonClicked);
connect(ui_.printSaleReceiptButton, &QPushButton::clicked, this,
&MainWindow::onPrintSaleReceiptButtonClicked);
connect(ui_.cancelAllArticlesButton, &QPushButton::clicked, this, connect(ui_.cancelAllArticlesButton, &QPushButton::clicked, this,
&MainWindow::onCancelAllArticlesButtonClicked); &MainWindow::onCancelAllArticlesButtonClicked);
connect(ui_.aboutQtAction, &QAction::triggered, this, &MainWindow::onAboutQt); connect(ui_.aboutQtAction, &QAction::triggered, this, &MainWindow::onAboutQt);
@ -169,8 +172,14 @@ void MainWindow::onSalesViewSelectionChanged(const QItemSelection& selected,
{ {
if (selected.size() > 0) { if (selected.size() > 0) {
ui_.cancelSaleButton->setEnabled(true); ui_.cancelSaleButton->setEnabled(true);
if (!selected.indexes()[0].parent().isValid())
ui_.printSaleReceiptButton->setEnabled(true);
else
ui_.printSaleReceiptButton->setEnabled(false);
} else { } else {
ui_.cancelSaleButton->setEnabled(false); 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) void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked)
{ {
if (ui_.basketView->model()->rowCount() == 0) if (ui_.basketView->model()->rowCount() == 0)

View file

@ -24,6 +24,7 @@ class MainWindow : public QMainWindow
const QItemSelection& deselected); const QItemSelection& deselected);
void onCancelArticleButtonClicked(bool checked); void onCancelArticleButtonClicked(bool checked);
void onCancelSaleButtonClicked(bool checked); void onCancelSaleButtonClicked(bool checked);
void onPrintSaleReceiptButtonClicked(bool checked);
void onCancelAllArticlesButtonClicked(bool checked); void onCancelAllArticlesButtonClicked(bool checked);
void onAboutQt(); void onAboutQt();
void onAbout(); void onAbout();

View file

@ -246,7 +246,7 @@ stornieren</string>
</widget> </widget>
</item> </item>
<item> <item>
<widget class="QPushButton" name="pushButton_2"> <widget class="QPushButton" name="printSaleReceiptButton">
<property name="enabled"> <property name="enabled">
<bool>false</bool> <bool>false</bool>
</property> </property>

View file

@ -9,5 +9,5 @@ set(PRINTER_SOURCES
) )
add_library(printer STATIC ${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}) target_include_directories(printer PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View file

@ -1,18 +1,19 @@
#include "posprinter.h" #include "posprinter.h"
#include <algorithm> #include <algorithm>
#include <boost/algorithm/string.hpp>
#include <exception> #include <exception>
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <boost/algorithm/string.hpp>
const std::string PosPrinter::Command::RESET = {0x1b, 0x40}; const std::string PosPrinter::Command::RESET = {0x1b, 0x40};
const std::string PosPrinter::Command::ENCODING = {'\x1b', '\x74', 16}; const std::string PosPrinter::Command::ENCODING = {'\x1b', '\x74', 16};
const std::string PosPrinter::Command::CENTERING = {'\x1b', '\x61', '\x01'}; 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_BIG = {'\x1b', '\x21', '\x10'};
const std::string PosPrinter::Command::FONT_SIZE_NORMAL = {'\x1b', '\x21', '\x00'}; 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}; const std::string PosPrinter::Command::FEED = {0x1b, 0x64, 0x03};
PosPrinter::PosPrinter() PosPrinter::PosPrinter()
@ -25,7 +26,7 @@ PosPrinter::PosPrinter()
} }
#if defined(_WIN32) #if defined(_WIN32)
libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK); // libusb_set_option(contextPtr_, LIBUSB_OPTION_USE_USBDK);
#endif #endif
libusb_device** devList; libusb_device** devList;
@ -126,7 +127,6 @@ void PosPrinter::printHeader()
void PosPrinter::printTest() void PosPrinter::printTest()
{ {
using namespace std::string_literals;
std::stringstream commandStream; std::stringstream commandStream;
commandStream << Command::ENCODING; commandStream << Command::ENCODING;
commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n" commandStream << "Der Drucker kann von KIMA2\nangesprochen werden.\n\n"
@ -134,4 +134,21 @@ void PosPrinter::printTest()
commandStream << Command::FEED; commandStream << Command::FEED;
printHeader(); printHeader();
write(commandStream.str()); 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());
} }

View file

@ -1,6 +1,8 @@
#ifndef POS_PRINTER_H #ifndef POS_PRINTER_H
#define POS_PRINTER_H #define POS_PRINTER_H
#include <sale.h>
#include <memory> #include <memory>
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
@ -21,14 +23,16 @@ class PosPrinter
void write(const std::string& text); void write(const std::string& text);
void printHeader(); void printHeader();
void printTest(); void printTest();
void printReceipt(Sale* sale);
struct Command { struct Command {
static const std::string RESET; static const std::string RESET;
static const std::string ENCODING; static const std::string ENCODING;
static const std::string CENTERING; 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_BIG;
static const std::string FONT_SIZE_NORMAL; static const std::string FONT_SIZE_NORMAL;
static const std::string LEFT_ALIGN;
static const std::string FEED; static const std::string FEED;
}; };