00001 #include "dataexercisevalidator.h"
00002
00003 DataExerciseValidator::DataExerciseValidator()
00004 {
00005 this->resetOk();
00006 }
00007
00008 void DataExerciseValidator::exerciseFinished(DataExercise *exercise, AccountingInformation *accountingRowInfos, QList<AccountingInformation> *listCredits, QList<AccountingInformation> *listDebits)
00009 {
00010 ALineEdit *p_accountingNumber = qobject_cast<ALineEdit*> (accountingRowInfos->value("accountingNumber"));
00011 ALineEdit *p_date = qobject_cast<ALineEdit*> (accountingRowInfos->value("date"));
00012
00013 if(p_accountingNumber != NULL && p_date != NULL)
00014 {
00015
00016 QListIterator<DataAccountingRecord *> iSolution(*(exercise->solutionRecords()));
00017 DataAccountingRecord *solutionRecord = NULL;
00018 bool recordInSolution = false;
00019 while(iSolution.hasNext())
00020 {
00021 solutionRecord = iSolution.next();
00022
00023 if(solutionRecord->documentNumber().toUpper() == p_accountingNumber->text().trimmed().toUpper())
00024 {
00025 recordInSolution = true;
00026
00027 if(solutionRecord->date() != QDate::fromString(p_date->text(), "d.M.yyyy"))
00028 {
00029 markWidgetWrong(p_date);
00030 this->_everythingOk = false;
00031 }
00032
00033 validateAccountingRows(solutionRecord->debits(), listDebits);
00034 validateAccountingRows(solutionRecord->credits(), listCredits);
00035 }
00036 }
00037 if(!recordInSolution)
00038 {
00039 markWidgetWrong(p_accountingNumber);
00040 markWidgetWrong(p_date);
00041 this->_everythingOk = false;
00042
00043 QListIterator<AccountingInformation> wrongDebit(*(listDebits));
00044 while(wrongDebit.hasNext())
00045 {
00046 AComboBox *wrong1 = qobject_cast<AComboBox*>(wrongDebit.next().value("account"));
00047 if(wrong1 != NULL)
00048 {
00049 markWidgetWrong(wrong1);
00050 }
00051 ALineEdit *wrong2 = qobject_cast<ALineEdit*>(wrongDebit.peekPrevious().value("amount"));
00052 if(wrong2 != NULL)
00053 {
00054 markWidgetWrong(wrong2);
00055 }
00056 }
00057 QListIterator<AccountingInformation> wrongCredit(*(listCredits));
00058 while(wrongCredit.hasNext())
00059 {
00060 AComboBox *wrong3 = qobject_cast<AComboBox*>(wrongCredit.next().value("account"));
00061 if(wrong3 != NULL)
00062 {
00063 markWidgetWrong(wrong3);
00064 }
00065 ALineEdit *wrong4 = qobject_cast<ALineEdit*>(wrongCredit.peekPrevious().value("amount"));
00066 if(wrong4 != NULL)
00067 {
00068 markWidgetWrong(wrong4);
00069 }
00070 }
00071 }
00072 }
00073
00074 if(this->everythingOk())
00075 {
00076 if(listDebits->count() < 2 || listCredits->count() < 2)
00077 {
00078 this->_everythingOk = false;
00079 }
00080 }
00081 }
00082
00083 void DataExerciseValidator::validateAccountingRows(QList<QPair<int, double> *> *solution, QList<AccountingInformation> *list)
00084 {
00085 QListIterator<AccountingInformation> iList(*(list));
00086 AccountingInformation user;
00087 AComboBox *p_account;
00088 ALineEdit *p_ammount;
00089 bool conversionOk1;
00090 bool conversionOk2;
00091 bool inSolution;
00092
00093 while(iList.hasNext())
00094 {
00095 user = iList.next();
00096 p_account = qobject_cast<AComboBox*> (user.value("account"));
00097 p_ammount = qobject_cast<ALineEdit*> (user.value("amount"));
00098 conversionOk1 = true;
00099 conversionOk2 = true;
00100 inSolution = false;
00101
00102 if(p_account->currentIndex() != 0)
00103 {
00104
00105 QStringList accountParts = p_account->currentText().split(" ", QString::SkipEmptyParts);
00106 int userDebAccount = accountParts.value(0).toInt(&conversionOk1);
00107 double userDebAmount = p_ammount->text().toDouble(&conversionOk2);
00108
00109 if(conversionOk1 && conversionOk2)
00110 {
00111
00112
00113
00114
00115 for(int i = 0; i < solution->count(); i++)
00116 {
00117 QPair<int, double> *solutionDeb = solution->value(i);
00118
00119 if(userDebAccount == solutionDeb->first)
00120 {
00121 inSolution = true;
00122
00123 if(userDebAmount != solutionDeb->second)
00124 {
00125 this->markWrong(p_ammount);
00126 }
00127 }
00128 }
00129 }
00130
00131
00132
00133 if((!conversionOk1) || (!conversionOk2) || (!inSolution))
00134 {
00135 this->markWrong(p_account);
00136 this->markWrong(p_ammount);
00137 }
00138 }
00139 }
00140 }
00141
00142 void DataExerciseValidator::markWrong(AComboBox *combobox)
00143 {
00144 markWidgetWrong(combobox);
00145 this->_everythingOk = false;
00146 }
00147
00148 void DataExerciseValidator::markWrong(ALineEdit *lineedit)
00149 {
00150 markWidgetWrong(lineedit);
00151 this->_everythingOk = false;
00152 }
00153
00154 void DataExerciseValidator::markWidgetWrong(QWidget *widget)
00155 {
00156 ALineEdit *line = qobject_cast<ALineEdit*>(widget);
00157 if(line)
00158 {
00159 line->setWrong();
00160 }
00161
00162 AComboBox *combo = qobject_cast<AComboBox*>(widget);
00163 if(combo)
00164 {
00165 combo->setWrong();
00166 }
00167 }