@ -1,5 +1,5 @@
/*
/*
* Copyright 2014 - 201 6 Markus Prasser
* Copyright 2014 - 201 8 Markus Prasser , Tobias Weiss
*
*
* This file is part of Labcontrol .
* This file is part of Labcontrol .
*
*
@ -24,57 +24,58 @@
# include "receipts_handler.h"
# include "receipts_handler.h"
# include "settings.h"
# include "settings.h"
extern std : : unique_ptr < lc : : Settings > settings ;
extern std : : unique_ptr < lc : : Settings > settings ;
lc : : ReceiptsHandler : : ReceiptsHandler ( const QString & argZTreeDataTargetPath ,
lc : : ReceiptsHandler : : ReceiptsHandler ( const QString & argZTreeDataTargetPath ,
bool argPrintReceiptsForLocalClients ,
bool argPrintReceiptsForLocalClients ,
const QString & argAnonymousReceiptsPlaceholder ,
const QString & argAnonymousReceiptsPlaceholder ,
const QString & argLatexHeaderName ,
const QString & argLatexHeaderName ,
QObject * argParent ) :
QObject * argParent ) :
QObject { argParent } ,
QObject { argParent } ,
anonymousReceiptsPlaceholder { argAnonymousReceiptsPlaceholder } ,
anonymousReceiptsPlaceholder { argAnonymousReceiptsPlaceholder } ,
dateString { QDateTime : : currentDateTime ( ) . toString ( " yyMMdd_hhmm " ) } ,
dateString { QDateTime : : currentDateTime ( ) . toString ( " yyMMdd_hhmm " ) } ,
expectedPaymentFileName { dateString + " .pay " } ,
expectedPaymentFileName { dateString + " .pay " } ,
expectedPaymentFilePath { argZTreeDataTargetPath + " / " + dateString + " .pay " } ,
expectedPaymentFilePath { argZTreeDataTargetPath + " / " + dateString + " .pay " } ,
latexHeaderName { argLatexHeaderName } ,
latexHeaderName { argLatexHeaderName } ,
paymentFile { expectedPaymentFilePath } ,
paymentFile { expectedPaymentFilePath } ,
printReceiptsForLocalClients { argPrintReceiptsForLocalClients } ,
printReceiptsForLocalClients { argPrintReceiptsForLocalClients } ,
timer { new QTimer { this } } ,
timer { new QTimer { this } } ,
zTreeDataTargetPath { argZTreeDataTargetPath }
zTreeDataTargetPath { argZTreeDataTargetPath }
{
{
qDebug ( ) < < " Expected payment file name is: " < < expectedPaymentFilePath ;
qDebug ( ) < < " Expected payment file name is: " < < expectedPaymentFilePath ;
// Create a QTimer regularly checking if the payment file was created and print it if so
// Create a QTimer regularly checking if the payment file was created and print it if so
connect ( timer , & QTimer : : timeout ,
connect ( timer , & QTimer : : timeout ,
this , & ReceiptsHandler : : PrintReceipts ) ;
this , & ReceiptsHandler : : PrintReceipts ) ;
timer - > start ( 2000 ) ;
timer - > start ( 2000 ) ;
}
}
lc : : ReceiptsHandler : : ReceiptsHandler ( const QString & argZTreeDataTargetPath ,
lc : : ReceiptsHandler : : ReceiptsHandler ( const QString & argZTreeDataTargetPath ,
bool argPrintReceiptsForLocalClients ,
bool argPrintReceiptsForLocalClients ,
const QString & argAnonymousReceiptsPlaceholder ,
const QString & argAnonymousReceiptsPlaceholder ,
const QString & argLatexHeaderName ,
const QString & argLatexHeaderName ,
const QString & argDateString , QObject * argParent ) :
const QString & argDateString , QObject * argParent ) :
QObject { argParent } ,
QObject { argParent } ,
anonymousReceiptsPlaceholder { argAnonymousReceiptsPlaceholder } ,
anonymousReceiptsPlaceholder { argAnonymousReceiptsPlaceholder } ,
dateString { argDateString } ,
dateString { argDateString } ,
expectedPaymentFileName { argDateString + " .pay " } ,
expectedPaymentFileName { argDateString + " .pay " } ,
expectedPaymentFilePath { argZTreeDataTargetPath + " / " + argDateString + " .pay " } ,
expectedPaymentFilePath { argZTreeDataTargetPath + " / " + argDateString + " .pay " } ,
latexHeaderName { argLatexHeaderName } ,
latexHeaderName { argLatexHeaderName } ,
paymentFile { expectedPaymentFilePath } ,
paymentFile { expectedPaymentFilePath } ,
printReceiptsForLocalClients { argPrintReceiptsForLocalClients } ,
printReceiptsForLocalClients { argPrintReceiptsForLocalClients } ,
zTreeDataTargetPath { argZTreeDataTargetPath }
zTreeDataTargetPath { argZTreeDataTargetPath }
{
{
qDebug ( ) < < " Expected payment file name is: " < < expectedPaymentFilePath ;
qDebug ( ) < < " Expected payment file name is: " < < expectedPaymentFilePath ;
PrintReceipts ( ) ;
PrintReceipts ( ) ;
}
}
void lc : : ReceiptsHandler : : PrintReceipts ( ) {
void lc : : ReceiptsHandler : : PrintReceipts ( )
{
// If the payment file exists, print it
// If the payment file exists, print it
if ( paymentFile . exists ( ) ) {
if ( paymentFile . exists ( ) ) {
qDebug ( ) < < " The payment file has been created and will be printed " ;
qDebug ( ) < < " The payment file has been created and will be printed " ;
if ( timer ) {
if ( timer ) {
timer - > stop ( ) ;
timer - > stop ( ) ;
}
}
@ -82,28 +83,30 @@ void lc::ReceiptsHandler::PrintReceipts() {
}
}
}
}
void lc : : ReceiptsHandler : : CreateReceiptsFromPaymentFile ( ) {
void lc : : ReceiptsHandler : : CreateReceiptsFromPaymentFile ( )
{
// Get the data needed for receipts creation from the payment file
// Get the data needed for receipts creation from the payment file
QVector < QString > * rawParticipantsData = nullptr ;
QVector < QString > * rawParticipantsData = nullptr ;
rawParticipantsData = GetParticipantsDataFromPaymentFile ( ) ;
rawParticipantsData = GetParticipantsDataFromPaymentFile ( ) ;
for ( int i = 0 ; i < rawParticipantsData - > size ( ) ; i + + ) {
for ( int i = 0 ; i < rawParticipantsData - > size ( ) ; i + + ) {
qDebug ( ) < < " Payment file line " < < QString : : number ( i ) < < " : \t " < < rawParticipantsData - > at ( i ) ;
qDebug ( ) < < " Payment file line " < < QString : : number ( i )
< < " : \t " < < rawParticipantsData - > at ( i ) ;
}
}
// Extract the data of the participant's whose receipts shall be printed
// Extract the data of the participant's whose receipts shall be printed
/* The tab separated fields in the payment file are:
/* The tab separated fields in the payment file are:
* SUBJECT COMPUTER INTERESTED NAME PROFIT SIGNATURE
* SUBJECT COMPUTER INTERESTED NAME PROFIT SIGNATURE
*/
*/
QVector < paymentEntry_t * > * participants = new QVector < paymentEntry_t * > ;
QVector < paymentEntry_t * > * participants = new QVector < paymentEntry_t * > ;
double overall_payoff = 0.0 ;
double overall_payoff = 0.0 ;
for ( QVector < QString > : : iterator it = rawParticipantsData - > begin ( ) ; it ! = rawParticipantsData - > end ( ) - 1 ; + + it ) {
for ( QVector < QString > : : iterator it = rawParticipantsData - > begin ( ) ;
it ! = rawParticipantsData - > end ( ) - 1 ; + + it ) {
// Split the lines containing the participants' data into their inidivual parts
// Split the lines containing the participants' data into their inidivual parts
QStringList temp_participant_data = it - > split ( ' \t ' , QString : : KeepEmptyParts ) ;
QStringList temp_participant_data = it - > split ( ' \t ' , QString : : KeepEmptyParts ) ;
qDebug ( ) < < temp_participant_data . join ( " - " ) ;
qDebug ( ) < < temp_participant_data . join ( " - " ) ;
if ( ! printReceiptsForLocalClients & & temp_participant_data . at ( 3 ) . contains ( " local " ) ) {
if ( ! printReceiptsForLocalClients & & temp_participant_data . at ( 3 ) . contains ( " local " ) ) {
qDebug ( ) < < " Receipt for local client " < < temp_participant_data . at ( 1 ) < < " will not be printed. " ;
qDebug ( ) < < " Receipt for local client " < < temp_participant_data . at ( 1 ) < < " will not be printed. " ;
}
} else {
else {
// Create a new struct instance for participant data and fill it
// Create a new struct instance for participant data and fill it
paymentEntry_t * participant = new paymentEntry_t ;
paymentEntry_t * participant = new paymentEntry_t ;
participant - > computer = temp_participant_data . at ( 1 ) ;
participant - > computer = temp_participant_data . at ( 1 ) ;
@ -117,14 +120,14 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
rawParticipantsData = nullptr ;
rawParticipantsData = nullptr ;
// Make receipts overview anonymous if requested (at this stage just names are removed, so that the overview still containts the client names
// Make receipts overview anonymous if requested (at this stage just names are removed, so that the overview still containts the client names
if ( ! anonymousReceiptsPlaceholder . isEmpty ( ) ) {
if ( ! anonymousReceiptsPlaceholder . isEmpty ( ) ) {
MakeReceiptsAnonymous ( participants , false ) ;
MakeReceiptsAnonymous ( participants , false ) ;
}
}
// Load the LaTeX header
// Load the LaTeX header
QString * latexText = LoadLatexHeader ( ) ;
QString * latexText = LoadLatexHeader ( ) ;
if ( latexText = = nullptr ) {
if ( latexText = = nullptr ) {
for ( auto s : * participants ) {
for ( auto s : * participants ) {
delete s ;
delete s ;
}
}
delete participants ;
delete participants ;
@ -133,12 +136,13 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
}
}
// Write the comprehension table
// Write the comprehension table
latexText - > append ( " \n \\ COMPREHENSION{ \n " ) ;
latexText - > append ( " \n \\ COMPREHENSION{ \n " ) ;
unsigned short int zeile = 0 ;
unsigned short int zeile = 0 ;
for ( auto s : * participants ) {
for ( auto s : * participants ) {
latexText - > append ( expectedPaymentFileName + " & " + s - > computer + " & " + s - > name + " & " + QString : : number ( s - > payoff , ' f ' , 2 ) + " \\ EUR \\ \\ \n " ) ;
latexText - > append ( expectedPaymentFileName + " & " + s - > computer + " & " + s - > name + " & " +
if ( zeile % 2 = = 0 ) {
QString : : number ( s - > payoff , ' f ' , 2 ) + " \\ EUR \\ \\ \n " ) ;
latexText - > append ( " \\ rowcolor[gray]{0.9} \n " ) ;
if ( zeile % 2 = = 0 ) {
latexText - > append ( " \\ rowcolor[gray]{0.9} \n " ) ;
}
}
+ + zeile ;
+ + zeile ;
}
}
@ -146,20 +150,21 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
// MISSING: Appending show up entries to the overview
// MISSING: Appending show up entries to the overview
// Make also the clients on the receipts anonymous. This is done as second step, so that the beforehand created overview still contains the clients
// Make also the clients on the receipts anonymous. This is done as second step, so that the beforehand created overview still contains the clients
if ( ! anonymousReceiptsPlaceholder . isEmpty ( ) ) {
if ( ! anonymousReceiptsPlaceholder . isEmpty ( ) ) {
MakeReceiptsAnonymous ( participants , true ) ;
MakeReceiptsAnonymous ( participants , true ) ;
}
}
// Add the LaTeX middle sequence
// Add the LaTeX middle sequence
latexText - > append ( " }{ " + QString : : number ( overall_payoff , ' f ' , 2 ) + " } \n \n %%Einzelquittungen \n " ) ;
latexText - > append ( " }{ " + QString : : number ( overall_payoff , ' f ' , 2 ) + " } \n \n %%Einzelquittungen \n " ) ;
// Write the single receipts
// Write the single receipts
for ( auto s : * participants ) {
for ( auto s : * participants ) {
if ( s - > payoff > = 0 ) {
if ( s - > payoff > = 0 ) {
latexText - > append ( " \\ GAINRECEIPT{ " + expectedPaymentFileName + " }{ " + s - > computer + " }{ " + s - > name + " }{ " + QString : : number ( s - > payoff , ' f ' , 2 ) + " } \n " ) ;
latexText - > append ( " \\ GAINRECEIPT{ " + expectedPaymentFileName + " }{ " + s - > computer + " }{ " + s - > name
}
+ " }{ " + QString : : number ( s - > payoff , ' f ' , 2 ) + " } \n " ) ;
else {
} else {
latexText - > append ( " \\ LOSSRECEIPT{ " + expectedPaymentFileName + " }{ " + s - > computer + " }{ " + s - > name + " }{ " + QString : : number ( s - > payoff , ' f ' , 2 ) + " } \n " ) ;
latexText - > append ( " \\ LOSSRECEIPT{ " + expectedPaymentFileName + " }{ " + s - > computer + " }{ " + s - > name
+ " }{ " + QString : : number ( s - > payoff , ' f ' , 2 ) + " } \n " ) ;
}
}
delete s ;
delete s ;
}
}
@ -172,44 +177,47 @@ void lc::ReceiptsHandler::CreateReceiptsFromPaymentFile() {
qDebug ( ) < < * latexText ;
qDebug ( ) < < * latexText ;
// Create the tex file
// Create the tex file
QFile * texFile = new QFile { zTreeDataTargetPath + " / " + dateString + " .tex " } ;
QFile * texFile = new QFile { zTreeDataTargetPath + " / " + dateString + " .tex " } ;
qDebug ( ) < < " Tex file " < < texFile - > fileName ( ) < < " will be created for receipts printing. " ;
qDebug ( ) < < " Tex file " < < texFile - > fileName ( ) < < " will be created for receipts printing. " ;
// Clean up any already existing files
// Clean up any already existing files
if ( texFile - > exists ( ) ) {
if ( texFile - > exists ( ) ) {
if ( ! texFile - > remove ( ) ) {
if ( ! texFile - > remove ( ) ) {
QMessageBox messageBox ( QMessageBox : : Critical , " Tex file removing failed " , " There already exists a tex file at ' " + texFile - > fileName ( )
QMessageBox messageBox ( QMessageBox : : Critical , " Tex file removing failed " ,
+ " ' which cannot be removed. The creation of the receipts printout may fail. " , QMessageBox : : Ok ) ;
" There already exists a tex file at ' " + texFile - > fileName ( )
+ " ' which cannot be removed. The creation of the receipts printout may fail. " , QMessageBox : : Ok ) ;
messageBox . exec ( ) ;
messageBox . exec ( ) ;
}
}
}
}
// Create a new file
// Create a new file
if ( ! texFile - > open ( QIODevice : : Text | QIODevice : : WriteOnly ) ) {
if ( ! texFile - > open ( QIODevice : : Text | QIODevice : : WriteOnly ) ) {
QMessageBox messageBox ( QMessageBox : : Critical , " Tex file creation failed " , " The creation of the tex file for receipts printing at ' " + texFile - > fileName ( )
QMessageBox messageBox ( QMessageBox : : Critical , " Tex file creation failed " ,
+ " ' failed. Receipts printing will not work. " , QMessageBox : : Ok ) ;
" The creation of the tex file for receipts printing at ' " + texFile - > fileName ( )
+ " ' failed. Receipts printing will not work. " , QMessageBox : : Ok ) ;
messageBox . exec ( ) ;
messageBox . exec ( ) ;
return ;
return ;
}
}
// Open a QTextStream to write to the file
// Open a QTextStream to write to the file
QTextStream out ( texFile ) ;
QTextStream out ( texFile ) ;
out < < * latexText ;
out < < * latexText ;
delete latexText ;
delete latexText ;
latexText = nullptr ;
latexText = nullptr ;
receiptsPrinter = new ReceiptsPrinter { dateString , zTreeDataTargetPath , this } ;
receiptsPrinter = new ReceiptsPrinter { dateString , zTreeDataTargetPath , this } ;
receiptsPrinter - > start ( ) ;
receiptsPrinter - > start ( ) ;
connect ( receiptsPrinter , & ReceiptsPrinter : : PrintingFinished ,
connect ( receiptsPrinter , & ReceiptsPrinter : : PrintingFinished ,
this , & ReceiptsHandler : : DeleteReceiptsPrinterInstance ) ;
this , & ReceiptsHandler : : DeleteReceiptsPrinterInstance ) ;
connect ( receiptsPrinter , & ReceiptsPrinter : : ErrorOccurred ,
connect ( receiptsPrinter , & ReceiptsPrinter : : ErrorOccurred ,
this , & ReceiptsHandler : : DisplayMessageBox ) ;
this , & ReceiptsHandler : : DisplayMessageBox ) ;
// Clean up
// Clean up
texFile - > close ( ) ;
texFile - > close ( ) ;
delete texFile ;
delete texFile ;
}
}
void lc : : ReceiptsHandler : : DeleteReceiptsPrinterInstance ( ) {
void lc : : ReceiptsHandler : : DeleteReceiptsPrinterInstance ( )
{
receiptsPrinter - > quit ( ) ;
receiptsPrinter - > quit ( ) ;
receiptsPrinter - > wait ( ) ;
receiptsPrinter - > wait ( ) ;
receiptsPrinter - > deleteLater ( ) ;
receiptsPrinter - > deleteLater ( ) ;
@ -219,33 +227,37 @@ void lc::ReceiptsHandler::DeleteReceiptsPrinterInstance() {
emit PrintingFinished ( ) ;
emit PrintingFinished ( ) ;
}
}
void lc : : ReceiptsHandler : : DisplayMessageBox ( QString * argErrorMessage , QString * argHeading ) {
void lc : : ReceiptsHandler : : DisplayMessageBox ( QString * argErrorMessage ,
QMessageBox messageBox ( QMessageBox : : Warning , * argHeading , * argErrorMessage , QMessageBox : : Ok ) ;
QString * argHeading )
{
QMessageBox messageBox ( QMessageBox : : Warning , * argHeading ,
* argErrorMessage , QMessageBox : : Ok ) ;
delete argHeading ;
delete argHeading ;
delete argErrorMessage ;
delete argErrorMessage ;
messageBox . exec ( ) ;
messageBox . exec ( ) ;
}
}
QVector < QString > * lc : : ReceiptsHandler : : GetParticipantsDataFromPaymentFile ( ) {
QVector < QString > * lc : : ReceiptsHandler : : GetParticipantsDataFromPaymentFile ( )
{
// Create the vector to store the single lines of the file
// Create the vector to store the single lines of the file
QVector < QString > * participantsData = new QVector < QString > ;
QVector < QString > * participantsData = new QVector < QString > ;
// Open the payment file for reading and create a QTextStream
// Open the payment file for reading and create a QTextStream
paymentFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
paymentFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ;
QTextStream in ( & paymentFile ) ;
QTextStream in ( & paymentFile ) ;
in . setCodec ( " ISO 8859-1 " ) ;
in . setCodec ( " ISO 8859-1 " ) ;
// Read the file line by line and store them in the vector
// Read the file line by line and store them in the vector
while ( true ) {
while ( true ) {
QString line = in . readLine ( ) ;
QString line = in . readLine ( ) ;
if ( line . isNull ( ) ) {
if ( line . isNull ( ) ) {
break ;
break ;
}
}
participantsData - > append ( line ) ;
participantsData - > append ( line ) ;
}
}
// Remove the first line, since it is not needed
// Remove the first line, since it is not needed
participantsData - > erase ( participantsData - > begin ( ) ) ;
participantsData - > erase ( participantsData - > begin ( ) ) ;
// Close the file afterwards
// Close the file afterwards
paymentFile . close ( ) ;
paymentFile . close ( ) ;
@ -253,38 +265,44 @@ QVector<QString> *lc::ReceiptsHandler::GetParticipantsDataFromPaymentFile() {
return participantsData ;
return participantsData ;
}
}
QString * lc : : ReceiptsHandler : : LoadLatexHeader ( ) {
QString * lc : : ReceiptsHandler : : LoadLatexHeader ( )
{
// Prepare all facilities to read the latex header file
// Prepare all facilities to read the latex header file
QFile latexHeaderFile ( settings - > lcDataDir + " / " + latexHeaderName + " _header.tex " ) ;
QFile latexHeaderFile ( settings - > lcDataDir + " / " + latexHeaderName + " _header.tex " ) ;
if ( ! latexHeaderFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
if ( ! latexHeaderFile . open ( QIODevice : : ReadOnly | QIODevice : : Text ) ) {
QMessageBox messageBox { QMessageBox : : Critical , tr ( " LaTeX header could not be loaded " ) ,
QMessageBox messageBox { QMessageBox : : Critical ,
tr ( " The LaTeX header at '%1/%2_header.tex' could not be loaded. Receipts printing will not work. " )
tr ( " LaTeX header could not be loaded " ) ,
. arg ( settings - > lcDataDir ) . arg ( latexHeaderName ) , QMessageBox : : Ok } ;
tr ( " The LaTeX header at '%1/%2_header.tex' could "
" not be loaded. Receipts printing will not work. " )
. arg ( settings - > lcDataDir ) . arg ( latexHeaderName ) , QMessageBox : : Ok } ;
messageBox . exec ( ) ;
messageBox . exec ( ) ;
return nullptr ;
return nullptr ;
}
}
QTextStream in ( & latexHeaderFile ) ;
QTextStream in ( & latexHeaderFile ) ;
QString * header = nullptr ;
QString * header = nullptr ;
header = new QString ( in . readAll ( ) ) ;
header = new QString ( in . readAll ( ) ) ;
latexHeaderFile . close ( ) ;
latexHeaderFile . close ( ) ;
return header ;
return header ;
}
}
void lc : : ReceiptsHandler : : MakeReceiptsAnonymous ( QVector < paymentEntry_t * > * argDataVector , bool argAlsoAnonymizeClients ) {
void lc : : ReceiptsHandler : : MakeReceiptsAnonymous ( QVector < paymentEntry_t * > * argDataVector ,
if ( ! argAlsoAnonymizeClients ) {
bool argAlsoAnonymizeClients )
{
if ( ! argAlsoAnonymizeClients ) {
qDebug ( ) < < " Names are made anonymous " ;
qDebug ( ) < < " Names are made anonymous " ;
for ( QVector < paymentEntry_t * > : : iterator it = argDataVector - > begin ( ) ; it ! = argDataVector - > end ( ) ; + + it ) {
for ( QVector < paymentEntry_t * > : : iterator it = argDataVector - > begin ( ) ; it ! = argDataVector - > end ( ) ;
( * it ) - > name = anonymousReceiptsPlaceholder ;
+ + it ) {
( * it ) - > name = anonymousReceiptsPlaceholder ;
}
} else {
qDebug ( ) < < " Clients and names are made anonymous " ;
for ( QVector < paymentEntry_t * > : : iterator it = argDataVector - > begin ( ) ; it ! = argDataVector - > end ( ) ;
+ + it ) {
( * it ) - > name = anonymousReceiptsPlaceholder ;
( * it ) - > computer = " \\ hspace{1cm} " ;
}
}
}
else {
qDebug ( ) < < " Clients and names are made anonymous " ;
for ( QVector < paymentEntry_t * > : : iterator it = argDataVector - > begin ( ) ; it ! = argDataVector - > end ( ) ; + + it ) {
( * it ) - > name = anonymousReceiptsPlaceholder ;
( * it ) - > computer = " \\ hspace{1cm} " ;
}
}
}
}
}