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

void lemonView::finishCurrentTransaction (  )  [private, slot]

Slot used to finish current TRansaction. It saves transaction on database.

Definition at line 1190 of file lemonview.cpp.

References Gaveta::addCard(), Gaveta::addCash(), Gaveta::incCardTransactions(), Gaveta::incCashTransactions(), Gaveta::insertTransactionId(), Gaveta::open(), printTicket(), refreshTotalLabel(), and Gaveta::substractCash().

{
  bool canfinish = true;
  refreshTotalLabel();
  QString msg;
  ui_mainview.groupWidgets->setCurrentIndex(pageMain);
  if (ui_mainview.editAmount->text().isEmpty()) ui_mainview.editAmount->setText("0.0");
  if (ui_mainview.checkCash->isChecked()) {
    if (ui_mainview.editAmount->text().toDouble()<totalSum) {
      canfinish = false;
      ui_mainview.editAmount->setFocus();
      ui_mainview.editAmount->setStyleSheet("background-color: rgb(255,100,0); color:white; selection-color: white; font-weight:bold;");
      ui_mainview.editCardNumber->setStyleSheet("");
      ui_mainview.editAmount->setSelection(0, ui_mainview.editAmount->text().length());
      msg = i18n("<html><font color=red weight=bold>Please fill the correct pay amount before finishing a transaction.</font></html>");
      ui_mainview.labelPayMsg->setText(msg);
      QTimer::singleShot(3000, this, SLOT(clearLabelPayMsg()));
    }
  }
  else {
    if (!ui_mainview.editCardNumber->hasAcceptableInput()) {
      canfinish = false;
      ui_mainview.editCardNumber->setFocus();
      ui_mainview.editCardNumber->setStyleSheet("background-color: rgb(255,100,0); color:white; font-weight:bold; selection-color: white;");
      ui_mainview.editAmount->setStyleSheet("");
      ui_mainview.editCardNumber->setSelection(0, ui_mainview.editCardNumber->text().length());
      msg = i18n("<html><font color=red weight=bold>Please enter the card number.</font></html>");
    }
    else if (!ui_mainview.editCardAuthNumber->hasAcceptableInput()) {
      canfinish = false;
      ui_mainview.editCardAuthNumber->setFocus();
      ui_mainview.editCardAuthNumber->setStyleSheet("background-color: rgb(255,100,0); color:white; font-weight:bold; selection-color: white;");
      ui_mainview.editAmount->setStyleSheet("");
      ui_mainview.editCardAuthNumber->setSelection(0, ui_mainview.editCardAuthNumber->text().length());
      msg = i18n("<html><font color=red weight=bold>Please enter the Authorisation number from the bank voucher.</font></html>");
    }
    ui_mainview.labelPayMsg->setText(msg);
    QTimer::singleShot(3000, this, SLOT(clearLabelPayMsg()));
  }
  if (ui_mainview.tableWidget->rowCount() == 0) canfinish = false;
  if (!canStartSelling()) {
    canfinish=false;
    KMessageBox::sorry(this, i18n("Before selling, you must start operations."));
  }

  if (canfinish)
  {
    ui_mainview.editAmount->setStyleSheet("");
    ui_mainview.editCardNumber->setStyleSheet("");
    PaymentType      pType;
    TransactionState tState = tCompleted;
    double           payWith = 0.0;
    double           payTotal = 0.0;
    double           changeGiven = 0.0;
    QString          authnumber = "'[Not Used]'";
    QString          cardNum = "'[Not Used]'";
    QString          paidStr = "'[Not Available]'";
    int              termnum = Settings::editTerminalNumber();
    QString qry;
    QStringList ilist;
    payTotal = totalSum;
    if (ui_mainview.checkCash->isChecked()) {
      pType = pCash;
      if (!ui_mainview.editAmount->text().isEmpty()) payWith = ui_mainview.editAmount->text().toDouble();
      changeGiven = payWith- totalSum;
    } else {
      pType = pCard;
      if (ui_mainview.editCardNumber->hasAcceptableInput()) cardNum = ui_mainview.editCardNumber->text();
      if (ui_mainview.editCardAuthNumber->hasAcceptableInput()) authnumber = ui_mainview.editCardAuthNumber->text();
      cardNum = "'"+cardNum+"'";
      authnumber = "'"+authnumber+"'";
      payWith = payTotal; //NOTE: Is this right? well, yes, paid is exact the total amount.
    }
    if (!db.isOpen()) db.open();
    bool ok = db.isOpen();
    if (ok) {
      int icount=0; //items sold (5 items sold: 4 sodas and 0.500 kg of sugar)...
      QDate date = QDate::currentDate();
      QString datestr = date.toString("d/MM/yyyy");
      //Update Products StockQTY for each sold product
      if (ui_mainview.tableWidget->rowCount()>0) {
        for (int row=0; row<ui_mainview.tableWidget->rowCount(); ++row)
        {
          QSqlQuery query(db);
          //we need to know how many items...
          QTableWidgetItem *itemq = ui_mainview.tableWidget->item(row, colQty);//item qty
          double iq = 0;
          QTableWidgetItem *itemcodei = ui_mainview.tableWidget->item(row, colCode);
          qulonglong icode = itemcodei->data(Qt::DisplayRole).toULongLong();
          ProductInfo info = productsHash.value(icode);
          if (itemq->data(Qt::DisplayRole).canConvert(QVariant::Double)) iq = itemq->data(Qt::DisplayRole).toDouble();
          QTableWidgetItem *item = ui_mainview.tableWidget->item(row, colCode);
          if (item->data(Qt::DisplayRole).canConvert(QVariant::String)) {
            QString pcode = item->data(Qt::DisplayRole).toString();
            if (info.units == uPiece) icount += iq; else icount += 1; // :)
            ilist.append(pcode+"/"+QString::number(iq));
            qry = QString("UPDATE products SET stockqty=stockqty-%1 , soldunits=soldunits+%2 , datelastsold=%3 WHERE code=%4")
                .arg(iq)
                .arg(iq)
                .arg("'"+datestr+"'")
                .arg(pcode);
            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("Finish Transaction: Error"));
            }//qry error
          }
        }//for
      }//if row>0
      // Update transactions
      QString ilistStr = "'" + ilist.join(",") + "'";
      QSqlQuery query2(db);
      qry = QString("UPDATE transactions SET clientid=%15, amount=%1 , paidwith=%2 , paymethod=%3 , state=%4 , cardnumber=%5 , changegiven=%6 , itemcount=%7 , itemslist=%8 , cardauthnumber=%9 , terminalnum=%10, points=%12, disc=%13, discmoney=%14 WHERE id=%11")
          .arg(payTotal)
          .arg(payWith)
          .arg(pType)
          .arg(tState)
          .arg(cardNum)
          .arg(changeGiven)
          .arg(icount)
          .arg(ilistStr)
          .arg(authnumber)
          .arg(termnum)
          .arg(currentTransaction)
          .arg(buyPoints)
          .arg(clientInfo.discount)
          .arg(discMoney)
          .arg(clientInfo.id);
      if (!query2.exec(qry)) {
        int errNum = query2.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("Update Transaction: Error"));
      }
      // Update client points
      QSqlQuery query3(db);
      qry = QString("UPDATE clients SET  points=points+%1 WHERE id=%2")
        .arg(buyPoints)
        .arg(clientInfo.id);
      if (!query2.exec(qry)) {
        int errNum = query2.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("Update Transaction: Error"));
      }
      if (drawer) {
        //FIXME: What to di first?... add or substract?... when there is No money or there is less money than the needed for the change.. what to do?
        if (ui_mainview.checkCash->isChecked()) {
          drawer->addCash(payWith);
          drawer->substractCash(changeGiven);
          drawer->incCashTransactions();
          drawer->open();
        } else {
          drawer->incCardTransactions();
          drawer->addCard(payWith);
        }
        drawer->insertTransactionId(getCurrentTransaction());
      }
      else {
        KMessageBox::error(this, i18n("The Drawer is not initialized, please start operation first."), i18n("Error") );
      }
      //update client info in the hash....
      clientInfo.points += buyPoints;
      clientsHash.remove(QString::number(clientInfo.id));
      clientsHash.insert(QString::number(clientInfo.id), clientInfo);
      updateClientInfo();
      TicketInfo ticket;
      ticket.number = currentTransaction;
      ticket.total  = payTotal;
      ticket.change = changeGiven;
      ticket.paidwith = payWith;
      ticket.itemcount = icount;
      ticket.cardnum = cardNum;
      ticket.cardAuthNum = authnumber;
      ticket.paidWithCard = ui_mainview.checkCard->isChecked();
      ticket.clientDisc = clientInfo.discount;
      ticket.clientDiscMoney = discMoney;
      ticket.buyPoints = buyPoints;
      ticket.clientPoints = clientInfo.points;
      printTicket(ticket);
      transactionInProgress = false;
      updateModelView();
    }
    else {
      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 saving transaction, click details to see the error details."), details, i18n("Error"));
    }
    ui_mainview.editItemCode->setFocus();
  }
}


Generated by  Doxygen 1.6.0   Back to index