Logo Search packages:      
Sourcecode: lemonpos-kde4 version File versions  Download package

bool lemonView::incrementTableItemQty ( QString  code,
double  q 
) [private, slot]

Slot to search for a product code in the table, if found then qty is incremented.

Definition at line 634 of file lemonview.cpp.

References displayItemInfo(), and refreshTotalLabel().

Referenced by insertItem(), and itemSearchDoubleClicked().

{
  double qty  = 1;
  double price=0.0;
  double discount_old=0.0;
  double qty_old=0.0;
  double stockqty=0;
  int    rows=0;
  bool done=false;
  ProductInfo info;

  //Este es el fix para el TODO de abajo..
//   if (productsHash.contains(code.toULongLong())) {
// 
//   }

  //This for cycle whas done before implementation of the productsHash...
  //TODO: Really FIX this, replace this for cycle code with a if.. with the productsHash... is more efficient.
  // Also the code to get stockqty from database could be replaced by this productsHash... all info is in there!
  for (int row=0; row<ui_mainview.tableWidget->rowCount(); ++row)
  {
    QTableWidgetItem *item = ui_mainview.tableWidget->item(row, colCode);//item code
    if (item->data(Qt::DisplayRole).canConvert(QVariant::String)) {
      QString text = item->data(Qt::DisplayRole).toString();
      if (text == code ) {
        //CONSULT DB, we need to know if there is stockqty availability
        if (!db.isOpen()) db.open();
        bool ok = db.isOpen();
        if (ok) {
          QSqlQuery query(db);
          QString qry = QString("SELECT stockqty FROM products WHERE code=%1").arg(code);
          if (!query.exec(qry)) {
            int errNum = query.lastError().number();
            QSqlError::ErrorType errType = query.lastError().type();
            QString errStr = query.lastError().text();
            QString details = i18n("Error #%1, Type:%2\n'%3'",QString::number(errNum), QString::number(errType),errStr);
            KMessageBox::detailedError(this, i18n("Lemon has encountered an error when querying the database, click details to see the error details."), details, i18n("Error"));
          }
          rows = query.size();
          while (query.next()) { //its supposed to have only one record...
            int fieldStockQty = query.record().indexOf("stockqty");
            stockqty = query.value(fieldStockQty).toDouble();//FIXED, it was int
          }
        }//if database open
        else {
        //There is an error openning data base..
          int errNum = db.lastError().number();
          QSqlError::ErrorType errType = db.lastError().type();
          QString errStr = db.lastError().text();
          QString details = i18n("Error #%1, Type:%2\n'%3'",QString::number(errNum), QString::number(errType),errStr);
          KMessageBox::detailedError(this, i18n("Lemon has encountered an error when openning database, click details to see the error details."), details, i18n("Error"));
        }
        //get item price
        QTableWidgetItem *itemP = ui_mainview.tableWidget->item(row, colPrice);//item price
        if (itemP->data(Qt::DisplayRole).canConvert(QVariant::Double))
          price = itemP->data(Qt::DisplayRole).toDouble();
        //get item qty to increment it by one...
        QTableWidgetItem *itemQ = ui_mainview.tableWidget->item(row, colQty);//item qty
        if ( itemQ->data(Qt::DisplayRole).canConvert(QVariant::Double) ) {
          qty = itemQ->data(Qt::DisplayRole).toDouble();
          qty_old = qty;
          if (stockqty>=q+qty) qty+=q; else {
            QString msg = i18n("<html><font color=red weight=bold>Product not available in stock.</font>");
            if (ui_mainview.groupWidgets->currentIndex() == pageMain) {
              ui_mainview.labelInsertCodeMsg->setText(msg);
              QTimer::singleShot(3000, this, SLOT(clearLabelInsertCodeMsg()));
            }
            if (ui_mainview.groupWidgets->currentIndex() == pageSearch) {
              ui_mainview.labelSearchMsg->setText(msg);
              QTimer::singleShot(3000, this, SLOT(clearLabelSearchMsg()) );
            }
          }
          itemQ->setData(Qt::EditRole, QVariant(qty));
          done = true;
        }//inc qty

        //get item discount
        QTableWidgetItem *itemD = ui_mainview.tableWidget->item(row, colDisc);//item discount
        if (itemD->data(Qt::DisplayRole).canConvert(QVariant::Double))
          discount_old = itemD->data(Qt::DisplayRole).toDouble();
        //calculate new discount
        double discountperitem = (discount_old/qty_old);
        double newdiscount = discountperitem*qty;
        itemD->setData(Qt::EditRole, QVariant(newdiscount));

        qulonglong c = code.toULongLong();
        if (productsHash.contains(c)) {
          info = productsHash.take(c);
          info.qtyOnList = qty; //qty is already added q+qty..
          productsHash.insert(c, info);
        }

        //get item Due to update it.
        QTableWidgetItem *itemDue = ui_mainview.tableWidget->item(row, colDue); //4 item Due
        itemDue->setData(Qt::EditRole, QVariant((price*qty)-newdiscount));
        refreshTotalLabel();
        displayItemInfo(item);
        ui_mainview.editItemCode->clear();
        break;
      }
    }
  }
  return done;
}


Generated by  Doxygen 1.6.0   Back to index