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::getTimestamp() { return timestamp_; }
std::string Sale::getTimestamp() const { return 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);
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

View File

@ -10,6 +10,7 @@
#include "settingsdialog.h"
#include <excelreader.h>
#include <posprinter.h>
#include <exception>
#include <regex>
@ -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)

View File

@ -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();

View File

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

View File

@ -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})

View File

@ -1,18 +1,19 @@
#include "posprinter.h"
#include <algorithm>
#include <boost/algorithm/string.hpp>
#include <exception>
#include <iostream>
#include <sstream>
#include <string>
#include <boost/algorithm/string.hpp>
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());
}

View File

@ -1,6 +1,8 @@
#ifndef POS_PRINTER_H
#define POS_PRINTER_H
#include <sale.h>
#include <memory>
#include <libusb-1.0/libusb.h>
@ -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;
};