70 lines
2.1 KiB
C++
70 lines
2.1 KiB
C++
#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();
|
|
}
|