diff options
Diffstat (limited to 'src/ui/ui_invoices.cpp')
| -rw-r--r-- | src/ui/ui_invoices.cpp | 94 |
1 files changed, 53 insertions, 41 deletions
diff --git a/src/ui/ui_invoices.cpp b/src/ui/ui_invoices.cpp index c08dd76..320392a 100644 --- a/src/ui/ui_invoices.cpp +++ b/src/ui/ui_invoices.cpp @@ -11,20 +11,25 @@ #include "administration.hpp" #include "locales.hpp" -void ui_draw_address_form(address* buffer); static view_state current_view_state = view_state::LIST; static invoice active_invoice = {0}; +static invoice selected_for_removal = {0}; cost_center* cost_center_list_buffer = 0; country_tax_bracket* tax_bracket_list_buffer = 0; +project* project_list_buffer = 0; +billing_item* invoice_items_buffer = 0; +void ui_draw_address_form(address* buffer); void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false, bool* on_autocomplete = 0); void ui_destroy_invoices() { free(cost_center_list_buffer); free(tax_bracket_list_buffer); + free(project_list_buffer); + free(invoice_items_buffer); } void ui_setup_invoices() @@ -33,10 +38,16 @@ void ui_setup_invoices() active_invoice = administration_invoice_create_empty(); u32 costcenter_count = administration_cost_center_count(); - cost_center_list_buffer = (cost_center*) malloc(sizeof(cost_center) * costcenter_count); // @leak + cost_center_list_buffer = (cost_center*) malloc(sizeof(cost_center) * costcenter_count); u32 tax_bracket_count = administration_tax_bracket_count(); - tax_bracket_list_buffer = (country_tax_bracket*) malloc(sizeof(country_tax_bracket) * tax_bracket_count); // @leak + tax_bracket_list_buffer = (country_tax_bracket*) malloc(sizeof(country_tax_bracket) * tax_bracket_count); + + u32 project_count = administration_project_count(); + project_list_buffer = (project*) malloc(sizeof(project) * project_count); + + u32 invoice_items_count = MAX_BILLING_ITEMS; + invoice_items_buffer = (billing_item*)malloc(sizeof(billing_item) * invoice_items_count); } void draw_tax_bracket_selector(char* tax_bracket_id) @@ -185,10 +196,8 @@ void draw_costcenter_selector(char* costcenter_id) void draw_project_selector(char* project_id) { project* selected_project = NULL; - - u32 project_count = administration_project_count(); - project* buffer = (project*) malloc(sizeof(project) * project_count); - project_count = administration_project_get_all(buffer); + project* buffer = project_list_buffer; + u32 project_count = administration_project_get_all(buffer); // Select project by given id. if (strlen(project_id) > 0) @@ -219,15 +228,12 @@ void draw_project_selector(char* project_id) if (selected_project_index != -1) { strops_copy(project_id, buffer[selected_project_index].id, MAX_LEN_ID); } - - free(buffer); } static void draw_invoice_items_form(invoice* invoice) { - u32 invoice_items = administration_billing_items_count(invoice); - billing_item* buffer = (billing_item*)malloc(sizeof(billing_item) * invoice_items); - administration_billing_item_get_all_for_invoice(invoice, buffer); + billing_item* buffer = invoice_items_buffer; + u32 invoice_items = administration_billing_item_get_all_for_invoice(invoice, buffer); if (ImGui::BeginTable("TableBillingItems", 9, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg)) { @@ -343,8 +349,6 @@ static void draw_invoice_items_form(invoice* invoice) ImGui::EndTable(); } - - free(buffer); } static void draw_invoice_form(invoice* buffer, bool viewing_only = false) @@ -396,12 +400,20 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) bool on_autocomplete; draw_contact_form_ex(&buffer->customer, false, true, &on_autocomplete); - // TODO: check if customer info is equal to contact stored in customer id, in case we select from dropdown and edit data after, - // this should be handled as a new contact and customer_id should be set to "" so we create a new contact. - if (on_autocomplete) { strops_copy(buffer->customer_id, buffer->customer.id, sizeof(buffer->customer_id)); } + + // Check if contact info is equal to contact stored in customer id, in case we select from dropdown and edit data after, + // this should be handled as a new contact and customer_id should be set to "" so we create a new contact. + contact lookup_buffer; + if (administration_contact_get_by_id(&lookup_buffer, buffer->customer_id)) + { + if (!administration_contact_equals(lookup_buffer, buffer->customer)) + { + buffer->customer_id[0] = '\0'; + } + } // 8. (optional) shipping address. ImGui::Checkbox("Shipping information differs from billing information (triangulation)", &buffer->is_triangulation); @@ -424,11 +436,14 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) ImGui::Spacing(); // 11. New billing item button. + bool max_items_reached = administration_billing_item_count(buffer) >= MAX_BILLING_ITEMS; + if (max_items_reached) ImGui::BeginDisabled(); if (ImGui::Button(localize("+ Billing item"))) { billing_item item = administration_billing_item_create_empty(); administration_billing_item_add_to_invoice(buffer, item); } + if (max_items_reached) ImGui::EndDisabled(); // 12. Dropdown for invoice currency. ImGui::SameLine(); @@ -530,34 +545,31 @@ static void ui_draw_invoices_list() active_invoice = administration_invoice_create_copy(&c); // We create a copy because of billing item list pointers. current_view_state = view_state::EDIT; } + + ImGui::SameLine(); + snprintf(btn_name, sizeof(btn_name), "%s##%d", localize("form.delete"), i); + if (ImGui::Button(btn_name)) { + selected_for_removal = c; + ImGui::OpenPopup("ConfirmDeletePopup"); + } } - - // ImGui::SameLine(); - // if (administration_contact_can_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"); - // } - // } } // Confirmation popup before contact is deleted definitively. - // 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_contact_remove(selected_for_removal); - // ImGui::CloseCurrentPopup(); - // } - // ImGui::SameLine(); - // if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { - // ImGui::CloseCurrentPopup(); - // } - // ImGui::EndPopup(); - // } + 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_invoice_remove(&selected_for_removal); + ImGui::CloseCurrentPopup(); + } + ImGui::SameLine(); + if (ImGui::Button(localize("form.no"), ImVec2(120, 0))) { + ImGui::CloseCurrentPopup(); + } + ImGui::EndPopup(); + } ImGui::EndTable(); } |
