use new pos printer settings

This commit is contained in:
Martin Brodbeck 2018-08-15 10:51:57 +02:00
parent dc74c59287
commit f0078647d4
9 changed files with 144 additions and 47 deletions

View File

@ -25,10 +25,10 @@ set(CORE_SOURCES
add_library(core STATIC ${CORE_SOURCES}) add_library(core STATIC ${CORE_SOURCES})
if (WIN32) if (WIN32)
target_link_libraries(core Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARY} ${JSONCPP_LIBRARY}) target_link_libraries(core printer Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARY} ${JSONCPP_LIBRARY})
target_link_libraries(core bcrypt) target_link_libraries(core bcrypt)
else() else()
target_link_libraries(core Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARIES} ${JSONCPP_LIBRARIES}) target_link_libraries(core printer Boost::boost Boost::date_time sqlite3 ${XLNT_LIBRARIES} ${JSONCPP_LIBRARIES})
endif() endif()
target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}) target_include_directories(core PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})

View File

@ -1,8 +1,8 @@
#include "utils.h" #include "utils.h"
#include <codecvt>
#include <iomanip> #include <iomanip>
#include <numeric> #include <numeric>
#include <codecvt>
std::string formatCentAsEuroString(const int cent, int width) std::string formatCentAsEuroString(const int cent, int width)
{ {
@ -21,10 +21,36 @@ std::string formatCentAsEuroString(const int cent, int width)
return currStream.str(); return currStream.str();
} }
std::optional<PrinterDevice> convertToPosPrinterDevice(const std::string& device,
const std::string& endpoint)
{
if (device.empty()) {
return std::nullopt;
}
PrinterDevice printerDevice;
std::string delimiter = ":";
try {
printerDevice.idVendor = std::stoi(device.substr(0, device.find(delimiter)), 0, 16);
printerDevice.idProduct = std::stoi(device.substr(device.find(delimiter) + 1), 0, 16);
if (endpoint.empty()) {
printerDevice.endpoint = 0x03;
} else {
printerDevice.endpoint = std::stoi(endpoint, 0, 16);
}
} catch (std::exception& ex) {
throw ex;
}
return printerDevice;
}
#ifdef _WIN32 #ifdef _WIN32
std::string convertFromUtf16ToUtf8(std::u16string& utf16String) std::string convertFromUtf16ToUtf8(std::u16string& utf16String)
{ {
std::string u8_conv = std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(utf16String); std::string u8_conv =
return u8_conv; std::wstring_convert<std::codecvt_utf8_utf16<char16_t>, char16_t>{}.to_bytes(utf16String);
return u8_conv;
} }
#endif #endif

View File

@ -1,10 +1,15 @@
#ifndef UTILS_H #ifndef UTILS_H
#define UTILS_H #define UTILS_H
#include <string> #include "posprinter.h"
#include <locale> #include <locale>
#include <optional>
#include <string>
std::string formatCentAsEuroString(const int cent, int width = 10); std::string formatCentAsEuroString(const int cent, int width = 10);
std::optional<PrinterDevice> convertToPosPrinterDevice(const std::string& vendor,
const std::string& endpoint);
#ifdef __WIN32 #ifdef __WIN32
std::string convertFromUtf16ToUtf8(std::u16string& utf16String); std::string convertFromUtf16ToUtf8(std::u16string& utf16String);

View File

