summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAldrik Ramaekers <aldrikboy@gmail.com>2025-09-16 16:16:34 +0200
committerAldrik Ramaekers <aldrikboy@gmail.com>2025-09-16 16:16:34 +0200
commitbb55b2341c53174ed53a70855ef63bb20c8dd814 (patch)
tree7964202d35bd7985c536fa8457c9755561d83675
parenta651d8e951ab35677af468b03a4bb02decbc105b (diff)
administration refactors
-rw-r--r--include/administration.hpp20
-rw-r--r--include/file_templates.hpp1
-rw-r--r--src/administration.cpp37
-rw-r--r--src/administration_reader.cpp18
-rw-r--r--src/administration_writer.cpp17
-rw-r--r--src/ui/ui_earnings.cpp2
-rw-r--r--src/ui/ui_main.cpp6
-rw-r--r--tests/administration_rw_tests.cpp (renamed from tests/run_tests.cpp)25
8 files changed, 69 insertions, 57 deletions
diff --git a/include/administration.hpp b/include/administration.hpp
index fd2d651..e74b49c 100644
--- a/include/administration.hpp
+++ b/include/administration.hpp
@@ -348,18 +348,24 @@ void administration_set_taxbracket_changed_event_callback(taxbracket_changed_
void administration_set_costcenter_changed_event_callback(costcenter_changed_event ev);
void administration_set_project_changed_event_callback(project_changed_event ev);
-// Other functions.
-// =======================
-administration* administration_get(); // TODO get rid of this and make indivual getters and setters
-char* administration_file_path_get();
+// Company info functions.
contact administration_company_info_get();
void administration_company_info_import(contact data);
void administration_company_info_set(contact data);
-void administration_create_income_statement(income_statement* statement);
-char* administration_get_currency_symbol_from_currency(char* code);
+
+// Other functions.
+// =======================
+char* administration_get_file_path();
+s32 administration_get_next_id();
+s32 administration_get_next_sequence_number();
+char* administration_get_currency_symbol_for_currency(char* code);
char* administration_get_default_currency();
+
+void administration_set_next_id(s32 nr);
+void administration_set_next_sequence_number(s32 nr);
+
+void administration_create_income_statement(income_statement* statement);
bool administration_can_create_invoices();
-bool administration_has_save_path();
// Contact functions.
// =======================
diff --git a/include/file_templates.hpp b/include/file_templates.hpp
index af2f373..ff3252f 100644
--- a/include/file_templates.hpp
+++ b/include/file_templates.hpp
@@ -45,7 +45,6 @@ const char* contact_save_template =
const char* administration_save_template =
"<Administration>\n"
" <NextId>{{NEXT_ID}}</NextId>\n"
-" <CompanyInfoId>{{COMPANY_ID}}</CompanyInfoId>\n"
" <NextSequenceNumber>{{NEXT_SEQUENCE_NUMBER}}</NextSequenceNumber>\n"
" <ProgramVersion>{{PROGRAM_VERSION}}</ProgramVersion>\n"
"</Administration>";
diff --git a/src/administration.cpp b/src/administration.cpp
index 30fbd76..90fc60a 100644
--- a/src/administration.cpp
+++ b/src/administration.cpp
@@ -513,9 +513,24 @@ void administration_create_default(char* save_file)
// Other functions.
// =======================
-bool administration_has_save_path()
+void administration_set_next_id(s32 nr)
{
- return strcmp(g_administration.path, "") != 0;
+ g_administration.next_id = nr;
+}
+
+void administration_set_next_sequence_number(s32 nr)
+{
+ g_administration.next_sequence_number = nr;
+}
+
+s32 administration_get_next_id()
+{
+ return g_administration.next_id;
+}
+
+s32 administration_get_next_sequence_number()
+{
+ return g_administration.next_sequence_number;
}
bool administration_can_create_invoices()
@@ -528,7 +543,7 @@ char* administration_get_default_currency()
return g_administration.default_currency;
}
-char* administration_get_currency_symbol_from_currency(char* code)
+char* administration_get_currency_symbol_for_currency(char* code)
{
// Major European currencies
if (strcmp(code, "EUR") == 0) return "€"; // Euro
@@ -794,9 +809,9 @@ void administration_create_income_statement(income_statement* statement)
log_add("Created income statement in %.3fms.", STOPWATCH_TIME);
}
-char* administration_file_path_get()
+char* administration_get_file_path()
{
- return g_administration.path;
+ return strlen(g_administration.path) == 0 ? NULL : g_administration.path;
}
contact administration_company_info_get()
@@ -820,11 +835,6 @@ void administration_company_info_set(contact data)
if (data_changed_event_callback) data_changed_event_callback();
}
-administration* administration_get()
-{
- return &g_administration;
-}
-
// Contact functions.
// =======================
bool administration_contact_import(contact data)
@@ -1124,10 +1134,6 @@ bool administration_project_import(project data)
bool administration_project_add(project data)
{
if (!administration_project_is_valid(data)) return false;
-
- data.state = project_state::PROJECT_RUNNING;
- data.start_date = time(NULL);
- data.end_date = 0;
project* new_project = (project*)malloc(sizeof(project));
memcpy((void*)new_project, (void*)&data, sizeof(project));
list_append(&g_administration.projects, new_project);
@@ -1188,6 +1194,9 @@ project administration_project_create_empty()
{
project result;
memset(&result, 0, sizeof(project));
+ result.state = project_state::PROJECT_RUNNING;
+ result.start_date = time(NULL);
+ result.end_date = 0;
snprintf(result.id, sizeof(result.id), "P/%d", administration_create_id());
return result;
}
diff --git a/src/administration_reader.cpp b/src/administration_reader.cpp
index 4fd1247..b7dfa8f 100644
--- a/src/administration_reader.cpp
+++ b/src/administration_reader.cpp
@@ -153,7 +153,7 @@ bool administration_reader_import_contact(char* buffer, size_t buffer_size)
struct xml_node* root = xml_document_root(document);
- contact data;
+ contact data = {0};
_get_xml_str(root, data.id, MAX_LEN_ID, "Id");
_get_xml_str(root, data.name, MAX_LEN_LONG_DESC, "Name");
data.type = (contact_type)_get_xml_s32(root, "Type");
@@ -186,7 +186,7 @@ bool administration_reader_import_project(char* buffer, size_t buffer_size)
struct xml_node* root = xml_document_root(document);
- project data;
+ project data = {0};
_get_xml_str(root, data.id, MAX_LEN_ID, "Id");
_get_xml_str(root, data.description, MAX_LEN_LONG_DESC, "Description");
data.state = (project_state)_get_xml_s32(root, "State");
@@ -209,7 +209,7 @@ bool administration_reader_import_cost_center(char* buffer, size_t buffer_size)
struct xml_node* root = xml_document_root(document);
- cost_center data;
+ cost_center data = {0};
_get_xml_str(root, data.id, MAX_LEN_ID, "Id");
_get_xml_str(root, data.code, MAX_LEN_CODE, "Code");
_get_xml_str(root, data.description, MAX_LEN_LONG_DESC, "Description");
@@ -230,7 +230,7 @@ bool administration_reader_import_tax_bracket(char* buffer, size_t buffer_size)
struct xml_node* root = xml_document_root(document);
- country_tax_bracket data;
+ country_tax_bracket data = {0};
_get_xml_str(root, data.id, MAX_LEN_ID, "Id");
_get_xml_str(root, data.country_code, MAX_LEN_COUNTRY_CODE, "CountryCode");
_get_xml_str(root, data.category_code, MAX_LEN_CODE, "Category");
@@ -252,13 +252,11 @@ bool administration_reader_import_administration_info(char* buffer, size_t buffe
struct xml_node* root = xml_document_root(document);
- administration* ad = administration_get();
- ad->next_id = _get_xml_s32(root, "NextId");
- ad->next_sequence_number = _get_xml_s32(root, "NextSequenceNumber");
- _get_xml_str(root, ad->company_info.id, MAX_LEN_ID, "CompanyInfoId");
+ administration_set_next_id(_get_xml_s32(root, "NextId"));
+ administration_set_next_sequence_number(_get_xml_s32(root, "NextSequenceNumber"));
- log_add("Loaded administration info in %.3fms. next_id=%d next_sequence_number=%d company_id=%s",
- STOPWATCH_TIME, ad->next_id, ad->next_sequence_number, ad->company_info.id);
+ log_add("Loaded administration info in %.3fms. next_id=%d next_sequence_number=%d",
+ STOPWATCH_TIME, administration_get_next_id(), administration_get_next_sequence_number());
return true;
} \ No newline at end of file
diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp
index 618b36e..d19fb51 100644
--- a/src/administration_writer.cpp
+++ b/src/administration_writer.cpp
@@ -80,7 +80,7 @@ static char* administration_writer_copy_template(const char* template_str, int*
static bool administration_writer_entry_exists(char* entry)
{
- struct zip_t *zip_read = zip_open(administration_file_path_get(), 0, 'r');
+ struct zip_t *zip_read = zip_open(administration_get_file_path(), 0, 'r');
int result = zip_entry_open(zip_read, entry);
zip_close(zip_read);
@@ -92,8 +92,8 @@ static bool _administration_writer_delete_entry_by_name(char* entry)
STOPWATCH_START;
bool result = 1;
- struct zip_t *zip_write = zip_open(administration_file_path_get(), 0, 'a');
- if (!zip_write) zip_write = zip_open(administration_file_path_get(), 0, 'w');
+ struct zip_t *zip_write = zip_open(administration_get_file_path(), 0, 'a');
+ if (!zip_write) zip_write = zip_open(administration_get_file_path(), 0, 'w');
char* indices[1] = {entry};
if (zip_entries_delete(zip_write, indices, 1) < 0) result = 0;
@@ -118,8 +118,8 @@ static bool administration_writer_write_to_zip(char* entry_to_replace, char* ori
bool entry_exists = administration_writer_entry_exists(entry_to_replace);
if (entry_exists) _administration_writer_delete_entry_by_name(entry_to_replace);
- struct zip_t *zip_write = zip_open(administration_file_path_get(), 0, 'a');
- if (!zip_write) zip_write = zip_open(administration_file_path_get(), 0, 'w');
+ struct zip_t *zip_write = zip_open(administration_get_file_path(), 0, 'a');
+ if (!zip_write) zip_write = zip_open(administration_get_file_path(), 0, 'w');
zip_entry_open(zip_write, entry_to_replace);
if (zip_entry_write(zip_write, orig_content, final_length) < 0) result = 0;
@@ -644,10 +644,9 @@ bool administration_writer_save_all_administration_info_blocking()
int buf_length = 0;
char* file_content = administration_writer_copy_template(administration_save_template, &buf_length);
- strops_replace(file_content, buf_length, "{{COMPANY_ID}}", administration_company_info_get().id);
- strops_replace_int32(file_content, buf_length, "{{NEXT_ID}}", administration_get()->next_id);
- strops_replace_int32(file_content, buf_length, "{{NEXT_SEQUENCE_NUMBER}}", administration_get()->next_sequence_number);
- strops_replace(file_content, buf_length, "{{PROGRAM_VERSION}}", administration_get()->program_version);
+ strops_replace_int32(file_content, buf_length, "{{NEXT_ID}}", administration_get_next_id());
+ strops_replace_int32(file_content, buf_length, "{{NEXT_SEQUENCE_NUMBER}}", administration_get_next_sequence_number());
+ strops_replace(file_content, buf_length, "{{PROGRAM_VERSION}}", PROGRAM_VERSION);
//// Write to Disk.
int final_length = (int)strlen(file_content);
diff --git a/src/ui/ui_earnings.cpp b/src/ui/ui_earnings.cpp
index f85a54c..002b2bc 100644
--- a/src/ui/ui_earnings.cpp
+++ b/src/ui/ui_earnings.cpp
@@ -43,7 +43,7 @@ void ui_draw_earnings()
ImGui::Spacing();
- char* currency_symbol = administration_get_currency_symbol_from_currency(administration_get_default_currency());
+ char* currency_symbol = administration_get_currency_symbol_for_currency(administration_get_default_currency());
if (ImGui::BeginTable("QuarterlyResultsTable", 5, ImGuiTableFlags_Borders | ImGuiTableFlags_RowBg | ImGuiTableFlags_SizingFixedFit | ImGuiTableFlags_Hideable))
{
diff --git a/src/ui/ui_main.cpp b/src/ui/ui_main.cpp
index 59c4156..3405a7e 100644
--- a/src/ui/ui_main.cpp
+++ b/src/ui/ui_main.cpp
@@ -144,12 +144,12 @@ void ui_draw_main()
ImGuiWindowFlags_NoBringToFrontOnFocus |
ImGuiWindowFlags_NoCollapse);
- char* path = administration_file_path_get();
- if (strlen(path) == 0) {
+ char* path = administration_get_file_path();
+ if (path == NULL) {
ImGui::Text("%s: %s", localize("ui.workingOn"), localize("ui.unsavedProject"));
}
else {
- ImGui::Text("%s: %s", localize("ui.workingOn"), administration_file_path_get());
+ ImGui::Text("%s: %s", localize("ui.workingOn"), path);
}
ImGui::SameLine();
diff --git a/tests/run_tests.cpp b/tests/administration_rw_tests.cpp
index e9092a6..6d57596 100644
--- a/tests/run_tests.cpp
+++ b/tests/administration_rw_tests.cpp
@@ -47,10 +47,7 @@ TEST _administration_rw_taxbracket(void)
{
ASSERT_EQ(count+1, administration_tax_bracket_count());
ASSERT_EQ(1, administration_tax_bracket_get_by_id(&pr, pw.id));
- ASSERT_STR_EQ(pr.id, pw.id);
- ASSERT_STR_EQ(pr.country_code, pw.country_code);
- ASSERT_STR_EQ(pr.category_code, pw.category_code);
- ASSERT_EQ(pr.rate, pw.rate);
+ ASSERT_MEM_EQ(&pw, &pr, sizeof(country_tax_bracket));
}
PASS();
@@ -80,9 +77,7 @@ TEST _administration_rw_costcenter(void)
{
ASSERT_EQ(count+1, administration_cost_center_count());
ASSERT_EQ(1, administration_cost_center_get_by_id(&pr, pw.id));
- ASSERT_STR_EQ(pr.id, pw.id);
- ASSERT_STR_EQ(pr.code, pw.code);
- ASSERT_STR_EQ(pr.description, pw.description);
+ ASSERT_MEM_EQ(&pw, &pr, sizeof(cost_center));
}
PASS();
@@ -110,8 +105,7 @@ TEST _administration_rw_project(void)
{
ASSERT_EQ(count+1, administration_project_count());
ASSERT_EQ(1, administration_project_get_by_id(&pr, pw.id));
- ASSERT_STR_EQ(pr.id, pw.id);
- ASSERT_STR_EQ(pr.description, pw.description);
+ ASSERT_MEM_EQ(&pw, &pr, sizeof(project));
}
PASS();
@@ -128,6 +122,13 @@ TEST _administration_rw_contact(void)
administration_create_empty(test_file_path);
{
pw = administration_contact_create_empty();
+ strops_copy(pw.name, "John Johnsson", sizeof(pw.name));
+ strops_copy(pw.address.address1, "Address 1", sizeof(pw.address.address1));
+ strops_copy(pw.address.country_code, "FR", sizeof(pw.address.country_code));
+ strops_copy(pw.address.city, "Paris", sizeof(pw.address.city));
+ strops_copy(pw.address.postal, "12345", sizeof(pw.address.postal));
+ pw.type = contact_type::CONTACT_CONSUMER;
+
count = administration_contact_count();
administration_contact_add(pw);
@@ -138,13 +139,13 @@ TEST _administration_rw_contact(void)
{
ASSERT_EQ(count+1, administration_contact_count());
ASSERT_EQ(1, administration_contact_get_by_id(&pr, pw.id));
- ASSERT_STR_EQ(pr.id, pw.id);
+ ASSERT_MEM_EQ(&pw, &pr, sizeof(contact));
}
PASS();
}
-SUITE(administration_io) {
+SUITE(administration_rw) {
SET_SETUP(setup_cb, NULL);
SET_TEARDOWN(teardown_cb, NULL);
@@ -159,6 +160,6 @@ int main(int argc, char **argv) {
timer_lib_initialize();
GREATEST_MAIN_BEGIN();
- RUN_SUITE(administration_io);
+ RUN_SUITE(administration_rw);
GREATEST_MAIN_END();
} \ No newline at end of file