diff options
Diffstat (limited to 'src/ui/ui_contacts.cpp')
| -rw-r--r-- | src/ui/ui_contacts.cpp | 228 |
1 files changed, 228 insertions, 0 deletions
diff --git a/src/ui/ui_contacts.cpp b/src/ui/ui_contacts.cpp new file mode 100644 index 0000000..3115a77 --- /dev/null +++ b/src/ui/ui_contacts.cpp @@ -0,0 +1,228 @@ +#include <stdio.h> + +#include "strops.hpp" +#include "ui.hpp" +#include "imgui.h" +#include "administration.hpp" +#include "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) { + strops_copy(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]; + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.view"), i); + if (ImGui::Button(btn_name)) { + active_contact = c; + current_view_state = view_state::VIEW; + } + + ImGui::SameLine(); + + snprintf(btn_name, sizeof(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. + 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 (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 ui_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 |
