diff options
Diffstat (limited to 'src/administration.cpp')
| -rw-r--r-- | src/administration.cpp | 109 |
1 files changed, 104 insertions, 5 deletions
diff --git a/src/administration.cpp b/src/administration.cpp index d463cec..1684442 100644 --- a/src/administration.cpp +++ b/src/administration.cpp @@ -13,6 +13,14 @@ administration g_administration; +data_changed_event data_changed_event_callback = 0; +data_deleted_event data_deleted_event_callback = 0; +invoice_changed_event invoice_changed_event_callback = 0; +contact_changed_event contact_changed_event_callback = 0; +taxbracket_changed_event taxbracket_changed_event_callback = 0; +costcenter_changed_event costcenter_changed_event_callback = 0; +project_changed_event project_changed_event_callback = 0; + static s32 administration_create_id() { return g_administration.next_id; @@ -27,6 +35,8 @@ static s32 administration_create_id() memcpy(tb->category_code, _code, sizeof(tb->category_code));\ list_append(&g_administration.tax_brackets, tb);\ g_administration.next_id++;\ + if (taxbracket_changed_event_callback) taxbracket_changed_event_callback(tb);\ + if (data_changed_event_callback) data_changed_event_callback();\ } static int compare_tax_countries(const void *a, const void *b) @@ -267,6 +277,8 @@ static void administration_create_default_cost_centers() memcpy(tb->code, _code, sizeof(tb->code));\ list_append(&g_administration.cost_centers, tb);\ g_administration.next_id++;\ + if (costcenter_changed_event_callback) costcenter_changed_event_callback(tb);\ + if (data_changed_event_callback) data_changed_event_callback();\ } ADD_COSTCENTER("costcenter.general_expenses", "GENE"); @@ -392,6 +404,43 @@ static s32 administration_create_sequence_number() return g_administration.next_sequence_number; } +// Callback functions. +// ======================= +void administration_set_data_changed_event_callback(data_changed_event ev) +{ + data_changed_event_callback = ev; +} + +void administration_set_data_deleted_event_callback(data_deleted_event ev) +{ + data_deleted_event_callback = ev; +} + +void administration_set_invoice_changed_event_callback(invoice_changed_event ev) +{ + invoice_changed_event_callback = ev; +} + +void administration_set_contact_changed_event_callback(contact_changed_event ev) +{ + contact_changed_event_callback = ev; +} + +void administration_set_taxbracket_changed_event_callback(taxbracket_changed_event ev) +{ + taxbracket_changed_event_callback = ev; +} + +void administration_set_costcenter_changed_event_callback(costcenter_changed_event ev) +{ + costcenter_changed_event_callback = ev; +} + +void administration_set_project_changed_event_callback(project_changed_event ev) +{ + project_changed_event_callback = ev; +} + // Setup functions. // ======================= void administration_create_empty(char* save_file) @@ -419,8 +468,6 @@ void administration_create() list_init(&g_administration.tax_brackets); list_init(&g_administration.cost_centers); strops_copy(g_administration.path, "", sizeof(g_administration.path)); - strops_copy(g_administration.program_version, PROGRAM_VERSION, sizeof(g_administration.program_version)); - //administration_writer_save_all_async(); log_add("Setup took %.3fms.", STOPWATCH_TIME); } @@ -447,7 +494,7 @@ void administration_destroy() // Other functions. // ======================= -bool administration_is_loaded() +bool administration_has_save_path() { return strcmp(g_administration.path, "") != 0; } @@ -741,6 +788,9 @@ void administration_company_info_set(contact data) { g_administration.company_info = data; strops_copy(g_administration.default_currency, administration_get_default_currency_for_country(g_administration.company_info.address.country_code), MAX_LEN_CURRENCY); + + if (contact_changed_event_callback) contact_changed_event_callback(&data); + if (data_changed_event_callback) data_changed_event_callback(); } administration* administration_get() @@ -760,6 +810,9 @@ bool administration_contact_add(contact data) g_administration.next_id++; + if (contact_changed_event_callback) contact_changed_event_callback(new_contact); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } @@ -780,6 +833,10 @@ bool administration_contact_update(contact data) if (strcmp(c->id, data.id) == 0) { memcpy(c, &data, sizeof(data)); + + if (contact_changed_event_callback) contact_changed_event_callback(c); + if (data_changed_event_callback) data_changed_event_callback(); + list_iterator_stop(&g_administration.contacts); return true; } @@ -795,9 +852,12 @@ bool administration_contact_remove(contact data) while (list_iterator_hasnext(&g_administration.contacts)) { contact* c = (contact *)list_iterator_next(&g_administration.contacts); - if (strcmp(c->id, data.id) == 0) { + if (strcmp(c->id, data.id) == 0) { list_iterator_stop(&g_administration.contacts); list_delete(&g_administration.contacts, c); + + if (data_deleted_event_callback) data_deleted_event_callback(c->id); + free(c); return true; } @@ -1023,6 +1083,9 @@ bool administration_project_add(project data) g_administration.next_id++; + if (project_changed_event_callback) project_changed_event_callback(new_project); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } @@ -1037,6 +1100,10 @@ bool administration_project_update(project data) if (strcmp(c->id, data.id) == 0) { memcpy(c, &data, sizeof(data)); list_iterator_stop(&g_administration.projects); + + if (project_changed_event_callback) project_changed_event_callback(c); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } } @@ -1054,6 +1121,9 @@ bool administration_project_remove(project data) if (strcmp(c->id, data.id) == 0) { list_iterator_stop(&g_administration.projects); list_delete(&g_administration.projects, c); + + if (data_deleted_event_callback) data_deleted_event_callback(c->id); + free(c); return true; } @@ -1105,9 +1175,18 @@ u32 administration_tax_bracket_count() bool administration_tax_bracket_add(country_tax_bracket data) { - ADD_BRACKET(data.country_code, data.rate, ""); + country_tax_bracket* tb = (country_tax_bracket*)malloc(sizeof(country_tax_bracket)); + memcpy((void*)tb, (void*)&data, sizeof(country_tax_bracket)); + snprintf(tb->id, sizeof(tb->id), "T/%d", administration_create_id()); + + g_administration.next_id++; + list_attributes_comparator(&g_administration.tax_brackets, compare_tax_countries); list_sort(&g_administration.tax_brackets, -1); + + if (taxbracket_changed_event_callback) taxbracket_changed_event_callback(&data); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } @@ -1153,6 +1232,10 @@ bool administration_tax_bracket_update(country_tax_bracket data) if (strcmp(c->id, data.id) == 0) { memcpy(c, &data, sizeof(data)); list_iterator_stop(&g_administration.tax_brackets); + + if (taxbracket_changed_event_callback) taxbracket_changed_event_callback(c); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } } @@ -1248,6 +1331,9 @@ bool administration_cost_center_add(cost_center data) g_administration.next_id++; + if (costcenter_changed_event_callback) costcenter_changed_event_callback(tb); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } @@ -1260,6 +1346,10 @@ bool administration_cost_center_update(cost_center data) if (strcmp(c->id, data.id) == 0) { memcpy(c, &data, sizeof(data)); list_iterator_stop(&g_administration.cost_centers); + + if (costcenter_changed_event_callback) costcenter_changed_event_callback(c); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } } @@ -1377,6 +1467,8 @@ bool administration_invoice_remove(invoice* inv) list_iterator_stop(&g_administration.invoices); administration_destroy_list(&c->billing_items); list_delete(&g_administration.invoices, c); + + if (data_deleted_event_callback) data_deleted_event_callback(c->id); if (inv->is_outgoing) g_administration.invoice_count--; else g_administration.expense_count--; @@ -1476,6 +1568,10 @@ bool administration_invoice_update(invoice* inv) { memcpy(c, inv, sizeof(invoice)); list_iterator_stop(&g_administration.invoices); + + if (invoice_changed_event_callback) invoice_changed_event_callback(c); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } } @@ -1507,6 +1603,9 @@ bool administration_invoice_add(invoice* inv) if (inv->is_outgoing) g_administration.invoice_count++; else g_administration.expense_count++; + if (invoice_changed_event_callback) invoice_changed_event_callback(new_inv); + if (data_changed_event_callback) data_changed_event_callback(); + return true; } |
