diff options
Diffstat (limited to 'src/ui/ui_invoices.cpp')
| -rw-r--r-- | src/ui/ui_invoices.cpp | 193 |
1 files changed, 11 insertions, 182 deletions
diff --git a/src/ui/ui_invoices.cpp b/src/ui/ui_invoices.cpp index c3c6b8c..f061e3f 100644 --- a/src/ui/ui_invoices.cpp +++ b/src/ui/ui_invoices.cpp @@ -33,19 +33,13 @@ static view_state current_view_state = view_state::LIST; static invoice active_invoice = {0}; static invoice selected_for_removal = {0}; -static tax_rate* tax_rate_list_buffer = 0; static billing_item* invoice_items_buffer = 0; -static project* project_list_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 draw_project_selector(char* project_id, project* buffer); +void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false); void ui_destroy_invoices() { - free(tax_rate_list_buffer); free(invoice_items_buffer); - free(project_list_buffer); } void ui_setup_invoices() @@ -53,129 +47,11 @@ void ui_setup_invoices() current_view_state = view_state::LIST; active_invoice = administration_invoice_create_empty(); - u32 tax_rate_count = administration_tax_rate_count(); - tax_rate_list_buffer = (tax_rate*) malloc(sizeof(tax_rate) * tax_rate_count); - u32 invoice_items_count = MAX_BILLING_ITEMS; invoice_items_buffer = (billing_item*)malloc(sizeof(billing_item) * invoice_items_count); - - u32 project_count = administration_project_count(); - project_list_buffer = (project*) malloc(sizeof(project) * project_count); -} - -void draw_tax_rate_selector(char* tax_rate_id, tax_rate* buffer, char* orig_country, char* dest_country) -{ - tax_rate* selected_tax_rate = NULL; - char* country_codes[2] = {orig_country, dest_country}; - u32 tax_rate_count = administration_tax_rate_get_by_country(buffer, 2, country_codes); - - // Select tax rate by given id. - if (strlen(tax_rate_id) > 0) - { - for (u32 i = 0; i < tax_rate_count; i++) - { - if (strcmp(buffer[i].id, tax_rate_id) == 0) - { - selected_tax_rate = &buffer[i]; - break; - } - } - } - - int selected_tax_rate_index = -1; - char rate_str_buf[40]; - rate_str_buf[0] = 0; - if (selected_tax_rate) - { - if (strcmp(selected_tax_rate->country_code, "00") == 0) { - char category_code_desc[MAX_LEN_LONG_DESC]; - snprintf(category_code_desc, MAX_LEN_LONG_DESC, "taxcategory.%s", selected_tax_rate->category_code); - snprintf(rate_str_buf, 40, "%s", localize(category_code_desc)); - } - else snprintf(rate_str_buf, 40, "%s/%.1f%%", selected_tax_rate->country_code, selected_tax_rate->rate); - } - - if (ImGui::BeginCombo("##Tax Bracket", rate_str_buf)) - { - for (u32 n = 0; n < tax_rate_count; n++) - { - bool is_selected = selected_tax_rate && strcmp(selected_tax_rate->id, buffer[n].id) == 0; - - if (strcmp(buffer[n].country_code, "00") == 0) { - char category_code_desc[MAX_LEN_LONG_DESC]; - snprintf(category_code_desc, MAX_LEN_LONG_DESC, "taxcategory.%s", buffer[n].category_code); - snprintf(rate_str_buf, 40, "%s", localize(category_code_desc)); - } - else snprintf(rate_str_buf, 40, "%s/%.1f%%", buffer[n].country_code, buffer[n].rate); - - if (ImGui::Selectable(rate_str_buf, is_selected)) { - selected_tax_rate_index = n; - } - } - ImGui::EndCombo(); - } - - if (selected_tax_rate_index != -1) { - strops_copy(tax_rate_id, buffer[selected_tax_rate_index].id, MAX_LEN_ID); - } } -bool draw_currency_selector(char* currency) -{ - int currentCurrency = 0; - bool result = false; - - // Top 15 most traded currencies + all EU official currencies - const char* currencies[] = { - // Top 15 - "EUR", "USD", "JPY", "GBP", "AUD", "CAD", "CHF", - "CNY", "HKD", "NZD", "SEK", "KRW", "SGD", "NOK", "MXN", - - // Additional EU currencies - "BGN", // Bulgarian Lev - "CZK", // Czech Koruna - "DKK", // Danish Krone - "HUF", // Hungarian Forint - "PLN", // Polish Zloty - "RON", // Romanian Leu - // "HRK", // Croatian Kuna (legacy, replaced by EUR in 2023) - }; - int currency_count = sizeof(currencies) / sizeof(char*); - - if (strlen(currency) > 0) - { - for (int i = 0; i < currency_count; i++) - { - if (strcmp(currencies[i], currency) == 0) - { - currentCurrency = i; - break; - } - } - } - - ImGui::SetNextItemWidth(100.0f); - if (ImGui::BeginCombo("##currency", currencies[currentCurrency])) - { - for (int n = 0; n < IM_ARRAYSIZE(currencies); n++) - { - bool isSelected = (currentCurrency == n); - if (ImGui::Selectable(currencies[n], isSelected)) - { - result = true; - strops_copy(currency, currencies[n], MAX_LEN_CURRENCY); - } - - if (isSelected) - ImGui::SetItemDefaultFocus(); - } - ImGui::EndCombo(); - } - - return result; -} - -void draw_invoice_items_form(invoice* invoice, bool is_outgoing) +void draw_invoice_items_form(invoice* invoice) { billing_item* buffer = invoice_items_buffer; u32 invoice_items = administration_billing_item_get_all_for_invoice(invoice, buffer); @@ -212,22 +88,7 @@ void draw_invoice_items_form(invoice* invoice, bool is_outgoing) ImGui::InputFloat("##amount", &item.amount, 0.0f, 0.0f, "%.0f"); ImGui::SameLine(); - // Toggle between X and % - { - const char* items[] = { "X", "%" }; - if (ImGui::BeginCombo("Mode", items[item.amount_is_percentage])) { - for (int n = 0; n < 2; n++) { - bool is_selected = (n == (int)item.amount_is_percentage); - if (ImGui::Selectable(items[n], is_selected)) { - item.amount_is_percentage = n; - } - if (is_selected) { - ImGui::SetItemDefaultFocus(); - } - } - ImGui::EndCombo(); - } - } + ImGui::FormToggleCombo(&item.amount_is_percentage, "X", "%"); ImGui::TableSetColumnIndex(2); ImGui::PushItemWidth(-1); @@ -243,31 +104,15 @@ void draw_invoice_items_form(invoice* invoice, bool is_outgoing) ImGui::InputFloat("##discount", &item.discount, 0.0f, 0.0f, "%.2f"); ImGui::SameLine(); - // Toggle between currency and % - { - const char* items[] = { item.currency, "%" }; - if (ImGui::BeginCombo("Mode##discountMode", items[item.discount_is_percentage])) { - for (int n = 0; n < 2; n++) { - bool is_selected = (n == (int)item.discount_is_percentage); - if (ImGui::Selectable(items[n], is_selected)) { - item.discount_is_percentage = n; - } - if (is_selected) { - ImGui::SetItemDefaultFocus(); - } - } - ImGui::EndCombo(); - } - } + ImGui::FormToggleCombo(&item.amount_is_percentage, item.currency, "%"); ImGui::TableSetColumnIndex(5); ImGui::Text("%.2f %s", item.net, item.currency); ImGui::TableSetColumnIndex(6); ImGui::PushItemWidth(-1); - draw_tax_rate_selector(item.tax_rate_id, tax_rate_list_buffer, - administration_company_info_get().address.country_code, - is_outgoing ? invoice->customer.address.country_code : invoice->supplier.address.country_code); + ImGui::FormTaxRateCombo(item.tax_rate_id, invoice->customer.address.country_code, invoice->supplier.address.country_code); + ImGui::PopItemWidth(); ImGui::TableSetColumnIndex(7); @@ -329,17 +174,9 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) { ImGui::BeginDisabled(); - // 1. Identifier - //ImGui::SetNextItemWidth(widthAvailable*0.2f); - //ImGui::InputText(localize("contact.form.identifier"), buffer->id, IM_ARRAYSIZE(buffer->id)); - - // 2. Sequential number ImGui::Text("%s: %s", localize("invoice.form.invoicenumber"), buffer->sequential_number); - - // 3. Supplier (you) ImGui::Text("%s: %s", localize("invoice.form.supplier"), buffer->supplier.name); - // 4. Invoice issued at tm issued_at_date = *gmtime(&buffer->issued_at); if (ImGui::DatePicker("##issuedAt", issued_at_date)) { @@ -348,7 +185,6 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) ImGui::SameLine(); ImGui::Text(localize("invoice.form.issuedat")); - // 5. Invoice expires at tm expires_at_date = *gmtime(&buffer->expires_at); if (ImGui::DatePicker("##expiresAt", expires_at_date)) { @@ -358,7 +194,6 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) ImGui::Text(localize("invoice.form.expiresat")); if (!viewing_only) ImGui::EndDisabled(); - // 6. Product/service delivered at tm delivered_at_date = *gmtime(&buffer->delivered_at); if (ImGui::DatePicker("##deliveredAt", delivered_at_date)) { @@ -369,27 +204,23 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) ImGui::Separator(); - // 7. Customer information ImGui::Text(localize("invoice.form.billinginformation")); - draw_contact_form_ex(&buffer->customer, false, true, 0); + draw_contact_form_ex(&buffer->customer, false, true); - // 8. (optional) shipping address. ImGui::Checkbox(localize("invoice.form.triangulation"), &buffer->is_triangulation); if (buffer->is_triangulation) { ImGui::Spacing(); ImGui::Text(localize("invoice.form.shippinginformation")); - draw_contact_form_ex(&buffer->addressee, 0,0,0); + draw_contact_form_ex(&buffer->addressee, 0,0); } ImGui::Separator(); - // 9. Project selection - draw_project_selector(buffer->project_id, project_list_buffer); + ImGui::FormProjectCombo(buffer->project_id); ImGui::Spacing(); ImGui::Spacing(); 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(localize("invoice.form.add")))) @@ -399,17 +230,15 @@ static void draw_invoice_form(invoice* buffer, bool viewing_only = false) } if (max_items_reached) ImGui::EndDisabled(); - // 12. Dropdown for invoice currency. ImGui::SameLine(); ImGui::Text("| %s: ", localize("invoice.form.currency")); ImGui::SameLine(); - if (draw_currency_selector(buffer->currency)) + if (ImGui::FormCurrencyCombo(buffer->currency)) { administration_invoice_set_currency(buffer, buffer->currency); } - // 13. Invoice items form - draw_invoice_items_form(buffer, true); + draw_invoice_items_form(buffer); if (viewing_only) ImGui::EndDisabled(); } |
