diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/administration.cpp | 61 | ||||
| -rw-r--r-- | src/administration_writer.cpp | 195 | ||||
| -rw-r--r-- | src/locales/en.cpp | 6 | ||||
| -rw-r--r-- | src/strops.cpp | 31 |
4 files changed, 200 insertions, 93 deletions
diff --git a/src/administration.cpp b/src/administration.cpp index d75aff7..b938271 100644 --- a/src/administration.cpp +++ b/src/administration.cpp @@ -1144,6 +1144,12 @@ bool administration_invoice_add(invoice* inv) invoice* new_inv = (invoice*)malloc(sizeof(invoice)); memcpy(new_inv, ©, sizeof(invoice)); + new_inv->payment_means.payment_method = PAYMENT_METHOD_DEBIT_TRANSFER; + strops_copy(new_inv->payment_means.payee_bank_account, inv->customer.bank_account, sizeof(new_inv->payment_means.payee_bank_account)); + strops_copy(new_inv->payment_means.payee_account_name, inv->customer.name, sizeof(new_inv->payment_means.payee_account_name)); + strops_copy(new_inv->payment_means.service_provider_id, "", sizeof(new_inv->payment_means.service_provider_id)); + strops_copy(new_inv->payment_means.payer_bank_account, inv->supplier.bank_account, sizeof(new_inv->payment_means.payer_bank_account)); + list_append(&g_administration.invoices, new_inv); g_administration.next_id++; @@ -1207,6 +1213,61 @@ u32 administration_invoice_get_all(invoice* buffer) return write_cursor; } +bool administration_invoice_get_subtotal_for_tax_bracket(invoice* invoice, country_tax_bracket bracket, tax_subtotal* buffer) +{ + bool result = false; + + buffer->tax = 0.0f; + buffer->total = 0.0f; + buffer->net = 0.0f; + + list_iterator_start(&invoice->billing_items); + while (list_iterator_hasnext(&invoice->billing_items)) { + billing_item* c = (billing_item *)list_iterator_next(&invoice->billing_items); + + if (strcmp(c->tax_bracket_id, bracket.id) == 0) + { + result = true; + buffer->tax += c->tax; + buffer->total += c->total; + buffer->net += c->net; + } + } + list_iterator_stop(&invoice->billing_items); + + return result; +} + +u32 administration_invoice_get_tax_brackets(invoice* invoice, country_tax_bracket* buffer) +{ + u32 write_cursor = 0; + + list_iterator_start(&invoice->billing_items); + while (list_iterator_hasnext(&invoice->billing_items)) { + billing_item c = *(billing_item *)list_iterator_next(&invoice->billing_items); + + country_tax_bracket bracket; + bool found = administration_get_tax_bracket_by_id(&bracket, c.tax_bracket_id); + if (found) { + + bool exists = false; + for (u32 i = 0; i < write_cursor; i++) { + if (strcmp(buffer[i].id, c.tax_bracket_id) == 0) { + exists = true; + break; + } + } + + if (!exists) { + buffer[write_cursor++] = bracket; + } + } + } + list_iterator_stop(&invoice->billing_items); + + return write_cursor; +} + static u32 administration_invoice_get_partial_list(u32 page_index, u32 page_size, invoice* buffer, bool want_outgoing) { assert(buffer); diff --git a/src/administration_writer.cpp b/src/administration_writer.cpp index 40fcd25..e00921d 100644 --- a/src/administration_writer.cpp +++ b/src/administration_writer.cpp @@ -22,6 +22,17 @@ void administration_writer_destroy() mtx_destroy(&_save_file_mutex); } +static char* administration_writer_copy_template(const char* template_str, int* buf_size) +{ + size_t template_size = strlen(template_str); + size_t buf_length = template_size*2; // Ballpark file content size. + char* file_content = (char*)malloc(buf_length); + memset(file_content, 0, buf_length); + memcpy(file_content, template_str, template_size); + *buf_size = (int)buf_length; + return file_content; +} + static bool administration_writer_entry_exists(char* entry) { bool entry_exists = false; @@ -158,8 +169,6 @@ static char* administration_writer_get_eas_scheme_for_address(address addr) bool administration_writer_save_invoice_blocking(invoice inv) { - #define APPEND(_txt, ...) file_content += sprintf(file_content, _txt, __VA_ARGS__); - bool result = 1; int buf_length = 15000; // Ballpark file content size. char* file_content = (char*)malloc(buf_length); @@ -196,6 +205,40 @@ bool administration_writer_save_invoice_blocking(invoice inv) strops_replace(file_content, buf_length, "{{CUSTOMER_COUNTRY}}", inv.customer.address.country_code); strops_replace(file_content, buf_length, "{{CUSTOMER_VAT_ID}}", inv.customer.taxid); + // Payment means + strops_replace_int32(file_content, buf_length, "{{PAYMENT_TYPE}}", inv.payment_means.payment_method); + strops_replace(file_content, buf_length, "{{SUPPLIER_IBAN}}", inv.payment_means.payee_bank_account); + strops_replace(file_content, buf_length, "{{SUPPLIER_BIC}}", inv.payment_means.service_provider_id); + + // Tax breakdown + strops_replace_float(file_content, buf_length, "{{TOTAL_TAX_AMOUNT}}", inv.total, 2); + + { // Create tax subtotal list. + country_tax_bracket* tax_bracket_buffer = (country_tax_bracket*)malloc(sizeof(country_tax_bracket)*administration_billing_item_count(&inv)); + u32 tax_bracket_count = administration_invoice_get_tax_brackets(&inv, tax_bracket_buffer); + //int tax_list_buf_length = 0; + //char* tax_list_file_content = administration_writer_copy_template(peppol_invoice_tax_subtotal_template, &tax_list_buf_length); + + for (u32 i = 0; i < tax_bracket_count; i++) + { + int tax_entry_buf_length = 0; + char* tax_entry_file_content = administration_writer_copy_template(peppol_invoice_tax_subtotal_template, &tax_entry_buf_length); + + tax_subtotal subtotal; + administration_invoice_get_subtotal_for_tax_bracket(&inv, tax_bracket_buffer[i], &subtotal); + + strops_replace(tax_entry_file_content, tax_entry_buf_length, "{{CURRENCY}}", inv.currency); + strops_replace_float(tax_entry_file_content, tax_entry_buf_length, "{{TAXABLE_AMOUNT}}", subtotal.net, 2); + strops_replace_float(tax_entry_file_content, tax_entry_buf_length, "{{TAX_AMOUNT}}", subtotal.tax, 2); + //strops_replace(tax_entry_file_content, tax_entry_buf_length, "{{TAX_CATEGORY}}", inv.currency); + strops_replace_float(tax_entry_file_content, tax_entry_buf_length, "{{TAX_PERCENT}}", tax_bracket_buffer[i].rate, 2); + + printf("%s\n", tax_entry_file_content); + } + + free(tax_bracket_buffer); + } + // Dates tm_info = localtime(&inv.issued_at); strftime(date_buffer, sizeof(date_buffer), "%Y-%m-%d", tm_info); @@ -236,29 +279,24 @@ static bool administration_writer_save_all_invoices_blocking() bool administration_writer_save_project_blocking(project project) { bool result = 1; - int buf_length = 1000; // Ballpark file content size. - char* file_content = (char*)malloc(buf_length); - memset(file_content, 0, buf_length); + int buf_length = 0; + char* file_content = administration_writer_copy_template(project_save_template, &buf_length); - char* orig_content = file_content; - - file_content += sprintf(file_content, "<project>\n"); - file_content += sprintf(file_content, "\t<id>%s</id>\n", project.id); - file_content += sprintf(file_content, "\t<description>%s</description>\n", project.description); - file_content += sprintf(file_content, "\t<state>%d</state>\n", project.state); - file_content += sprintf(file_content, "\t<start_date>%lld</start_date>\n", (long long)project.start_date); - file_content += sprintf(file_content, "\t<end_date>%lld</end_date>\n", (long long)project.end_date); - file_content += sprintf(file_content, "</project>\n"); + strops_replace(file_content, buf_length, "{{PROJECT_ID}}", project.id); + strops_replace(file_content, buf_length, "{{PROJECT_DESCRIPTION}}", project.description); + strops_replace_int32(file_content, buf_length, "{{PROJECT_STATE}}", project.state); + strops_replace_int64(file_content, buf_length, "{{PROJECT_STARTDATE}}", (long long)project.start_date); + strops_replace_int64(file_content, buf_length, "{{PROJECT_ENDDATE}}", (long long)project.end_date); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", project.id); - int final_length = (int)strlen(orig_content); - if (!xml_parse_document((uint8_t*)orig_content, final_length)) result = 0; - else if (!administration_writer_write_to_zip(final_path, orig_content, final_length)) result = 0; + int final_length = (int)strlen(file_content); + if (!xml_parse_document((uint8_t*)file_content, final_length)) result = 0; + else if (!administration_writer_write_to_zip(final_path, file_content, final_length)) result = 0; - free(orig_content); + free(file_content); return result; } @@ -285,27 +323,22 @@ static bool administration_writer_save_all_projects_blocking() bool administration_writer_save_cost_center_blocking(cost_center cost) { bool result = 1; - int buf_length = 1000; // Ballpark file content size. - char* file_content = (char*)malloc(buf_length); - memset(file_content, 0, buf_length); + int buf_length = 0; + char* file_content = administration_writer_copy_template(costcenter_save_template, &buf_length); - char* orig_content = file_content; - - file_content += sprintf(file_content, "<cost_center>\n"); - file_content += sprintf(file_content, "\t<id>%s</id>\n", cost.id); - file_content += sprintf(file_content, "\t<code>%s</code>\n", cost.code); - file_content += sprintf(file_content, "\t<description>%s</description>\n", cost.description); - file_content += sprintf(file_content, "</cost_center>\n"); + strops_replace(file_content, buf_length, "{{COSTCENTER_ID}}", cost.id); + strops_replace(file_content, buf_length, "{{COSTCENTER_CODE}}", cost.code); + strops_replace(file_content, buf_length, "{{COSTCENTER_DESCRIPTION}}", cost.description); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", cost.id); - int final_length = (int)strlen(orig_content); - if (!xml_parse_document((uint8_t*)orig_content, final_length)) result = 0; - else if (!administration_writer_write_to_zip(final_path, orig_content, final_length)) result = 0; - - free(orig_content); + int final_length = (int)strlen(file_content); + if (!xml_parse_document((uint8_t*)file_content, final_length)) result = 0; + else if (!administration_writer_write_to_zip(final_path, file_content, final_length)) result = 0; + + free(file_content); return result; } @@ -332,27 +365,23 @@ static bool administration_writer_save_all_cost_centers_blocking() bool administration_writer_save_tax_bracket_blocking(country_tax_bracket bracket) { bool result = 1; - int buf_length = 1000; // Ballpark file content size. - char* file_content = (char*)malloc(buf_length); - memset(file_content, 0, buf_length); - char* orig_content = file_content; + int buf_length = 0; + char* file_content = administration_writer_copy_template(taxbracket_save_template, &buf_length); - file_content += sprintf(file_content, "<country_tax_bracket>\n"); - file_content += sprintf(file_content, "\t<id>%s</id>\n", bracket.id); - file_content += sprintf(file_content, "\t<country_code>%s</country_code>\n", bracket.country_code); - file_content += sprintf(file_content, "\t<rate>%.2f</rate>\n", bracket.rate); - file_content += sprintf(file_content, "\t<description>%s</description>\n", bracket.description); - file_content += sprintf(file_content, "</country_tax_bracket>\n"); + strops_replace(file_content, buf_length, "{{TAXBRACKET_ID}}", bracket.id); + strops_replace(file_content, buf_length, "{{TAXBRACKET_COUNTRY}}", bracket.country_code); + strops_replace_float(file_content, buf_length, "{{TAXBRACKET_RATE}}", bracket.rate, 2); + strops_replace(file_content, buf_length, "{{TAXBRACKET_DESCRIPTION}}", bracket.description); //// Write to Disk. char final_path[50]; snprintf(final_path, 50, "%s.xml", bracket.id); - int final_length = (int)strlen(orig_content); - if (!xml_parse_document((uint8_t*)orig_content, final_length)) result = 0; - else if (!administration_writer_write_to_zip(final_path, orig_content, final_length)) result = 0; + int final_length = (int)strlen(file_content); + if (!xml_parse_document((uint8_t*)file_content, final_length)) result = 0; + else if (!administration_writer_write_to_zip(final_path, file_content, final_length)) result = 0; - free(orig_content); + free(file_content); return result; } @@ -381,39 +410,32 @@ static bool administration_writer_save_all_tax_brackets_blocking() bool administration_writer_save_contact_blocking(contact c) { bool result = 1; - int buf_length = 2000; // Ballpark contact content size. - char* file_content = (char*)malloc(buf_length); - memset(file_content, 0, buf_length); - - char* orig_content = file_content; - - file_content += sprintf(file_content, "<contact>\n"); - - file_content += sprintf(file_content, "\t<id>%s</id>\n", c.id); - file_content += sprintf(file_content, "\t<name>%s</name>\n", c.name); - file_content += sprintf(file_content, "\t<type>%d</type>\n", c.type); - file_content += sprintf(file_content, "\t<taxid>%s</taxid>\n", c.taxid); - file_content += sprintf(file_content, "\t<businessid>%s</businessid>\n", c.businessid); - file_content += sprintf(file_content, "\t<email>%s</email>\n", c.email); - file_content += sprintf(file_content, "\t<phone_number>%s</phone_number>\n", c.phone_number); - file_content += sprintf(file_content, "\t<bank_account>%s</bank_account>\n", c.bank_account); - - file_content += sprintf(file_content, "\t<address>\n"); - file_content += sprintf(file_content, "\t\t<address1>%s</address1>\n", c.address.address1); - file_content += sprintf(file_content, "\t\t<address2>%s</address2>\n", c.address.address2); - file_content += sprintf(file_content, "\t\t<country_code>%s</country_code>\n", c.address.country_code); - file_content += sprintf(file_content, "\t</address>\n"); - - file_content += sprintf(file_content, "</contact>\n"); + int buf_length = 0; + char* file_content = administration_writer_copy_template(contact_save_template, &buf_length); + + strops_replace(file_content, buf_length, "{{CONTACT_ID}}", c.id); + strops_replace(file_content, buf_length, "{{CONTACT_NAME}}", c.name); + strops_replace_int32(file_content, buf_length, "{{CONTACT_TYPE}}", c.type); + strops_replace(file_content, buf_length, "{{CONTACT_TAXID}}", c.taxid); + strops_replace(file_content, buf_length, "{{CONTACT_BUSINESSID}}", c.businessid); + strops_replace(file_content, buf_length, "{{CONTACT_EMAIL}}", c.email); + strops_replace(file_content, buf_length, "{{CONTACT_PHONENUMBER}}", c.phone_number); + strops_replace(file_content, buf_length, "{{CONTACT_BANKACCOUNT}}", c.bank_account); + strops_replace(file_content, buf_length, "{{CONTACT_ADDRESS1}}", c.address.address1); + strops_replace(file_content, buf_length, "{{CONTACT_ADDRESS2}}", c.address.address2); + strops_replace(file_content, buf_length, "{{CONTACT_COUNTRY}}", c.address.country_code); + strops_replace(file_content, buf_length, "{{CONTACT_CITY}}", c.address.city); + strops_replace(file_content, buf_length, "{{CONTACT_POSTAL}}", c.address.postal); + strops_replace(file_content, buf_length, "{{CONTACT_REGION}}", c.address.region); char final_path[50]; snprintf(final_path, 50, "%s.xml", c.id); - int final_length = (int)strlen(orig_content); - if (!xml_parse_document((uint8_t*)orig_content, final_length)) result = 0; - else if (!administration_writer_write_to_zip(final_path, orig_content, final_length)) result = 0; + int final_length = (int)strlen(file_content); + if (!xml_parse_document((uint8_t*)file_content, final_length)) result = 0; + else if (!administration_writer_write_to_zip(final_path, file_content, final_length)) result = 0; - free(orig_content); + free(file_content); return result; } @@ -446,24 +468,19 @@ static bool administration_writer_save_all_contacts_blocking() bool administration_writer_save_all_administration_info_blocking() { bool result = 1; - int buf_length = 1000; // Ballpark file content size. - char* file_content = (char*)malloc(buf_length); - memset(file_content, 0, buf_length); - char* orig_content = file_content; + int buf_length = 0; + char* file_content = administration_writer_copy_template(administration_save_template, &buf_length); - //// Cost centers. - file_content += sprintf(file_content, "<administration>\n"); - file_content += sprintf(file_content, "\t<next_id>%d</next_id>\n", administation_get()->next_id); - file_content += sprintf(file_content, "\t<next_sequence_number>%d</next_sequence_number>\n", administation_get()->next_sequence_number); - file_content += sprintf(file_content, "\t<program_version>%s</program_version>\n", administation_get()->program_version); - file_content += sprintf(file_content, "</administration>"); + strops_replace_int32(file_content, buf_length, "{{NEXT_ID}}", administation_get()->next_id); + strops_replace_int32(file_content, buf_length, "{{NEXT_SEQUENCE_NUMBER}}", administation_get()->next_sequence_number); + strops_replace(file_content, buf_length, "{{PROGRAM_VERSION}}", administation_get()->program_version); //// Write to Disk. - int final_length = (int)strlen(orig_content); - if (!xml_parse_document((uint8_t*)orig_content, final_length)) result = 0; - else if (!administration_writer_write_to_zip(ADMIN_FILE_INFO, orig_content, final_length)) result = 0; + int final_length = (int)strlen(file_content); + if (!xml_parse_document((uint8_t*)file_content, final_length)) result = 0; + else if (!administration_writer_write_to_zip(ADMIN_FILE_INFO, file_content, final_length)) result = 0; - free(orig_content); + free(file_content); return result; } diff --git a/src/locales/en.cpp b/src/locales/en.cpp index 767a07d..b528088 100644 --- a/src/locales/en.cpp +++ b/src/locales/en.cpp @@ -96,12 +96,12 @@ locale_entry en_locales[] = { {"contact.form.email", "Email address"}, {"contact.form.phonenumber", "Phone number"}, {"contact.form.bankaccount", "Bank account"}, + {"contact.form.city", "City"}, + {"contact.form.postal", "Postal"}, + {"contact.form.region", "Region"}, {"contact.table.identifier", "Identifier"}, {"contact.table.name", "Name"}, {"contact.table.address", "Address"}, - {"contact.table.city", "City"}, - {"contact.table.postal", "Postal"}, - {"contact.table.region", "Region"}, // Project strings. {"project.form.identifier", "Identifier"}, diff --git a/src/strops.cpp b/src/strops.cpp index 3389eb1..208aaed 100644 --- a/src/strops.cpp +++ b/src/strops.cpp @@ -1,5 +1,6 @@ #include <string.h> #include <ctype.h> +#include <stdio.h> #include "strops.hpp" @@ -47,14 +48,42 @@ void strops_replace(char *buf, size_t buf_size, const char *search, const char * // Ensure space size_t remaining = buf_size - (w - buf) - 1; size_t copy_len = (replace_len < remaining) ? replace_len : remaining; + size_t skip_size = search_len; + + if (replace_len > search_len) + { + memmove(w+replace_len, w+search_len, remaining - replace_len); + skip_size = replace_len; + } memcpy(w, replace, copy_len); w += copy_len; - r += search_len; + r += skip_size; } else { *w++ = *r++; } } *w = '\0'; // terminate +} + +void strops_replace_int32(char *buf, size_t buf_size, const char *search, int32_t number) +{ + char num_buf[200]; + snprintf(num_buf, 200, "%d", number); + strops_replace(buf, buf_size, search, num_buf); +} + +void strops_replace_int64(char *buf, size_t buf_size, const char *search, int64_t number) +{ + char num_buf[200]; + snprintf(num_buf, 200, "%lld", number); + strops_replace(buf, buf_size, search, num_buf); +} + +void strops_replace_float(char *buf, size_t buf_size, const char *search, float number, int decimals) +{ + char num_buf[200]; + snprintf(num_buf, 200, "%.*f", decimals, number); + strops_replace(buf, buf_size, search, num_buf); }
\ No newline at end of file |
