116 lines
8.6 KiB
C++
116 lines
8.6 KiB
C++
// 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_) return;
|
|
ticket_ = std::make_unique<TicketPopover>(*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<int>(), 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; });
|
|
}
|
|
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<int64_t>()) + ((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<int>("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<int64_t>(99999999, f.at("max").get<int64_t>());
|
|
request.value = std::clamp(f.at("value").get<int64_t>(), 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<Rml::String>("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<int64_t>(99999999, bank.at("summary").at(kind == "deposit" ? "cash" : "deposit").get<int64_t>() / 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<std::string>(); request.unit = "Stück";
|
|
request.max = std::min<int64_t>(99999999, asset.at(buy ? "maxToBuy" : "owned").get<int64_t>()); 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 = "<h2>Kredit</h2><div class=\"credit-portrait\">" + img("art.bank-adviser-v1.png", 620, 515, false, false, 1000) + "</div>";
|
|
Json data;
|
|
try { data = engine_credit_projection(engine_data_, engine_credit_); }
|
|
catch (const std::exception &) { return out + "<p class=\"bank-note\">Der Bankkredit ist in diesem Kontext nicht verfügbar.</p>"; }
|
|
out += "<div class=\"credit-facts\" style=\"top:135dp;height:520dp;\">";
|
|
for (size_t i = 0; i < 8; ++i) {
|
|
const auto &f = data.at("fields")[i]; const auto value = f.at("value").get<int64_t>();
|
|
const auto text = i == 3 || i == 4 ? number(value) + " Monate" : money(value * 100, {{"code", "EUR"}, {"minor_units", 2}});
|
|
out += "<div class=\"credit-fact\" style=\"top:" + std::to_string(i * 65) + "dp;font-size:30dp;\"><span>" + esc(engine_text(f.at("label"))) + "</span><strong>";
|
|
if (data.at("saved")[0] == 0 && f.at("editable") == true) out += btn("engine-credit-edit", esc(text), "bank-control", "id=\"engine-credit-" + std::to_string(i) + "\" data-id=\"" + std::to_string(i) + "\" style=\"width:780dp;height:56dp;font-size:30dp;\"");
|
|
else out += esc(text);
|
|
out += "</strong></div>";
|
|
}
|
|
out += "</div><figure class=\"credit-schedule\"><div class=\"credit-schedule-caption\"><h3>Kreditvorschau</h3></div>";
|
|
out += "<div style=\"position:absolute;top:140dp;left:60dp;right:60dp;font-size:39dp;line-height:1.8;\"><p>Gebühren: " + esc(engine_price(data.at("fees"))) + "</p><p>Erstattung: " + esc(engine_price(data.at("refund"))) + "</p><p>Bereits getilgter Betrag: " + esc(engine_price(data.at("principalPaid"))) + "</p></div>";
|
|
out += "<p class=\"credit-schedule-note\">" + std::string(data.at("saved")[0] == 0 ? "Die Engine berechnet Laufzeit, Raten und Gebühren. Änderungen erst nach Bestätigung." : "Laufender Kredit · Änderungen folgen mit vollständiger Gebührenvorschau.") + "</p></figure>";
|
|
out += "<nav class=\"bank-actions\">" + btn("engine-credit-reset", "Zurücksetzen", "bank-control", "id=\"engine-credit-reset\"") + btn("engine-credit-apply", "Verbindlich buchen", "bank-control", "id=\"engine-credit-apply\" " + std::string(engine_credit_edits_.empty() ? "disabled" : "")) + "</nav>";
|
|
return out;
|
|
}
|