@ -166,6 +166,10 @@ void MainWindow::setSaleModel()
ui_.salesView->setModel(new SaleModel(getMarketplace(), ui_.salesView)); ui_.salesView->setModel(new SaleModel(getMarketplace(), ui_.salesView));
ui_.salesView->setColumnHidden(2, true); ui_.salesView->setColumnHidden(2, true);
ui_.salesView->resizeColumnToContents(0); ui_.salesView->resizeColumnToContents(0);
ui_.printSaleReceiptButton->setEnabled(false);
ui_.cancelSaleButton->setEnabled(false);
connect(static_cast<BasketModel*>(ui_.basketView->model()), &BasketModel::basketDataChanged, connect(static_cast<BasketModel*>(ui_.basketView->model()), &BasketModel::basketDataChanged,
static_cast<SaleModel*>(ui_.salesView->model()), &SaleModel::onBasketDataChanged); static_cast<SaleModel*>(ui_.salesView->model()), &SaleModel::onBasketDataChanged);
connect(ui_.salesView->selectionModel(), &QItemSelectionModel::selectionChanged, this, connect(ui_.salesView->selectionModel(), &QItemSelectionModel::selectionChanged, this,
@ -349,11 +353,25 @@ void MainWindow::onPrintSaleReceiptButtonClicked([[maybe_unused]] bool checked)
if (selModel->hasSelection() == false) if (selModel->hasSelection() == false)
return; return;
QSettings settings{};
QString posPrinterDevice = settings.value("global/posPrinterDevice", "").toString();
QString posPrinterEndpoint = settings.value("global/posPrinterEndpoint", "").toString();
auto indexes = selModel->selectedRows(); auto indexes = selModel->selectedRows();
auto& sale = marketplace_->getSales().at(indexes[0].row()); auto& sale = marketplace_->getSales().at(indexes[0].row());
PosPrinter printer;
if (printer.isValid()) auto printerDevice = convertToPosPrinterDevice(posPrinterDevice.toStdString(), posPrinterEndpoint.toStdString());
printer.printSaleReceipt(sale.get());
std::unique_ptr<PosPrinter> printer;
if (printerDevice) {
printer = std::make_unique<PosPrinter>(*printerDevice);
} else {
printer = std::make_unique<PosPrinter>();
}
if (printer->isValid())
printer->printSaleReceipt(sale.get());
} }
void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked) void MainWindow::onCancelAllArticlesButtonClicked([[maybe_unused]] bool checked)

View File

