diff options
| author | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-09 08:33:08 +0200 |
|---|---|---|
| committer | Aldrik Ramaekers <aldrikboy@gmail.com> | 2025-08-09 08:33:08 +0200 |
| commit | 5d34aff5888d3f0c624251f15bedb96c347978d6 (patch) | |
| tree | 69a49651271d645ca7eaa114cb1830bf759c0930 /src/views/contacts.cpp | |
| parent | b94a7ae06b20d550c727d5192cea8baf3e8fb641 (diff) | |
refactors
Diffstat (limited to 'src/views/contacts.cpp')
| -rw-r--r-- | src/views/contacts.cpp | 227 |
1 files changed, 0 insertions, 227 deletions
diff --git a/src/views/contacts.cpp b/src/views/contacts.cpp deleted file mode 100644 index 31f95e3..0000000 --- a/src/views/contacts.cpp +++ /dev/null @@ -1,227 +0,0 @@ -#include <stdio.h> - -#include "views.hpp" -#include "imgui.h" -#include "../administration.hpp" -#include "../locales/locales.hpp" - -static view_state current_view_state = LIST; -static contact selected_for_removal; - -static contact active_contact; - -static void draw_contact_form() -{ - static const char* selected_country = NULL; - - if (ImGui::Button(localize("form.back"))) { - current_view_state = view_state::LIST; - memset(&active_contact, 0, sizeof(contact)); - selected_country = 0; - return; - } - ImGui::Spacing(); - - bool viewing_only = (current_view_state == view_state::VIEW); - - ImGui::BeginDisabled(); - - float widthAvailable = ImGui::GetContentRegionAvail().x; - - ImGui::SetNextItemWidth(widthAvailable*0.2f); - ImGui::InputText(localize("contact.form.identifier"), active_contact.id, IM_ARRAYSIZE(active_contact.id)); - if (!viewing_only) ImGui::EndDisabled(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.fullname"), localize("contact.form.fullname"), active_contact.name, IM_ARRAYSIZE(active_contact.name)); - ImGui::SameLine();view_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.address1"), localize("contact.form.address1"), active_contact.address1, IM_ARRAYSIZE(active_contact.address1)); - ImGui::SameLine();view_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.address2"), localize("contact.form.address2"), active_contact.address2, IM_ARRAYSIZE(active_contact.address2)); - ImGui::SameLine();view_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - - const char* countries[] = { localize("country.AT"),localize("country.BE"),localize("country.BG"),localize("country.HR"),localize("country.CY"),localize("country.CZ"),localize("country.DK"),localize("country.EE"),localize("country.FI"),localize("country.FR"),localize("country.DE"),localize("country.GR"),localize("country.HU"),localize("country.IE"),localize("country.IT"),localize("country.LV"),localize("country.LT"),localize("country.LU"),localize("country.MT"),localize("country.NL"),localize("country.PL"),localize("country.PT"),localize("country.RO"),localize("country.SK"),localize("country.SI"),localize("country.ES"),localize("country.SE") }; - s32 country_count = sizeof(countries) / sizeof(countries[0]); - if (selected_country == 0) { - for (int i = 0; i < country_count; i++) - { - if (strcmp(countries[i], active_contact.country) == 0) - { - selected_country = countries[i]; - break; - } - } - } - - if (ImGui::BeginCombo(localize("contact.form.country"), selected_country)) - { - for (int n = 0; n < IM_ARRAYSIZE(countries); n++) - { - bool is_selected = (selected_country == countries[n]); - if (ImGui::Selectable(countries[n], is_selected)) - selected_country = countries[n]; - } - ImGui::EndCombo(); - } - if (selected_country) { - strncpy(active_contact.country, selected_country, IM_ARRAYSIZE(active_contact.country)); - } - ImGui::SameLine();view_draw_required_tag(); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.taxnumber"), localize("contact.form.taxnumber"), active_contact.taxid, IM_ARRAYSIZE(active_contact.taxid)); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.businessnumber"), localize("contact.form.businessnumber"), active_contact.businessid, IM_ARRAYSIZE(active_contact.businessid)); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.email"), localize("contact.form.email"), active_contact.email, IM_ARRAYSIZE(active_contact.email)); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.phonenumber"), localize("contact.form.phonenumber"), active_contact.phone_number, IM_ARRAYSIZE(active_contact.phone_number)); - - ImGui::SetNextItemWidth(widthAvailable*0.5f); - ImGui::InputTextWithHint(localize("contact.form.bankaccount"), localize("contact.form.bankaccount"), active_contact.bank_account, IM_ARRAYSIZE(active_contact.bank_account)); - - if (viewing_only) ImGui::EndDisabled(); - - if (!viewing_only) { - bool can_save = strlen(active_contact.name) > 0 && strlen(active_contact.address1) > 0 && - strlen(active_contact.address2) > 0 && strlen(active_contact.country) > 0; - - if (!can_save) ImGui::BeginDisabled(); - // Save button - ImGui::Spacing(); - if (ImGui::Button(localize("form.save"))) { - if (current_view_state == view_state::CREATE) - administration_create_contact(active_contact); - - else if (current_view_state == view_state::EDIT) - administration_update_contact(active_contact); - - memset(&active_contact, 0, sizeof(contact)); - current_view_state = view_state::LIST; - selected_country = 0; - } - if (!can_save) ImGui::EndDisabled(); - } - else { - // TODO list invoices connected to contact. - } -} - -static void draw_contact_list() -{ - const u32 items_per_page = 50; - static s32 current_page = 0; - s32 max_page = (administration_get_contact_count() + items_per_page - 1) / items_per_page; - if (max_page == 0) max_page = 1; - - if (ImGui::Button(localize("form.create"))) - { - current_view_state = view_state::CREATE; - memset(&active_contact, 0, sizeof(contact)); - snprintf(active_contact.id, IM_ARRAYSIZE(active_contact.id), "C/%d", administration_create_id()); - } - - if (current_page >= max_page-1) current_page = max_page-1; - if (current_page < 0) current_page = 0; - - ImGui::SameLine(); - bool enable_prev = current_page > 0; - if (!enable_prev) ImGui::BeginDisabled(); - if (ImGui::Button("<< Prev") && current_page > 0) current_page--; - if (!enable_prev) ImGui::EndDisabled(); - - ImGui::SameLine(); - ImGui::Text("(%d/%d)", current_page+1, max_page); - - ImGui::SameLine(); - bool enable_next = current_page < max_page-1; - if (!enable_next) ImGui::BeginDisabled(); - if (ImGui::Button("Next >>") && current_page < max_page-1) current_page++; - if (!enable_next) ImGui::EndDisabled(); - - ImGui::Spacing(); - - if (ImGui::BeginTable("TableContacts", 4, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { - - ImGui::TableSetupColumn(localize("contact.table.identifier"), ImGuiTableColumnFlags_WidthFixed, 80); - ImGui::TableSetupColumn(localize("contact.table.name"), ImGuiTableColumnFlags_WidthStretch); - ImGui::TableSetupColumn(localize("contact.table.address"), ImGuiTableColumnFlags_WidthStretch); - ImGui::TableSetupColumn("", ImGuiTableColumnFlags_WidthFixed, 160); - ImGui::TableHeadersRow(); - - contact contact_list[items_per_page]; - u32 contact_count = administration_get_contacts(current_page, items_per_page, contact_list); - - for (u32 i = 0; i < contact_count; i++) { - contact c = contact_list[i]; - - ImGui::TableNextRow(); - ImGui::TableSetColumnIndex(0); ImGui::Text(c.id); - ImGui::TableSetColumnIndex(1); ImGui::Text(c.name); - ImGui::TableSetColumnIndex(2); ImGui::Text("%s %s", c.address1, c.address2); - - ImGui::TableSetColumnIndex(3); - - char btn_name[20]; - sprintf(btn_name, "%s##%d", localize("form.view"), i); - if (ImGui::Button(btn_name)) { - active_contact = c; - current_view_state = view_state::VIEW; - } - - ImGui::SameLine(); - - sprintf(btn_name, "%s##%d", localize("form.change"), i); - if (ImGui::Button(btn_name)) { - active_contact = c; - current_view_state = view_state::EDIT; - } - - ImGui::SameLine(); - - // TODO check to make sure no invoices are connected to this contact. - sprintf(btn_name, "%s##%d", localize("form.delete"), i); - if (ImGui::Button(btn_name)) { - selected_for_removal = c; - ImGui::OpenPopup("ConfirmDeletePopup"); - } - } - - if (ImGui::BeginPopupModal("ConfirmDeletePopup", nullptr, ImGuiWindowFlags_AlwaysAutoResize|ImGuiWindowFlags_NoMove|ImGuiWindowFlags_NoTitleBar)) { - ImGui::Text(localize("form.confirmDelete")); - ImGui::Separator(); - - if (ImGui::Button(localize("form.yes"), ImVec2(120, 0))) { - administration_remove_contact(selected_for_removal); - ImGui::CloseCurrentPopup(); - } - ImGui::SameLine(); - if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { - ImGui::CloseCurrentPopup(); - } - ImGui::EndPopup(); - } - - ImGui::EndTable(); - } -} - -void views_draw_contacts() -{ - switch(current_view_state) - { - case view_state::LIST: draw_contact_list(); break; - case view_state::CREATE: draw_contact_form(); break; - case view_state::EDIT: draw_contact_form(); break; - case view_state::VIEW: draw_contact_form(); break; - } -}
\ No newline at end of file |
