/src/Towary.cpp

https://github.com/rafalrusin/qfaktury · C++ · 188 lines · 128 code · 30 blank · 30 comment · 17 complexity · 158221a1f11bd6bdc4602760d8c6fb4f MD5 · raw file

  1. #include "moc_Towary.cpp"
  2. #include "Settings.h"
  3. #include <QMessageBox>
  4. #include <QDesktopServices>
  5. #include <QUrl>
  6. #include <QtDebug>
  7. /** Constructor
  8. */
  9. Towary::Towary(QWidget *parent, int mode, IDataLayer *dl): QDialog(parent) {
  10. workMode = mode;
  11. dataLayer = dl;
  12. setupUi(this);
  13. init();
  14. }
  15. /** Init
  16. */
  17. void Towary::init() {
  18. selectData("", 0);
  19. jednCombo->addItems(sett().value("jednostki").toString().split("|"));
  20. cbVat->addItems(sett().value("stawki").toString().split("|"));
  21. connect(okButton, SIGNAL(clicked()), this, SLOT(okClick()));
  22. connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
  23. connect(nettoEdit, SIGNAL(valueChanged(double)), this, SLOT(nettoChanged(double)));
  24. connect(spinBox2, SIGNAL(valueChanged(int)), this, SLOT(spinChanged(int)));
  25. connect(pkwiuBtn, SIGNAL(clicked()), this, SLOT(pkwiuGet()));
  26. }
  27. /******************** SLOTS START ***************************/
  28. /** Slot
  29. * save data to XML file and returns row for products table
  30. */
  31. void Towary::okClick() {
  32. if (nameEdit->text() == "") {
  33. QMessageBox::critical(0, "QFaktury", trUtf8("Musisz podać nazwe."));
  34. return;
  35. }
  36. QString pkwiu = pkwiuEdit->text();
  37. if (pkwiu == "")
  38. pkwiu = " ";
  39. QString skrot = skrotEdit->text();
  40. if (skrot == "")
  41. skrot = " ";
  42. QString kod = kodEdit->text();
  43. if (kod == "")
  44. kod = " ";
  45. if (workMode == 1) {
  46. updateData();
  47. ret = idxEdit->text() + "|" + nameEdit->text() + "|" + skrot + "|"
  48. + kod + "|" + pkwiu + "|" + typeCombo->currentText() + "|"
  49. + jednCombo->currentText() + "|" + netto[0] + "|"
  50. + netto[1] + "|" + netto[2] + "|" + netto[3] + "|"
  51. + cbVat->currentText();
  52. accept();
  53. } else {
  54. if (insertData()) {
  55. ret = idxEdit->text() + "|" + nameEdit->text() + "|" + skrot + "|"
  56. + kod + "|" + pkwiu + "|" + typeCombo->currentText() + "|"
  57. + jednCombo->currentText() + "|" + netto[0] + "|"
  58. + netto[1] + "|" + netto[2] + "|" + netto[3] + "|"
  59. + cbVat->currentText();
  60. accept();
  61. }
  62. }
  63. }
  64. /** Slot
  65. * spinBox with list of prices changed
  66. */
  67. void Towary::spinChanged(int a) {
  68. nettoEdit->setValue(netto[a - 1].toDouble());
  69. }
  70. /** Slot
  71. * Nett value changed
  72. */
  73. void Towary::nettoChanged(double ) {
  74. // qDebug ()<<nettoEdit->text ();
  75. netto[spinBox2->value() - 1] = nettoEdit->cleanText();
  76. }
  77. /** Slot
  78. * Find PKWIU code on the net
  79. */
  80. void Towary::pkwiuGet() {
  81. QDesktopServices::openUrl(QUrl(tr("http://www.klasyfikacje.pl/")));
  82. }
  83. /******************** SLOTS END ***************************/
  84. /** Loads data into the form
  85. */
  86. void Towary::selectData(QString idx, int type) {
  87. if (idx == "") {
  88. netto.append("0");
  89. netto.append("0");
  90. netto.append("0");
  91. netto.append("0");
  92. nettoEdit->setValue(0);
  93. } else {
  94. setWindowTitle(trUtf8("Edytuj towar/usługę"));
  95. typeCombo->setEnabled(false);
  96. }
  97. ProductData prodData = dataLayer->productsSelectData(idx, type);
  98. if (workMode==0) {
  99. idx = QString::number(prodData.lastProdId);
  100. idxEdit->setText(idx);
  101. } else {
  102. getData(prodData);
  103. }
  104. typeCombo->setCurrentIndex(type);
  105. }
  106. /** Saves data from the form
  107. */
  108. bool Towary::insertData() {
  109. nettoChanged(0);
  110. ProductData prodData;
  111. setData(prodData);
  112. dataLayer->productsInsertData(prodData, typeCombo->currentIndex());
  113. return true;
  114. }
  115. /** Modify product
  116. * Searches for the right one and saves it.
  117. */
  118. bool Towary::updateData() {
  119. nettoChanged(0);
  120. ProductData prodData;
  121. setData(prodData);
  122. dataLayer->productsUpdateData(prodData, typeCombo->currentIndex(), idxEdit->text() );
  123. return true;
  124. }
  125. /** Load from the form to Data object
  126. */
  127. void Towary::getData(ProductData prodData) {
  128. idxEdit->setText(QString::number(prodData.id));
  129. nameEdit->setText(prodData.name);
  130. kodEdit->setText(prodData.code);
  131. skrotEdit->setText(prodData.desc);
  132. pkwiuEdit->setText(prodData.pkwiu);
  133. int current = 0;
  134. current = sett().value("jednostki").toString().split("|").indexOf(prodData.quanType);
  135. jednCombo->setCurrentIndex(current);
  136. current = sett().value("stawki").toString().split("|").indexOf(QString::number(prodData.vat));
  137. cbVat->setCurrentIndex(current);
  138. nettoEdit->setValue(prodData.prices[0]);
  139. netto[0] = sett().numberToString( prodData.prices[0]);
  140. netto[1] = sett().numberToString( prodData.prices[1]);
  141. netto[2] = sett().numberToString( prodData.prices[2]);
  142. netto[3] = sett().numberToString( prodData.prices[3]);
  143. }
  144. /** Display productData
  145. */
  146. void Towary::setData(ProductData &prodData) {
  147. prodData.id = idxEdit->text().toInt();
  148. prodData.name = nameEdit->text();
  149. prodData.desc = skrotEdit->text();
  150. prodData.code = kodEdit->text();
  151. prodData.pkwiu = pkwiuEdit->text();
  152. prodData.quanType = jednCombo->currentText();
  153. prodData.prices[0] = sett().stringToDouble(netto[0]);
  154. prodData.prices[1] = sett().stringToDouble(netto[1]);
  155. prodData.prices[2] = sett().stringToDouble(netto[2]);
  156. prodData.prices[3] = sett().stringToDouble(netto[3]);
  157. prodData.vat = cbVat->currentText().toInt();
  158. }