@ -3,6 +3,7 @@
#include "mainwindow.h" #include "mainwindow.h"
#include <posprinter.h> #include <posprinter.h>
#include <utils.h>
#include <QFileDialog> #include <QFileDialog>
#include <QFontDatabase> #include <QFontDatabase>
@ -90,7 +91,8 @@ void ReportDialog::onPrintReportButtonClicked()
.arg("Auszahlung\n", -11); .arg("Auszahlung\n", -11);
content.append( content.append(
"---------------------------------------------------------------------------\n"); "---------------------------------------------------------------------------\n");
for (unsigned int j = 0; j < ENTRIES_PER_PAGE && (i - 1) * ENTRIES_PER_PAGE + j < sellers.size(); ++j) { for (unsigned int j = 0;
j < ENTRIES_PER_PAGE && (i - 1) * ENTRIES_PER_PAGE + j < sellers.size(); ++j) {
int idx = (i - 1) * ENTRIES_PER_PAGE + j; int idx = (i - 1) * ENTRIES_PER_PAGE + j;
if (sellers.at(idx)->getUuidAsString() == "11111111-1111-1111-1111-111111111111") { if (sellers.at(idx)->getUuidAsString() == "11111111-1111-1111-1111-111111111111") {
continue; continue;
@ -168,6 +170,8 @@ void ReportDialog::onPrintSellerReceiptButtonClicked()
QSettings settings; QSettings settings;
int feeInPercent = settings.value("global/feeInPercent").toInt(); int feeInPercent = settings.value("global/feeInPercent").toInt();
int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt();
QString posPrinterDevice = settings.value("global/posPrinterDevice", "").toString();
QString posPrinterEndpoint = settings.value("global/posPrinterEndpoint", "").toString();
auto selModel = ui_.reportView->selectionModel(); auto selModel = ui_.reportView->selectionModel();
if (selModel->hasSelection() == false) if (selModel->hasSelection() == false)
@ -175,9 +179,20 @@ void ReportDialog::onPrintSellerReceiptButtonClicked()
auto indexes = selModel->selectedRows(); auto indexes = selModel->selectedRows();
auto& seller = market_->getSellers().at(indexes[0].row()); auto& seller = market_->getSellers().at(indexes[0].row());
PosPrinter printer;
if (printer.isValid()) auto printerDevice =
printer.printSellerReceipt(seller.get(), feeInPercent, maxFeeInEuro * 100); convertToPosPrinterDevice(posPrinterDevice.toStdString(), posPrinterEndpoint.toStdString());
std::unique_ptr<PosPrinter> printer;
if (printerDevice) {
printer = std::make_unique<PosPrinter>(*printerDevice);
} else {
printer = std::make_unique<PosPrinter>();
}
if (printer->isValid())
printer->printSellerReceipt(seller.get(), feeInPercent, maxFeeInEuro * 100);
} }
void ReportDialog::onReportViewSelectionChanged(const QItemSelection& selected, void ReportDialog::onReportViewSelectionChanged(const QItemSelection& selected,

View File

@ -4,6 +4,7 @@
#include <database.h> #include <database.h>
#include <posprinter.h> #include <posprinter.h>
#include <utils.h>
#include <exception> #include <exception>
#include <stdexcept> #include <stdexcept>
@ -18,6 +19,7 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
QSettings settings{}; QSettings settings{};
int cashPointNo = settings.value("global/cashPointNo").toInt(); int cashPointNo = settings.value("global/cashPointNo").toInt();
QString posPrinterDevice = settings.value("global/posPrinterDevice").toString(); QString posPrinterDevice = settings.value("global/posPrinterDevice").toString();
QString posPrinterEndpoint = settings.value("global/posPrinterEndpoint").toString();
int feeInPercent = settings.value("global/feeInPercent").toInt(); int feeInPercent = settings.value("global/feeInPercent").toInt();
int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt(); int maxFeeInEuro = settings.value("global/maxFeeInEuro").toInt();
@ -26,6 +28,7 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
ui_.cashPointNoSpinBox->setValue(cashPointNo); ui_.cashPointNoSpinBox->setValue(cashPointNo);
ui_.posPrinterDeviceEdit->setText(posPrinterDevice); ui_.posPrinterDeviceEdit->setText(posPrinterDevice);
ui_.posPrinterEndpointEdit->setText(posPrinterEndpoint);
ui_.feePercentSpinBox->setValue(feeInPercent); ui_.feePercentSpinBox->setValue(feeInPercent);
ui_.maxFeeSpinBox->setValue(maxFeeInEuro); ui_.maxFeeSpinBox->setValue(maxFeeInEuro);
@ -37,26 +40,27 @@ SettingsDialog::SettingsDialog(QWidget* parent, Qt::WindowFlags f) : QDialog(par
printer.printTest(); printer.printTest();
} else { } else {
std::string posPrinterDeviceString = ui_.posPrinterDeviceEdit->text().toStdString(); std::string posPrinterDeviceString = ui_.posPrinterDeviceEdit->text().toStdString();
std::string delimiter = ":"; std::string posPrinterEndpointString =
PrinterDevice printerDevice; ui_.posPrinterEndpointEdit->text().toStdString();
try { try {
printerDevice.idVendor = std::stoi( auto printerDevice =
posPrinterDeviceString.substr(0, posPrinterDeviceString.find(delimiter)), 0, convertToPosPrinterDevice(posPrinterDeviceString, posPrinterEndpointString);
16);
printerDevice.idProduct = std::stoi( if (printerDevice) {
posPrinterDeviceString.substr(posPrinterDeviceString.find(delimiter) + 1), PosPrinter printer(*printerDevice);
0, 16); printer.printTest();
}
} catch (std::exception&) { } catch (std::exception&) {
QMessageBox(QMessageBox::Icon::Warning, "Falsche Eingabe", QMessageBox(QMessageBox::Icon::Warning, "Falsche Eingabe",
QString("Eingabeformat für den Bondrucker (hexadezimale IDs): " QString("Eingabeformat für Device (hexadezimale IDs): "
"<VendorID>:<ProductID>\nBeispiel: 0416:5011"), "<VendorID>:<ProductID>\nBeispiel: 0416:5011\n "
"Eingabeformat für Endpoint: Hexadezimale Adresse"),
QMessageBox::StandardButton::Ok, this) QMessageBox::StandardButton::Ok, this)
.exec(); .exec();
return; return;
} }
PosPrinter printer(printerDevice);
printer.printTest();
} }
} catch (std::runtime_error& err) { } catch (std::runtime_error& err) {
QMessageBox(QMessageBox::Icon::Warning, "Bondrucker Fehler", QMessageBox(QMessageBox::Icon::Warning, "Bondrucker Fehler",
@ -75,7 +79,8 @@ void SettingsDialog::accept()
int oldCashPointNo = settings.value("global/cashPointNo").toInt(); int oldCashPointNo = settings.value("global/cashPointNo").toInt();
int newCashPointNo = ui_.cashPointNoSpinBox->value(); int newCashPointNo = ui_.cashPointNoSpinBox->value();
settings.setValue("global/posPrinterDevice", ui_.posPrinterDeviceEdit->text()); settings.setValue("global/posPrinterDevice", ui_.posPrinterDeviceEdit->text().trimmed());
settings.setValue("global/posPrinterEndpoint", ui_.posPrinterEndpointEdit->text().trimmed());
settings.setValue("global/feeInPercent", ui_.feePercentSpinBox->value()); settings.setValue("global/feeInPercent", ui_.feePercentSpinBox->value());
settings.setValue("global/maxFeeInEuro", ui_.maxFeeSpinBox->value()); settings.setValue("global/maxFeeInEuro", ui_.maxFeeSpinBox->value());

View File

@ -6,14 +6,17 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>400</width> <width>392</width>
<height>203</height> <height>242</height>
</rect> </rect>
</property> </property>
<property name="windowTitle"> <property name="windowTitle">
<string>Einstellungen</string> <string>Einstellungen</string>
</property> </property>
<layout class="QGridLayout" name="gridLayout"> <layout class="QGridLayout" name="gridLayout">
<item row="0" column="1">
<widget class="QSpinBox" name="cashPointNoSpinBox"/>
</item>
<item row="0" column="0"> <item row="0" column="0">
<widget class="QLabel" name="label"> <widget class="QLabel" name="label">
<property name="text"> <property name="text">
@ -21,51 +24,65 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="0" column="1">
<widget class="QSpinBox" name="cashPointNoSpinBox"/>
</item>
<item row="1" column="0"> <item row="1" column="0">
<widget class="QLabel" name="label_2"> <widget class="QLabel" name="label_2">
<property name="text"> <property name="text">
<string>Bondrucker Gerätedatei:</string> <string>Bondrucker:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="1" column="1"> <item row="1" column="1">
<widget class="QLineEdit" name="posPrinterDeviceEdit"> <widget class="QLineEdit" name="posPrinterDeviceEdit">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="toolTip"> <property name="toolTip">
<string>&lt;idVendor&gt;:&lt;idProduct&gt;</string> <string>&lt;idVendor&gt;:&lt;idProduct&gt;</string>
</property> </property>
<property name="placeholderText">
<string>VendorId:ProductId</string>
</property>
</widget> </widget>
</item> </item>
<item row="1" column="2"> <item row="1" column="3">
<widget class="QPushButton" name="testPosPrinterButton"> <widget class="QPushButton" name="testPosPrinterButton">
<property name="text"> <property name="text">
<string>Testen</string> <string>Testen</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="0"> <item row="3" column="0">
<widget class="QLabel" name="label_3"> <widget class="QLabel" name="label_3">
<property name="text"> <property name="text">
<string>Gebühr in %:</string> <string>Gebühr in %:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1"> <item row="3" column="1">
<widget class="QSpinBox" name="feePercentSpinBox"/> <widget class="QSpinBox" name="feePercentSpinBox">
<property name="value">
<number>20</number>
</property>
</widget>
</item> </item>
<item row="3" column="0"> <item row="4" column="0">
<widget class="QLabel" name="label_4"> <widget class="QLabel" name="label_4">
<property name="text"> <property name="text">
<string>max. Gebühr in €:</string> <string>max. Gebühr in €:</string>
</property> </property>
</widget> </widget>
</item> </item>
<item row="3" column="1"> <item row="4" column="1">
<widget class="QSpinBox" name="maxFeeSpinBox"/> <widget class="QSpinBox" name="maxFeeSpinBox">
<property name="value">
<number>50</number>
</property>
</widget>
</item> </item>
<item row="4" column="0" colspan="3"> <item row="5" column="0" colspan="4">
<widget class="QDialogButtonBox" name="buttonBox"> <widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
@ -75,6 +92,13 @@
</property> </property>
</widget> </widget>
</item> </item>
<item row="2" column="1">
<widget class="QLineEdit" name="posPrinterEndpointEdit">
<property name="placeholderText">
<string>Endpoint Address</string>
</property>
</widget>
</item>
</layout> </layout>
</widget> </widget>
<resources/> <resources/>

View File

@ -46,8 +46,9 @@ PosPrinter::PosPrinter(const PrinterDevice& printerDevice) : printerDevice_(prin
if (printerDevice_.idVendor == 0) { if (printerDevice_.idVendor == 0) {
for (const auto& supported : supportedPrinters_.models) { for (const auto& supported : supportedPrinters_.models) {
if (desc.idVendor == supported.first && desc.idProduct == supported.second) { if (desc.idVendor == std::get<0>(supported) && desc.idProduct == std::get<1>(supported)) {
numDevice = i; numDevice = i;
printerEndpoint_ = std::get<2>(supported);
break; break;
} }
} }
@ -55,6 +56,7 @@ PosPrinter::PosPrinter(const PrinterDevice& printerDevice) : printerDevice_(prin
if (desc.idVendor == printerDevice_.idVendor && if (desc.idVendor == printerDevice_.idVendor &&
desc.idProduct == printerDevice_.idProduct) { desc.idProduct == printerDevice_.idProduct) {
numDevice = i; numDevice = i;
printerEndpoint_ = printerDevice.endpoint;
break; break;
} }
} }
@ -122,7 +124,7 @@ void PosPrinter::write(const std::string& text)
int length = text_escaped.length(); int length = text_escaped.length();
int actual{0}; int actual{0};
int retValue = int retValue =
libusb_bulk_transfer(devicePtr_, (0x03 | LIBUSB_ENDPOINT_OUT), libusb_bulk_transfer(devicePtr_, (printerEndpoint_ | LIBUSB_ENDPOINT_OUT),
(unsigned char*)text_escaped.c_str(), length, &actual, 10000); (unsigned char*)text_escaped.c_str(), length, &actual, 10000);
if (retValue != 0 || actual != length) if (retValue != 0 || actual != length)
std::cerr << "Write Error" << std::endl; std::cerr << "Write Error" << std::endl;

View File

@ -9,16 +9,17 @@
#include <libusb-1.0/libusb.h> #include <libusb-1.0/libusb.h>
struct SupportedPrinters { struct SupportedPrinters {
std::array<std::pair<int, int>, 2> models{ std::array<std::tuple<int, int, int>, 2> models{
// {Vendor ID, Model ID} // {Vendor ID, Model ID, Endpoint}
std::make_pair(0x0456, 0x0808), std::make_tuple(0x0456, 0x0808, 0x03),
std::make_pair(0x0416, 0x5011), std::tuple(0x0416, 0x5011, 0x03),
}; };
}; };
struct PrinterDevice { struct PrinterDevice {
int idVendor{}; int idVendor{};
int idProduct{}; int idProduct{};
int endpoint{};
}; };
class PosPrinter class PosPrinter
@ -50,6 +51,7 @@ class PosPrinter
libusb_device_handle* devicePtr_{}; libusb_device_handle* devicePtr_{};
SupportedPrinters supportedPrinters_; SupportedPrinters supportedPrinters_;
PrinterDevice printerDevice_{}; PrinterDevice printerDevice_{};
int printerEndpoint_{0x03};
}; };
#endif #endif