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

void lemonView::insertItem ( QString  code  )  [private, slot]

Slot used to get a product info from the database and insert it to the table

Definition at line 739 of file lemonview.cpp.

References doInsertItem(), and incrementTableItemQty().

Referenced by itemSearchDoubleClicked().

{
  QString desc="";
  double qty  = 1; //by default 1, TODO: falta hacer que pueda modificarse esto manualmente.
  double price=0.0;
  double discount=0.0;
  QByteArray photo;
  int units;
  double cost;
  double tax1;
  double tax2;
  int    category;
  int    validuntilday=0;
  int    validuntilmonth=0;
  int    validuntilyear=0;
  double stockqty=0;
  int    rows = 0;
  qulonglong thecode;
  qulonglong points=0;
  QString uLabel;
  ProductInfo info;


  if (!incrementTableItemQty(code, qty) ) {
    if (!db.isOpen()) db.open();
    bool ok = db.isOpen();
    if (ok) {
      QSqlQuery query(db); QSqlQuery query2(db); //using 1 query variable, sometimes crashes the app. (i think due to time to complete the query1, and executing query2 while not finished query1)
      QString qry = QString("SELECT * 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"));
      } //FIXME: Could be necesary to add an else satement here?.. in case the query does not execute, does not continue the rest of the code...
      rows = query.size();
      while (query.next()) {
        int fieldPrice    = query.record().indexOf("price");
        int fieldDesc     = query.record().indexOf("name");
        int fieldStockQty = query.record().indexOf("stockqty");
        int fieldCost     = query.record().indexOf("cost");
        int fieldUnits    = query.record().indexOf("units");
        int fieldTaxp     = query.record().indexOf("taxpercentage");
        int fieldEtraTax  = query.record().indexOf("extrataxes");
        int fieldCategory = query.record().indexOf("category");
        int fieldPhoto    = query.record().indexOf("photo");
        int fieldCode     = query.record().indexOf("code");
        int fieldPoints   = query.record().indexOf("points");
        thecode       = query.value(fieldCode).toULongLong();
        desc     = query.value(fieldDesc).toString();
        price    = query.value(fieldPrice).toDouble();
        stockqty = query.value(fieldStockQty).toDouble();
        cost     = query.value(fieldCost).toDouble();
        units    = query.value(fieldUnits).toInt();
        tax1     = query.value(fieldTaxp).toDouble();
        tax2     = query.value(fieldEtraTax).toDouble();
        category = query.value(fieldCategory).toInt();
        photo    = query.value(fieldPhoto).toByteArray();
        points   = query.value(fieldPoints).toULongLong();
        //Add to productsHash
        info.code = thecode;
        info.desc = desc;
        info.price = price;
        info.cost = cost;
        info.tax  = tax1;
        info.extratax = tax2;
        double pWOtax = price/(1+((tax1+tax2)/100));
        info.totaltax = pWOtax*((tax1+tax2)/100); // in money...
        info.photo = photo;
        info.stockqty = stockqty;
        info.units = units;
        info.category = category;
        info.utility = cost - price;
        info.points = points;
        info.qtyOnList = qty;
      }
      qry = QString("SELECT * from offers WHERE product_id=%1").arg(code);
      if (!query2.exec(qry)) {
        int errNum = query.lastError().number();
        QSqlError::ErrorType errType = query2.lastError().type();
        QString errStr = query2.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"));
      }
      //int drows = query2.size();
      //FIXME:Here, could be more than one row for the result (2 discounts for a product, in diferent dates)
      while (query2.next()) {
        int fieldDiscount = query2.record().indexOf("discount");
        int fieldValidUntilday = query2.record().indexOf("validuntilday");
        int fieldValidUntilmonth = query2.record().indexOf("validuntilmonth");
        int fieldValidUntilyear = query2.record().indexOf("validuntilyear");
        discount = query2.value(fieldDiscount).toDouble();
        info.discpercentage = discount; //Discount in percentage...
        validuntilday = query2.value(fieldValidUntilday).toInt();
        validuntilmonth = query2.value(fieldValidUntilmonth).toInt();
        validuntilyear  = query2.value(fieldValidUntilyear).toInt();
      }
      //get units descriptions
      qry = QString("SELECT * from measures WHERE id=%1").arg(info.units);
      QSqlQuery query3(db);
      if (!query3.exec(qry)) {
        int errNum = query.lastError().number();
        QSqlError::ErrorType errType = query3.lastError().type();
        QString errStr = query3.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"));
      }
      while (query3.next()) {
        int fieldUD = query3.record().indexOf("text");
        uLabel = query3.value(fieldUD).toString();
        info.unitStr=uLabel; //Added: Dec 15 2007
      }//query3 - get descritptions
    }//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"));
    }
    //CHeck for a valid discount.
    QDate  date = QDate::currentDate();
    QDate  discountdate = QDate(validuntilyear, validuntilmonth, validuntilday);
    if (discountdate < date) {
      discount = 0.0;
      info.validDiscount=false;
      info.disc = 0.0;
    } else {
      discount = ((discount/100)*price); //discount is finally in money, not in percentage.
      info.validDiscount=true;
      info.disc = discount;
    }
    //now we have the discount to incorporate it to the hash...
    productsHash.insert(thecode, info);
    QString msg;
    if ( rows == 0 )  msg = i18n("<html><font color=red weight=bold>Product not found in database.</font></html>");
    else if ( rows > 0 && stockqty >=  qty ) doInsertItem(code, desc, qty, price, discount, uLabel); else
            msg = i18n("<html><font color=red weight=bold>Product not available in stock.</font></html>");
            if (!msg.isEmpty()) {
              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()) );
              }
              ui_mainview.editItemCode->clear();
            }
  }
}


Generated by  Doxygen 1.6.0   Back to index