00001 #include <QDebug>
00002 #include <QDate>
00003 #include <assert.h>
00004 #include "accountingmodel.h"
00005
00006 AccountingModel::AccountingModel(QObject *parent) : QAbstractTableModel(parent)
00007 {
00008 addColumn(0, "BelegNr");
00009 addColumn(1, "Datum");
00010 addColumn(2, "Text");
00011 addColumn(3, "Soll");
00012 addColumn(4, "Haben");
00013 }
00014
00015 int AccountingModel::rowCount(const QModelIndex &parent) const
00016 {
00017 Q_UNUSED(parent);
00018 return p_entries.count();
00019 }
00020
00021 int AccountingModel::columnCount(const QModelIndex &parent) const
00022 {
00023 Q_UNUSED(parent);
00024 return p_headers.count();
00025 }
00026
00027 QStringList AccountingModel::data(const int row) const
00028 {
00029 return p_entries.value(row);
00030 }
00031
00032 QVariant AccountingModel::headerData(int section, Qt::Orientation orientation, int role) const
00033 {
00034 if(role != Qt::DisplayRole)
00035 {
00036 return QVariant();
00037 }
00038
00039 if(orientation == Qt::Horizontal)
00040 {
00041 return p_headers.value(section);
00042 }
00043 return QVariant();
00044 }
00045
00046 AccountingModel::~AccountingModel()
00047 {
00048 qDeleteAll(table);
00049 table.clear();
00050 }
00051
00052 Qt::ItemFlags AccountingModel::flags(const QModelIndex &index) const
00053 {
00054 if(!index.isValid())
00055 {
00056 return 0;
00057 }
00058
00059 return Qt::ItemIsEnabled | Qt::ItemIsSelectable;
00060 }
00061
00062 QVariant AccountingModel::data(const QModelIndex &index, int role) const
00063 {
00064 if(!index.isValid() || index.row() >= p_entries.size() || index.row() < 0)
00065 {
00066 return QVariant();
00067 }
00068
00069 if(role == Qt::DisplayRole)
00070 {
00071 return p_entries.value(index.row()).value(index.column());
00072 }
00073 return QVariant();
00074 }
00075
00076 bool AccountingModel::setData(const QModelIndex &index, const QVariant &value, int role)
00077 {
00078 if(index.isValid() && role == Qt::EditRole)
00079 {
00080 int row = index.row();
00081 QStringList p = p_entries.value(row);
00082
00083 p.replace(index.column(), value.toString());
00084
00085 p_entries.replace(row, p);
00086
00087 emit dataChanged(index, index);
00088
00089 return true;
00090 }
00091 return false;
00092 }
00093
00094 bool AccountingModel::insertRows(int position, int rows, const QModelIndex &index)
00095 {
00096 Q_UNUSED(index);
00097 beginInsertRows(QModelIndex(), position, position + rows - 1);
00098
00099 for (int row = 0; row < rows; row++)
00100 {
00101 QStringList pair;
00102 for(int i = 0; i < p_headers.size(); i++)
00103 {
00104 pair << QString();
00105 }
00106 p_entries.insert(position, pair);
00107 }
00108
00109 endInsertRows();
00110 return true;
00111 }
00112
00113 bool AccountingModel::removeRows(int position, int rows, const QModelIndex &index)
00114 {
00115 Q_UNUSED(index);
00116 beginRemoveRows(QModelIndex(), position, position+rows-1);
00117
00118 for (int row = 0; row < rows; ++row)
00119 {
00120 p_entries.removeAt(position);
00121 }
00122
00123 endRemoveRows();
00124 return true;
00125 }
00126
00127 void AccountingModel::addRow(QString accountingNumber, QString date, QString contraAccount, QString debit, QString credit)
00128 {
00129 insertRows(rowCount(), 1, QModelIndex());
00130
00131 int row = rowCount() - 1;
00132 QModelIndex in = index(row, 0, QModelIndex());
00133 setData(in, accountingNumber, Qt::EditRole);
00134
00135 in = index(row, 1, QModelIndex());
00136 setData(in, date, Qt::EditRole);
00137
00138 in = index(row, 2, QModelIndex());
00139 setData(in, contraAccount, Qt::EditRole);
00140
00141 in = index(row, 3, QModelIndex());
00142 setData(in, debit, Qt::EditRole);
00143
00144 in = index(row, 4, QModelIndex());
00145 setData(in, credit, Qt::EditRole);
00146 }
00147
00148 bool AccountingModel::insertColumns(int position, int columns, const QModelIndex &index)
00149 {
00150 Q_UNUSED(index);
00151
00152 beginInsertColumns(QModelIndex(), position, position + columns - 1);
00153 for(int j = 0; j < p_entries.size(); j++)
00154 {
00155 QStringList e = p_entries.value(j);
00156
00157
00158 for(int k = 0; k < columns; k++)
00159 {
00160 e.insert(position, QString());
00161 }
00162
00163 p_entries.replace(j, e);
00164 }
00165 endInsertColumns();
00166
00167 return true;
00168 }
00169
00170 void AccountingModel::addColumn(int position, QString columnName)
00171 {
00172 insertColumn(position, QModelIndex());
00173 p_headers.insert(position, columnName);
00174 }
00175
00176 void AccountingModel::buildTable()
00177 {
00178 double amount, total = 0;
00179 int i, j;
00180 DataAccountingRecord *currentAccountingRecord;
00181
00182 qDeleteAll(table);
00183 table.clear();
00184
00185
00186 for(i = 0; i < exercise->accounts()->size(); i++)
00187 {
00188 if(exercise->accounts()->at(i)->accountNumber() == accountNumber)
00189 {
00190 QList<QVariant> *row = new QList<QVariant>();
00191
00192
00193 amount = 0;
00194 total += amount;
00195
00196
00197
00198 (*row) << QVariant() << ("01.01." + QString::number(QDate::currentDate().year())) << "9800" << (amount > 0 ? amount : QVariant()) << (amount < 0 ? -amount : QVariant());
00199 table.append(row);
00200 break;
00201 }
00202 }
00203
00204
00205
00206 for(i = 0; i < exercise->accountingRecords()->size(); i++)
00207 {
00208 currentAccountingRecord = exercise->accountingRecords()->at(i);
00209
00210
00211 for(j = 0; j < currentAccountingRecord->debits()->size(); j++)
00212 {
00213
00214
00215 if(currentAccountingRecord->debits()->at(j)->first == accountNumber)
00216 {
00217
00218 assert(currentAccountingRecord->debits()->at(j)->second > 0);
00219
00220 total += currentAccountingRecord->debits()->at(j)->second;
00221 appendRecord(currentAccountingRecord, currentAccountingRecord->debits()->at(j)->second);
00222 break;
00223 }
00224 }
00225
00226
00227
00228 for(j = 0; j < currentAccountingRecord->credits()->size(); j++)
00229 {
00230
00231 if(currentAccountingRecord->credits()->at(j)->first == accountNumber)
00232 {
00233
00234 assert(currentAccountingRecord->credits()->at(j)->second > 0);
00235
00236 total -= currentAccountingRecord->debits()->at(j)->second;
00237 appendRecord(currentAccountingRecord, -currentAccountingRecord->credits()->at(j)->second);
00238 break;
00239 }
00240 }
00241 }
00242
00243
00244 QList<QVariant> *row = new QList<QVariant>();
00245
00246 (*row) << QVariant() << QVariant() << "Saldo" << (total < 0 ? -total : QVariant()) << (total > 0 ? total : QVariant());
00247 table.append(row);
00248
00249
00250 }
00251
00252 void AccountingModel::appendRecord(DataAccountingRecord *currentAccountingRecord, double amount)
00253 {
00254 QList<QVariant> *row = new QList<QVariant>();
00255 QList<QPair<int, double> *> *otherSide;
00256 QString text, s;
00257 int i;
00258
00259 (*row) << currentAccountingRecord->documentNumber() << currentAccountingRecord->date().toString("dd.MM.yyyy");
00260
00261 if(amount > 0)
00262 {
00263 otherSide = currentAccountingRecord->credits();
00264 }
00265 else
00266 {
00267 otherSide = currentAccountingRecord->debits();
00268 }
00269
00270
00271 for(i = 0; i < otherSide->size(); i++)
00272 {
00273 s.sprintf("%4d", otherSide->at(i)->first);
00274 text += (text.length() ? ", " : "") + s;
00275 }
00276
00277 (*row) << text << (amount > 0 ? amount : QVariant()) << (amount < 0 ? -amount : QVariant());
00278
00279 table.append(row);
00280 }
00281
00282 double AccountingModel::debitSum()
00283 {
00284 double sum = 0;
00285 for(int i = 0; i < p_entries.count(); i++)
00286 {
00287 sum += p_entries.value(i).value(3).toDouble();
00288 }
00289 return sum;
00290 }
00291
00292 double AccountingModel::creditSum()
00293 {
00294 double sum = 0;
00295 for(int i = 0; i < p_entries.count(); i++)
00296 {
00297 sum += p_entries.value(i).value(4).toDouble();
00298 }
00299 return sum;
00300 }
00301