00001 #include <QDomElement>
00002 #include <QDebug>
00003 #include <QtAlgorithms>
00004 #include "dataaccountingrecord.h"
00005 #include "datahash.h"
00006 #include "datadefinitions.h"
00007
00008 bool DataAccountingRecord::loadDataNode(const QDomNode data)
00009 {
00010 QDomNode node = data;
00011
00012 empty = false;
00013 if(node.toElement().attribute("date") != "")
00014 {
00015 dateValue = QDate::fromString(node.toElement().attribute("date"), "dd.MM.yyyy");
00016 if(!dateValue.isValid())
00017 {
00018 qDebug() << "Warning while loading XML: Date of accounting record is not valid!";
00019 }
00020 }
00021 documentNumberValue = node.toElement().attribute("documentNumber");
00022
00023 QDomNode recordNode = node.firstChild();
00024 while(!recordNode.isNull())
00025 {
00026 if(recordNode.nodeName() == "debit")
00027 {
00028 loadSubrecords(recordNode, &debitList);
00029 }
00030 else if(recordNode.nodeName() == "credit")
00031 {
00032 loadSubrecords(recordNode, &creditList);
00033 }
00034 else if(recordNode.nodeName() != "#comment")
00035 {
00036 qDebug() << "Unknown element in buchung tag:" << recordNode.nodeName();
00037 }
00038 recordNode = recordNode.nextSibling();
00039 }
00040 return true;
00041 }
00042
00043 bool DataAccountingRecord::loadSubrecords(const QDomNode data, QList<QPair<int, double> *> *targetList)
00044 {
00045 QDomNode node = data.firstChild();
00046 while(!node.isNull())
00047 {
00048 if(node.nodeName() == "account")
00049 {
00050 bool intOk, doubleOk;
00051
00052 QPair<int, double> *pair = new QPair<int, double>(node.toElement().attribute("number").toInt(&intOk), node.firstChild().nodeValue().simplified().toDouble(&doubleOk));
00053 if(!intOk || !doubleOk)
00054 {
00055 delete pair;
00056 qDebug() << "Error loading XML: Couldn't convert number and/or value of accounting record!";
00057 return false;
00058 }
00059
00060 targetList->append(pair);
00061 }
00062 else if(node.nodeName() != "#comment")
00063 {
00064 qDebug() << "Unknown element in subrecords:" << node.nodeName();
00065 }
00066 node = node.nextSibling();
00067 }
00068 return true;
00069 }
00070
00071 QDomElement DataAccountingRecord::saveDataNode(QDomDocument *xml)
00072 {
00073 QDomElement record = xml->createElement("record");
00074 record.setAttribute("date", dateValue.toString("dd.MM.yyyy"));
00075 record.setAttribute("documentNumber", documentNumberValue);
00076
00077 QDomElement eDebit = xml->createElement("debit");
00078 saveSubrecords(xml, &eDebit, &debitList);
00079 record.appendChild(eDebit);
00080
00081 QDomElement eCredit = xml->createElement("credit");
00082 saveSubrecords(xml, &eCredit, &creditList);
00083 record.appendChild(eCredit);
00084
00085 return record;
00086 }
00087
00088 void DataAccountingRecord::saveSubrecords(QDomDocument *xml, QDomElement *parent, QList<QPair<int, double> *> *records)
00089 {
00090 for(int i = 0; i < records->size(); i++)
00091 {
00092 QDomElement account = xml->createElement("account");
00093 QString normalized = normalizedAccountNumber((*records)[i]->first);
00094 account.setAttribute("number", normalized);
00095 QString doubleValue = QString::number((*records)[i]->second, 'f', 2);
00096 account.appendChild(xml->createTextNode(doubleValue));
00097
00098 parent->appendChild(account);
00099 }
00100 }
00101
00102 void DataAccountingRecord::hash(unsigned int &startValue)
00103 {
00104
00105
00106 DataHash::hash(documentNumberValue, startValue);
00107 DataHash::hash(dateValue.toString("dd.MM.yyyy"), startValue);
00108 DataHash::hash(&empty, sizeof(empty), startValue);
00109
00110 for(int i = 0; i < debitList.size(); i++)
00111 {
00112 DataHash::hash(&(debitList[i]->first), sizeof(debitList[i]->first), startValue);
00113 DataHash::hash(&(debitList[i]->second), sizeof(debitList[i]->second), startValue);
00114 }
00115 for(int i = 0; i < creditList.size(); i++)
00116 {
00117 DataHash::hash(&(creditList[i]->first), sizeof(creditList[i]->first), startValue);
00118 DataHash::hash(&(creditList[i]->second), sizeof(creditList[i]->second), startValue);
00119 }
00120 }
00121
00122 DataAccountingRecord::DataAccountingRecord()
00123 {
00124 empty = true;
00125 }
00126
00127 DataAccountingRecord::~DataAccountingRecord()
00128 {
00129 debitList.clear();
00130 creditList.clear();
00131 }