Products linked with rests

This commit is contained in:
2023-04-14 08:37:59 +02:00
parent 5264238212
commit baefd64ce6
4 changed files with 37 additions and 4 deletions

View File

@@ -25,6 +25,7 @@ Page {
delegate: ItemDelegate { delegate: ItemDelegate {
width: productsList.width width: productsList.width
font.strikeout: modelData.rest === 0
text: modelData.name text: modelData.name
height: spinbox.height height: spinbox.height
@@ -33,7 +34,7 @@ Page {
width: 150 width: 150
from: 0 from: 0
to: 100 * 100 to: modelData.rest * 100
stepSize: 50 stepSize: 50
anchors.right: parent.right anchors.right: parent.right
editable: true editable: true

View File

@@ -7,11 +7,19 @@ ProductsViewModel::ProductsViewModel(QObject *parent)
: QObject{parent} : QObject{parent}
{ {
connect(m_productsModel, &BaseModel::dataChanged, this, &ProductsViewModel::productsChanged); 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 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 QVariantList ProductsViewModel::order() const
@@ -59,20 +67,37 @@ void ProductsViewModel::setOrderValue(const QString &productId, float value)
void ProductsViewModel::submitOrder() void ProductsViewModel::submitOrder()
{ {
BaseModel *ordersModel = ModelsRegister::model("orders"); m_ordersModel->submitItem({
ordersModel->submitItem({
{ "userId", settings()->selectedUserId() }, { "userId", settings()->selectedUserId() },
{ "storeId", settings()->selectedStoreId() }, { "storeId", settings()->selectedStoreId() },
{ "products", m_order.values() }, { "products", m_order.values() },
{ "amount", orderSum() } { "amount", orderSum() }
}); });
updateRests();
m_order.clear(); m_order.clear();
emit orderChanged(); emit orderChanged();
} }
float ProductsViewModel::productRest(const QString &productId) const
{
QString storeId = settings()->selectedStoreId();
return m_restsModel->productRest(storeId, productId);
}
SettingsService *ProductsViewModel::settings() const SettingsService *ProductsViewModel::settings() const
{ {
return SettingsService::instance(); 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);
}
}

View File

@@ -4,6 +4,8 @@
#include <QObject> #include <QObject>
#include <QVariantMap> #include <QVariantMap>
#include "models/restsmodel.h"
#include "services/modelsregister.h" #include "services/modelsregister.h"
class SettingsService; class SettingsService;
@@ -31,8 +33,12 @@ signals:
private: private:
SettingsService *settings() const; SettingsService *settings() const;
float productRest(const QString &productId) const;
void updateRests() const;
BaseModel *m_productsModel = ModelsRegister::model("products"); BaseModel *m_productsModel = ModelsRegister::model("products");
BaseModel *m_ordersModel = ModelsRegister::model("orders");
RestsModel *m_restsModel = ModelsRegister::get<RestsModel>("rests");
QVariantMap m_order; QVariantMap m_order;
}; };

View File

@@ -5,6 +5,7 @@
RestsViewModel::RestsViewModel(QObject *parent) RestsViewModel::RestsViewModel(QObject *parent)
: QObject{parent} : QObject{parent}
{ {
connect(m_restsModel, &BaseModel::dataChanged, this, &RestsViewModel::productsChanged);
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);
} }