0

I'm having problem with the calculations. When build the system and test it, the correct amount showed in the interface isn't showing up/saved in database. The amount is something different than the one in the interface.

Note:

  1. SELECT SUM(TotalPrice) FROM Sold (Screenshot of the Sold table will be attached together.
  2. I will attach the screenshots below. Have a look on it to get clear picture of the situation.
  3. I've marked the problem code with the comment //<--THIS-->//.

Here's my code:

void CashRegister::on_pushButton_next_clicked()
{
    ui->tableView->show();
    ui->pushButton_finish->show();
    ui->label_totalAmount->show();

    QString receiptNo = ui->label_receiptNum->text();
    QString itemSelect = ui->comboBox_itemID->currentText();
    QString qtySold = ui->lineEdit_qtySold->text();
    QDate currentDate = QDate::currentDate();   //<--Solved-->//
    //QString totalPrice1 = ui->label_totalAmount->text();

    double price=0, totalPrice=0;

    QSqlQuery myqry;
    myqry.exec("SELECT ItemPrice, ItemName FROM Item WHERE ItemId='"+itemSelect+"' ");
    if (myqry.next())
        price = myqry.value(0).toDouble();

    totalPrice = price * qtySold.toInt();

    QString totalPriceString = QString::number(totalPrice);

    myqry.prepare("INSERT INTO Sold (ReceiptId, ItemId, SoldQuantity, TotalPrice) VALUES ('"+receiptNo+"', '"+itemSelect+"', '"+qtySold+"', '"+totalPriceString+"') ");
    myqry.exec();

    ui->comboBox_itemID->setCurrentIndex(0);
    ui->lineEdit_qtySold->clear();

    myqry.prepare("SELECT Item.ItemId, Item.ItemName, Item.ItemPrice, Sold.SoldQuantity FROM Item, Sold WHERE Item.ItemId = Sold.ItemId AND Sold.ReceiptId='"+receiptNo+"' ");
    myqry.exec();


    QSqlQueryModel *myModel = new QSqlQueryModel();
    myModel->setQuery(myqry);
    ui->tableView->setModel(myModel);

    double sum = 0;

    myqry.exec("SELECT SUM(TotalPrice) FROM Sold WHERE ReceiptId = '"+receiptNo+"' ");
    if (myqry.next())
        sum += myqry.value(0).toDouble();

    ui->label_totalAmount->setText("TOTAL AMOUNT: RM" + QString::number(sum));

    QString totalPriceReceipt = QString::number(sum);

    //<--THIS-->//
    myqry.prepare("INSERT INTO Receipt (ReceiptId, ReceiptDate, ReceiptAmount) VALUES ('"+receiptNo+"', '"+currentDate.toString()+"', '"+totalPriceReceipt+"') ");
    myqry.exec();
}

Different amount in Interface and DB

Sold Table

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
joeBoy69
  • 21
  • 3
  • provide a [mcve] – eyllanesc May 12 '18 at 15:40
  • The SQLite database does not notify changes to your clients, so I recommend you close the application you use to inspect the database and open it back to see the changes or press the sync button. – eyllanesc May 12 '18 at 15:44
  • @eyllanesc I already did it but the same. If you realise, you can see that it's only fetching the first row which is the first item's price and not the total of the receipt. – joeBoy69 May 12 '18 at 17:27
  • Provide an MCVE and we can help, bye ;-) – eyllanesc May 12 '18 at 17:28

0 Answers0