// SPDX-License-Identifier: GPL-3.0-only void App::engine_finance_submit(const Json &command) { auto link = engine_link_; engine_start("Änderung wird verarbeitet …", [link, command] { auto response = link->command(command); return EngineReply{link, link->snapshot(), response, nullptr}; }, true); } void App::engine_ticket_bind() { if (ticket_ && ticket_engine_bound_) return; // A preview editor captures its DataSource by reference. Destroy it while // that provider still exists, before the career handoff releases the source. ticket_.reset(); ticket_ = std::make_unique(*context_, *doc_, [this] { Json state; if (engine_fresh_ && !engine_busy_) { try { state = engine_ticket_projection(engine_data_); } catch (const std::exception &) {} } return Snapshot{input_revision(), world_, state}; }, [this](const Action &action) { try { if (!engine_fresh_ || engine_busy_ || action.revision != input_revision()) return ActionResult{false, input_revision(), "Die Daten haben sich geändert."}; auto command = engine_ticket_command(engine_data_, action.value.get(), editor::new_id("request:ui-")); engine_finance_submit(command); return ActionResult{true, input_revision(), {}}; } catch (const std::exception &e) { return ActionResult{false, input_revision(), e.what()}; } }, [this] { focus_after_render_ = "account-ticket"; dirty_ = true; }); ticket_engine_bound_ = true; } bool App::engine_bank_action(const std::string &action, Rml::Element *target) { if (!engine_fresh_ || engine_busy_) return true; if (action == "engine-credit-reset") { engine_credit_ = engine_data_.at("credit"); engine_credit_edits_ = Json::array(); refresh(); return true; } if (action == "engine-credit-apply") { const auto data = engine_credit_projection(engine_data_, engine_credit_); const auto command = engine_credit_command(engine_data_, engine_credit_, engine_credit_edits_, editor::new_id("request:ui-")); std::string text = "Kreditänderungen verbindlich buchen? Gebühren: " + engine_price(data.at("fees")) + ". Erstattung: " + engine_price(data.at("refund")) + "."; for (const auto &f : data.at("fields")) text += " " + engine_text(f.at("label")) + ": " + number(f.at("value").get()) + ((f.at("field") == 3 || f.at("field") == 4) ? " Monate." : " €. "); confirmation(text, [this, command] { focus_after_render_ = "window-close"; engine_finance_submit(command); }); return true; } if (action != "bank-calc" && action != "engine-credit-edit") return false; if (!target) return true; AmountRequest request; request.return_focus = target->GetId(); request.step = 1; const auto meta = engine_data_.at("desktop").at("meta"); const auto fresh = [this, meta] { return engine_fresh_ && !engine_busy_ && menu_ == "bank" && meta == engine_data_.at("desktop").at("meta"); }; if (action == "engine-credit-edit") { const auto index = target->GetAttribute("data-id", -1); const auto credit = engine_credit_projection(engine_data_, engine_credit_); if (credit.at("saved")[0] != 0) return true; if (index < 0 || index > 7 || credit.at("fields")[size_t(index)].at("editable") != true) return true; const auto f = credit.at("fields")[size_t(index)]; request.title = engine_text(f.at("label")); request.unit = index == 3 || index == 4 ? "Monate" : "€"; request.min = f.at("min"); request.max = std::min(99999999, f.at("max").get()); request.value = std::clamp(f.at("value").get(), request.min, request.max); request.suggestion_step = index == 3 || index == 4 ? 6 : 100000; request.review = false; const auto edits_before = engine_credit_edits_; if (edits_before.size() >= 64) { message("Bitte den Kreditentwurf zurücksetzen, bevor du weitere Felder änderst."); return true; } request.accept = [this, fresh, edits_before, index](int64_t value) { if (!fresh() || bank_view_ != "credit" || engine_credit_edits_ != edits_before) return false; auto edits = edits_before; edits.push_back({{"field", index}, {"value", value}}); auto link = engine_link_; engine_start("Kreditvorschau wird berechnet …", [link, edits] { auto response = link->read({{"kind", "credit"}, {"lenderId", "bank"}, {"edits", edits}}); return EngineReply{link, link->snapshot(), {{"messageType", "credit_preview"}, {"read", response}, {"edits", edits}}, nullptr}; }); return true; }; } else { const auto kind = target->GetAttribute("data-kind", ""); const auto bank = engine_bank_projection(engine_data_); Json payload; int64_t unit_price = 0; const bool transfer = kind == "deposit" || kind == "withdraw"; if (transfer && bank_view_ == "savings") { payload = {{"action", kind}}; request.title = kind == "deposit" ? "Einzahlen" : "Abheben"; request.unit = "€"; request.max = std::min(99999999, bank.at("summary").at(kind == "deposit" ? "cash" : "deposit").get() / 100); request.suggestion_step = 100000; } else { const bool property = kind == "property-buy" || kind == "property-sell"; const bool stock = kind == "stock-buy" || kind == "stock-sell"; if ((!property && !stock) || (property && bank_view_ != "properties") || (stock && bank_view_ != "stocks")) return true; const bool buy = kind == "property-buy" || kind == "stock-buy"; const auto &asset = bank.at("assets").at(property ? "properties" : "stocks").at(size_t(property ? (buy ? bank_property_ : bank_owned_) : bank_stock_)); payload = {{"action", buy ? "buy" : "sell"}, {"assetId", asset.at("id")}}; unit_price = asset.at("price"); request.title = std::string(buy ? "Kaufen: " : "Verkaufen: ") + asset.at("name").get(); request.unit = "Stück"; request.max = std::min(99999999, asset.at(buy ? "maxToBuy" : "owned").get()); request.suggestion_step = property ? 1 : 100; request.review_text = [unit_price](int64_t value) { return "Kurswert ohne zusätzliche Gebühren: " + money(unit_price * value, {{"code", "EUR"}, {"minor_units", 2}}) + ". Kauf- und Verkaufskosten berechnet die Bank."; }; } request.min = 1; request.value = request.max >= 1 ? 1 : 0; request.accept = [this, fresh, payload, transfer](int64_t value) mutable { if (!fresh()) return false; if (transfer) payload["amount"] = {{"currency", "EUR"}, {"minorUnits", 2}, {"minor", std::to_string(value * 100)}}; else payload["count"] = value; engine_finance_submit(engine_bank_command(engine_data_, payload, editor::new_id("request:ui-"))); return true; }; } open_amount(std::move(request)); return true; } std::string App::engine_credit_view() { std::string out; Json data; try { data = engine_credit_projection(engine_data_, engine_credit_); } catch (const std::exception &) { return "
Der Bankkredit ist in diesem Kontext nicht verfügbar.
"; } // the original's labels arrive in capitals: one capital per word reads calmer const auto calm = [](std::string text) { bool first = true; for (size_t i = 0; i < text.size(); ++i) { const unsigned char c = static_cast(text[i]); if (c == 0xc3 && i + 1 < text.size()) { unsigned char next = static_cast(text[i + 1]); if (!first && (next == 0x84 || next == 0x96 || next == 0x9c)) text[i + 1] = static_cast(next + 0x20); ++i; first = false; } else if (std::isalpha(c)) { if (!first) text[i] = static_cast(std::tolower(c)); first = false; } else if (c == ' ' || c == '-') first = true; } return text; }; // the loan as the bank states it: every figure a line; what may be changed is a button out += "
" + pg_kicker("Kredit"); int64_t total = 0, paid = 0; for (size_t i = 0; i < 8; ++i) { const auto &f = data.at("fields")[i]; const auto value = f.at("value").get(); if (i == 1) total = value; if (i == 5) paid = value; const auto text = i == 3 || i == 4 ? number(value) + " Monate" : money(value * 100, {{"code", "EUR"}, {"minor_units", 2}}); out += "
" + esc(calm(engine_text(f.at("label")))) + ""; if (data.at("saved")[0] == 0 && f.at("editable") == true) out += pg_button("engine-credit-edit", esc(text), "bk-edit", "id=\"engine-credit-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\""); else out += "" + esc(text) + ""; out += "
"; } out += "
"; for (const auto &[label, key] : std::array, 3>{{{"Gebühren", "fees"}, {"Erstattung", "refund"}, {"Bereits getilgt", "principalPaid"}}}) out += "
" + esc(engine_price(data.at(key))) + "" + label + "
"; // how much of the whole sum is paid back const double done = total > 0 ? std::clamp(double(paid) * 100.0 / double(total), 0.0, 100.0) : 0; out += "
Zurückgezahlt" + (total > 0 ? std::to_string(int(std::lround(done))) + " %" : std::string("kein Kredit")) + "
" + pg_bar(done, "sage") + "
" + std::string(data.at("saved")[0] == 0 ? "Die Bank berechnet Laufzeit, Raten und Gebühren. Gebucht wird erst nach deiner Bestätigung." : "Laufender Kredit · Änderungen folgen mit vollständiger Gebührenvorschau.") + "
"; out += "
" + pg_button("engine-credit-reset", "Zurücksetzen", "block", "id=\"engine-credit-reset\"") + pg_button("engine-credit-apply", "Verbindlich buchen", "primary block", "id=\"engine-credit-apply\" " + std::string(engine_credit_edits_.empty() ? "disabled=\"disabled\"" : "")) + "
"; return out; }