Choice model settings was added
This commit is contained in:
78
viewmodels/productsviewmodel.cpp
Normal file
78
viewmodels/productsviewmodel.cpp
Normal file
@@ -0,0 +1,78 @@
|
||||
#include "productsviewmodel.h"
|
||||
|
||||
#include "services/settingsservice.h"
|
||||
#include "models/ordersmodel.h"
|
||||
|
||||
ProductsViewModel::ProductsViewModel(QObject *parent)
|
||||
: QObject{parent}
|
||||
{
|
||||
connect(&m_productsModel, &BaseModel::dataChanged, this, &ProductsViewModel::productsChanged);
|
||||
}
|
||||
|
||||
QVariantList ProductsViewModel::products() const
|
||||
{
|
||||
return m_productsModel.items();
|
||||
}
|
||||
|
||||
QVariantList ProductsViewModel::order() const
|
||||
{
|
||||
QVariantList res;
|
||||
|
||||
for (auto it = m_order.constBegin(); it != m_order.constEnd(); ++it) {
|
||||
QVariantMap product = m_productsModel.item(it.key());
|
||||
product["count"] = it.value().toMap().value("quantity").toDouble();
|
||||
res << product;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
float ProductsViewModel::orderSum() const
|
||||
{
|
||||
float res = 0.0;
|
||||
|
||||
for (auto it = m_order.constBegin(); it != m_order.constEnd(); ++it) {
|
||||
QVariantMap product = m_productsModel.item(it.key());
|
||||
float price = product.value("price", 0.0).toFloat();
|
||||
float quantity = it.value().toMap().value("quantity").toDouble();
|
||||
res += quantity * price;
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
void ProductsViewModel::setOrderValue(const QString &productId, float value)
|
||||
{
|
||||
if (value) {
|
||||
float price = m_productsModel.itemProperty(productId, "price", 0.0).toFloat();
|
||||
m_order[productId] = QVariantMap {
|
||||
{ "productId", productId },
|
||||
{ "quantity", value},
|
||||
{ "price", price }
|
||||
};
|
||||
} else {
|
||||
m_order.remove(productId);
|
||||
}
|
||||
|
||||
emit orderChanged();
|
||||
}
|
||||
|
||||
void ProductsViewModel::submitOrder()
|
||||
{
|
||||
OrdersModel model;
|
||||
model.submitOrder(QVariantMap {
|
||||
{ "userId", settings()->selectedUserId() },
|
||||
{ "storeId", settings()->selectedStoreId() },
|
||||
{ "products", m_order.values() },
|
||||
{ "amount", orderSum() }
|
||||
});
|
||||
|
||||
m_order.clear();
|
||||
|
||||
emit orderChanged();
|
||||
}
|
||||
|
||||
SettingsService *ProductsViewModel::settings() const
|
||||
{
|
||||
return SettingsService::instance();
|
||||
}
|
||||
Reference in New Issue
Block a user