/*************************************************************************** libMiniCash Copyright © 2013-2022 christoph holzheuer c.holzheuer@sourceworx.org This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version. ***************************************************************************/ #include #include #include #include using namespace std; /** * @brief Macht aus int x den String 000x zum schönaussehen. * @param count der Wert * @param len die voranzustellenden Nullen * * Macht aus int x den String 000x zum schönaussehen. * */ QString MCSalesModel::formatInt(int count, int len ) { QString result( "%0" ); result = result.arg( count, len, 10, QChar( '0' ) ); return result; } /** * @brief Formatiert einen double als String im Währungsformat: 2.3 -> 2,30 € * * Formatiert einen double als String im Währungsformat: 2.3 -> 2,30 € * */ QString MCSalesModel::toCurrency( double amount ) { QLocale loc; return loc.toCurrencyString( amount ); } /** * @brief Konvertiert einen String-Zahlenwert in * deutscher Schreibweise zum double. * * Macht aus einem Zahlen-String in deutscher Schreibweise: * 1,50 (statt 1.5) einen double. * */ double MCSalesModel::toDoubleLocale( QString amount ) { QLocale converter( QLocale::German ); bool ok; return converter.toDouble( amount, &ok ) ; } /** * @brief Konvertiert einen Währungs-String zurück ins Gleitkommaformat. * @param Eingabewert als String * @return 0.0 im Fehlerfall, sonst: Der Zahlenwert des String * * Das €-Zeichen wird abgeschitten: "23,20 €" wird zu 23.2 * */ double MCSalesModel::fromCurrency( QString amount ) { if( amount.isEmpty() || amount.isNull() ) return 0.0; QString raw = amount.split( ' ' ).at(0); return toDoubleLocale( raw ); } /** * @brief Defaultkonstruktor. */ MCSalesModel::MCSalesModel( QObject *parent ) : QStandardItemModel( parent ) { QStringList header; header << "Kunde" << "Verkäufernummer" << "lfd. Nummer" << "Verkaufspreis"; setHorizontalHeaderLabels( header ); } /** * @brief Speichert einen Verkaufseintrag. * * @param trCount laufende Transaktionsnummer * @param trSellerID die Kundennummer * @param trItemNo die Artikelnummer * @param trPrice der Preis * */ void MCSalesModel::appendEntry( const QString& trCount, const QString& trSellerID, const QString& trItemNo, const QString& trPrice ) { QStandardItem* item1 = new QStandardItem( trCount ); QStandardItem* item2 = new QStandardItem( trSellerID ); // wir formatieren gleich auf 3 Stellen: 12 -> 012 QStandardItem* item3 = new QStandardItem( formatInt( trItemNo.toInt(), 3 ) ); // wild: price ist ein string mit komma: "2,6" QStandardItem* item4 = new QStandardItem( toCurrency( toDoubleLocale( trPrice ) ) ); //QStandardItem* item4 = new QStandardItem( "77,77 €" ); item1->setTextAlignment ( Qt::AlignCenter ); item2->setTextAlignment ( Qt::AlignCenter ); item3->setTextAlignment ( Qt::AlignCenter ); item4->setTextAlignment ( Qt::AlignRight ); QList items; items.append( item1 ); items.append( item2 ); items.append( item3 ); items.append( item4 ); appendRow( items ); } /** * @brief Transaktionen aus einem TextStream einlesen * @param input TextStream zeigt auf eine Datei mit Transaktionen (==Verkäufen) */ void MCSalesModel::appendTransactions( QTextStream& input ) { while( !input.atEnd() ) { QString line = input.readLine(); QStringList entries = line.simplified().split( QRegularExpression("\\s+") ); if( entries.size() < MCSalesItemMap::MaxSize ) continue; const QString& tcount = entries[ MCSalesItemMap::Count ]; const QString& sellerID = entries[ MCSalesItemMap::SellerID ]; const QString& itemNo = entries[ MCSalesItemMap::ItemNo ]; const QString& price = entries[ MCSalesItemMap::Price ]; /// die Transaktionsnummer entspricht der Anzahl der Kunden /// und auch anzeigen appendEntry ( tcount, sellerID, itemNo, price ); } /// while }