From 18bfbc423d188683973a0a3d6c31c9225158e262 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 18 Oct 2025 23:10:20 +0200 Subject: tax rate refactor --- src/ui/ui_settings.cpp | 210 ++++++++++++------------------------------------- 1 file changed, 51 insertions(+), 159 deletions(-) (limited to 'src/ui/ui_settings.cpp') diff --git a/src/ui/ui_settings.cpp b/src/ui/ui_settings.cpp index 7de543f..7feacb2 100644 --- a/src/ui/ui_settings.cpp +++ b/src/ui/ui_settings.cpp @@ -20,6 +20,7 @@ #include "memops.hpp" #include "locales.hpp" #include "importer.hpp" +#include "countries.hpp" #include "administration.hpp" #include "administration_writer.hpp" @@ -47,171 +48,59 @@ void ui::setup_settings() select_company_tab = 1; company_info = administration::company_info_get(); - tax_rate_count = administration::tax_rate_count(); - tax_rates = (tax_rate*)memops::alloc(tax_rate_count * sizeof(tax_rate)); - administration::tax_rate_get_all(tax_rates); + if (administration::can_create_invoices()) { + tax_rates = (tax_rate*)memops::alloc(sizeof(tax_rate) * 400); + tax_rate_count = country::get_available_tax_rates(administration::company_info_get().address.country_code, tax_rates, 400); - cost_center_count = administration::cost_center_count(); - cost_centers = (cost_center*)memops::alloc(cost_center_count * sizeof(cost_center)); - administration::cost_center_get_all(cost_centers); + cost_center_count = administration::cost_center_count(); + cost_centers = (cost_center*)memops::alloc(cost_center_count * sizeof(cost_center)); + administration::cost_center_get_all(cost_centers); - new_service = administration::get_ai_service(); + new_service = administration::get_ai_service(); + } } static void draw_vat_rates() { - static bool is_adding_item = false; - static tax_rate new_tax_rate; - - static bool is_editing_item = false; - static u32 editing_item_index = 0; + tax_rate_type type_iter = tax_rate_type::TAX_RATE_OUTGOING_INVOICE; + go_again: - if (ImGui::BeginTable("TableVatRates", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { + char id[MAX_LEN_SHORT_DESC]; + strops::format(id, MAX_LEN_SHORT_DESC, "TableVatRates##%d", static_cast(type_iter)); - ImGui::TableSetupColumn(locale::get("settings.vat.table.country"), ImGuiTableColumnFlags_WidthFixed, 220); - ImGui::TableSetupColumn(locale::get("settings.vat.table.rates")); + if (ImGui::BeginTable(id, 3, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { - // Used to generate headers for each individual country. - char prev_country[MAX_LEN_COUNTRY_CODE]; - prev_country[0] = 0; + ImGui::TableSetupColumn(locale::get("settings.vat.table.outgoing"), ImGuiTableColumnFlags_WidthStretch); + ImGui::TableSetupColumn(locale::get("settings.vat.table.rates"), ImGuiTableColumnFlags_WidthFixed, 120); + ImGui::TableSetupColumn(locale::get("settings.vat.table.section"), ImGuiTableColumnFlags_WidthFixed, 200); + ImGui::TableHeadersRow(); for (u32 i = 0; i < tax_rate_count; i++) { tax_rate c = tax_rates[i]; + if (c.type != type_iter) continue; - // Set to false for shared rates. - bool can_be_modified = false; - - // Check for fixed rates shared accross countries. - if (strcmp(c.country_code, "00") == 0) - { - strops::copy(prev_country, c.country_code, 3); - can_be_modified = false; - } - // Generate headers per country. - else if (strcmp(c.country_code, prev_country) != 0) - { - strops::copy(prev_country, c.country_code, 3); - - // Empty row. - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); ImGui::Text(""); - ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(69, 69, 69, 255)); - ImGui::TableSetColumnIndex(1); ImGui::Text(""); - ImGui::TableSetBgColor(ImGuiTableBgTarget_CellBg, IM_COL32(69, 69, 69, 255)); - - ImGui::TableNextRow(); - - char locale_buf[20]; - strops::format(locale_buf, sizeof(locale_buf), "country.%s", c.country_code); - ImGui::TableSetColumnIndex(0); - ImGui::Text(locale::get(locale_buf)); - - // If not adding an item already, show + button next to country name. - if (!is_adding_item) - { - ImGui::SameLine(); - char btn_name[20]; - strops::format(btn_name, sizeof(btn_name), "+##%d",i); - ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(0,0)); - if (ImGui::Button(btn_name, ImVec2(20,20))) { - is_adding_item = true; - is_editing_item = false; - new_tax_rate = administration::tax_rate_create_empty(); - strops::copy(new_tax_rate.country_code, c.country_code, 3); - } - ImGui::PopStyleVar(); - } - - ImGui::TableSetColumnIndex(1); ImGui::Text(""); - } - - // Column 1: description of tax rate. Is only displayed on shared tax rates for clarity. ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); - - char category_code_desc[MAX_LEN_LONG_DESC]; - strops::format(category_code_desc, MAX_LEN_LONG_DESC, "taxcategory.%s", c.category_code); - ImGui::Text(can_be_modified ? "" : locale::get(category_code_desc)); - - - // Column 2: When editing, show input for new rate. Else we display the stored rate and check for modify request. - ImGui::TableSetColumnIndex(1); - if (is_editing_item && editing_item_index == i) - { - ImGui::InputFloat("##Rate", &new_tax_rate.rate, 1.0f, 5.0f, "%.2f"); - if (new_tax_rate.rate < 0.0f) new_tax_rate.rate = 0.0f; - if (new_tax_rate.rate > 100.0f) new_tax_rate.rate = 100.0f; - - ImGui::SameLine(); - if (ImGui::Button(locale::get("form.save"))) { - is_editing_item = false; - is_adding_item = false; - - administration::tax_rate_update(new_tax_rate); - - ui::destroy_settings(); - ui::setup_settings(); - } - - ImGui::SameLine(); - if (ImGui::Button(locale::get("form.cancel"))) { - is_editing_item = false; - is_adding_item = false; - memops::zero(&new_tax_rate, sizeof(new_tax_rate)); - } - } - else - { - ImGui::Text("%.2f%%", c.rate); - - if (can_be_modified && ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) - { - is_editing_item = true; - is_adding_item = false; - editing_item_index = i; - new_tax_rate = c; - } - } - - // When adding a new entry it is displayed at the bottom of the list of the country we are adding to. - // Check for end of list (for last country in the list), or check if next country differs from current country. - // If it is different we have reached the end of the list for the current country. - if (i == tax_rate_count-1 || (i < tax_rate_count-1 && strcmp(tax_rates[i+1].country_code, c.country_code) != 0)) - { - if (is_adding_item && strcmp(new_tax_rate.country_code, prev_country) == 0) - { - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); ImGui::Text(""); - ImGui::TableSetColumnIndex(1); - ImGui::InputFloat("##Rate", &new_tax_rate.rate, 1.0f, 5.0f, "%.2f"); - - if (new_tax_rate.rate < 0.0f) new_tax_rate.rate = 0.0f; - if (new_tax_rate.rate > 100.0f) new_tax_rate.rate = 100.0f; - - ImGui::SameLine(); - if (ImGui::Button(locale::get("form.save"))) { - is_editing_item = false; - is_adding_item = false; - - administration::tax_rate_add(new_tax_rate); - - ui::destroy_settings(); - ui::setup_settings(); - } - - ImGui::SameLine(); - if (ImGui::Button(locale::get("form.cancel"))) { - is_editing_item = false; - is_adding_item = false; - memops::zero(&new_tax_rate, sizeof(new_tax_rate)); - } - } + char localized_code[MAX_LEN_LONG_DESC]; + strops::format(localized_code, MAX_LEN_LONG_DESC, "taxrate.code.%s", c.internal_code); + ImGui::TableSetColumnIndex(0); ImGui::TextUnformatted(locale::get(localized_code)); + ImGui::TableSetColumnIndex(1); ImGui::Text("%.2f%%", c.rate); + ImGui::TableSetColumnIndex(2); + for (u32 x = 0; x < c.tax_section_count; x++) { + if (x == 0) ImGui::TextUnformatted(c.tax_sections[x]); + else ImGui::Text(", %s", c.tax_sections[x]); + if (x != c.tax_section_count-1) ImGui::SameLine(0, 0); } } ImGui::EndTable(); + ImGui::Spacing(); + ImGui::Spacing(); + ImGui::Spacing(); } + + type_iter = static_cast(static_cast(type_iter) + 1); + if (type_iter < TAX_RATE_TYPE_END) goto go_again; } static void draw_cost_centers() @@ -226,6 +115,7 @@ static void draw_cost_centers() ImGui::TableSetupColumn(locale::get("settings.costcenters.table.code"), ImGuiTableColumnFlags_WidthFixed, 140); ImGui::TableSetupColumn(locale::get("settings.costcenters.table.description")); + ImGui::TableHeadersRow(); for (u32 i = 0; i < cost_center_count; i++) { cost_center c = cost_centers[i]; @@ -443,20 +333,22 @@ void ui::draw_settings() ImGui::EndTabItem(); } - if (ImGui::BeginTabItem(locale::get("settings.table.vatrates"))) - { - draw_vat_rates(); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem(locale::get("settings.table.costcenters"))) - { - draw_cost_centers(); - ImGui::EndTabItem(); - } - if (ImGui::BeginTabItem(locale::get("settings.table.services"))) - { - draw_services(); - ImGui::EndTabItem(); + if (administration::can_create_invoices()) { + if (ImGui::BeginTabItem(locale::get("settings.table.vatrates"))) + { + draw_vat_rates(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem(locale::get("settings.table.costcenters"))) + { + draw_cost_centers(); + ImGui::EndTabItem(); + } + if (ImGui::BeginTabItem(locale::get("settings.table.services"))) + { + draw_services(); + ImGui::EndTabItem(); + } } ImGui::EndTabBar(); } -- cgit v1.2.3-70-g09d2