From 66d21e5fef32f64219f0f0b86c3b4b4a74623ba1 Mon Sep 17 00:00:00 2001 From: Aldrik Ramaekers Date: Sat, 9 Aug 2025 15:51:45 +0200 Subject: cost center crud --- src/ui/ui_contacts.cpp | 13 +++--- src/ui/ui_projects.cpp | 2 +- src/ui/ui_settings.cpp | 113 +++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 121 insertions(+), 7 deletions(-) (limited to 'src/ui') diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp index 24f45f7..e00fb78 100644 --- a/src/ui/ui_contacts.cpp +++ b/src/ui/ui_contacts.cpp @@ -196,12 +196,13 @@ static void draw_contact_list() } ImGui::SameLine(); - - // TODO check to make sure no invoices are connected to this contact. - snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i); - if (ImGui::Button(btn_name)) { - selected_for_removal = c; - ImGui::OpenPopup("ConfirmDeletePopup"); + if (administration_can_contact_be_deleted(c)) + { + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i); + if (ImGui::Button(btn_name)) { + selected_for_removal = c; + ImGui::OpenPopup("ConfirmDeletePopup"); + } } } diff --git a/src/ui/ui_projects.cpp b/src/ui/ui_projects.cpp index a831f60..377155e 100644 --- a/src/ui/ui_projects.cpp +++ b/src/ui/ui_projects.cpp @@ -130,7 +130,7 @@ static void draw_project_list() current_view_state = view_state::VIEW; } - if (c.state == project_state::RUNNING) + if (c.state == project_state::PROJECT_RUNNING) { ImGui::SameLine(); snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.change"), i); diff --git a/src/ui/ui_settings.cpp b/src/ui/ui_settings.cpp index 97e0503..7854be3 100644 --- a/src/ui/ui_settings.cpp +++ b/src/ui/ui_settings.cpp @@ -10,9 +10,19 @@ extern bool draw_contact_form(contact* buffer, bool back_button_enabled = true, bool viewing_only = false); static contact company_info; + country_tax_bracket* tax_brackets; u32 tax_bracket_count; +cost_center* cost_centers; +u32 cost_center_count; + +void ui_destroy_settings() +{ + free(tax_brackets); + free(cost_centers); +} + void ui_setup_settings() { company_info = administration_get_company_info(); @@ -20,6 +30,10 @@ void ui_setup_settings() tax_bracket_count = administration_get_tax_bracket_count(); tax_brackets = (country_tax_bracket*)malloc(tax_bracket_count * sizeof(country_tax_bracket)); administration_get_tax_brackets(tax_brackets); + + cost_center_count = administration_get_cost_center_count(); + cost_centers = (cost_center*)malloc(cost_center_count * sizeof(cost_center)); + administration_get_cost_centers(cost_centers); } static void ui_draw_vat_rates() @@ -66,6 +80,100 @@ static void ui_draw_vat_rates() } } +static void ui_draw_cost_centers() +{ + static bool is_adding_item = false; + static cost_center new_cost_center; + + static bool is_editing_item = false; + static u32 editing_item_index = 0; + + if (ImGui::BeginTable("TableCostCenters", 2, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { + + ImGui::TableSetupColumn(localize("settings.costcenters.table.code"), ImGuiTableColumnFlags_WidthFixed, 140); + ImGui::TableSetupColumn(localize("settings.costcenters.table.description")); + + for (u32 i = 0; i < cost_center_count; i++) { + cost_center c = cost_centers[i]; + + ImGui::TableNextRow(); + ImGui::TableSetColumnIndex(0); ImGui::Text(c.code); + ImGui::TableSetColumnIndex(1); + + if (is_editing_item && editing_item_index == i) + { + ImGui::InputText("##Description", new_cost_center.description, IM_ARRAYSIZE(new_cost_center.description)); + + bool is_desc_valid = administration_verify_cost_center_description(new_cost_center.description); + + if (!is_desc_valid) ImGui::BeginDisabled(); + ImGui::SameLine(); + if (ImGui::Button(localize("form.save"))) { + is_editing_item = false; + is_adding_item = false; + + administration_update_cost_center(new_cost_center); + + ui_destroy_settings(); + ui_setup_settings(); + } + if (!is_desc_valid) ImGui::EndDisabled(); + } + else + { + ImGui::Text(localize(c.description)); + } + + if (ImGui::IsItemHovered() && ImGui::IsMouseDoubleClicked(ImGuiMouseButton_Left)) + { + is_editing_item = true; + is_adding_item = false; + editing_item_index = i; + new_cost_center = c; + } + } + + if (is_adding_item) + { + ImGui::TableNextRow(); + + bool is_code_valid = administration_verify_cost_center_code(new_cost_center.code); + if (!is_code_valid) ImGui::PushStyleColor(ImGuiCol_FrameBg, IM_COL32(255, 0, 0, 64)); + ImGui::TableSetColumnIndex(0); ImGui::InputText("##Code", new_cost_center.code, IM_ARRAYSIZE(new_cost_center.code)); + if (!is_code_valid) ImGui::PopStyleColor(); + + bool is_desc_valid = administration_verify_cost_center_description(new_cost_center.description); + if (!is_desc_valid) ImGui::PushStyleColor(ImGuiCol_FrameBg, IM_COL32(255, 0, 0, 64)); + ImGui::TableSetColumnIndex(1); ImGui::InputText("##Description", new_cost_center.description, IM_ARRAYSIZE(new_cost_center.description)); + if (!is_desc_valid) ImGui::PopStyleColor(); + + bool can_save = is_code_valid && is_desc_valid; + + if (!can_save) ImGui::BeginDisabled(); + ImGui::SameLine(); + if (ImGui::Button(localize("form.create"))) + { + is_adding_item = false; + is_editing_item = false; + administration_add_cost_center(new_cost_center); + + ui_destroy_settings(); + ui_setup_settings(); + } + if (!can_save) ImGui::EndDisabled(); + } + + ImGui::EndTable(); + } + + if (!is_adding_item && ImGui::Button(localize("form.create"))) + { + is_adding_item = true; + is_editing_item = false; + memset(&new_cost_center, 0, sizeof(new_cost_center)); + } +} + void ui_draw_settings() { if (ImGui::BeginTabBar("SettingsTabBar")) @@ -84,6 +192,11 @@ void ui_draw_settings() ui_draw_vat_rates(); ImGui::EndTabItem(); } + if (ImGui::BeginTabItem(localize("settings.table.costcenters"))) + { + ui_draw_cost_centers(); + ImGui::EndTabItem(); + } ImGui::EndTabBar(); } } \ No newline at end of file -- cgit v1.2.3-70-g09d2