|
@@ -1,16 +1,12 @@
|
|
|
#include "restsviewmodel.h"
|
|
#include "restsviewmodel.h"
|
|
|
|
|
|
|
|
-#include "models/basemodel.h"
|
|
|
|
|
#include "services/settingsservice.h"
|
|
#include "services/settingsservice.h"
|
|
|
|
|
|
|
|
RestsViewModel::RestsViewModel(QObject *parent)
|
|
RestsViewModel::RestsViewModel(QObject *parent)
|
|
|
: QObject{parent}
|
|
: QObject{parent}
|
|
|
{
|
|
{
|
|
|
- connect(m_restsModel, &BaseModel::dataChanged, this, &RestsViewModel::reloadRests);
|
|
|
|
|
connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged);
|
|
connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged);
|
|
|
connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged);
|
|
connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged);
|
|
|
-
|
|
|
|
|
- reloadRests();
|
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
QVariantList RestsViewModel::products() const
|
|
QVariantList RestsViewModel::products() const
|
|
@@ -21,11 +17,10 @@ QVariantList RestsViewModel::products() const
|
|
|
const QVariantList products = m_productsModel->items();
|
|
const QVariantList products = m_productsModel->items();
|
|
|
for (const QVariant &product : products) {
|
|
for (const QVariant &product : products) {
|
|
|
QString productId = product.toMap().value("id").toString();
|
|
QString productId = product.toMap().value("id").toString();
|
|
|
- QVariantMap rest = m_rests.value(storeId).value(productId).toMap();
|
|
|
|
|
res << QVariantMap {
|
|
res << QVariantMap {
|
|
|
{ "productId", productId },
|
|
{ "productId", productId },
|
|
|
{ "title", m_productsModel->itemProperty(productId, "name").toString() },
|
|
{ "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)
|
|
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
|
|
SettingsService *RestsViewModel::settings() const
|
|
|
{
|
|
{
|
|
|
return SettingsService::instance();
|
|
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();
|
|
|
|
|
-}
|
|
|