Products linked with rests
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
#include <QObject>
|
||||
#include <QVariantMap>
|
||||
|
||||
#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<RestsModel>("rests");
|
||||
QVariantMap m_order;
|
||||
};
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user