From baefd64ce66487f6d9a822456eab2a28de100b75 Mon Sep 17 00:00:00 2001 From: "Denis V. Dedkov" Date: Fri, 14 Apr 2023 08:37:59 +0200 Subject: [PATCH] Products linked with rests --- qml/Views/ProductsView.qml | 3 ++- viewmodels/productsviewmodel.cpp | 31 ++++++++++++++++++++++++++++--- viewmodels/productsviewmodel.h | 6 ++++++ viewmodels/restsviewmodel.cpp | 1 + 4 files changed, 37 insertions(+), 4 deletions(-) diff --git a/qml/Views/ProductsView.qml b/qml/Views/ProductsView.qml index 1e2e80a..d1d9ee4 100644 --- a/qml/Views/ProductsView.qml +++ b/qml/Views/ProductsView.qml @@ -25,6 +25,7 @@ Page { delegate: ItemDelegate { width: productsList.width + font.strikeout: modelData.rest === 0 text: modelData.name height: spinbox.height @@ -33,7 +34,7 @@ Page { width: 150 from: 0 - to: 100 * 100 + to: modelData.rest * 100 stepSize: 50 anchors.right: parent.right editable: true diff --git a/viewmodels/productsviewmodel.cpp b/viewmodels/productsviewmodel.cpp index f826945..9181143 100644 --- a/viewmodels/productsviewmodel.cpp +++ b/viewmodels/productsviewmodel.cpp @@ -7,11 +7,19 @@ ProductsViewModel::ProductsViewModel(QObject *parent) : QObject{parent} { connect(m_productsModel, &BaseModel::dataChanged, this, &ProductsViewModel::productsChanged); + connect(m_restsModel, &BaseModel::dataChanged, this, &ProductsViewModel::productsChanged); + connect(settings(), &SettingsService::selectedStoreIdChanged, this, &ProductsViewModel::productsChanged); } QVariantList ProductsViewModel::products() const { - return m_productsModel->items(); + QVariantList res; + for (const QVariant &product : m_productsModel->items()) { + QVariantMap item = product.toMap(); + item["rest"] = productRest(item.value("id").toString()); + res << item; + } + return res; } QVariantList ProductsViewModel::order() const @@ -59,20 +67,37 @@ void ProductsViewModel::setOrderValue(const QString &productId, float value) void ProductsViewModel::submitOrder() { - BaseModel *ordersModel = ModelsRegister::model("orders"); - ordersModel->submitItem({ + m_ordersModel->submitItem({ { "userId", settings()->selectedUserId() }, { "storeId", settings()->selectedStoreId() }, { "products", m_order.values() }, { "amount", orderSum() } }); + updateRests(); + m_order.clear(); emit orderChanged(); } +float ProductsViewModel::productRest(const QString &productId) const +{ + QString storeId = settings()->selectedStoreId(); + return m_restsModel->productRest(storeId, productId); +} + SettingsService *ProductsViewModel::settings() const { return SettingsService::instance(); } + +void ProductsViewModel::updateRests() const +{ + QString storeId = settings()->selectedStoreId(); + for (auto it = m_order.constBegin(); it != m_order.constEnd(); ++it) { + float quantity = it.value().toMap().value("quantity", 0.0).toFloat(); + float rest = productRest(it.key()); + m_restsModel->submitRest(storeId, it.key(), rest - quantity); + } +} diff --git a/viewmodels/productsviewmodel.h b/viewmodels/productsviewmodel.h index b0f8dcb..615678e 100644 --- a/viewmodels/productsviewmodel.h +++ b/viewmodels/productsviewmodel.h @@ -4,6 +4,8 @@ #include #include +#include "models/restsmodel.h" + #include "services/modelsregister.h" class SettingsService; @@ -31,8 +33,12 @@ signals: private: SettingsService *settings() const; + float productRest(const QString &productId) const; + void updateRests() const; BaseModel *m_productsModel = ModelsRegister::model("products"); + BaseModel *m_ordersModel = ModelsRegister::model("orders"); + RestsModel *m_restsModel = ModelsRegister::get("rests"); QVariantMap m_order; }; diff --git a/viewmodels/restsviewmodel.cpp b/viewmodels/restsviewmodel.cpp index b0c9d97..3fecd03 100644 --- a/viewmodels/restsviewmodel.cpp +++ b/viewmodels/restsviewmodel.cpp @@ -5,6 +5,7 @@ RestsViewModel::RestsViewModel(QObject *parent) : QObject{parent} { + connect(m_restsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged); connect(m_productsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged); connect(settings(), &SettingsService::selectedStoreIdChanged, this, &RestsViewModel::productsChanged); }