00001 #include <QColor>
00002 #include <QDebug>
00003 #include "costcentremodel.h"
00004 #include "../Models/costtypemodel.h"
00005 #include "../Data/datadefinitions.h"
00006
00007 CostCentreModel::CostCentreModel(uint id, uint type, QString name, QObject *parent) : QAbstractTableModel(parent), AbstractModel(id, type, name)
00008 {
00009 columns = new CostCentreColumns();
00010 rows = new CostCentreRows();
00011 costcentre = new CostCentre();
00012
00013
00014 lastid = 0;
00015
00016 addColumn(0, columns->costcentrecolumns(CostCentreColumns::CostType));
00017 addColumn(1, columns->costcentrecolumns(CostCentreColumns::Costs));
00018 addColumn(2, columns->costcentrecolumns(CostCentreColumns::CostAllocation));
00019 addColumn(3, columns->costcentrecolumns(CostCentreColumns::CostAllocationType));
00020 addColumn(4, columns->costcentrecolumns(CostCentreColumns::EditInformation));
00021 addColumn(5, columns->costcentrecolumns(CostCentreColumns::RowID));
00022 addColumn(6, columns->costcentrecolumns(CostCentreColumns::AssociationID));
00023 addColumn(7, columns->costcentrecolumns(CostCentreColumns::FileAssociatedRow));
00024
00025
00026 refreshSums();
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081 }
00082
00083 void CostCentreModel::setCostTypeModel(CostTypeModel *model)
00084 {
00085 costTypeModel = model;
00086 costTypeFileId = costTypeModel->fileId();
00087
00088 connect(costTypeModel, SIGNAL(rowEdited(int)), this, SLOT(costTypeRowEdited(int)));
00089 connect(costTypeModel, SIGNAL(rowRemoved(int)), this, SLOT(costTypeRowRemoved(int)));
00090 }
00091
00092 QDomElement CostCentreModel::documentNode()
00093 {
00094 QDomDocument doc;
00095
00096 QDomElement costcentre = doc.createElement("CostCentre");
00097
00098 QDomElement rowid = doc.createElement("lastrowid");
00099 QDomText text = doc.createTextNode(QString::number(lastid));
00100 rowid.appendChild(text);
00101 costcentre.appendChild(rowid);
00102
00103 QDomElement costTypeFile = doc.createElement("costtypefileid");
00104 QDomText costtypefileidtext = doc.createTextNode(QString::number(costTypeFileId));
00105 costTypeFile.appendChild(costtypefileidtext);
00106 costcentre.appendChild(costTypeFile);
00107
00108 QDomElement data = doc.createElement("data");
00109 costcentre.appendChild(data);
00110
00111
00112 QDomElement costcentres = doc.createElement("costcentres");
00113
00114 QList<int> costCentreColumns;
00115 for(int column = 0; column < p_headers.count(); column++)
00116 {
00117 if(columns->isCostCentre(p_headers.value(column)))
00118 {
00119 costCentreColumns << column;
00120 }
00121 }
00122 qDebug()<<"costCentreColumns: " << costCentreColumns;
00123
00124 for(int column = 0; column < costCentreColumns.count(); column++)
00125 {
00126 QDomElement cc = doc.createElement("costcentre");
00127 cc.setAttribute("costcentreid", QString::number(column));
00128 cc.setAttribute("name", p_headers.value(costCentreColumns.value(column)));
00129 cc.setAttribute("type", p_entries.value(p_entries.count() - 1).value(costCentreColumns.value(column)));
00130 cc.setAttribute("base", p_entries.value(p_entries.count() - 3).value(costCentreColumns.value(column)));
00131
00132 costcentres.appendChild(cc);
00133 }
00134 data.appendChild(costcentres);
00135
00136
00137 if(p_entries.count() > 6)
00138 {
00139 QDomElement positions = doc.createElement("positions");
00140 for(int row = 1; row < p_entries.count() - 6; row++)
00141 {
00142 QStringList currentRow = p_entries.value(row);
00143 if(currentRow.value(currentRow.count() - 4) != "costcentretype" && !(currentRow.value(currentRow.count() - 4)).startsWith("sum") && currentRow.value(currentRow.count() - 4) != "edit")
00144 {
00145 QDomElement line = doc.createElement("position");
00146 line.setAttribute("name", currentRow.value(CostCentreColumns::CostType));
00147 line.setAttribute("editInfo", currentRow.value(currentRow.count() - 4));
00148 line.setAttribute("rowid", currentRow.value(currentRow.count() - 3));
00149 line.setAttribute("associationid", currentRow.value(currentRow.count() - 2));
00150 line.setAttribute("fileAssociation", currentRow.value(currentRow.count() - 1));
00151
00152 QDomElement costs = doc.createElement("costs");
00153 QDomText coststext = doc.createTextNode(currentRow.value(CostCentreColumns::Costs));
00154 costs.appendChild(coststext);
00155 line.appendChild(costs);
00156
00157 QDomElement costcentrecosts = doc.createElement("costcentrecosts");
00158 int costcentreid = 0;
00159 for(int i = 2; i < currentRow.count() - 6; i++)
00160 {
00161 QDomElement cost = doc.createElement("cost");
00162 cost.setAttribute("costcentreid", QString::number(costcentreid));
00163 QDomText costtext = doc.createTextNode(currentRow.value(i));
00164 cost.appendChild(costtext);
00165
00166 costcentrecosts.appendChild(cost);
00167 costcentreid++;
00168 }
00169 line.appendChild(costcentrecosts);
00170
00171 QDomElement costalloc = doc.createElement("costalloc");
00172 QDomText costalloctext = doc.createTextNode(currentRow.value(currentRow.count() - 6));
00173 costalloc.appendChild(costalloctext);
00174 line.appendChild(costalloc);
00175
00176 QDomElement costalloc2 = doc.createElement("costalloc2");
00177 QDomText costalloctext2 = doc.createTextNode(currentRow.value(currentRow.count() - 5));
00178 costalloc2.appendChild(costalloctext2);
00179 line.appendChild(costalloc2);
00180
00181 positions.appendChild(line);
00182 }
00183 }
00184 data.appendChild(positions);
00185 }
00186
00187 QDomElement file = doc.createElement("file");
00188 file.setAttribute("type", "costcentre");
00189 file.setAttribute("name", fileName());
00190 file.setAttribute("fileid", fileId());
00191
00192 file.appendChild(costcentre);
00193
00194 return file;
00195 }
00196
00197 bool CostCentreModel::loadDocument(const QDomElement file)
00198 {
00199
00200 QDomNode costtypes = file.firstChild();
00201 while(!costtypes.isNull())
00202 {
00203 QDomElement costtype = costtypes.toElement();
00204 if(costtype.tagName() == "CostCentre")
00205 {
00206 QDomNode elements = costtype.firstChild();
00207 while(!elements.isNull())
00208 {
00209 QDomElement element = elements.toElement();
00210 if(element.tagName() == "lastrowid")
00211 {
00212 QDomNode lastrowid = element.firstChild();
00213
00214 bool ok = true;
00215 lastid = lastrowid.nodeValue().toUInt(&ok);
00216 if(!ok)
00217 {
00218 lastid = 0;
00219 }
00220 }
00221 else if(element.tagName() == "costtypefileid")
00222 {
00223 QDomNode costTypeFile = element.firstChild();
00224
00225 bool ok = true;
00226 costTypeFileId = costTypeFile.nodeValue().toUInt(&ok);
00227 if(!ok)
00228 {
00229 costTypeFileId = 0;
00230 }
00231 }
00232 else if(element.tagName() == "data")
00233 {
00234 QDomNode dataelements = element.firstChild();
00235 while(!dataelements.isNull())
00236 {
00237 QDomElement dataelement = dataelements.toElement();
00238
00239 if(dataelement.tagName() == "positions")
00240 {
00241 QDomNode positions = dataelement.firstChild();
00242 while(!positions.isNull())
00243 {
00244 QDomElement position = positions.toElement();
00245
00246 if(position.tagName() == "position")
00247 {
00248 QString s_name = "";
00249 QString s_completeCosts = "";
00250 QStringList s_costs;
00251 QString s_costalloc = "";
00252 QString s_costalloc2 = "";
00253 QString s_editInfo = "";
00254 QString s_rowid = "";
00255 QString s_associationid = "";
00256 QString s_fileAssociationid = "";
00257
00258 s_name = position.attribute("name", "");
00259 s_rowid = position.attribute("rowid", "");
00260 s_associationid = position.attribute("associationid", "");
00261 s_fileAssociationid = position.attribute("fileAssociation", "");
00262 s_editInfo = position.attribute("editInfo", "");
00263
00264 QDomNode values = position.firstChild();
00265 while(!values.isNull())
00266 {
00267 QDomElement value = values.toElement();
00268
00269 if(value.tagName() == "costs")
00270 {
00271 QDomNode costs = value.firstChild();
00272 s_completeCosts = costs.nodeValue();
00273 }
00274 else if(value.tagName() == "costcentrecosts")
00275 {
00276 QDomNode costcentres = value.firstChild();
00277 while(!costcentres.isNull())
00278 {
00279 QDomElement costcentre = costcentres.toElement();
00280 if(costcentre.tagName() == "cost")
00281 {
00282 QDomNode c = costcentre.firstChild();
00283
00284 s_costs << c.nodeValue();
00285 }
00286 costcentres = costcentres.nextSibling();
00287 }
00288 }
00289 else if(value.tagName() == "costalloc")
00290 {
00291 QDomNode costalloc = value.firstChild();
00292 s_costalloc = costalloc.nodeValue();
00293 }
00294 else if(value.tagName() == "costalloc2")
00295 {
00296 QDomNode costalloc2 = value.firstChild();
00297 s_costalloc2 = costalloc2.nodeValue();
00298 }
00299
00300 values = values.nextSibling();
00301 }
00302 QStringList p;
00303 p << s_name;
00304 p << s_completeCosts;
00305 p << s_costs;
00306 p << s_costalloc;
00307 p << s_costalloc2;
00308 p << s_editInfo;
00309 p << s_rowid;
00310 p << s_associationid;
00311 p << s_fileAssociationid;
00312 addPosition(p, true);
00313 }
00314 positions = positions.nextSibling();
00315 }
00316 }
00317 else if(dataelement.tagName() == "costcentres")
00318 {
00319 QDomNode costcentres = dataelement.firstChild();
00320 while(!costcentres.isNull())
00321 {
00322 QDomElement costcentre = costcentres.toElement();
00323
00324 if(costcentre.tagName() == "costcentre")
00325 {
00326 QString base = costcentre.attribute("base", "");
00327
00328
00329 int type = costcentre.attribute("type", "0").toInt();
00330 QString name = costcentre.attribute("name", "");
00331
00332 addCostCentre(name, type, base);
00333 }
00334 costcentres = costcentres.nextSibling();
00335 }
00336 }
00337 dataelements = dataelements.nextSibling();
00338 }
00339 }
00340 elements = elements.nextSibling();
00341 }
00342 }
00343 costtypes = costtypes.nextSibling();
00344 }
00345 return true;
00346 }
00347
00348 int CostCentreModel::rowCount(const QModelIndex &parent) const
00349 {
00350 Q_UNUSED(parent);
00351 return p_entries.count();
00352 }
00353
00354 int CostCentreModel::columnCount(const QModelIndex &parent) const
00355 {
00356 Q_UNUSED(parent);
00357 return p_headers.count();
00358 }
00359
00360 QStringList CostCentreModel::data(const int row) const
00361 {
00362 return p_entries.value(row);
00363 }
00364
00365 QVariant CostCentreModel::data(const QModelIndex &index, int role) const
00366 {
00367 if(!index.isValid())
00368 {
00369 return QVariant();
00370 }
00371
00372 if(index.row() >= p_entries.size() || index.row() < 0)
00373 {
00374 return QVariant();
00375 }
00376
00377 if(role == Qt::BackgroundRole)
00378 {
00379 if(index.row() == rowCount(QModelIndex()) - 1 || index.row() == rowCount(QModelIndex()) - 2 || index.row() == rowCount(QModelIndex()) - 3 || index.row() == rowCount(QModelIndex()) - 4 || index.row() == rowCount(QModelIndex()) - 5)
00380 {
00381 QColor c;
00382 c.setNamedColor("#eaeaea");
00383 return c;
00384 }
00385 else
00386 {
00387 if(index.row() % 2 == 0)
00388 {
00389 return QColor(Qt::white);
00390 }
00391 else
00392 {
00393 QColor c;
00394 c.setNamedColor("#f3f3f3");
00395 return c;
00396 }
00397 }
00398 }
00399 if(role == Qt::DisplayRole || role == Qt::EditRole)
00400 {
00401 QStringList pair = p_entries.value(index.row());
00402 if(pair.size() > index.column())
00403 {
00404 return pair.value(index.column());
00405 }
00406 }
00407 return QVariant();
00408 }
00409
00410 QVariant CostCentreModel::headerData(int section, Qt::Orientation orientation, int role) const
00411 {
00412 if(role != Qt::DisplayRole)
00413 {
00414 return QVariant();
00415 }
00416
00417 if(orientation == Qt::Horizontal)
00418 {
00419 return p_headers.value(section);
00420 }
00421 return QVariant();
00422 }
00423
00424 bool CostCentreModel::insertRows(int position, int rows, const QModelIndex &index)
00425 {
00426 Q_UNUSED(index);
00427 beginInsertRows(QModelIndex(), position, position + rows - 1);
00428
00429 for (int row = 0; row < rows; row++)
00430 {
00431 QStringList pair;
00432 for(int i = 0; i < p_headers.size(); i++)
00433 {
00434 if(i == columnIdIndex(columns->costcentrecolumns(CostCentreColumns::RowID)))
00435 {
00436 pair << QString::number(lastid++);
00437 }
00438 else
00439 {
00440 pair << "";
00441 }
00442 }
00443 p_entries.insert(position, pair);
00444 }
00445
00446 endInsertRows();
00447 return true;
00448 }
00449
00450 bool CostCentreModel::insertColumns(int position, int columns, const QModelIndex &index)
00451 {
00452 Q_UNUSED(index);
00453
00454 beginInsertColumns(QModelIndex(), position, position + columns - 1);
00455 for(int j = 0; j < p_entries.size(); j++)
00456 {
00457 QStringList e = p_entries.value(j);
00458
00459
00460 for(int k = 0; k < columns; k++)
00461 {
00462 e.insert(position, "");
00463 }
00464
00465 p_entries.replace(j, e);
00466 }
00467 endInsertColumns();
00468
00469 return true;
00470 }
00471
00472 bool CostCentreModel::removeRows(int position, int rows, const QModelIndex &index)
00473 {
00474 Q_UNUSED(index);
00475 beginRemoveRows(QModelIndex(), position, position+rows-1);
00476
00477
00478
00479 for (int row = 0; row < rows; ++row)
00480 {
00481 p_entries.removeAt(position);
00482 }
00483
00484 endRemoveRows();
00485 return true;
00486 }
00487
00488 double CostCentreModel::sums(int sumType)
00489 {
00490 double sum = 0;
00491 for(int i = 0; i < p_entries.size(); i++)
00492 {
00493 QStringList entry = p_entries.value(i);
00494
00495 int editColumn = columnCount(QModelIndex()) - 4;
00496 if(entry.value(editColumn) != "sum1" &&
00497 entry.value(editColumn) != "sum2" &&
00498 entry.value(editColumn) != "sum3" &&
00499 entry.value(editColumn) != "sum4" &&
00500 entry.value(editColumn) != "sum5 " &&
00501 entry.value(editColumn) != "edit" &&
00502 entry.value(editColumn) != "costcentretype")
00503 {
00504 switch(sumType)
00505 {
00506 case CostCentreSumTypes::Total:
00507 {
00508 sum += entry.value(CostCentreColumns::Costs).toDouble();
00509 break;
00510 }
00511 case CostCentreSumTypes::DirectCosts:
00512 {
00513 int columnid = columnIdIndex(columns->costcentrecolumns(CostCentreColumns::CostAllocationType));
00514 QString allocationtypes = entry.value(columnid);
00515 QStringList allocations = allocationtypes.split("/");
00516
00517 qDebug()<<"allocations: " << allocations;
00518
00519 for(int columns = 2; columns < entry.count() - 6; columns++)
00520 {
00521
00522 int c = columns;
00523 c -= 2;
00524
00525 if(allocations.value(c) == "E")
00526 {
00527
00528 sum += entry.value(columns).toDouble();
00529 }
00530 }
00531 break;
00532 }
00533 case CostCentreSumTypes::FixedCosts:
00534 {
00535 int columnid = columnIdIndex(columns->costcentrecolumns(CostCentreColumns::CostAllocationType));
00536 QString allocationtypes = entry.value(columnid);
00537 QStringList allocations = allocationtypes.split("/");
00538
00539
00540
00541 for(int columns = 2; columns < entry.count() - 6; columns++)
00542 {
00543
00544 int c = columns;
00545 c -= 2;
00546
00547 if(allocations.value(c) == "G")
00548 {
00549 qDebug()<<"sum+=" << entry.value(columns).toDouble();
00550
00551 }
00552 }
00553 break;
00554 }
00555 default:
00556 {
00557 break;
00558 }
00559 }
00560 }
00561 }
00562 return sum;
00563 }
00564
00565 double CostCentreModel::sums(int sumType, int column)
00566 {
00567 double sum = 0;
00568 for(int i = 0; i < p_entries.size(); i++)
00569 {
00570 QStringList entry = p_entries.value(i);
00571
00572 int editColumn = columnCount(QModelIndex()) - 4;
00573 if(entry.value(editColumn) != "sum1" &&
00574 entry.value(editColumn) != "sum2" &&
00575 entry.value(editColumn) != "sum3" &&
00576 entry.value(editColumn) != "sum4" &&
00577 entry.value(editColumn) != "sum5 " &&
00578 entry.value(editColumn) != "edit" &&
00579 entry.value(editColumn) != "costcentretype")
00580 {
00581 switch(sumType)
00582 {
00583 case CostCentreSumTypes::Total:
00584 {
00585 sum += entry.value(column).toDouble();
00586 break;
00587 }
00588 case CostCentreSumTypes::DirectCosts:
00589 {
00590 int columnid = columnIdIndex(columns->costcentrecolumns(CostCentreColumns::CostAllocationType));
00591 QString allocationtypes = entry.value(columnid);
00592 QStringList allocations = allocationtypes.split("/");
00593
00594 columnid = column;
00595 columnid -= 2;
00596 if(allocations.value(columnid) == "E")
00597 {
00598 sum += entry.value(column).toDouble();
00599 }
00600 break;
00601 }
00602 case CostCentreSumTypes::FixedCosts:
00603 {
00604 int columnid = columnIdIndex(columns->costcentrecolumns(CostCentreColumns::CostAllocationType));
00605 QString allocationtypes = entry.value(columnid);
00606 QStringList allocations = allocationtypes.split("/");
00607
00608 columnid = column;
00609 columnid -= 2;
00610 if(allocations.value(columnid) == "G")
00611 {
00612 sum += entry.value(column).toDouble();
00613 }
00614 break;
00615 }
00616 default:
00617 {
00618 break;
00619 }
00620 }
00621 }
00622 }
00623 return sum;
00624 }
00625
00626 bool CostCentreModel::removeColumns(int position, int columns, const QModelIndex &index)
00627 {
00628 Q_UNUSED(index);
00629 beginRemoveColumns(QModelIndex(), position, position + columns - 1);
00630
00631
00632 for (int column = 0; column < columns; ++column)
00633 {
00634 for(int row = 0; row < rowCount(QModelIndex()); row++)
00635 {
00636 QStringList rowlist = p_entries.value(row);
00637 rowlist.removeAt(position + column);
00638 p_entries.replace(row, rowlist);
00639 }
00640 p_headers.removeAt(position + column);
00641 }
00642
00643 endRemoveColumns();
00644 return true;
00645 }
00646
00647 bool CostCentreModel::setData(const QModelIndex &index, const QVariant &value, int role)
00648 {
00649 if(index.isValid() && role == Qt::EditRole)
00650 {
00651 int row = index.row();
00652 QStringList p = p_entries.value(row);
00653 for(int i = 0; i < p_headers.size(); i++)
00654 {
00655 if(index.column() == i)
00656 {
00657
00658 if(columns->isCostCentre(p_headers.value(index.column())) && index.row() == CostCentreRows::CostCentreType)
00659 {
00660
00661 QString userVisibleString = value.toString();
00662
00663
00664
00665 int editRow = rowIdIndex("edit");
00666 if(editRow != -1)
00667 {
00668 QModelIndex in = this->index(editRow, index.column(), QModelIndex());
00669 int editInfoType = in.data(Qt::DisplayRole).toInt();
00670 if(editInfoType == 0 || editInfoType == 1)
00671 {
00672
00673 if(userVisibleString != costcentre->costcentre(editInfoType) && userVisibleString != "")
00674 {
00675 if(editInfoType == 0)
00676 {
00677 setData(in, 1, Qt::EditRole);
00678 }
00679 else
00680 {
00681 setData(in, 0, Qt::EditRole);
00682 }
00683 }
00684 }
00685 }
00686 }
00687 p.replace(i, value.toString());
00688 }
00689 }
00690
00691 p_entries.replace(row, p);
00692
00693 emit dataChanged(index, index);
00694 return true;
00695 }
00696 return false;
00697 }
00698
00699 Qt::ItemFlags CostCentreModel::flags(const QModelIndex &index) const
00700 {
00701 if(!index.isValid())
00702 {
00703 return Qt::ItemIsEnabled;
00704 }
00705 if(p_entries.value(index.row()).value(CostCentreRows::CostCentreType) == rows->costcentrerows(CostCentreRows::CostCentreType))
00706 {
00707
00708 if(columns->isCostCentre(p_headers.value(index.column())))
00709 {
00710
00711 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
00712 }
00713 }
00714 return QAbstractTableModel::flags(index) | Qt::ItemIsSelectable;
00715 }
00716
00717 QList<QStringList> CostCentreModel::entries()
00718 {
00719 return p_entries;
00720 }
00721
00722 QStringList CostCentreModel::headers()
00723 {
00724 return p_headers;
00725 }
00726
00727 int CostCentreModel::costCentreCount()
00728 {
00729
00730 return p_headers.count() - 8;
00731 }
00732
00733 void CostCentreModel::setBase(QString columnName, QString base)
00734 {
00735 int column = p_headers.indexOf(columnName);
00736 if(column != -1)
00737 {
00738 if(p_entries.count() > 3)
00739 {
00740 QStringList row = p_entries.value(p_entries.count() - 3);
00741 row.replace(column, base);
00742 p_entries.replace(p_entries.count() - 3, row);
00743 }
00744 }
00745 }
00746
00747 void CostCentreModel::setBase(int column, QString base)
00748 {
00749 QString name = p_headers.value(column);
00750 setBase(name, base);
00751 }
00752
00753 bool CostCentreModel::addCostCentre(QString columnName, int type, QString base)
00754 {
00755
00756
00757
00758 for(int i = 0; i < p_headers.count(); i++)
00759 {
00760 if(p_headers.value(i) == columnName)
00761 {
00762 return false;
00763 }
00764 if(p_headers.value(i) == columns->costcentrecolumns(CostCentreColumns::CostAllocation))
00765 {
00766
00767 addColumn(i, columnName);
00768
00769
00770
00771 int editIndex = rowIdIndex("edit");
00772 if(editIndex != -1)
00773 {
00774
00775 QModelIndex in = index(editIndex, i, QModelIndex());
00776 setData(in, type, Qt::EditRole);
00777 }
00778
00779 refreshSums();
00780
00781 setBase(columnName, base);
00782
00783 return true;
00784 }
00785 }
00786 return false;
00787 }
00788
00789 void CostCentreModel::addColumn(int position, QString columnName)
00790 {
00791 insertColumn(position, QModelIndex());
00792 p_headers.insert(position, columnName);
00793 }
00794
00795 void CostCentreModel::addPosition(QStringList columns, bool overwriteRowId)
00796 {
00797
00798
00799
00800 int rowid = rowIdIndex("sum1");
00801 if(rowid != -1)
00802 {
00803 addPosition(rowid, columns, overwriteRowId);
00804 }
00805 }
00806
00807 void CostCentreModel::addPosition(int position, QStringList columns, bool overwriteRowId)
00808 {
00809 insertRows(position, 1, QModelIndex());
00810 for(int i = 0; i < columns.count(); i++)
00811 {
00812 QModelIndex in = index(position, i, QModelIndex());
00813 if((i != columnCount(QModelIndex()) - 3) || overwriteRowId)
00814 {
00815
00816 setData(in, columns.value(i), Qt::EditRole);
00817 }
00818 }
00819 refreshSums();
00820 }
00821
00822 bool CostCentreModel::containsCostCentre(QString name, int type)
00823 {
00824 return false;
00825 }
00826
00827 bool CostCentreModel::containsColumn(QString name)
00828 {
00829 return false;
00830 }
00831
00832 bool CostCentreModel::containsPosition(QStringList row)
00833 {
00834 return false;
00835 }
00836
00837 bool CostCentreModel::searchModel(QString entry)
00838 {
00839
00840
00841 for(int i = 0; i < p_entries.count(); i++)
00842 {
00843 if(p_entries.value(i).value(columnIdIndex(columns->costcentrecolumns(CostCentreColumns::EditInformation))) == entry)
00844 {
00845 return true;
00846 }
00847 }
00848 return false;
00849 }
00850
00851 int CostCentreModel::rowIdIndex(QString row)
00852 {
00853 for(int i = 0; i < p_entries.count(); i++)
00854 {
00855 if(p_entries.value(i).value(columnIdIndex(columns->costcentrecolumns(CostCentreColumns::EditInformation))) == row)
00856 {
00857 return i;
00858 }
00859 }
00860 return -1;
00861 }
00862
00863 int CostCentreModel::columnIdIndex(QString columnName)
00864 {
00865 for(int i = 0; i < p_headers.count(); i++)
00866 {
00867 if(p_headers.value(i) == columnName)
00868 {
00869 return i;
00870 }
00871 }
00872 return -1;
00873 }
00874
00875 bool CostCentreModel::isCostCentreColumn(int column)
00876 {
00877
00878 QString heading = p_headers.value(column);
00879
00880
00881 return columns->isCostCentre(heading);
00882 }
00883
00884 void CostCentreModel::costTypeRowEdited(int rowid)
00885 {
00886 qDebug()<<"costTypeRowEdited: " << rowid;
00887 QStringList columns;
00888 for(int row = 0; row < p_entries.count(); row++)
00889 {
00890 columns = p_entries.value(row);
00891
00892
00893 if(columns.value(columnCount(QModelIndex()) - 1).toInt() == rowid)
00894 {
00895 emit rowChangedWarning(rowid);
00896 return;
00897 }
00898 }
00899 }
00900
00901 void CostCentreModel::costTypeRowRemoved(int rowid)
00902 {
00903 qDebug()<<"costTypeRowRemoved: " << rowid;
00904 QStringList columns;
00905 for(int row = 0; row < p_entries.count(); row++)
00906 {
00907 columns = p_entries.value(row);
00908
00909
00910 if(columns.value(columnCount(QModelIndex()) - 1).toInt() == rowid)
00911 {
00912 emit rowRemovedWarning(rowid);
00913 return;
00914 }
00915 }
00916 }
00917
00918 void CostCentreModel::refreshSums()
00919 {
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931 if(!searchModel("edit"))
00932 {
00933 int row = 0;
00934 if(rowCount(QModelIndex()) > 0)
00935 {
00936 row = rowCount(QModelIndex()) - 1;
00937 }
00938 else
00939 {
00940 row = rowCount(QModelIndex());
00941 }
00942
00943 if(insertRow(row, QModelIndex()))
00944 {
00945 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
00946 setData(in, rows->costcentrerows(CostCentreRows::EditInformation));
00947
00948 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
00949 if(columnId != -1)
00950 {
00951 in = index(row, columnId, QModelIndex());
00952 setData(in, "edit", Qt::EditRole);
00953 }
00954 }
00955 }
00956
00957
00958
00959 if(!searchModel("costcentretype") && costCentreCount() > 0)
00960 {
00961 if(insertRow(0, QModelIndex()))
00962 {
00963 QModelIndex in = index(0, CostCentreColumns::CostType, QModelIndex());
00964 setData(in, rows->costcentrerows(CostCentreRows::CostCentreType));
00965
00966 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
00967 if(columnId != -1)
00968 {
00969 in = index(0, columnId, QModelIndex());
00970 setData(in, "costcentretype", Qt::EditRole);
00971 }
00972 }
00973 }
00974 int indexCostCentreType = rowIdIndex("costcentretype");
00975 int indexEditInfo = rowIdIndex("edit");
00976 if(indexCostCentreType != -1 && indexEditInfo != -1)
00977 {
00978 for(int i = 0; i < p_entries.count(); i++)
00979 {
00980
00981
00982 QStringList row = p_entries.value(i);
00983 if(row.value(CostCentreColumns::CostType) == rows->costcentrerows(CostCentreRows::EditInformation))
00984 {
00985
00986 for(int j = 0; j < row.count(); j++)
00987 {
00988 if(isCostCentreColumn(j))
00989 {
00990 if(row.value(j) == QString::number(CostCentre::PrimaryCostCentre) || row.value(j) == QString::number(CostCentre::ServiceCostCentre))
00991 {
00992
00993 QModelIndex in = index(indexCostCentreType, j, QModelIndex());
00994 setData(in, costcentre->costcentre(row.value(j).toInt()), Qt::EditRole);
00995 }
00996 }
00997 }
00998 }
00999 }
01000 }
01001
01002
01003
01004 if(!searchModel("sum1"))
01005 {
01006
01007
01008
01009
01010
01011 int row = -1;
01012
01013 if(searchModel("sum2"))
01014 {
01015 row = rowIdIndex("sum2");
01016 }
01017 else if(searchModel("sum3"))
01018 {
01019 row = rowIdIndex("sum3");
01020 }
01021 else if(searchModel("sum4"))
01022 {
01023 row = rowIdIndex("sum4");
01024 }
01025 else if(searchModel("sum5"))
01026 {
01027 row = rowIdIndex("sum5");
01028 }
01029
01030 if(row == -1)
01031 {
01032
01033 if(rowCount(QModelIndex()) > 0)
01034 {
01035 row = rowCount(QModelIndex()) - 1;
01036 }
01037 else
01038 {
01039 row = rowCount(QModelIndex());
01040 }
01041 }
01042
01043 if(insertRow(row, QModelIndex()))
01044 {
01045 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
01046 setData(in, rows->costcentrerows(CostCentreRows::Sum));
01047
01048 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
01049 if(columnId != -1)
01050 {
01051 in = index(row, columnId, QModelIndex());
01052 setData(in, "sum1", Qt::EditRole);
01053 }
01054 }
01055 }
01056 int row = rowIdIndex("sum1");
01057 QModelIndex in = index(row, CostCentreColumns::Costs, QModelIndex());
01058 setData(in, sums(CostCentreSumTypes::Total));
01059 int columns = columnCount(QModelIndex()) - 6;
01060 for(int i = 2; i < columns; i++)
01061 {
01062 in = index(row, i, QModelIndex());
01063 setData(in, sums(CostCentreSumTypes::Total, i));
01064 }
01065
01066
01067
01068 if(!searchModel("sum2"))
01069 {
01070
01071
01072
01073
01074
01075 int row = -1;
01076
01077 if(searchModel("sum1"))
01078 {
01079 row = rowIdIndex("sum1");
01080
01081
01082 row++;
01083 }
01084 else if(searchModel("sum3"))
01085 {
01086 row = rowIdIndex("sum3");
01087 }
01088 else if(searchModel("sum4"))
01089 {
01090 row = rowIdIndex("sum4");
01091 }
01092 else if(searchModel("sum5"))
01093 {
01094 row = rowIdIndex("sum5");
01095 }
01096
01097 if(row == -1)
01098 {
01099
01100 if(rowCount(QModelIndex()) > 0)
01101 {
01102 row = rowCount(QModelIndex()) - 1;
01103 }
01104 else
01105 {
01106 row = rowCount(QModelIndex());
01107 }
01108 }
01109
01110 if(insertRow(row, QModelIndex()))
01111 {
01112 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
01113 setData(in, rows->costcentrerows(CostCentreRows::DirectCosts));
01114
01115 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
01116 if(columnId != -1)
01117 {
01118 in = index(row, columnId, QModelIndex());
01119 setData(in, "sum2", Qt::EditRole);
01120 }
01121 }
01122 }
01123 row = rowIdIndex("sum2");
01124 in = index(row, CostCentreColumns::Costs, QModelIndex());
01125 setData(in, sums(CostCentreSumTypes::DirectCosts));
01126 columns = columnCount(QModelIndex()) - 6;
01127 for(int i = 2; i < columns; i++)
01128 {
01129 in = index(row, i, QModelIndex());
01130 setData(in, sums(CostCentreSumTypes::DirectCosts, i));
01131 }
01132
01133
01134
01135 if(!searchModel("sum3"))
01136 {
01137
01138
01139
01140
01141
01142 int row = -1;
01143
01144 if(searchModel("sum2"))
01145 {
01146 row = rowIdIndex("sum2");
01147
01148
01149 row++;
01150 }
01151 else if(searchModel("sum1"))
01152 {
01153 row = rowIdIndex("sum1");
01154
01155
01156 row++;
01157 }
01158 else if(searchModel("sum4"))
01159 {
01160 row = rowIdIndex("sum4");
01161 }
01162 else if(searchModel("sum5"))
01163 {
01164 row = rowIdIndex("sum5");
01165 }
01166
01167 if(row == -1)
01168 {
01169
01170 if(rowCount(QModelIndex()) > 0)
01171 {
01172 row = rowCount(QModelIndex()) - 1;
01173 }
01174 else
01175 {
01176 row = rowCount(QModelIndex());
01177 }
01178 }
01179
01180 if(insertRow(row, QModelIndex()))
01181 {
01182 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
01183 setData(in, rows->costcentrerows(CostCentreRows::FixedCosts));
01184
01185 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
01186 if(columnId != -1)
01187 {
01188 in = index(row, columnId, QModelIndex());
01189 setData(in, "sum3", Qt::EditRole);
01190 }
01191 }
01192 }
01193 row = rowIdIndex("sum3");
01194 in = index(row, CostCentreColumns::Costs, QModelIndex());
01195 setData(in, sums(CostCentreSumTypes::FixedCosts));
01196 columns = columnCount(QModelIndex()) - 6;
01197 for(int i = 2; i < columns; i++)
01198 {
01199 in = index(row, i, QModelIndex());
01200 setData(in, sums(CostCentreSumTypes::FixedCosts, i));
01201 }
01202
01203
01204
01205 if(!searchModel("sum4"))
01206 {
01207
01208
01209
01210
01211
01212 int row = -1;
01213
01214 if(searchModel("sum3"))
01215 {
01216 row = rowIdIndex("sum3");
01217
01218
01219 row++;
01220 }
01221 else if(searchModel("sum2"))
01222 {
01223 row = rowIdIndex("sum2");
01224
01225
01226 row++;
01227 }
01228 else if(searchModel("sum1"))
01229 {
01230 row = rowIdIndex("sum1");
01231
01232
01233 row++;
01234 }
01235 else if(searchModel("sum5"))
01236 {
01237 row = rowIdIndex("sum5");
01238 }
01239
01240 if(row == -1)
01241 {
01242
01243 if(rowCount(QModelIndex()) > 0)
01244 {
01245 row = rowCount(QModelIndex()) - 1;
01246 }
01247 else
01248 {
01249 row = rowCount(QModelIndex());
01250 }
01251 }
01252
01253 if(insertRow(row, QModelIndex()))
01254 {
01255 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
01256 setData(in, rows->costcentrerows(CostCentreRows::Base));
01257
01258 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
01259 if(columnId != -1)
01260 {
01261 in = index(row, columnId, QModelIndex());
01262 setData(in, "sum4", Qt::EditRole);
01263 }
01264 }
01265 }
01266
01267
01268
01269 if(!searchModel("sum5"))
01270 {
01271
01272
01273
01274
01275
01276 int row = -1;
01277
01278 if(searchModel("sum4"))
01279 {
01280 row = rowIdIndex("sum4");
01281
01282
01283 row++;
01284 }
01285 else if(searchModel("sum3"))
01286 {
01287 row = rowIdIndex("sum3");
01288
01289
01290 row++;
01291 }
01292 else if(searchModel("sum2"))
01293 {
01294 row = rowIdIndex("sum2");
01295
01296
01297 row++;
01298 }
01299 else if(searchModel("sum1"))
01300 {
01301 row = rowIdIndex("sum1");
01302
01303
01304 row++;
01305 }
01306
01307 if(row == -1)
01308 {
01309
01310 if(rowCount(QModelIndex()) > 0)
01311 {
01312 row = rowCount(QModelIndex()) - 1;
01313 }
01314 else
01315 {
01316 row = rowCount(QModelIndex());
01317 }
01318 }
01319
01320 if(insertRow(row, QModelIndex()))
01321 {
01322 QModelIndex in = index(row, CostCentreColumns::CostType, QModelIndex());
01323 setData(in, rows->costcentrerows(CostCentreRows::Overhead));
01324
01325 int columnId = columnIdIndex(rows->costcentrerows(CostCentreRows::EditInformation));
01326 if(columnId != -1)
01327 {
01328 in = index(row, columnId, QModelIndex());
01329 setData(in, "sum5", Qt::EditRole);
01330 }
01331 }
01332 }
01333 }