00001 #include "../Models/costunitmodel.h"
00002 #include "../Data/settings.h"
00003
00004 CostUnitModel::CostUnitModel(Settings &set, uint id, uint type, QString name, QObject *parent) : QAbstractTableModel(parent), AbstractModel(id, type, name)
00005 {
00006 settings = &set;
00007
00008 p_product = new CostUnitProduct();
00009 }
00010
00011 QDomElement CostUnitModel::documentNode()
00012 {
00013 qDebug()<<"documentNode";
00014 QDomDocument doc;
00015
00016 QDomElement product = doc.createElement("product");
00017 product.setAttribute("name", p_product->productName());
00018
00019 QDomElement user = doc.createElement("user");
00020 product.appendChild(user);
00021
00022 QList<double> items = p_product->userList();
00023
00024 for(int i = 0; i < items.count(); i++)
00025 {
00026 QDomElement row = doc.createElement("row");
00027 row.setAttribute("id", i);
00028 row.setAttribute("value", items.value(i));
00029 user.appendChild(row);
00030 }
00031
00032 QDomElement surcharges = doc.createElement("costcentre-surcharges");
00033 product.appendChild(surcharges);
00034
00035 int fileid = p_product->fileIDforCostCentreSurcharge();
00036 QDomElement filecostcentres = doc.createElement("file");
00037 filecostcentres.setAttribute("id", fileid);
00038 surcharges.appendChild(filecostcentres);
00039
00040 QDomElement costcentres = doc.createElement("costcentres");
00041 QList<int> ccentres = p_product->costCentreIDs();
00042 for(int i = 0; i < ccentres.count(); i++)
00043 {
00044 QDomElement costcentre = doc.createElement("costcentre");
00045 costcentre.setAttribute("id", ccentres.value(i));
00046 costcentres.appendChild(costcentre);
00047 }
00048 surcharges.appendChild(costcentres);
00049
00050 QDomElement file = doc.createElement("file");
00051 file.setAttribute("type", "costunit");
00052 file.setAttribute("name", fileName());
00053 file.setAttribute("fileid", fileId());
00054
00055 file.appendChild(product);
00056
00057 return file;
00058 }
00059
00060 bool CostUnitModel::loadDocument(const QDomElement file)
00061 {
00062 qDebug()<<"loadDocument";
00063
00064 p_product = new CostUnitProduct();
00065
00066 QDomNode products = file.firstChild();
00067 while(!products.isNull())
00068 {
00069 QDomElement product = products.toElement();
00070 if(product.tagName() == "product")
00071 {
00072 p_product->setProductName(product.attribute("name", ""));
00073
00074 QDomNode users = product.firstChild();
00075 while(!users.isNull())
00076 {
00077 QDomElement userorcostcentre = users.toElement();
00078 if(userorcostcentre.tagName() == "user")
00079 {
00080 QDomNode rows = userorcostcentre.firstChild();
00081 while(!rows.isNull())
00082 {
00083 QDomElement row = rows.toElement();
00084 if(row.tagName() == "row")
00085 {
00086 switch(row.attribute("id", "-1").toInt())
00087 {
00088 case 0:
00089 p_product->setDirectCosts(row.attribute("value", "-1").toDouble());
00090 break;
00091 case 1:
00092 p_product->setMarkup(row.attribute("value", "-1").toDouble());
00093 break;
00094 case 2:
00095 p_product->setCommission(row.attribute("value", "-1").toDouble());
00096 break;
00097 case 3:
00098 p_product->setTradeDiscount(row.attribute("value", "-1").toDouble());
00099 break;
00100 case 4:
00101 p_product->setDiscount(row.attribute("value", "-1").toDouble());
00102 break;
00103 case 5:
00104 p_product->setSalesTax(row.attribute("value", "-1").toDouble());
00105 break;
00106 default:
00107 return false;
00108 }
00109 }
00110 rows = rows.nextSibling();
00111 }
00112 }
00113 else if(userorcostcentre.tagName() == "costcentre-surcharges")
00114 {
00115 QDomNode surcharges = userorcostcentre.firstChild();
00116 while(!surcharges.isNull())
00117 {
00118 QDomElement element = surcharges.toElement();
00119 if(element.tagName() == "file")
00120 {
00121 p_product->setFileIDforCostCentreSurcharge(element.attribute("id", "-1").toInt());
00122 }
00123 else if(element.tagName() == "costcentres")
00124 {
00125 QDomNode costcentres = element.firstChild();
00126 QList<int> costcentreids;
00127 while(!costcentres.isNull())
00128 {
00129 QDomElement costcentre = costcentres.toElement();
00130 if(costcentre.tagName() == "costcentre")
00131 {
00132 costcentreids.append(costcentre.attribute("id", "-1").toInt());
00133 }
00134 costcentres = costcentres.nextSibling();
00135 }
00136 p_product->setCostCentreIDsForSurcharge(costcentreids);
00137 }
00138 surcharges = surcharges.nextSibling();
00139 }
00140 }
00141 users = users.nextSibling();
00142 }
00143 }
00144 products = products.nextSibling();
00145 }
00146
00147 setProduct(p_product->productName(), QString::number(p_product->directCosts()), QString::number(p_product->markup()), QString::number(p_product->commission()), QString::number(p_product->tradeDiscount()), QString::number(p_product->discount()), QString::number(p_product->salesTax()));
00148 qDebug()<<"end loadDataNode";
00149 return true;
00150 }
00151
00152 int CostUnitModel::rowCount(const QModelIndex &parent) const
00153 {
00154 Q_UNUSED(parent);
00155 return entries.size();
00156 }
00157
00158 int CostUnitModel::columnCount(const QModelIndex &parent) const
00159 {
00160 Q_UNUSED(parent);
00161 return 2;
00162 }
00163
00164 QVariant CostUnitModel::headerData(int section, Qt::Orientation orientation, int role) const
00165 {
00166 if(role != Qt::DisplayRole)
00167 {
00168 return QVariant();
00169 }
00170
00171 if(orientation == Qt::Horizontal)
00172 {
00173 return "";
00174 }
00175 return QVariant();
00176 }
00177
00178 QVariant CostUnitModel::data(const QModelIndex &index, int role) const
00179 {
00180 if(!index.isValid())
00181 {
00182 return QVariant();
00183 }
00184
00185 if(index.row() >= entries.size() || index.row() < 0)
00186 {
00187 return QVariant();
00188 }
00189 if(role == Qt::BackgroundRole)
00190 {
00191 if(index.row() == rowCount(QModelIndex())-1)
00192 {
00193 QColor c;
00194 c.setNamedColor("#eaeaea");
00195 return c;
00196 }
00197 else
00198 {
00199 if(index.row() %2 ==0)
00200 {
00201 return QColor(Qt::white);
00202 }
00203 else
00204 {
00205 QColor c;
00206 c.setNamedColor("#f3f3f3");
00207 return c;
00208 }
00209 }
00210 }
00211 if(role == Qt::DecorationRole)
00212 {
00213 QStringList pair = entries.value(index.row());
00214 if(index.column() == 0)
00215 {
00216 if(pair.value(0).contains("Selbstkosten"))
00217 {
00218 return QColor(settings->productionCostsCUColor());
00219 }
00220 else if(pair.value(0).contains("Gewinn"))
00221 {
00222 return QColor(settings->profitCUColor());
00223 }
00224 else if(pair.value(0).contains("Provisionen"))
00225 {
00226 return QColor(settings->commissionCUColor());
00227 }
00228 else if(pair.value(0).contains("Skonto"))
00229 {
00230 return QColor(settings->tradeDiscountCUColor());
00231 }
00232 else if(pair.value(0).contains("Rabatte"))
00233 {
00234 return QColor(settings->discountCUColor());
00235 }
00236 else if(pair.value(0).contains("Umsatzsteuer"))
00237 {
00238 return QColor(settings->salesTaxCUColor());
00239 }
00240 }
00241 }
00242 if(role == Qt::DisplayRole)
00243 {
00244 QStringList pair = entries.value(index.row());
00245 if(index.column() == 0)
00246 {
00247 return pair.value(0);
00248 }
00249 else if(index.column() == 1)
00250 {
00251 return pair.value(1);
00252 }
00253 }
00254 return QVariant();
00255 }
00256
00257 bool CostUnitModel::insertRows(int position, int rows, const QModelIndex &index)
00258 {
00259 Q_UNUSED(index);
00260 beginInsertRows(QModelIndex(), position, position+rows-1);
00261
00262 for (int row=0; row < rows; row++)
00263 {
00264 QStringList pair;
00265 pair<<""<<""<<"";
00266 entries.insert(position, pair);
00267 }
00268
00269 endInsertRows();
00270 return true;
00271 }
00272
00273 bool CostUnitModel::removeRows(int position, int rows, const QModelIndex &index)
00274 {
00275 Q_UNUSED(index);
00276 beginRemoveRows(QModelIndex(), position, position+rows-1);
00277
00278 for (int row=0; row < rows; ++row)
00279 {
00280 entries.removeAt(position);
00281 }
00282
00283 endRemoveRows();
00284 return true;
00285 }
00286
00287 bool CostUnitModel::setData(const QModelIndex &index, const QVariant &value, int role)
00288 {
00289 if(index.isValid() && role == Qt::EditRole)
00290 {
00291 int row = index.row();
00292 QStringList p = entries.value(row);
00293 if(index.column() == 0)
00294 {
00295 p.replace(0,value.toString());
00296 }
00297 else if(index.column() == 1)
00298 {
00299 p.replace(1,value.toString());
00300 }
00301 else
00302 {
00303 return false;
00304 }
00305
00306 entries.replace(row, p);
00307 setModified(true);
00308 emit(dataChanged(index, index));
00309 return true;
00310 }
00311 return false;
00312 }
00313
00314 Qt::ItemFlags CostUnitModel::flags(const QModelIndex &index) const
00315 {
00316 if(!index.isValid())
00317 {
00318 return Qt::ItemIsEnabled;
00319 }
00320 return QAbstractTableModel::flags(index) | Qt::ItemIsEditable;
00321 }
00322
00323 void CostUnitModel::clear()
00324 {
00325 if(rowCount(QModelIndex())>0)
00326 {
00327 removeRows(0,rowCount(QModelIndex()));
00328 }
00329 p_product = new CostUnitProduct();
00330 }
00331
00332 QList<QStringList> CostUnitModel::getList()
00333 {
00334 return entries;
00335 }
00336
00337 bool CostUnitModel::isModified()
00338 {
00339 return modified;
00340 }
00341
00342 void CostUnitModel::setModified(bool b)
00343 {
00344 modified = b;
00345 }
00346
00347 void CostUnitModel::setAdditionalCharges(QList<int> data)
00348 {
00349 qDebug()<<"TODO: costcentres for additional charges";
00350 if(!p_product)
00351 {
00352 p_product = new CostUnitProduct();
00353 }
00354 p_product->setCostCentreIDsForSurcharge(data);
00355 }
00356
00357 void CostUnitModel::setProduct(QString textProductName, QString textDirectCosts, QString textMarkup, QString textCommission, QString textTradeDiscount, QString textDiscount, QString textSalesTax)
00358 {
00359 if(textProductName != "")
00360 {
00361 bool ok;
00362 textDirectCosts.toDouble(&ok) && textMarkup.toDouble(&ok) && textCommission.toDouble(&ok) && textTradeDiscount.toDouble(&ok) && textDiscount.toDouble(&ok) && textSalesTax.toDouble(&ok);
00363 if(!ok)
00364 {
00365 QMessageBox::warning(NULL, tr("Fehler"), tr("Sie haben ungültige Daten eingegeben!"));
00366 return ;
00367 }
00368 else
00369 {
00370 p_product = new CostUnitProduct();
00371 p_product->setProductName(textProductName);
00372 p_product->setProductData(textDirectCosts, textMarkup, textCommission, textTradeDiscount, textDiscount, textSalesTax);
00373
00374 createPieChart();
00375 }
00376 }
00377 else
00378 {
00379 QMessageBox::warning(NULL, tr("Fehler"), tr("Sie müssen einen Produktnamen eingeben!"));
00380 return ;
00381 }
00382 }
00383
00384 void CostUnitModel::createPieChart()
00385 {
00386 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00387 QModelIndex i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00388 setData(i, tr("Selbstkosten"), Qt::EditRole);
00389 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00390 setData(i, QString::number(p_product->productionCosts()), Qt::EditRole);
00391
00392 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00393 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00394 setData(i, "+ " + QString::number(p_product->markup()) + tr("% Gewinn"), Qt::EditRole);
00395 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00396 setData(i, QString::number(p_product->profitCalculation()), Qt::EditRole);
00397
00398 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00399 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00400 setData(i, tr("Nettoverkaufspreis"), Qt::EditRole);
00401 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00402 setData(i, QString::number(p_product->netSalesPrice()), Qt::EditRole);
00403
00404 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00405 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00406 setData(i, "+ " + QString::number(p_product->commission()) + tr("% Provisionen"), Qt::EditRole);
00407 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00408 setData(i, QString::number(p_product->commissionCalculation()), Qt::EditRole);
00409
00410 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00411 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00412 setData(i, tr("Kassapreis"), Qt::EditRole);
00413 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00414 setData(i, QString::number(p_product->cashPrice()), Qt::EditRole);
00415
00416 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00417 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00418 setData(i, "+ " + QString::number(p_product->tradeDiscount()) + tr("% Skonto"), Qt::EditRole);
00419 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00420 setData(i, QString::number(p_product->discountCalculation()), Qt::EditRole);
00421
00422 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00423 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00424 setData(i, tr("Zielpreis"), Qt::EditRole);
00425 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00426 setData(i, QString::number(p_product->targetPrice()), Qt::EditRole);
00427
00428 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00429 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00430 setData(i, "+ " + QString::number(p_product->discount()) + tr("% Rabatte"), Qt::EditRole);
00431 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00432 setData(i, QString::number(p_product->salesDiscountCalculation()), Qt::EditRole);
00433
00434 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00435 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00436 setData(i, tr("Bruttoverkaufspreis exkl. USt."), Qt::EditRole);
00437 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00438 setData(i, QString::number(p_product->grossSalesPriceWithoutSalesTax()), Qt::EditRole);
00439
00440 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00441 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00442 setData(i, "+ " + QString::number(p_product->salesTax()) + tr("% Umsatzsteuer"), Qt::EditRole);
00443 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00444 setData(i, QString::number(p_product->salesTaxCalculation()), Qt::EditRole);
00445
00446 insertRows(rowCount(QModelIndex()), 1, QModelIndex());
00447 i = index(rowCount(QModelIndex())-1, 0, QModelIndex());
00448 setData(i, tr("Bruttoverkaufspreis inkl. USt."), Qt::EditRole);
00449 i = index(rowCount(QModelIndex())-1, 1, QModelIndex());
00450 setData(i, QString::number(p_product->grossSalesPriceWithSalesTax()), Qt::EditRole);
00451
00452 dataChanged(QModelIndex(), QModelIndex());
00453 }