Rests model was added

This commit is contained in:
2023-04-13 17:57:27 +02:00
parent fc1d96437f
commit 5264238212
10 changed files with 91 additions and 52 deletions

View File

@@ -1,16 +1,12 @@
#include "restsviewmodel.h"
#include "models/basemodel.h"
#include "services/settingsservice.h"
RestsViewModel::RestsViewModel(QObject *parent)
: QObject{parent}
{
connect(m_restsModel, &BaseModel::dataChanged, this, &RestsViewModel::reloadRests);
connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged);
connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged);
reloadRests();
}
QVariantList RestsViewModel::products() const
@@ -21,11 +17,10 @@ QVariantList RestsViewModel::products() const
const QVariantList products = m_productsModel->items();
for (const QVariant &product : products) {
QString productId = product.toMap().value("id").toString();
QVariantMap rest = m_rests.value(storeId).value(productId).toMap();
res << QVariantMap {
{ "productId", productId },
{ "title", m_productsModel->itemProperty(productId, "name").toString() },
{ "rest", rest.value("rest", 0.0).toFloat() }
{ "rest", m_restsModel->productRest(storeId, productId) }
};
}
@@ -34,36 +29,10 @@ QVariantList RestsViewModel::products() const
void RestsViewModel::setProductRest(const QString &productId, float rest)
{
QString storeId = settings()->selectedStoreId();
QVariantMap productRest = m_rests.value(storeId).value(productId).toMap();
productRest["rest"] = rest;
productRest["storeId"] = storeId;
productRest["productId"] = productId;
QString restId = productRest.value("id").toString();
if (restId.isEmpty()) {
m_restsModel->addItem(productRest);
} else {
m_restsModel->modifyItem(restId, productRest);
}
m_restsModel->submitRest(settings()->selectedStoreId(), productId, rest);
}
SettingsService *RestsViewModel::settings() const
{
return SettingsService::instance();
}
void RestsViewModel::reloadRests()
{
m_rests.clear();
const QVariantList rests = m_restsModel->items();
for (const QVariant &varRest : rests) {
QVariantMap rest = varRest.toMap();
QString storeId = rest.value("storeId").toString();
QVariantMap storeRests = m_rests.value(storeId);
storeRests[rest.value("productId").toString()] = rest;
m_rests[storeId] = storeRests;
}
emit productsChanged();
}