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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user