Bind native balance planning and restore its saved runtime value (by Codex)

This commit is contained in:
Codex committed 2026-10-29 13:32:10 +01:00
1 parent 256d6d5211
commit fd5c2b5330
24 files changed
+717 -28

No files matched your search

+198
View File
@@ -0,0 +1,198 @@
// SPDX-License-Identifier: GPL-3.0-only
Json App::balance_rows() const {
if (!engine_mode_)
return demo_balance_rows(snap().state, *world_);
return engine_balance_projection(
engine_data_,
balance_future_ ? engine_balance_future_ : engine_data_.at("balance"));
}
std::string App::balance_cash(const Json &minor) const {
return engine_mode_
? engine_price({{"currency", "EUR"},
{"minorUnits", 2},
{"minor", std::to_string(minor.get<int64_t>())}})
: cash(minor.get<int64_t>());
}
void App::engine_balance_sync() {
const auto &meta = engine_data_.at("desktop").at("meta");
if (meta != engine_balance_context_) {
engine_balance_context_ = meta;
engine_balance_edits_ = Json::array();
engine_balance_future_ = Json();
engine_balance_base_ = Json();
balance_future_ = false;
}
if (engine_balance_committing_) {
engine_balance_committing_ = false;
if (engine_response_.value("messageType", "") == "command_result") {
const auto after = engine_balance_after_;
engine_balance_after_.clear();
engine_balance_continue(after);
} else {
engine_balance_after_.clear();
message(engine_notice_);
}
}
}
void App::engine_balance_preview(const EngineReply &reply) {
const auto &read = reply.response.at("read");
if (reply.response.at("edits") != engine_balance_edits_ ||
reply.bundle.at("desktop").at("meta") != engine_balance_context_)
throw editor::Error("Der Bilanzentwurf hat sich geändert.");
if (read.value("messageType", "") != "snapshot") {
engine_balance_edits_ = engine_balance_future_.is_object()
? engine_balance_future_.at("data").at("edits")
: Json::array();
engine_balance_feedback_ = engine_text(read.value("message", Json()));
message(engine_balance_feedback_);
return;
}
(void)engine_balance_projection(engine_data_, read);
engine_balance_future_ = read;
if (engine_balance_edits_.empty())
engine_balance_base_ = read;
balance_future_ = true;
engine_balance_feedback_ = engine_balance_edits_.empty()
? "Prognose der Engine für zwölf Monate."
: "Planentwurf · Noch nicht übernommen.";
focus_after_render_ = "balance-toggle";
}
void App::engine_balance_load_future() {
auto link = engine_link_;
const auto edits = engine_balance_edits_;
engine_start("Bilanzprognose wird berechnet …", [link, edits] {
const auto bundle = link->snapshot();
return EngineReply{link,
bundle,
{{"messageType", "balance_preview"},
{"edits", edits},
{"read", link->read({{"kind", "balance"},
{"view", "future"},
{"edits", edits}})}},
nullptr};
});
}
void App::engine_balance_continue(const std::string &after) {
engine_balance_edits_ = Json::array();
engine_balance_future_ = engine_balance_base_ = Json();
balance_future_ = false;
menu_.clear();
refresh();
if (after == "quit")
engine_quit();
else if (after == "save")
engine_save();
else if (after == "library-save")
engine_files_begin("save");
else if (after == "load")
engine_files_begin("load");
}
void App::engine_balance_close(const std::string &after) {
if (engine_balance_edits_.empty()) {
engine_balance_continue(after);
return;
}
if (engine_busy_ || !engine_fresh_)
return;
const auto request = engine_balance_command(
engine_data_, engine_balance_future_, editor::new_id("request:ui-"));
const auto &d = engine_balance_future_.at("data");
std::string text = "Zukunftsplanung mit einem Ergebnis von " +
engine_price(d.at("net")) + " übernehmen?";
if (d.at("warning") == true)
text += " " + engine_text(d.at("warningText"));
engine_balance_after_ = after;
confirmation(text, [this, request, after] {
engine_balance_after_ = after;
engine_balance_committing_ = true;
auto link = engine_link_;
engine_start(
"Bilanzplanung wird übernommen …",
[link, request] {
const auto response = link->command(request);
return EngineReply{link, link->snapshot(), response, nullptr};
},
true);
});
popup_accept_ = "Übernehmen";
popup_extra_ =
btn("balance-discard", "Entwurf verwerfen", "", "id=\"balance-discard\"");
}
bool App::engine_balance_action(const std::string &action,
Rml::Element *target) {
if (menu_ != "balance")
return false;
if (action == "balance-discard") {
const auto after = engine_balance_after_;
popup_.clear();
reset_popup();
engine_balance_after_.clear();
engine_balance_continue(after);
return true;
}
if (engine_busy_ || !engine_fresh_)
return true;
if (action == "close") {
engine_balance_close("menu");
return true;
}
if (action == "balance-toggle") {
if (balance_future_) {
balance_future_ = false;
refresh();
} else if (engine_balance_future_.is_object()) {
balance_future_ = true;
refresh();
} else
engine_balance_load_future();
return true;
}
if (action != "balance-plan" || !target || !balance_future_ ||
!engine_balance_future_.is_object())
return action.starts_with("balance-");
const int index = target->GetAttribute<int>("data-id", -1);
const auto rows = balance_rows();
if (index < 0 || size_t(index) >= rows.at("plan").size())
return true;
const auto row = rows.at("plan")[size_t(index)];
if (row.at("editable") != true)
return true;
const auto context = engine_balance_context_, before = engine_balance_edits_;
const auto id = row.at("id");
AmountRequest a;
a.title = row.at("name");
a.unit = "€";
a.min = 0;
a.max = row.at("maximum");
a.value = row.at("amount").get<int64_t>() / 100;
a.step = 1;
a.suggestion_step = 50000;
a.return_focus = target->GetId();
a.review = false;
a.accept = [this, context, before, id](int64_t value) {
if (menu_ != "balance" || engine_balance_context_ != context ||
engine_balance_edits_ != before || !engine_balance_base_.is_object())
return false;
auto edits = before;
edits.erase(std::remove_if(edits.begin(), edits.end(),
[&](const auto &e) { return e.at("id") == id; }),
edits.end());
const auto &items = engine_balance_base_.at("data").at("items");
const auto found =
std::find_if(items.begin(), items.end(),
[&](const auto &r) { return r.at("id") == id; });
if (found == items.end())
return false;
if (engine_money_minor(found->at("amount")) != value * 100)
edits.push_back({{"id", id},
{"amount",
{{"currency", "EUR"},
{"minorUnits", 2},
{"minor", std::to_string(value * 100)}}}});
engine_balance_edits_ = edits;
engine_balance_load_future();
return true;
};
open_amount(std::move(a));
return true;
}