Rests view was added
This commit is contained in:
69
viewmodels/restsviewmodel.cpp
Normal file
69
viewmodels/restsviewmodel.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#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
|
||||
{
|
||||
QVariantList res;
|
||||
|
||||
const QString storeId = settings()->selectedStoreId();
|
||||
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() }
|
||||
};
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
35
viewmodels/restsviewmodel.h
Normal file
35
viewmodels/restsviewmodel.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef RESTSVIEWMODEL_H
|
||||
#define RESTSVIEWMODEL_H
|
||||
|
||||
#include <QObject>
|
||||
|
||||
#include "services/modelsregister.h"
|
||||
|
||||
class SettingsService;
|
||||
class RestsViewModel : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
Q_PROPERTY(QVariantList products READ products NOTIFY productsChanged)
|
||||
|
||||
public:
|
||||
explicit RestsViewModel(QObject *parent = nullptr);
|
||||
|
||||
QVariantList products() const;
|
||||
|
||||
Q_INVOKABLE void setProductRest(const QString &productId, float rest);
|
||||
|
||||
signals:
|
||||
void productsChanged();
|
||||
|
||||
private:
|
||||
SettingsService *settings() const;
|
||||
void reloadRests();
|
||||
|
||||
BaseModel *m_productsModel = ModelsRegister::model("products");
|
||||
BaseModel *m_restsModel = ModelsRegister::model("rests");
|
||||
|
||||
QMap<QString, QVariantMap> m_rests;
|
||||
};
|
||||
|
||||
#endif // RESTSVIEWMODEL_H
|
||||
Reference in New Issue
Block a user