The read of a loan carries charge: the fees and, for a running loan of the bank, the interest adjustment that commit_bank_credit books. With it the page offers the fields of a running loan as well; a backend that does not state the charge keeps them closed. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
177 lines
13 KiB
C++
177 lines
13 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_ && 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<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; });
|
|
ticket_engine_bound_ = true;
|
|
}
|
|
// Reads the loan the current manager has with a lender, without any edit.
|
|
void App::engine_credit_load(const std::string &lender) {
|
|
if (!engine_fresh_ || engine_busy_ || lender.empty()) return;
|
|
auto link = engine_link_;
|
|
engine_start("Kredit wird gelesen …", [link, lender] {
|
|
auto response = link->read({{"kind", "credit"}, {"lenderId", lender}, {"edits", Json::array()}});
|
|
return EngineReply{link, link->snapshot(), {{"messageType", "credit_preview"}, {"read", response}, {"edits", Json::array()}, {"lender", lender}}, nullptr};
|
|
});
|
|
}
|
|
// Keeps the loan on screen the one its page is about: the bank's on "Bank", the chosen fellow
|
|
// manager's on "Spieler". Every accepted state brings the bank's loan, so the other is read again.
|
|
void App::engine_credit_follow() {
|
|
if (menu_ != "bank" || !engine_fresh_ || engine_busy_) return;
|
|
if (bank_view_ == "players") {
|
|
if (!bank_lender_.empty() && engine_credit_lender_ != bank_lender_ && engine_credit_refused_ != bank_lender_) engine_credit_load(bank_lender_);
|
|
} else if (engine_credit_lender_ != "bank") {
|
|
engine_credit_ = engine_data_.value("credit", Json()); engine_credit_edits_ = Json::array(); engine_credit_lender_ = "bank"; needs_render_ = true;
|
|
}
|
|
}
|
|
bool App::engine_bank_action(const std::string &action, Rml::Element *target) {
|
|
if (!engine_fresh_ || engine_busy_) return true;
|
|
if (action == "bank-lender" && target) {
|
|
// a fellow manager is chosen as the lender: his loan is read
|
|
bank_lender_ = target->GetAttribute<Rml::String>("data-id", ""); engine_credit_refused_.clear(); focus_after_render_ = target->GetId();
|
|
if (engine_credit_lender_ != bank_lender_) engine_credit_load(bank_lender_); else refresh();
|
|
return true;
|
|
}
|
|
if (action == "engine-credit-reset") {
|
|
if (engine_credit_lender_ == "bank") { engine_credit_ = engine_data_.at("credit"); engine_credit_edits_ = Json::array(); refresh(); }
|
|
else engine_credit_load(engine_credit_lender_);
|
|
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(data.contains("charge") ? "charge" : "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_);
|
|
// a running loan is changed only where the backend states what the change costs at once
|
|
if (credit.at("saved")[0] != 0 && !credit.contains("charge")) 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" && bank_view_ != "players") || engine_credit_edits_ != edits_before) return false;
|
|
auto edits = edits_before; edits.push_back({{"field", index}, {"value", value}});
|
|
auto link = engine_link_;
|
|
const auto lender = engine_credit_lender_;
|
|
engine_start("Kreditvorschau wird berechnet …", [link, edits, lender] {
|
|
auto response = link->read({{"kind", "credit"}, {"lenderId", lender}, {"edits", edits}});
|
|
return EngineReply{link, link->snapshot(), {{"messageType", "credit_preview"}, {"read", response}, {"edits", edits}, {"lender", lender}}, 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;
|
|
Json data;
|
|
const bool fellow = engine_credit_lender_ != "bank";
|
|
try { data = engine_credit_projection(engine_data_, engine_credit_); }
|
|
catch (const std::exception &) { return "<div class=\"pg-panel pg-grow\"><div class=\"pg-empty\"><span>" + std::string(fellow ? "Dieser Kredit ist in diesem Kontext nicht verfügbar." : "Der Bankkredit ist in diesem Kontext nicht verfügbar.") + "</span></div></div>"; }
|
|
std::string lender_name;
|
|
if (fellow) try { const auto people = bank_players(); for (const auto &person : people) if (person.value("id", "") == engine_credit_lender_) lender_name = person.value("name", ""); } catch (const std::exception &) {}
|
|
// 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<unsigned char>(text[i]);
|
|
if (c == 0xc3 && i + 1 < text.size()) {
|
|
unsigned char next = static_cast<unsigned char>(text[i + 1]);
|
|
if (!first && (next == 0x84 || next == 0x96 || next == 0x9c)) text[i + 1] = static_cast<char>(next + 0x20);
|
|
++i; first = false;
|
|
} else if (std::isalpha(c)) { if (!first) text[i] = static_cast<char>(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 += "<div class=\"pg-panel pg-col pg-grow\" style=\"" + pg_css("gap:4px;padding:12px 16px;") + "\">" + pg_kicker(fellow ? "Kredit von " + lender_name : std::string("Kredit"));
|
|
const bool running = data.at("saved")[0] != 0, open = !running || data.contains("charge");
|
|
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<int64_t>();
|
|
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 += "<div class=\"pg-kv bk-credit-line\"><span>" + esc(calm(engine_text(f.at("label")))) + "</span>";
|
|
if (open && 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 += "<strong>" + esc(text) + "</strong>";
|
|
out += "</div>";
|
|
}
|
|
out += "<div class=\"bk-tiles\" style=\"margin-top:" + pg(8) + ";\">";
|
|
for (const auto &[label, key] : std::array<std::pair<const char *, const char *>, 3>{{{"Gebühren", data.contains("charge") ? "charge" : "fees"}, {"Erstattung", "refund"}, {"Bereits getilgt", "principalPaid"}}})
|
|
out += "<div class=\"bk-tile\"><strong>" + esc(engine_price(data.at(key))) + "</strong><span>" + label + "</span></div>";
|
|
// 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 += "</div><div class=\"bk-part\"><div class=\"pg-row\" style=\"justify-content:space-between;\"><span>Zurückgezahlt</span><strong>" + (total > 0 ? std::to_string(int(std::lround(done))) + " %" : std::string("kein Kredit")) +
|
|
"</strong></div>" + pg_bar(done, "sage") + "</div><span class=\"pg-sp\"></span><span class=\"pg-mut\" style=\"display:block;\">" +
|
|
std::string(running ? (open ? "Laufender Kredit: Summe, Laufzeit und Rückzahlung lassen sich ändern. " + std::string(fellow ? "Der Mitspieler berechnet nichts." : "Die Bank berechnet dafür Gebühren und gleicht die Zinsen aus.") + " Gebucht wird erst nach deiner Bestätigung." : "") : fellow ? "Ohne Gebühren. Die Summe geht vom Konto des Mitspielers ab, die Raten fließen an ihn. Gebucht wird erst nach deiner Bestätigung; er wird nicht gefragt." : "Die Bank berechnet Laufzeit, Raten und Gebühren. Gebucht wird erst nach deiner Bestätigung.") + std::string(open ? "" : "Laufender Kredit · Änderungen folgen mit vollständiger Gebührenvorschau.") + "</span></div>";
|
|
out += "<div class=\"pg-row\" style=\"" + pg_css("gap:8px;") + "\">" + 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\"" : "")) + "</div>";
|
|
return out;
|
|
}
|