#define _CRT_SECURE_NO_WARNINGS #include #include #include "ImGuiDatePicker/ImGuiDatePicker.hpp" //#include "tinyfiledialogs/tinyfiledialogs.h" #include "strops.hpp" #include "ui.hpp" #include "imgui.h" #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}; void draw_contact_form_ex(contact* buffer, bool viewing_only = false, bool with_autocomplete = false, bool* on_autocomplete = 0); void ui_setup_invoices() { current_view_state = view_state::LIST; active_invoice = administration_create_empty_invoice(); } void draw_costcenter_selector(char* costcenter_id) { cost_center* selected_costcenter = NULL; u32 costcenter_count = administration_get_cost_center_count(); cost_center* buffer = (cost_center*) malloc(sizeof(cost_center) * costcenter_count); costcenter_count = administration_get_cost_centers(buffer); // Select cost center by given id. if (strlen(costcenter_id) > 0) { for (u32 i = 0; i < costcenter_count; i++) { if (strcmp(buffer[i].id, costcenter_id) == 0) { selected_costcenter = &buffer[i]; break; } } } int selected_costcenter_index = -1; if (ImGui::BeginCombo("Cost center", selected_costcenter == NULL ? NULL : localize(selected_costcenter->description))) { for (u32 n = 0; n < costcenter_count; n++) { bool is_selected = selected_costcenter && strcmp(selected_costcenter->id, buffer[n].id) == 0; if (ImGui::Selectable(localize(buffer[n].description), is_selected)) { selected_costcenter_index = n; } } ImGui::EndCombo(); } if (selected_costcenter_index != -1) { strops_copy(costcenter_id, buffer[selected_costcenter_index].id, 16); } free(buffer); } void draw_project_selector(char* project_id) { project* selected_project = NULL; u32 project_count = administration_get_project_count(); project* buffer = (project*) malloc(sizeof(project) * project_count); project_count = administration_get_all_projects(buffer); // Select project by given id. if (strlen(project_id) > 0) { for (u32 i = 0; i < project_count; i++) { if (strcmp(buffer[i].id, project_id) == 0) { selected_project = &buffer[i]; break; } } } int selected_project_index = -1; if (ImGui::BeginCombo("Project", selected_project == NULL ? NULL : selected_project->description)) { for (u32 n = 0; n < project_count; n++) { bool is_selected = selected_project && strcmp(selected_project->id, buffer[n].id) == 0; if (ImGui::Selectable(buffer[n].description, is_selected)) { selected_project_index = n; } } ImGui::EndCombo(); } if (selected_project_index != -1) { strops_copy(project_id, buffer[selected_project_index].id, 16); } free(buffer); } void draw_invoice_form(invoice* buffer, bool viewing_only = false) { //float widthAvailable = ImGui::GetContentRegionAvail().x; ImGui::BeginDisabled(); // 1. Identifier //ImGui::SetNextItemWidth(widthAvailable*0.2f); //ImGui::InputText(localize("contact.form.identifier"), buffer->id, IM_ARRAYSIZE(buffer->id)); if (!viewing_only) ImGui::EndDisabled(); // 2. Sequential number ImGui::Text("Invoice number: %s", buffer->sequential_number); // 3. Supplier (you) ImGui::Text("Supplier: %s", buffer->supplier.name); // 6. Invoice issued at ImGui::BeginDisabled(); tm issued_at_date = *gmtime(&buffer->issued_at); if (ImGui::DatePicker("##issuedAt", issued_at_date)) { buffer->issued_at = mktime(&issued_at_date); } ImGui::SameLine(); ImGui::Text("Invoice issued at"); ImGui::EndDisabled(); // 7. Invoice expires at ImGui::BeginDisabled(); tm expires_at_date = *gmtime(&buffer->expires_at); if (ImGui::DatePicker("##expiresAt", expires_at_date)) { buffer->expires_at = mktime(&expires_at_date); } ImGui::SameLine(); ImGui::Text("Invoice expires at"); ImGui::EndDisabled(); // 8. Product/service delivered at tm delivered_at_date = *gmtime(&buffer->delivered_at); if (ImGui::DatePicker("##deliveredAt", delivered_at_date)) { buffer->delivered_at = mktime(&delivered_at_date); } ImGui::SameLine(); ImGui::Text("Product/service delivered at"); ImGui::Separator(); // 4. Customer information ImGui::Text("Billing information"); bool on_autocomplete; draw_contact_form_ex(&buffer->customer, false, true, &on_autocomplete); if (on_autocomplete) { strops_copy(buffer->customer_id, buffer->customer.id, sizeof(buffer->customer_id)); } // 5. (optional) shipping address. static bool shipping_is_billing_addr = true; ImGui::Checkbox("Shipping information is billing information", &shipping_is_billing_addr); if (!shipping_is_billing_addr) { ImGui::Spacing(); ImGui::Text("Shipping information"); ui_draw_address_form(&buffer->shipping_address); } ImGui::Separator(); // 9. Project selection draw_project_selector(buffer->project_id); // 10. Cost center selection draw_costcenter_selector(buffer->cost_center_id); //ImGui::SetNextItemWidth(widthAvailable*0.5f); //ImGui::InputTextWithHint("Invoice number", "Invoice number", buffer->sequential_number, IM_ARRAYSIZE(buffer->sequential_number)); //ImGui::SameLine();ui_helper_draw_required_tag(); if (viewing_only) ImGui::EndDisabled(); } void draw_invoices_list() { // Table header controls: create button and pagination. if (ImGui::Button(localize("form.create"))) { current_view_state = view_state::CREATE; active_invoice = administration_create_empty_invoice(); active_invoice.supplier = administration_get_company_info(); strops_copy(active_invoice.supplier_id, active_invoice.supplier.id, sizeof(active_invoice.supplier_id)); } } void ui_draw_invoices() { switch(current_view_state) { case view_state::LIST: draw_invoices_list(); break; case view_state::CREATE: if (ImGui::Button(localize("form.back"))) { current_view_state = view_state::LIST; } draw_invoice_form(&active_invoice); //if () { //administration_create_invoice(active_invoice); //current_view_state = view_state::LIST; //} break; case view_state::EDIT: break; case view_state::VIEW: break; } }